@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
238 lines • 9.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperationalCostModule = void 0;
const ethers_1 = require("ethers");
const __1 = require("../..");
const COMPUTATIONAL_COST_TABLE_NAME = 'coputational_cost_table';
const EXPIRATION_FOR_COMPUTATIONAL_COST = 3600 * 24 * 10; // 10 days
const COMPUTATIONAL_COST_TABLE_NAME_EXPIRATION = 30 * 1000; // 30s
const GAS_PRICE_EXPIRATION = 10 * 1000; // 10s
const getEstimatedComputationalRawCost = async (contract, ideaCreation, inBehalfOf) => {
const contractAddress = contract.gate.address;
const key = COMPUTATIONAL_COST_TABLE_NAME +
'_' +
contractAddress +
'_' +
ideaCreation +
'_' +
inBehalfOf;
/* This is for dev for determining values */
// await CacheStorageModule.updateData(
// key,
// JSON.stringify({
// costs: [500000000],
// }),
// EXPIRATION_FOR_COMPUTATIONAL_COST,
// )
// console.log('key GET')
// console.log(key)
const slots = __1.PreCacheModule.getExpiringSlots('computationalCost', 10, COMPUTATIONAL_COST_TABLE_NAME_EXPIRATION);
const dataOutdated = slots.get(key);
let dataUpdated;
if (dataOutdated !== undefined) {
dataUpdated = dataOutdated;
}
else {
dataUpdated = await __1.CacheStorageModule.getData(key);
}
if (!(0, __1.isNullOrUndefined)(dataUpdated)) {
// console.log('dataUpdated')
// console.log(dataUpdated)
const costRecords = JSON.parse(dataUpdated);
const estimatedComputationalRawCost = mediumCost(costRecords);
return {
estimatedComputationalRawCost,
costRecords,
};
}
else {
const estimatedComputationalRawCost = ideaCreation
? contract.recipe.gasUnitsCost.ideaCreation
: contract.recipe.gasUnitsCost.ideaStageCreation;
const costRecords = {
costs: [estimatedComputationalRawCost],
};
await __1.CacheStorageModule.addData(key, JSON.stringify(costRecords), EXPIRATION_FOR_COMPUTATIONAL_COST);
slots.add(key, JSON.stringify(costRecords));
return {
estimatedComputationalRawCost,
costRecords,
};
}
};
const updateEstimatedComputationalRawCost = async (contractAddress, ideaCreation, inBehalfOf, updatedValue) => {
const key = COMPUTATIONAL_COST_TABLE_NAME +
'_' +
contractAddress +
'_' +
ideaCreation +
'_' +
inBehalfOf;
await __1.CacheStorageModule.updateData(key, JSON.stringify(updatedValue), EXPIRATION_FOR_COMPUTATIONAL_COST);
const slots = __1.PreCacheModule.getExpiringSlots('computationalCost', 10, COMPUTATIONAL_COST_TABLE_NAME_EXPIRATION);
slots.add(key, JSON.stringify(updatedValue));
};
const mediumCost = (record) => {
const costs = record.costs.length > 10 ? record.costs.slice(-10) : record.costs;
const sum = costs.reduce((a, b) => a + b, 0);
const avg = sum / costs.length || 0;
return Math.floor(avg);
};
const increaseEstimatedComputationalRawCost = async (contractAddress, ideaCreation, inBehalfOf) => {
const key = COMPUTATIONAL_COST_TABLE_NAME +
'_' +
contractAddress +
'_' +
ideaCreation +
'_' +
inBehalfOf;
const dataUpdated = await __1.CacheStorageModule.getData(key);
// console.log('key INCREASE')
// console.log(key)
if (!(0, __1.isNullOrUndefined)(dataUpdated)) {
// console.log('dataUpdated:')
// console.log(dataUpdated)
const dataUpdatedRestored = JSON.parse(dataUpdated);
let computationalGuess = mediumCost(dataUpdatedRestored);
computationalGuess = computationalGuess * 5;
const toRecord = {
costs: [computationalGuess],
};
await updateEstimatedComputationalRawCost(contractAddress, ideaCreation, inBehalfOf, toRecord);
}
};
const decreaseEstimatedComputationalRawCost = async (contractAddress, ideaCreation, inBehalfOf) => {
const key = COMPUTATIONAL_COST_TABLE_NAME +
'_' +
contractAddress +
'_' +
ideaCreation +
'_' +
inBehalfOf;
const dataUpdated = await __1.CacheStorageModule.getData(key);
if (!(0, __1.isNullOrUndefined)(dataUpdated)) {
const dataUpdatedRestored = JSON.parse(dataUpdated);
let computationalGuess = mediumCost(dataUpdatedRestored);
computationalGuess = computationalGuess / 5;
const toRecord = {
costs: [computationalGuess],
};
await updateEstimatedComputationalRawCost(contractAddress, ideaCreation, inBehalfOf, toRecord);
}
};
const IXILY_TAX_EXPIRATION = 10 * 60 * 1000; // 10 minutes
const getCurrentIxilyTax = async (contract) => {
const slots = __1.PreCacheModule.getExpiringSlots('ixilyTax', 1, IXILY_TAX_EXPIRATION);
const savedIxilyTax = slots.get('tax');
if (savedIxilyTax !== undefined) {
return savedIxilyTax;
}
if (contract.recipe.isTestnet) {
return ethers_1.BigNumber.from(0);
}
const gWei = await contract.gate.getIdeaCreationTax();
const value = gWei.mul(ethers_1.BigNumber.from(10).pow(9));
slots.add('tax', value);
return value;
};
const getEstimatedCosts = async (contract, clientInBehalf, newIdea = true) => {
const weiBigNumberFee = await getCurrentIxilyTax(contract);
__1.LogModule.dev('got weiBigNumberFee:');
__1.LogModule.dev(weiBigNumberFee);
const balancePrior = await contract.gate.provider.getBalance(clientInBehalf || (await contract.gate.signer.getAddress()));
__1.LogModule.dev('balancePrior:');
__1.LogModule.dev(balancePrior);
const slots = __1.PreCacheModule.getExpiringSlots('gasPrice', 10, GAS_PRICE_EXPIRATION);
const savedGasPrice = slots.get(contract.gate.address);
let gasPrice;
if (savedGasPrice !== undefined) {
gasPrice = savedGasPrice;
}
else {
gasPrice = await contract.gate.provider.getGasPrice();
slots.add(contract.gate.address, gasPrice);
}
__1.LogModule.dev('gasPrice:');
__1.LogModule.dev(gasPrice);
const { estimatedComputationalRawCost, costRecords } = await getEstimatedComputationalRawCost(contract, newIdea, clientInBehalf !== undefined);
__1.LogModule.dev('estimatedComputationalRawCost');
__1.LogModule.dev(estimatedComputationalRawCost);
__1.LogModule.dev('costRecords');
__1.LogModule.dev(costRecords);
const totalGasCost = gasPrice.mul(estimatedComputationalRawCost);
__1.LogModule.dev('totalGasCost');
__1.LogModule.dev(totalGasCost);
const totalCost = totalGasCost.add(weiBigNumberFee);
__1.LogModule.dev('totalCost');
__1.LogModule.dev(totalCost);
const cost = {
ixilyTax: {
weiBigNumber: weiBigNumberFee,
weiHex: weiBigNumberFee.toHexString(),
},
walletBalancePrior: {
weiBigNumber: balancePrior,
weiHex: balancePrior.toHexString(),
},
estimatedGasPrice: {
weiBigNumber: gasPrice,
weiHex: gasPrice.toHexString(),
},
estimatedComputationalRawCost,
costRecords,
estimatedTotalGasCost: {
weiBigNumber: totalGasCost,
weiHex: totalGasCost.toHexString(),
},
estimatedTotalCost: {
weiBigNumber: totalCost,
weiHex: totalCost.toHexString(),
},
haveEnoughForTry: balancePrior.sub(totalCost).gt(0),
};
__1.LogModule.dev('cost');
__1.LogModule.dev(cost);
return cost;
};
const getFinalCosts = async (costPrior, nftCommitted, contract, clientInBehalf, newIdea = true) => {
const balanceAfter = await contract.gate.provider.getBalance(clientInBehalf || (await contract.gate.signer.getAddress()));
costPrior.walletBalanceAfter = {
weiBigNumber: balanceAfter,
weiHex: balanceAfter.toHexString(),
};
const totalCost = costPrior.walletBalancePrior.weiBigNumber.sub(costPrior.walletBalanceAfter.weiBigNumber);
costPrior.totalCost = {
weiBigNumber: totalCost,
weiHex: totalCost.toHexString(),
};
const totalGasCost = totalCost.sub(costPrior.ixilyTax.weiBigNumber);
costPrior.gasGost = {
weiBigNumber: totalGasCost,
weiHex: totalGasCost.toHexString(),
};
// TODO: get real gas price and calculate computational raw cost
const effectiveGasPrice = nftCommitted.effectiveGasPrice;
const cumulativeGasUsed = nftCommitted.cumulativeGasUsed.toNumber();
costPrior.gasPrice = {
weiBigNumber: effectiveGasPrice,
weiHex: effectiveGasPrice.toHexString(),
};
costPrior.computationalRawCost = cumulativeGasUsed;
const costTrimmed = costPrior.costRecords.costs.length > 12
? costPrior.costRecords.costs.slice(-12)
: costPrior.costRecords.costs;
costTrimmed.push(cumulativeGasUsed);
const toRecord = {
costs: costTrimmed,
};
await updateEstimatedComputationalRawCost(contract.gate.address, newIdea, clientInBehalf !== undefined, toRecord);
return costPrior;
};
exports.OperationalCostModule = {
getCurrentIxilyTax,
increaseEstimatedComputationalRawCost,
decreaseEstimatedComputationalRawCost,
getEstimatedCosts,
getFinalCosts,
};
//# sourceMappingURL=operational-cost.module.js.map