diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index b63e911cc..ecd2bd4ea 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -10,7 +10,8 @@ import { getFairGasPrice, setContractDefaults, configHelperNetworks, - getFreOrderParams + getFreOrderParams, + estimateGas } from '../utils' import { ConsumeMarketFee, @@ -95,17 +96,12 @@ export class Datatoken { this.config ) - // Estimate gas cost for mint method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .approve(spender, this.web3.utils.toWei(amount)) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + dtContract.methods.approve, + spender, + this.web3.utils.toWei(amount) + ) } /** @@ -127,12 +123,11 @@ export class Datatoken { this.config ) - const estGas = await this.estGasApprove( - dtAddress, - spender, - amount, + const estGas = await estimateGas( address, - dtContract + dtContract.methods.approve, + spender, + this.web3.utils.toWei(amount) ) // Call mint contract method @@ -169,17 +164,12 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .mint(toAddress || address, this.web3.utils.toWei(amount)) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + dtContract.methods.mint, + toAddress || address, + this.web3.utils.toWei(amount) + ) } /** @@ -204,37 +194,28 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - if (!fixedRateParams.allowedConsumer) fixedRateParams.allowedConsumer = '0x0000000000000000000000000000000000000000' const withMint = fixedRateParams.withMint ? 1 : 0 - let estGas - try { - estGas = await dtContract.methods - .createFixedRate( - fixedRateParams.fixedRateAddress, - [ - fixedRateParams.baseTokenAddress, - address, - fixedRateParams.marketFeeCollector, - fixedRateParams.allowedConsumer - ], - [ - fixedRateParams.baseTokenDecimals, - fixedRateParams.datatokenDecimals, - fixedRateParams.fixedRate, - fixedRateParams.marketFee, - withMint - ] - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + dtContract.methods.createFixedRate, + fixedRateParams.fixedRateAddress, + [ + fixedRateParams.baseTokenAddress, + address, + fixedRateParams.marketFeeCollector, + fixedRateParams.allowedConsumer + ], + [ + fixedRateParams.baseTokenDecimals, + fixedRateParams.datatokenDecimals, + fixedRateParams.fixedRate, + fixedRateParams.marketFee, + withMint + ] + ) } /** @@ -264,11 +245,23 @@ export class Datatoken { // should check ERC20Deployer role using erc721 level .. - const estGas = await this.estGasCreateFixedRate( - dtAddress, + const estGas = await estimateGas( address, - fixedRateParams, - dtContract + dtContract.methods.createFixedRate, + fixedRateParams.fixedRateAddress, + [ + fixedRateParams.baseTokenAddress, + fixedRateParams.owner, + fixedRateParams.marketFeeCollector, + fixedRateParams.allowedConsumer + ], + [ + fixedRateParams.baseTokenDecimals, + fixedRateParams.datatokenDecimals, + fixedRateParams.fixedRate, + fixedRateParams.marketFee, + withMint + ] ) // Call createFixedRate contract method @@ -325,23 +318,15 @@ export class Datatoken { if (!dispenserParams.withMint) dispenserParams.withMint = false - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .createDispenser( - dispenserAddress, - dispenserParams.maxTokens, - dispenserParams.maxBalance, - dispenserParams.withMint, - dispenserParams.allowedSwapper - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + dtContract.methods.createDispenser, + dispenserAddress, + dispenserParams.maxTokens, + dispenserParams.maxBalance, + dispenserParams.withMint, + dispenserParams.allowedSwapper + ) } /** @@ -374,12 +359,14 @@ export class Datatoken { // should check ERC20Deployer role using erc721 level .. - const estGas = await this.estGasCreateDispenser( - dtAddress, + const estGas = await estimateGas( address, + dtContract.methods.createDispenser, dispenserAddress, - dispenserParams, - dtContract + dispenserParams.maxTokens, + dispenserParams.maxBalance, + dispenserParams.withMint, + dispenserParams.allowedSwapper ) // Call createFixedRate contract method @@ -424,12 +411,11 @@ export class Datatoken { const capAvailble = await this.getCap(dtAddress) if (new Decimal(capAvailble).gte(amount)) { - const estGas = await this.estGasMint( - dtAddress, + const estGas = await estimateGas( address, - amount, - toAddress, - dtContract + dtContract.methods.mint, + toAddress || address, + this.web3.utils.toWei(amount) ) // Call mint contract method @@ -467,17 +453,7 @@ export class Datatoken { this.config ) - // Estimate gas cost for addMinter method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .addMinter(minter) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.addMinter, minter) } /** @@ -502,7 +478,7 @@ export class Datatoken { throw new Error(`Caller is not ERC20Deployer`) } // Estimate gas cost for addMinter method - const estGas = await this.estGasAddMinter(dtAddress, address, minter, dtContract) + const estGas = await estimateGas(address, dtContract.methods.addMinter, minter) // Call addMinter function of the contract const trxReceipt = await dtContract.methods.addMinter(minter).send({ @@ -537,18 +513,7 @@ export class Datatoken { // should check ERC20Deployer role using erc721 level .. - // Estimate gas for removeMinter method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .removeMinter(minter) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, dtContract.methods.removeMinter, minter) } /** @@ -574,7 +539,7 @@ export class Datatoken { throw new Error(`Caller is not ERC20Deployer`) } - const estGas = await this.estGasRemoveMinter(dtAddress, address, minter, dtContract) + const estGas = await estimateGas(address, dtContract.methods.removeMinter, minter) // Call dtContract function of the contract const trxReceipt = await dtContract.methods.removeMinter(minter).send({ @@ -607,18 +572,7 @@ export class Datatoken { this.config ) - // Estimate gas for addFeeManager method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .addPaymentManager(paymentManager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, dtContract.methods.addPaymentManager, paymentManager) } /** @@ -643,11 +597,10 @@ export class Datatoken { throw new Error(`Caller is not ERC20Deployer`) } - const estGas = await this.estGasAddPaymentManager( - dtAddress, + const estGas = await estimateGas( address, - paymentManager, - dtContract + dtContract.methods.addPaymentManager, + paymentManager ) // Call addPaymentManager function of the contract @@ -681,16 +634,7 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .removePaymentManager(paymentManager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.removePaymentManager, paymentManager) } /** @@ -715,11 +659,10 @@ export class Datatoken { throw new Error(`Caller is not ERC20Deployer`) } - const estGas = await this.estGasRemovePaymentManager( - dtAddress, + const estGas = await estimateGas( address, - paymentManager, - dtContract + dtContract.methods.removePaymentManager, + paymentManager ) // Call removeFeeManager function of the contract @@ -755,16 +698,7 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .setPaymentCollector(paymentCollector) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.setPaymentCollector, paymentCollector) } /** @@ -789,11 +723,10 @@ export class Datatoken { throw new Error(`Caller is not Fee Manager`) } - const estGas = await this.estGasSetPaymentCollector( - dtAddress, + const estGas = await estimateGas( address, - paymentCollector, - dtContract + dtContract.methods.setPaymentCollector, + paymentCollector ) // Call setFeeCollector method of the contract @@ -862,16 +795,7 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .transfer(toAddress, amount) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.transfer, toAddress, amount) } /** @@ -893,12 +817,11 @@ export class Datatoken { this.config ) try { - const estGas = await this.estGasTransfer( - dtAddress, - toAddress, - amount, + const estGas = await estimateGas( address, - dtContract + dtContract.methods.transfer, + toAddress, + amount ) // Call transfer function of the contract const trxReceipt = await dtContract.methods.transfer(toAddress, amount).send({ @@ -939,17 +862,14 @@ export class Datatoken { this.config ) - // Estimate gas for startOrder method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .startOrder(consumer, serviceIndex, providerFees, consumeMarketFee) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + dtContract.methods.startOrder, + consumer, + serviceIndex, + providerFees, + consumeMarketFee + ) } /** Start Order: called by payer or consumer prior ordering a service consume on a marketplace. @@ -981,14 +901,13 @@ export class Datatoken { } } try { - const estGas = await this.estGasStartOrder( - dtAddress, + const estGas = await estimateGas( address, + dtContract.methods.startOrder, consumer, serviceIndex, providerFees, - consumeMarketFee, - dtContract + consumeMarketFee ) const trxReceipt = await dtContract.methods @@ -1024,17 +943,12 @@ export class Datatoken { contractInstance || new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) - // Estimate gas for startOrder method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .buyFromFreAndOrder(orderParams, freParams) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + dtContract.methods.buyFromFreAndOrder, + orderParams, + freParams + ) } /** Buys 1 DT from the FRE and then startsOrder, while burning that DT @@ -1054,12 +968,11 @@ export class Datatoken { try { const freContractParams = getFreOrderParams(freParams) - const estGas = await this.estGasBuyFromFreAndOrder( - dtAddress, + const estGas = await estimateGas( address, + dtContract.methods.buyFromFreAndOrder, orderParams, - freContractParams, - dtContract + freContractParams ) const trxReceipt = await dtContract.methods @@ -1095,17 +1008,12 @@ export class Datatoken { contractInstance || new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) - // Estimate gas for startOrder method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .buyFromDispenserAndOrder(orderParams, dispenserContract) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + dtContract.methods.buyFromDispenserAndOrder, + orderParams, + dispenserContract + ) } /** Gets DT from dispenser and then startsOrder, while burning that DT @@ -1123,12 +1031,11 @@ export class Datatoken { ): Promise { const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) try { - const estGas = await this.estGasBuyFromDispenserAndOrder( - dtAddress, + const estGas = await estimateGas( address, + dtContract.methods.buyFromDispenserAndOrder, orderParams, - dispenserContract, - dtContract + dispenserContract ) const trxReceipt = await dtContract.methods @@ -1165,16 +1072,7 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .setData(value) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.setData, value) } /** setData @@ -1199,7 +1097,7 @@ export class Datatoken { this.config ) - const estGas = await this.estGasSetData(dtAddress, address, value, dtContract) + const estGas = await estimateGas(address, dtContract.methods.setData, value) // Call setData function of the contract const trxReceipt = await dtContract.methods.setData(value).send({ @@ -1229,17 +1127,7 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .cleanPermissions() - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, dtContract.methods.cleanPermissions) } /** @@ -1261,7 +1149,7 @@ export class Datatoken { this.config ) - const estGas = await this.estGasCleanPermissions(dtAddress, address, dtContract) + const estGas = await estimateGas(address, dtContract.methods.cleanPermissions) // Call cleanPermissions function of the contract const trxReceipt = await dtContract.methods.cleanPermissions().send({