@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
387 lines • 15.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NftModule = void 0;
const ethers_1 = require("ethers");
const rxjs_1 = require("rxjs");
const __1 = require("../..");
const createIdeaCore = async (contract, _strategyKey, _rulesVersion, _metadata, _subscribers, _ideaKey, clientInBehalf, gasAggressivity = 2) => {
__1.LogModule.dev('pre commit');
const callerAddress = clientInBehalf !== undefined
? clientInBehalf
: await contract.gate.signer.getAddress();
__1.LogModule.dev(`Caller address: ${callerAddress}`);
__1.LogModule.dev(`Strategy key: ${_strategyKey}`);
__1.LogModule.dev(`Idea key: ${_ideaKey}`);
const balance = await contract.gate.signer.getBalance();
const balanceInEth = __1.ethers.utils.formatEther(balance);
__1.LogModule.dev(`Caller balance: ${Number(balanceInEth).toFixed(4)} ETH`);
// to avoid twice data
// if the user is a owner and this appears in the subscribers list, "remove" it
// (i.e. apply the filer)
_subscribers =
_subscribers?.filter((address) => address?.toLowerCase() !== callerAddress?.toLowerCase()) || [];
__1.LogModule.dev(`Subscribers post:`, _subscribers);
__1.LogModule.dev(`clientInBehalf:`, clientInBehalf);
__1.LogModule.dev('_ideaKey:', _ideaKey);
const costPrior = await __1.OperationalCostModule.getEstimatedCosts(contract, clientInBehalf, _ideaKey === undefined);
const APPLY_SETTINGS = true;
let optionalSettings = {};
if (APPLY_SETTINGS) {
// const gasLimit = costPrior.estimatedComputationalRawCost
const gasLimit = costPrior.estimatedComputationalRawCost +
Math.floor((1 / 10) * costPrior.estimatedComputationalRawCost);
const gasPrice = costPrior.estimatedGasPrice.weiBigNumber.add(costPrior.estimatedGasPrice.weiBigNumber
.mul(gasAggressivity)
.div(100));
const value = costPrior.ixilyTax.weiBigNumber;
__1.LogModule.dev('currentIxilyTax:');
__1.LogModule.dev(value);
optionalSettings = {
gasLimit,
gasPrice,
value,
};
}
if (optionalSettings.value === 0 || optionalSettings.value === undefined) {
throw new Error('optionalSettings.value is 0 or undefined');
}
// console.log('costPrior:')
// console.log(costPrior)
const dealErrIfAboutCost = async (err) => {
if (err.message) {
for (const increaseIndex of contract.recipe.errorsDealWith
.increaseGasUnits) {
if (err.message.includes(increaseIndex)) {
await __1.OperationalCostModule.increaseEstimatedComputationalRawCost(contract.gate.address, _ideaKey === undefined, clientInBehalf !== undefined);
}
}
for (const decreaseIndex of contract.recipe.errorsDealWith
.decreaseGasUnits) {
if (err.message.includes(decreaseIndex)) {
await __1.OperationalCostModule.decreaseEstimatedComputationalRawCost(contract.gate.address, _ideaKey === undefined, clientInBehalf !== undefined);
}
}
}
// console.log('err')
// console.log(err)
// console.log('err.message')
// console.log(err.message)
throw err;
};
const nftCommit = clientInBehalf === undefined
? _ideaKey === undefined
? await contract
.gate.createIdea(_strategyKey, _rulesVersion, _metadata, _subscribers, optionalSettings)
.catch(dealErrIfAboutCost)
: await contract
.gate.createIdeaStage(_strategyKey, _rulesVersion, _metadata, _subscribers, _ideaKey, optionalSettings)
.catch(dealErrIfAboutCost)
: _ideaKey === undefined
? await contract
.gate.providerCreateIdea(clientInBehalf, _strategyKey, _rulesVersion, _metadata, _subscribers, optionalSettings)
.catch(dealErrIfAboutCost)
: await contract
.gate.providerCreateIdeaStage(clientInBehalf, _strategyKey, _rulesVersion, _metadata, _subscribers, _ideaKey, optionalSettings)
.catch(dealErrIfAboutCost);
__1.LogModule.dev('nftCommit');
__1.LogModule.dev(nftCommit);
const nftCommitted = await nftCommit.wait().catch(dealErrIfAboutCost);
__1.LogModule.dev('createNftIdea (nftCommitted)');
__1.LogModule.dev(nftCommitted);
const abiForEvent = [
'event IdeaCreated(address,uint256,string,uint256,uint256,uint256)',
];
const ifaceForEvent = new __1.ethers.utils.Interface(abiForEvent);
const parsedLogs = [];
for (const logIn of nftCommitted.logs) {
try {
const parsedLog = ifaceForEvent.parseLog(logIn);
parsedLogs.push(parsedLog);
}
catch (e) { }
}
// console.log('nftCommitted logs')
// console.log(parsedLogs)
if (parsedLogs.length !== 1) {
throw new Error('IdeaNftCreation: IdeaCreated event not found');
}
// console.log('nftCommitted events')
// console.log(nftCommitted.events!)
// for (const evt of nftCommitted.events!) {
// console.log('event:')
// console.log(evt)
// console.log('topics:')
// console.log(evt.topics)
// }
// const targetEvent = nftCommitted.events!.find(
// (one: any) => one.event === 'IdeaCreated',
// )
const targetEvent = parsedLogs[0];
if (targetEvent == undefined) {
throw new Error('IdeaNftCreation: IdeaCreated event not found');
}
else {
// log.dev(targetEvent.args)
const nftId = targetEvent.args[1];
const strategyKey = targetEvent.args[2];
const ideaKey = targetEvent.args[3];
const ideaStageKey = targetEvent.args[4];
if (_strategyKey === strategyKey) {
const openseaUrl = `https://testnets.opensea.io/assets/${nftCommitted.to}/${nftId.toNumber()}`;
const costAfter = await __1.OperationalCostModule.getFinalCosts(costPrior, nftCommitted, contract, clientInBehalf, _ideaKey === undefined);
// console.log('costAfter')
// console.log(costAfter)
return {
nftId: nftId.toNumber(),
strategyKey: _strategyKey,
ideaKey: ideaKey.toNumber(),
ideaStageKey: ideaStageKey.toNumber(),
openseaUrl,
transactionHash: nftCommitted.transactionHash,
blockNumber: nftCommitted.blockNumber,
cost: costAfter,
};
}
else {
throw new Error('IdeaNftCreation: Strategy key mismatch');
}
}
};
const createIdea = async (contract, _strategyKey, _rulesVersion, _metadata, _subscribers, _ideaKey, clientInBehalf, gasAggressivity = 2) => {
return __1.IdeaTrafficDirectorModule.runSequentialTicket([clientInBehalf || ''], async () => {
const result = await createIdeaCore(contract, _strategyKey, _rulesVersion, _metadata, _subscribers, _ideaKey, clientInBehalf, gasAggressivity);
return result;
});
};
const giveIdeaTo = async (contract, _ideaNftId, _clientAddresses) => {
__1.LogModule.dev('_clientAddresses:');
__1.LogModule.dev(_clientAddresses);
const nftCommitted = await contract.gate.giveIdeaTo(_ideaNftId, _clientAddresses, {
gasLimit: 2000000,
});
await nftCommitted.wait();
};
/*
* Returns the list of walllet addresses that have access to the idea
*
* @param contract The contract instance
* @param _ideaNftId The idea NFT id
* @returns The list of wallet addresses
*/
const getIdeaViewers = async (contract, _ideaNftId) => {
const ideaViewers = await contract.gate.getIdeaViewers(_ideaNftId);
return ideaViewers;
};
const watchEvents = async (contract) => {
const ideaFilter = contract.gate.filters.IdeaCreated();
const ideaEvent$ = new rxjs_1.BehaviorSubject({
network: contract.recipe.chain,
contractAddress: contract.gate.address,
});
// console.log('Watching the Mint events...')
contract.gate.on(ideaFilter, (...args) => {
ideaEvent$.next({
network: contract.recipe.chain,
contractAddress: contract.gate.address,
event: {
creatorAddress: args[0],
nftId: args[1].toNumber(),
strategyKey: args[2],
ideaKey: args[3].toNumber(),
stageKey: args[4].toNumber(),
blockNumber: args[5].toNumber(),
},
});
});
return ideaEvent$;
};
const readEvents = async (contract, fromBlock, toBlock) => {
const ideaFilter = contract.gate.filters.IdeaCreated();
// console.log('Querying the Mint events...')
const mintEvents = await contract.gate.queryFilter(ideaFilter, fromBlock, toBlock);
// console.log('mintEvents')
// console.log(mintEvents)
return mintEvents;
};
const getFirstBlock = async (contract) => {
return contract.gate.getFirstEventBlock();
};
const getLastBlock = async (contract) => {
return contract.gate.getLastEventBlock();
};
const listStrategies = async (_creator, contract) => {
const listStrategies = await contract.gate.listStrategies(_creator);
return listStrategies;
};
const listAllStrategies = async (contract) => {
const listedAllStrategies = await contract.gate.listAllStrategies();
return listedAllStrategies;
};
const listIdeas = async (_creator, _strategyKey, contract) => {
// log.dev('pre listIdeas:')
const listedIdeas = await contract.gate.listIdeas(_creator, _strategyKey);
// log.dev('ends here')
// log.dev('listedIdeas')
// log.dev(listedIdeas)
return listedIdeas.map((each) => each.toNumber());
};
const listStages = async (_creator, _strategyKey, _ideaKey, contract) => {
const listedStages = await floodProtectiveWrapper(async () => {
return contract.gate.listStages(_creator, _strategyKey, _ideaKey);
}, 'listStages');
return listedStages.map((each) => each.toNumber());
};
const getCreatorOfIdea = async (nftIdea, contract) => {
const creator = await contract.gate.getCreatorOfNft(nftIdea);
return creator.toString();
};
const getIdeaNftIdByKeys = async (_creator, _strategyKey, _ideaKey, _ideaStageKey, contract) => {
// provide contract
return floodProtectiveWrapper(async () => {
return (await contract.gate.getIdeaByKeys(_creator, _strategyKey, _ideaKey, _ideaStageKey)).toNumber();
}, 'getIdeaNftIdByKeys');
};
const getIdeaMetadataUriByNftId = async (ideaNft, contract) => {
if (typeof ideaNft === 'number') {
try {
ideaNft = ethers_1.BigNumber.from(ideaNft.toString());
}
catch (e) { }
}
let mid = await floodProtectiveWrapper(async () => {
return contract.gate.uri(Number(ideaNft));
}, 'getIdeaMetadataUriByNftId', 10);
if (mid === 'https://ipfs.io/ipfs/') {
throw new Error('NFT_NOT_FOUND');
}
return mid;
};
const getMetadataIdByBlockId = async (blockIdParam, contract) => {
// console.log(
// 'nft.module getMetadataIdByBlockId (blockIdParam)',
// blockIdParam,
// )
const data = await contract.gate.getMetadataIdByBlockId(blockIdParam);
if ([null, undefined, 'none'].includes(data[2])) {
throw new Error('NFT MODULE ERROR: No metadata found for this block');
}
const blockId = data[0].toNumber();
const nftId = data[1].toNumber();
const metadataId = data[2];
console.log('nft.module getMetadataIdByBlockId (blockId)', blockId);
console.log('nft.module getMetadataIdByBlockId (nftId)', nftId);
console.log('nft.module getMetadataIdByBlockId (metadataId)', metadataId);
const response = {
blockId,
nftId,
metadataId,
};
console.log('nft.module getMetadataIdByBlockId (response)', response);
return response;
};
const authorizeProvider = async (provider, contract) => {
const auth = await contract.gate.authorizeProvider(provider, {
gasLimit: 2000000,
});
await auth.wait();
};
const revokeProvider = async (provider, contract) => {
const auth = await contract.gate.revokeProvider(provider);
await auth.wait();
};
/*
* Provider check is supposed to be called by PROVIDER BACKEND to
* test if he is authorized by client
*/
const providerCheck = async (client, contract) => {
const auth = await contract.gate.providerCheck(client);
return auth;
};
/*
* Authorize Check is supposed to be called by CLIENT ON BROWSER to
* test if he authorized the provider
*/
const authorizeCheck = async (provider, contract) => {
const auth = await contract.gate.authorizeCheck(provider);
return auth;
};
const getOwnedBy = async (owner, contract) => {
const nfts = await contract.gate.getNftsOwnedBy(owner);
return nfts.map((each) => each.toNumber());
};
const getCreatedBy = async (creator, contract) => {
const nfts = await contract.gate.getNftsCreatedBy(creator);
return nfts.map((each) => each.toNumber());
};
const getLastId = async (contract) => {
const lastNftId = await contract.gate.getLastNftId();
return lastNftId?.toNumber();
};
const floodProtectiveWrapper = async (_f, _fName, times = 10) => {
let attempts = 0;
let value = 'null';
const randomBetweenOneAnd20 = Math.floor(Math.random() * 20) + 1;
await new Promise((resolve) => setTimeout(resolve, randomBetweenOneAnd20));
while (value === 'null') {
try {
value = await _f();
}
catch (err) {
if (attempts >= times) {
const errorParsed = err.message || JSON.stringify(err);
console.error(errorParsed);
throw new Error(`Error in ${_fName} ` + times + ` times as logged.`);
}
const randomBetweenOneAnd20 = Math.floor(Math.random() * 20) + 1;
await new Promise((resolve) => setTimeout(resolve, randomBetweenOneAnd20 * 10));
}
attempts++;
}
return value;
};
exports.NftModule = {
/* MINT METHODS */
mint: {
/* can be called by client or provider */
general: {
createIdea,
giveIdeaTo,
},
/* by client user direct only */
clientOnly: {
authorizeProvider,
revokeProvider,
},
},
/* VIEWS */
view: {
/* can be called by client or provider */
general: {
getFirstBlock,
getLastBlock,
watchEvents,
readEvents,
listStrategies,
listAllStrategies,
listIdeas,
listStages,
getCreatorOfIdea,
getIdeaNftIdByKeys,
getIdeaMetadataUriByNftId,
getMetadataIdByBlockId,
getOwnedBy,
getCreatedBy,
getLastId,
getIdeaViewers,
},
/* by provider only */
providerOnly: {
providerCheck,
},
/* by client only */
clientOnly: {
authorizeCheck,
},
},
};
//# sourceMappingURL=nft.module.js.map