From 00fc5d21e9e819e0ef47254dd5e4721b52c9d2fa Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 15:29:00 +0200 Subject: [PATCH] use estimateGas() function in NFTFactory --- src/factories/NFTFactory.ts | 300 +++++++++++++++++------------------- 1 file changed, 140 insertions(+), 160 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index d01b207e..465135bd 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -11,7 +11,8 @@ import { getErcCreationParams, getPoolCreationParams, configHelperNetworks, - setContractDefaults + setContractDefaults, + estimateGas } from '../utils' import { Config } from '../models/index.js' import { @@ -86,25 +87,18 @@ export class NftFactory { * @return {Promise} NFT datatoken address */ public async estGasCreateNFT(address: string, nftData: NftCreateData): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .deployERC721Contract( - nftData.name, - nftData.symbol, - nftData.templateIndex, - addressZERO, - addressZERO, - nftData.tokenURI, - nftData.transferable, - nftData.owner - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.deployERC721Contract, + nftData.name, + nftData.symbol, + nftData.templateIndex, + addressZERO, + addressZERO, + nftData.tokenURI, + nftData.transferable, + nftData.owner + ) } /** @@ -131,7 +125,18 @@ export class NftFactory { if ((await this.getNFTTemplate(nftData.templateIndex)).isActive === false) { throw new Error(`Template is not active`) } - const estGas = await this.estGasCreateNFT(address, nftData) + const estGas = await estimateGas( + address, + this.factory721.methods.deployERC721Contract, + nftData.name, + nftData.symbol, + nftData.templateIndex, + addressZERO, + addressZERO, + nftData.tokenURI, + nftData.transferable, + nftData.owner + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -253,16 +258,11 @@ export class NftFactory { address: string, templateAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .add721TokenTemplate(templateAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.add721TokenTemplate, + templateAddress + ) } /** @@ -282,7 +282,11 @@ export class NftFactory { throw new Error(`Template cannot be ZERO address`) } - const estGas = await this.estGasAddNFTTemplate(address, templateAddress) + const estGas = await estimateGas( + address, + this.factory721.methods.add721TokenTemplate, + templateAddress + ) // Invoke add721TokenTemplate function of the contract const trxReceipt = await this.factory721.methods @@ -306,16 +310,11 @@ export class NftFactory { address: string, templateIndex: number ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .disable721TokenTemplate(templateIndex) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.disable721TokenTemplate, + templateIndex + ) } /** @@ -338,7 +337,11 @@ export class NftFactory { if (templateIndex === 0) { throw new Error(`Template index cannot be ZERO`) } - const estGas = await this.estGasDisableNFTTemplate(address, templateIndex) + const estGas = await estimateGas( + address, + this.factory721.methods.disable721TokenTemplate, + templateIndex + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -362,16 +365,11 @@ export class NftFactory { address: string, templateIndex: number ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .reactivate721TokenTemplate(templateIndex) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.reactivate721TokenTemplate, + templateIndex + ) } /** @@ -395,7 +393,11 @@ export class NftFactory { throw new Error(`Template index cannot be ZERO`) } - const estGas = await this.estGasReactivateNFTTemplate(address, templateIndex) + const estGas = await estimateGas( + address, + this.factory721.methods.reactivate721TokenTemplate, + templateIndex + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -419,17 +421,7 @@ export class NftFactory { address: string, templateAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .addTokenTemplate(templateAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.factory721.methods.addTokenTemplate, templateAddress) } /** @@ -449,7 +441,11 @@ export class NftFactory { throw new Error(`Template cannot be address ZERO`) } - const estGas = await this.estGasAddTokenTemplate(address, templateAddress) + const estGas = await estimateGas( + address, + this.factory721.methods.addTokenTemplate, + templateAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -473,16 +469,11 @@ export class NftFactory { address: string, templateIndex: number ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .disableTokenTemplate(templateIndex) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.disableTokenTemplate, + templateIndex + ) } /** @@ -508,7 +499,11 @@ export class NftFactory { if ((await this.getTokenTemplate(templateIndex)).isActive === false) { throw new Error(`Template is already disabled`) } - const estGas = await this.estGasDisableTokenTemplate(address, templateIndex) + const estGas = await estimateGas( + address, + this.factory721.methods.disableTokenTemplate, + templateIndex + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -532,16 +527,11 @@ export class NftFactory { address: string, templateIndex: number ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .reactivateTokenTemplate(templateIndex) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.reactivateTokenTemplate, + templateIndex + ) } /** @@ -568,7 +558,11 @@ export class NftFactory { throw new Error(`Template is already active`) } - const estGas = await this.estGasReactivateTokenTemplate(address, templateIndex) + const estGas = await estimateGas( + address, + this.factory721.methods.reactivateTokenTemplate, + templateIndex + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -591,16 +585,7 @@ export class NftFactory { address: string, orders: TokenOrder[] ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.factory721.methods - .startMultipleTokenOrder(orders) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, this.factory721.methods.startMultipleTokenOrder, orders) } /** @@ -623,7 +608,11 @@ export class NftFactory { throw new Error(`Too many orders`) } - const estGas = await this.estGasStartMultipleTokenOrder(address, orders) + const estGas = await estimateGas( + address, + this.factory721.methods.startMultipleTokenOrder, + orders + ) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -650,18 +639,13 @@ export class NftFactory { nftCreateData: NftCreateData, ercParams: Erc20CreateParams ): Promise { - // Get estimated gas value - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - const ercCreateData = getErcCreationParams(ercParams) - estGas = await this.factory721.methods - .createNftWithErc20(nftCreateData, ercCreateData) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + const ercCreateData = getErcCreationParams(ercParams) + return estimateGas( + address, + this.factory721.methods.createNftWithErc20, + nftCreateData, + ercCreateData + ) } /** @@ -680,7 +664,13 @@ export class NftFactory { ): Promise { const ercCreateData = getErcCreationParams(ercParams) - const estGas = await this.estGasCreateNftWithErc20(address, nftCreateData, ercParams) + const estGas = await estimateGas( + address, + this.factory721.methods.createNftWithErc20, + nftCreateData, + ercCreateData + ) + // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods .createNftWithErc20(nftCreateData, ercCreateData) @@ -707,18 +697,15 @@ export class NftFactory { ercParams: Erc20CreateParams, poolParams: PoolCreationParams ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - const ercCreateData = getErcCreationParams(ercParams) - const poolData = await getPoolCreationParams(this.web3, poolParams) - estGas = await this.factory721.methods - .createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + const ercCreateData = getErcCreationParams(ercParams) + const poolData = await getPoolCreationParams(this.web3, poolParams) + return estimateGas( + address, + this.factory721.methods.createNftWithErc20WithPool, + nftCreateData, + ercCreateData, + poolData + ) } /** @@ -737,15 +724,17 @@ export class NftFactory { ercParams: Erc20CreateParams, poolParams: PoolCreationParams ): Promise { - const estGas = await this.estGasCreateNftErc20WithPool( - address, - nftCreateData, - ercParams, - poolParams - ) const ercCreateData = getErcCreationParams(ercParams) const poolData = await getPoolCreationParams(this.web3, poolParams) + const estGas = await estimateGas( + address, + this.factory721.methods.createNftWithErc20WithPool, + nftCreateData, + ercCreateData, + poolData + ) + // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods .createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData) @@ -771,20 +760,15 @@ export class NftFactory { ercParams: Erc20CreateParams, freParams: FreCreationParams ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - const ercCreateData = getErcCreationParams(ercParams) const fixedData = await getFreCreationParams(freParams) - - try { - estGas = await this.factory721.methods - .createNftWithErc20WithFixedRate(nftCreateData, ercCreateData, fixedData) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.factory721.methods.createNftWithErc20WithFixedRate, + nftCreateData, + ercCreateData, + fixedData + ) } /** @@ -806,11 +790,12 @@ export class NftFactory { const ercCreateData = getErcCreationParams(ercParams) const fixedData = getFreCreationParams(freParams) - const estGas = await this.estGasCreateNftErc20WithFixedRate( + const estGas = await estimateGas( address, + this.factory721.methods.createNftWithErc20WithFixedRate, nftCreateData, - ercParams, - freParams + ercCreateData, + fixedData ) // Invoke createToken function of the contract @@ -838,20 +823,14 @@ export class NftFactory { ercParams: Erc20CreateParams, dispenserParams: DispenserCreationParams ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - const ercCreateData = getErcCreationParams(ercParams) - - try { - estGas = await this.factory721.methods - .createNftWithErc20WithDispenser(nftCreateData, ercCreateData, dispenserParams) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - LoggerInstance.error('Failed to estimate gas for createNftErc20WithDispenser', e) - } - return estGas + return estimateGas( + address, + this.factory721.methods.createNftWithErc20WithDispenser, + nftCreateData, + ercCreateData, + dispenserParams + ) } /** @@ -875,10 +854,11 @@ export class NftFactory { dispenserParams.maxBalance = Web3.utils.toWei(dispenserParams.maxBalance) dispenserParams.maxTokens = Web3.utils.toWei(dispenserParams.maxTokens) - const estGas = await this.estGasCreateNftErc20WithDispenser( + const estGas = await estimateGas( address, + this.factory721.methods.createNftWithErc20WithDispenser, nftCreateData, - ercParams, + ercCreateData, dispenserParams )