From a4a7a7e4691b615324f1f926ec286e44bbfa2c9f Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 1 Apr 2022 12:49:38 +0200 Subject: [PATCH 01/31] add transfer() and estTransfer() functions --- src/utils/TokenUtils.ts | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index dd2cd649..d2f28974 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -82,12 +82,86 @@ export async function approve( }) } catch (e) { LoggerInstance.error( - `ERRPR: Failed to approve spender to spend tokens : ${e.message}` + `ERROR: Failed to approve spender to spend tokens : ${e.message}` ) } return result } +/** + * Estimate gas cost for transfer function + * @param {String} account + * @param {String} tokenAddress + * @param {String} recipient + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ +export async function estTransfer( + web3: Web3, + account: string, + tokenAddress: string, + recipient: string, + amount: string, + contractInstance?: Contract +): Promise { + const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) + + const gasLimitDefault = GASLIMIT_DEFAULT + let estGas + try { + estGas = await tokenContract.methods + .transfer(recipient, amount) + .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + LoggerInstance.error('estimate gas failed for approve!', e) + } + return estGas +} + +/** + * Moves amount tokens from the caller’s account to recipient. + * @param {String} account + * @param {String} tokenAddress + * @param {String} recipient + * @param {String} amount (always expressed as wei) + * @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed + */ +export async function transfer( + web3: Web3, + account: string, + tokenAddress: string, + recipient: string, + amount: string, + force = false +): Promise { + const tokenContract = new web3.eth.Contract(minAbi, tokenAddress) + + let result = null + const amountFormatted = await amountToUnits(web3, tokenAddress, amount) + const estGas = await estTransfer( + web3, + account, + tokenAddress, + recipient, + amountFormatted, + tokenContract + ) + + try { + result = await tokenContract.methods.transfer(recipient, amountFormatted).send({ + from: account, + gas: estGas + 1, + gasPrice: await getFairGasPrice(web3, null) + }) + } catch (e) { + LoggerInstance.error(`ERROR: Failed to transfer tokens : ${e.message}`) + } + return result +} + /** * Get Allowance for any erc20 * @param {Web3} web3 From 02fa3b89120426e5170c50bd18c0940eb74e3167 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 1 Apr 2022 16:27:41 +0200 Subject: [PATCH 02/31] remove unused parameters --- src/utils/TokenUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index d2f28974..6d28225f 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -134,8 +134,7 @@ export async function transfer( account: string, tokenAddress: string, recipient: string, - amount: string, - force = false + amount: string ): Promise { const tokenContract = new web3.eth.Contract(minAbi, tokenAddress) From 0735a4b4e3dfda4a9e370d7db6b835b652d18765 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Fri, 1 Apr 2022 16:31:37 +0200 Subject: [PATCH 03/31] update comments related to amount units --- src/utils/TokenUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index 6d28225f..ec29d788 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -45,7 +45,7 @@ export async function estApprove( * @param {String} account * @param {String} tokenAddress * @param {String} spender - * @param {String} amount (always expressed as wei) + * @param {String} amount amount of ERC20 tokens, expressed without decimals (not as wei) * @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed */ export async function approve( @@ -126,7 +126,7 @@ export async function estTransfer( * @param {String} account * @param {String} tokenAddress * @param {String} recipient - * @param {String} amount (always expressed as wei) + * @param {String} amount amount of ERC20 tokens, expressed without decimals (not as wei) * @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed */ export async function transfer( From f34573be44fc28caca122ae758204f4bc4b85075 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 09:37:30 +0200 Subject: [PATCH 04/31] create a generic estimateGas() function --- src/utils/TokenUtils.ts | 90 ++++++++++++----------------------------- 1 file changed, 26 insertions(+), 64 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index ec29d788..a2148a3b 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -8,34 +8,31 @@ import Web3 from 'web3' import { GASLIMIT_DEFAULT } from '.' /** - * Estimate gas cost for approval function - * @param {String} account - * @param {String} tokenAddress - * @param {String} spender - * @param {String} amount - * @param {String} force - * @param {Contract} contractInstance optional contract instance - * @return {Promise} + * Estimates the gas used when a function would be executed on chain + * @param {Contract} tokenContract token contract instance + * @param {string} from account that calls the function + * @param {string} functionSignature signature of the function + * @param {...any[]} args arguments of the function + * @return {Promise} gas cost of the function */ -export async function estApprove( - web3: Web3, - account: string, - tokenAddress: string, - spender: string, - amount: string, - contractInstance?: Contract +export async function estimateGas( + tokenContract: Contract, + from: string, + functionSignature: string, + ...args: any[] ): Promise { - const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) - const gasLimitDefault = GASLIMIT_DEFAULT let estGas try { - estGas = await tokenContract.methods - .approve(spender, amount) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) + estGas = await tokenContract.methods[functionSignature].apply(null, args).estimateGas( + { + from: from + }, + (err, estGas) => (err ? gasLimitDefault : estGas) + ) } catch (e) { estGas = gasLimitDefault - LoggerInstance.error('estimate gas failed for approve!', e) + LoggerInstance.error(`ERROR: Estimate gas failed for ${functionSignature}!`, e) } return estGas } @@ -65,13 +62,12 @@ export async function approve( } let result = null const amountFormatted = await amountToUnits(web3, tokenAddress, amount) - const estGas = await estApprove( - web3, + const estGas = await estimateGas( + tokenContract, account, - tokenAddress, + 'approve(address,uint256)', spender, - amountFormatted, - tokenContract + amountFormatted ) try { @@ -88,39 +84,6 @@ export async function approve( return result } -/** - * Estimate gas cost for transfer function - * @param {String} account - * @param {String} tokenAddress - * @param {String} recipient - * @param {String} amount - * @param {String} force - * @param {Contract} contractInstance optional contract instance - * @return {Promise} - */ -export async function estTransfer( - web3: Web3, - account: string, - tokenAddress: string, - recipient: string, - amount: string, - contractInstance?: Contract -): Promise { - const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) - - const gasLimitDefault = GASLIMIT_DEFAULT - let estGas - try { - estGas = await tokenContract.methods - .transfer(recipient, amount) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - LoggerInstance.error('estimate gas failed for approve!', e) - } - return estGas -} - /** * Moves amount tokens from the caller’s account to recipient. * @param {String} account @@ -140,13 +103,12 @@ export async function transfer( let result = null const amountFormatted = await amountToUnits(web3, tokenAddress, amount) - const estGas = await estTransfer( - web3, + const estGas = await estimateGas( + tokenContract, account, - tokenAddress, + 'transfer(address,uint256)', recipient, - amountFormatted, - tokenContract + amountFormatted ) try { From 2fe89b476af38ae20adba546905c3d0e80a0ded2 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 4 Apr 2022 10:22:03 +0200 Subject: [PATCH 05/31] update comments --- src/utils/TokenUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index a2148a3b..0095aac6 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -42,7 +42,7 @@ export async function estimateGas( * @param {String} account * @param {String} tokenAddress * @param {String} spender - * @param {String} amount amount of ERC20 tokens, expressed without decimals (not as wei) + * @param {String} amount amount of ERC20 tokens (not as wei) * @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed */ export async function approve( @@ -89,7 +89,7 @@ export async function approve( * @param {String} account * @param {String} tokenAddress * @param {String} recipient - * @param {String} amount amount of ERC20 tokens, expressed without decimals (not as wei) + * @param {String} amount amount of ERC20 tokens (not as wei) * @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed */ export async function transfer( From 605812ee7f038f24e4bf06cd8397a3643c10358b Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 10:24:27 +0200 Subject: [PATCH 06/31] estimate gas without using the function signature --- src/utils/TokenUtils.ts | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index 0095aac6..18973849 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,5 +1,4 @@ import Decimal from 'decimal.js' -import { Contract } from 'web3-eth-contract' import { amountToUnits, getFairGasPrice, unitsToAmount } from './ContractUtils' import { minAbi } from './minAbi' import LoggerInstance from './Logger' @@ -9,32 +8,28 @@ import { GASLIMIT_DEFAULT } from '.' /** * Estimates the gas used when a function would be executed on chain - * @param {Contract} tokenContract token contract instance * @param {string} from account that calls the function - * @param {string} functionSignature signature of the function + * @param {Function} functionToEstimateGas function that we need to estimate the gas * @param {...any[]} args arguments of the function * @return {Promise} gas cost of the function */ export async function estimateGas( - tokenContract: Contract, from: string, - functionSignature: string, + functionToEstimateGas: Function, ...args: any[] ): Promise { - const gasLimitDefault = GASLIMIT_DEFAULT - let estGas + let estimatedGas = GASLIMIT_DEFAULT try { - estGas = await tokenContract.methods[functionSignature].apply(null, args).estimateGas( + estimatedGas = await functionToEstimateGas.apply(null, args).estimateGas( { from: from }, - (err, estGas) => (err ? gasLimitDefault : estGas) + (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas) ) } catch (e) { - estGas = gasLimitDefault - LoggerInstance.error(`ERROR: Estimate gas failed for ${functionSignature}!`, e) + LoggerInstance.error(`ERROR: Estimate gas failed!`, e) } - return estGas + return estimatedGas } /** @@ -63,9 +58,8 @@ export async function approve( let result = null const amountFormatted = await amountToUnits(web3, tokenAddress, amount) const estGas = await estimateGas( - tokenContract, account, - 'approve(address,uint256)', + tokenContract.methods.approve, spender, amountFormatted ) @@ -104,9 +98,8 @@ export async function transfer( let result = null const amountFormatted = await amountToUnits(web3, tokenAddress, amount) const estGas = await estimateGas( - tokenContract, account, - 'transfer(address,uint256)', + tokenContract.methods.transfer, recipient, amountFormatted ) From 23101a185fb4f885a8c699b2c8795278f1169dd9 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 14:53:04 +0200 Subject: [PATCH 07/31] add generic estimateGas() function --- src/utils/ContractUtils.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 139791dd..1d55f4c9 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -11,6 +11,7 @@ import { import { Config } from '../models' import { minAbi } from './minAbi' import LoggerInstance from './Logger' +import { GASLIMIT_DEFAULT } from './Constants' export function setContractDefaults(contract: Contract, config: Config): Contract { if (config) { @@ -162,3 +163,29 @@ export async function amountToUnits( LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18', ${e.message}`) } } + +/** + * Estimates the gas used when a function would be executed on chain + * @param {string} from account that calls the function + * @param {Function} functionToEstimateGas function that we need to estimate the gas + * @param {...any[]} args arguments of the function + * @return {Promise} gas cost of the function + */ +export async function estimateGas( + from: string, + functionToEstimateGas: Function, + ...args: any[] +): Promise { + let estimatedGas = GASLIMIT_DEFAULT + try { + estimatedGas = await functionToEstimateGas.apply(null, args).estimateGas( + { + from: from + }, + (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas) + ) + } catch (e) { + LoggerInstance.error(`ERROR: Estimate gas failed!`, e) + } + return estimatedGas +} From 2af104497a1f1c4336378a123747d23068c36165 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 15:28:35 +0200 Subject: [PATCH 08/31] change return type --- src/utils/ContractUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 1d55f4c9..6386c303 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -175,7 +175,7 @@ export async function estimateGas( from: string, functionToEstimateGas: Function, ...args: any[] -): Promise { +): Promise { let estimatedGas = GASLIMIT_DEFAULT try { estimatedGas = await functionToEstimateGas.apply(null, args).estimateGas( From 00fc5d21e9e819e0ef47254dd5e4721b52c9d2fa Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 15:29:00 +0200 Subject: [PATCH 09/31] 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 ) From c936c47260b84a191cd7d7717d04c025c24043a7 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 16:21:43 +0200 Subject: [PATCH 10/31] use estimateGas() function in Router --- src/pools/Router.ts | 229 ++++++++++++++++---------------------------- 1 file changed, 82 insertions(+), 147 deletions(-) diff --git a/src/pools/Router.ts b/src/pools/Router.ts index 0c6f56f5..1852008d 100644 --- a/src/pools/Router.ts +++ b/src/pools/Router.ts @@ -3,7 +3,12 @@ import Web3 from 'web3' import { TransactionReceipt } from 'web3-core' import { AbiItem } from 'web3-utils' import defaultRouter from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' -import { getFairGasPrice, setContractDefaults, configHelperNetworks } from '../utils' +import { + getFairGasPrice, + setContractDefaults, + configHelperNetworks, + estimateGas +} from '../utils' import { Operation } from '../@types/Router' import { Config } from '../models/index.js' @@ -47,16 +52,7 @@ export class Router { * @return {Promise} Transaction receipt */ public async estGasBuyDTBatch(address: string, operations: Operation[]): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .buyDTBatch(operations) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, this.router.methods.buyDTBatch, operations) } /** @@ -69,7 +65,7 @@ export class Router { address: string, operations: Operation[] ): Promise { - const estGas = await this.estGasBuyDTBatch(address, operations) + const estGas = await estimateGas(address, this.router.methods.buyDTBatch, operations) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.buyDTBatch(operations).send({ @@ -134,19 +130,8 @@ export class Router { address: string, tokenAddress: string, contractInstance?: Contract - ) { - const routerContract = contractInstance || this.router - - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await routerContract.methods - .addApprovedToken(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + ): Promise { + return estimateGas(address, this.router.methods.addApprovedToken, tokenAddress) } /** @@ -163,7 +148,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasAddApprovedToken(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.addApprovedToken, + tokenAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addApprovedToken(tokenAddress).send({ @@ -186,19 +175,8 @@ export class Router { address: string, tokenAddress: string, contractInstance?: Contract - ) { - const routerContract = contractInstance || this.router - - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await routerContract.methods - .removeApprovedToken(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + ): Promise { + return estimateGas(address, this.router.methods.removeApprovedToken, tokenAddress) } /** @@ -215,7 +193,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasRemoveApprovedToken(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.removeApprovedToken, + tokenAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.removeApprovedToken(tokenAddress).send({ @@ -234,17 +216,7 @@ export class Router { * @return {Promise} */ public async estGasAddSSContract(address: string, tokenAddress: string): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .addSSContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.addSSContract, tokenAddress) } /** @@ -261,7 +233,12 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasAddSSContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.addSSContract, + tokenAddress + ) + // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addSSContract(tokenAddress).send({ from: address, @@ -282,17 +259,7 @@ export class Router { address: string, tokenAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .removeSSContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.removeSSContract, tokenAddress) } /** @@ -309,7 +276,12 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasRemoveSSContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.removeSSContract, + tokenAddress + ) + // Invoke createToken function of the contract const trxReceipt = await this.router.methods.removeSSContract(tokenAddress).send({ from: address, @@ -330,17 +302,7 @@ export class Router { address: string, tokenAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .addFixedRateContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.addFixedRateContract, tokenAddress) } /** @@ -357,7 +319,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasAddFixedRateContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.addFixedRateContract, + tokenAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addFixedRateContract(tokenAddress).send({ @@ -379,17 +345,7 @@ export class Router { address: string, tokenAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .removeFixedRateContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.removeFixedRateContract, tokenAddress) } /** @@ -406,7 +362,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasRemoveFixedRateContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.removeFixedRateContract, + tokenAddress + ) // Invoke removeFixedRateContract function of the contract const trxReceipt = await this.router.methods @@ -430,17 +390,7 @@ export class Router { address: string, tokenAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .addDispenserContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.addDispenserContract, tokenAddress) } /** @@ -457,7 +407,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasAddDispenserContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.addDispenserContract, + tokenAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addDispenserContract(tokenAddress).send({ @@ -479,17 +433,7 @@ export class Router { address: string, tokenAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .removeDispenserContract(tokenAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.removeDispenserContract, tokenAddress) } /** @@ -506,7 +450,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasRemoveDispenserContract(address, tokenAddress) + const estGas = await estimateGas( + address, + this.router.methods.removeDispenserContract, + tokenAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods @@ -547,17 +495,14 @@ export class Router { newConsumeFee: number, newProviderFee: number ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .updateOPCFee(newSwapOceanFee, newSwapNonOceanFee, newConsumeFee, newProviderFee) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + this.router.methods.updateOPCFee, + newSwapOceanFee, + newSwapNonOceanFee, + newConsumeFee, + newProviderFee + ) } /** @@ -580,8 +525,9 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasUpdateOPCFee( + const estGas = await estimateGas( address, + this.router.methods.updateOPCFee, newSwapOceanFee, newSwapNonOceanFee, newConsumeFee, @@ -610,17 +556,7 @@ export class Router { address: string, templateAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .addPoolTemplate(templateAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, this.router.methods.addPoolTemplate, templateAddress) } /** @@ -637,7 +573,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasAddPoolTemplate(address, templateAddress) + const estGas = await estimateGas( + address, + this.router.methods.addPoolTemplate, + templateAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods.addPoolTemplate(templateAddress).send({ @@ -659,16 +599,7 @@ export class Router { address: string, templateAddress: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.router.methods - .removePoolTemplate(templateAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, this.router.methods.removePoolTemplate, templateAddress) } /** @@ -685,7 +616,11 @@ export class Router { throw new Error(`Caller is not Router Owner`) } - const estGas = await this.estGasRemovePoolTemplate(address, templateAddress) + const estGas = await estimateGas( + address, + this.router.methods.removePoolTemplate, + templateAddress + ) // Invoke createToken function of the contract const trxReceipt = await this.router.methods From 1380661c9773108ca1f0610903bd71e396ea8ba0 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 16:26:40 +0200 Subject: [PATCH 11/31] use estimateGas() function in TokenUtils --- src/utils/TokenUtils.ts | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index dd2cd649..8cbc8d8e 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,6 +1,6 @@ import Decimal from 'decimal.js' import { Contract } from 'web3-eth-contract' -import { amountToUnits, getFairGasPrice, unitsToAmount } from './ContractUtils' +import { amountToUnits, estimateGas, getFairGasPrice, unitsToAmount } from './ContractUtils' import { minAbi } from './minAbi' import LoggerInstance from './Logger' import { TransactionReceipt } from 'web3-core' @@ -27,17 +27,7 @@ export async function estApprove( ): Promise { const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) - const gasLimitDefault = GASLIMIT_DEFAULT - let estGas - try { - estGas = await tokenContract.methods - .approve(spender, amount) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - LoggerInstance.error('estimate gas failed for approve!', e) - } - return estGas + return estimateGas(account, tokenContract.methods.approve, spender, amount) } /** @@ -65,13 +55,11 @@ export async function approve( } let result = null const amountFormatted = await amountToUnits(web3, tokenAddress, amount) - const estGas = await estApprove( - web3, + const estGas = await estimateGas( account, - tokenAddress, + tokenContract.methods.approve, spender, - amountFormatted, - tokenContract + amountFormatted ) try { From 652ec73892b3cd9efecebce5f49c741be639a4fd Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 17:21:41 +0200 Subject: [PATCH 12/31] solve lint errors --- src/utils/TokenUtils.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index 8cbc8d8e..e0ba3867 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,11 +1,15 @@ import Decimal from 'decimal.js' import { Contract } from 'web3-eth-contract' -import { amountToUnits, estimateGas, getFairGasPrice, unitsToAmount } from './ContractUtils' +import { + amountToUnits, + estimateGas, + getFairGasPrice, + unitsToAmount +} from './ContractUtils' import { minAbi } from './minAbi' import LoggerInstance from './Logger' import { TransactionReceipt } from 'web3-core' import Web3 from 'web3' -import { GASLIMIT_DEFAULT } from '.' /** * Estimate gas cost for approval function From 2c0f3ae108e22a8bdddcd7165aadece72b33e680 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 17:29:55 +0200 Subject: [PATCH 13/31] use estimateGas() function in SideStaking --- src/pools/ssContracts/SideStaking.ts | 51 ++++++++++++---------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/pools/ssContracts/SideStaking.ts b/src/pools/ssContracts/SideStaking.ts index b1dda5d0..20a80e03 100644 --- a/src/pools/ssContracts/SideStaking.ts +++ b/src/pools/ssContracts/SideStaking.ts @@ -2,7 +2,12 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Contract } from 'web3-eth-contract' -import { LoggerInstance, getFairGasPrice, configHelperNetworks } from '../../utils' +import { + LoggerInstance, + getFairGasPrice, + configHelperNetworks, + estimateGas +} from '../../utils' import BigNumber from 'bignumber.js' import SideStakingTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json' import defaultErc20Abi from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' @@ -288,16 +293,7 @@ export class SideStaking { const sideStaking = contractInstance || new this.web3.eth.Contract(this.ssAbi as AbiItem[], ssAddress) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await sideStaking.methods - .getVesting(datatokenAddress) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, sideStaking.methods.getVesting, datatokenAddress) } /** Send vested tokens available to the publisher address, can be called by anyone @@ -315,12 +311,12 @@ export class SideStaking { const sideStaking = new this.web3.eth.Contract(this.ssAbi, ssAddress) let result = null - const estGas = await this.estGetVesting( + const estGas = await estimateGas( account, - ssAddress, - datatokenAddress, - sideStaking + sideStaking.methods.getVesting, + datatokenAddress ) + try { result = await sideStaking.methods.getVesting(datatokenAddress).send({ from: account, @@ -352,16 +348,13 @@ export class SideStaking { const sideStaking = contractInstance || new this.web3.eth.Contract(this.ssAbi as AbiItem[], ssAddress) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await sideStaking.methods - .setPoolSwapFee(datatokenAddress, poolAddress, swapFee) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + account, + sideStaking.methods.setPoolSwapFee, + datatokenAddress, + poolAddress, + swapFee + ) } /** Send vested tokens available to the publisher address, can be called by anyone @@ -381,14 +374,14 @@ export class SideStaking { const sideStaking = new this.web3.eth.Contract(this.ssAbi, ssAddress) let result = null - const estGas = await this.estSetPoolSwapFee( + const estGas = await estimateGas( account, - ssAddress, + sideStaking.methods.setPoolSwapFee, datatokenAddress, poolAddress, - swapFee, - sideStaking + swapFee ) + try { result = await sideStaking.methods .setPoolSwapFee(datatokenAddress, poolAddress, swapFee) From 09d768398660b73b8cfc4b9c9ecdfa2e73145049 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 17:41:00 +0200 Subject: [PATCH 14/31] use estimateGas() function in Dispenser --- src/pools/dispenser/Dispenser.ts | 153 ++++++++++++++----------------- 1 file changed, 71 insertions(+), 82 deletions(-) diff --git a/src/pools/dispenser/Dispenser.ts b/src/pools/dispenser/Dispenser.ts index 22b1f42b..87606427 100644 --- a/src/pools/dispenser/Dispenser.ts +++ b/src/pools/dispenser/Dispenser.ts @@ -8,7 +8,8 @@ import { LoggerInstance as logger, getFairGasPrice, configHelperNetworks, - setContractDefaults + setContractDefaults, + estimateGas } from '../../utils/' import { Datatoken } from '../../tokens' import { Config } from '../../models/index.js' @@ -90,23 +91,15 @@ export class Dispenser { maxBalance: string, allowedSwapper: string ): Promise { - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await this.dispenserContract.methods - .create( - dtAddress, - this.web3.utils.toWei(maxTokens), - this.web3.utils.toWei(maxBalance), - address, - allowedSwapper - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + this.dispenserContract.methods.create, + dtAddress, + this.web3.utils.toWei(maxTokens), + this.web3.utils.toWei(maxBalance), + address, + allowedSwapper + ) } /** @@ -125,11 +118,13 @@ export class Dispenser { maxBalance: string, allowedSwapper: string ): Promise { - const estGas = await this.estGasCreate( - dtAddress, + const estGas = await estimateGas( + address, + this.dispenserContract.methods.create, + dtAddress, + this.web3.utils.toWei(maxTokens), + this.web3.utils.toWei(maxBalance), address, - maxTokens, - maxBalance, allowedSwapper ) @@ -164,20 +159,13 @@ export class Dispenser { maxBalance: string, address: string ): Promise { - let estGas - const gasLimitDefault = this.GASLIMIT_DEFAULT - try { - estGas = await this.dispenserContract.methods - .activate( - dtAddress, - this.web3.utils.toWei(maxTokens), - this.web3.utils.toWei(maxBalance) - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.dispenserContract.methods.activate, + dtAddress, + this.web3.utils.toWei(maxTokens), + this.web3.utils.toWei(maxBalance) + ) } /** @@ -195,7 +183,14 @@ export class Dispenser { address: string ): Promise { try { - const estGas = await this.estGasActivate(dtAddress, maxTokens, maxBalance, address) + const estGas = await estimateGas( + address, + this.dispenserContract.methods.activate, + dtAddress, + this.web3.utils.toWei(maxTokens), + this.web3.utils.toWei(maxBalance) + ) + const trxReceipt = await this.dispenserContract.methods .activate( dtAddress, @@ -221,16 +216,7 @@ export class Dispenser { * @return {Promise} */ public async estGasDeactivate(dtAddress: string, address: string): Promise { - let estGas - const gasLimitDefault = this.GASLIMIT_DEFAULT - try { - estGas = await this.dispenserContract.methods - .deactivate(dtAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, this.dispenserContract.methods.deactivate, dtAddress) } /** @@ -244,7 +230,12 @@ export class Dispenser { address: string ): Promise { try { - const estGas = await this.estGasDeactivate(dtAddress, address) + const estGas = await estimateGas( + address, + this.dispenserContract.methods.deactivate, + dtAddress + ) + const trxReceipt = await this.dispenserContract.methods.deactivate(dtAddress).send({ from: address, gas: estGas + 1, @@ -269,16 +260,12 @@ export class Dispenser { address: string, newAllowedSwapper: string ): Promise { - let estGas - const gasLimitDefault = this.GASLIMIT_DEFAULT - try { - estGas = await this.dispenserContract.methods - .setAllowedSwapper(dtAddress, newAllowedSwapper) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.dispenserContract.methods.setAllowedSwapper, + dtAddress, + newAllowedSwapper + ) } /** @@ -294,11 +281,13 @@ export class Dispenser { newAllowedSwapper: string ): Promise { try { - const estGas = await this.estGasSetAllowedSwapper( - dtAddress, + const estGas = await estimateGas( address, + this.dispenserContract.methods.setAllowedSwapper, + dtAddress, newAllowedSwapper ) + const trxReceipt = await this.dispenserContract.methods .setAllowedSwapper(dtAddress, newAllowedSwapper) .send({ @@ -326,16 +315,13 @@ export class Dispenser { amount: string = '1', destination: string ): Promise { - let estGas - const gasLimitDefault = this.GASLIMIT_DEFAULT - try { - estGas = await this.dispenserContract.methods - .dispense(dtAddress, this.web3.utils.toWei(amount), destination) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + this.dispenserContract.methods.dispense, + dtAddress, + this.web3.utils.toWei(amount), + destination + ) } /** @@ -354,7 +340,14 @@ export class Dispenser { amount: string = '1', destination: string ): Promise { - const estGas = await this.estGasDispense(dtAddress, address, amount, destination) + const estGas = await estimateGas( + address, + this.dispenserContract.methods.dispense, + dtAddress, + this.web3.utils.toWei(amount), + destination + ) + try { const trxReceipt = await this.dispenserContract.methods .dispense(dtAddress, this.web3.utils.toWei(amount), destination) @@ -378,16 +371,7 @@ export class Dispenser { * @return {Promise} */ public async estGasOwnerWithdraw(dtAddress: string, address: string): Promise { - let estGas - const gasLimitDefault = this.GASLIMIT_DEFAULT - try { - estGas = await this.dispenserContract.methods - .ownerWithdraw(dtAddress) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, this.dispenserContract.methods.ownerWithdraw, dtAddress) } /** @@ -400,7 +384,12 @@ export class Dispenser { dtAddress: string, address: string ): Promise { - const estGas = await this.estGasOwnerWithdraw(dtAddress, address) + const estGas = await estimateGas( + address, + this.dispenserContract.methods.ownerWithdraw, + dtAddress + ) + try { const trxReceipt = await this.dispenserContract.methods .ownerWithdraw(dtAddress) From 47e47863b387108e83373247dd1f56a2f0f54a81 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 18:08:21 +0200 Subject: [PATCH 15/31] use estimateGas() function in Pool --- src/pools/balancer/Pool.ts | 269 ++++++++++++++++--------------------- 1 file changed, 113 insertions(+), 156 deletions(-) diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 3a936956..802f128f 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -8,7 +8,8 @@ import { setContractDefaults, unitsToAmount, amountToUnits, - LoggerInstance + LoggerInstance, + estimateGas } from '../../utils' import BigNumber from 'bignumber.js' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' @@ -91,16 +92,7 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .setSwapFee(fee) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, poolContract.methods.setSwapFee, fee) } /** @@ -121,7 +113,7 @@ export class Pool { this.config ) let result = null - const estGas = await this.estSetSwapFee(account, poolAddress, fee) + const estGas = await estimateGas(account, pool.methods.setSwapFee, fee) try { result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({ @@ -595,16 +587,7 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .collectOPC() - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, poolContract.methods.collectOPC) } /** @@ -619,7 +602,7 @@ export class Pool { this.config ) let result = null - const estGas = await this.estCollectOPC(address, poolAddress) + const estGas = await estimateGas(address, pool.methods.collectOPC) try { result = await pool.methods.collectOPC().send({ @@ -653,16 +636,7 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .collectMarketFee() - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, poolContract.methods.collectMarketFee) } /** @@ -684,7 +658,7 @@ export class Pool { this.config ) let result = null - const estGas = await this.estCollectMarketFee(address, poolAddress) + const estGas = await estimateGas(address, pool.methods.collectMarketFee) try { result = await pool.methods.collectMarketFee().send({ @@ -721,16 +695,12 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .updatePublishMarketFee(newPublishMarketAddress, newPublishMarketSwapFee) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.updatePublishMarketFee, + newPublishMarketAddress, + this.web3.utils.toWei(newPublishMarketSwapFee) + ) } /** @@ -756,9 +726,9 @@ export class Pool { ) let result = null - const estGas = await this.estUpdatePublishMarketFee( + const estGas = await estimateGas( address, - poolAddress, + pool.methods.updatePublishMarketFee, newPublishMarketAddress, this.web3.utils.toWei(newPublishMarketSwapFee) ) @@ -822,28 +792,21 @@ export class Pool { ) : MaxUint256 - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .swapExactAmountIn( - [ - tokenInOutMarket.tokenIn, - tokenInOutMarket.tokenOut, - tokenInOutMarket.marketFeeAddress - ], - [ - tokenAmountIn, - minAmountOut, - maxPrice, - this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) - ] - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.swapExactAmountIn, + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + tokenAmountIn, + minAmountOut, + maxPrice, + this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) + ] + ) } /** @@ -875,13 +838,6 @@ export class Pool { throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`) } - const estGas = await this.estSwapExactAmountIn( - address, - poolAddress, - tokenInOutMarket, - amountsInOutMaxFee - ) - const tokenAmountIn = await amountToUnits( this.web3, tokenInOutMarket.tokenIn, @@ -894,8 +850,6 @@ export class Pool { amountsInOutMaxFee.minAmountOut ) - let result = null - const maxPrice = amountsInOutMaxFee.maxPrice ? await amountToUnits( this.web3, @@ -904,6 +858,23 @@ export class Pool { ) : MaxUint256 + const estGas = await estimateGas( + address, + pool.methods.swapExactAmountIn, + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + tokenAmountIn, + minAmountOut, + maxPrice, + this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) + ] + ) + + let result = null try { result = await pool.methods .swapExactAmountIn( @@ -954,8 +925,6 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - const maxAmountIn = await amountToUnits( this.web3, tokenInOutMarket.tokenIn, @@ -976,27 +945,21 @@ export class Pool { ) : MaxUint256 - let estGas - try { - estGas = await poolContract.methods - .swapExactAmountOut( - [ - tokenInOutMarket.tokenIn, - tokenInOutMarket.tokenOut, - tokenInOutMarket.marketFeeAddress - ], - [ - maxAmountIn, - tokenAmountOut, - maxPrice, - this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) - ] - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.swapExactAmountOut, + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + maxAmountIn, + tokenAmountOut, + maxPrice, + this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) + ] + ) } /** @@ -1024,13 +987,6 @@ export class Pool { throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`) } - const estGas = await this.estSwapExactAmountOut( - account, - poolAddress, - tokenInOutMarket, - amountsInOutMaxFee - ) - const maxAmountIn = await amountToUnits( this.web3, tokenInOutMarket.tokenIn, @@ -1051,6 +1007,22 @@ export class Pool { ) : MaxUint256 + const estGas = await estimateGas( + account, + pool.methods.swapExactAmountOut, + [ + tokenInOutMarket.tokenIn, + tokenInOutMarket.tokenOut, + tokenInOutMarket.marketFeeAddress + ], + [ + maxAmountIn, + tokenAmountOut, + maxPrice, + this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) + ] + ) + try { result = await pool.methods .swapExactAmountOut( @@ -1100,16 +1072,12 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .joinPool(poolAmountOut, maxAmountsIn) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.joinPool, + poolAmountOut, + maxAmountsIn + ) } /** @@ -1143,9 +1111,9 @@ export class Pool { let result = null - const estGas = await this.estJoinPool( + const estGas = await estimateGas( address, - poolAddress, + pool.methods.joinPool, this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn ) @@ -1187,16 +1155,12 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .exitPool(poolAmountIn, minAmountsOut) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.exitPool, + poolAmountIn, + minAmountsOut + ) } /** @@ -1226,14 +1190,15 @@ export class Pool { const amount = await amountToUnits(this.web3, tokens[i], minAmountsOut[i]) weiMinAmountsOut.push(amount) } - let result = null - const estGas = await this.estExitPool( + + const estGas = await estimateGas( account, - poolAddress, + pool.methods.exitPool, this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut ) + let result = null try { result = await pool.methods .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) @@ -1272,16 +1237,12 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .joinswapExternAmountIn(tokenAmountIn, minPoolAmountOut) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.joinswapExternAmountIn, + tokenAmountIn, + minPoolAmountOut + ) } /** @@ -1312,9 +1273,9 @@ export class Pool { } const amountInFormatted = await amountToUnits(this.web3, tokenIn, tokenAmountIn) - const estGas = await this.estJoinswapExternAmountIn( + const estGas = await estimateGas( account, - poolAddress, + pool.methods.joinswapExternAmountIn, amountInFormatted, this.web3.utils.toWei(minPoolAmountOut) ) @@ -1360,16 +1321,12 @@ export class Pool { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await poolContract.methods - .exitswapPoolAmountIn(poolAmountIn, minTokenAmountOut) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + poolContract.methods.exitswapPoolAmountIn, + poolAmountIn, + minTokenAmountOut + ) } /** @@ -1411,9 +1368,9 @@ export class Pool { await this.getBaseToken(poolAddress), minTokenAmountOut ) - const estGas = await this.estExitswapPoolAmountIn( + const estGas = await estimateGas( account, - poolAddress, + pool.methods.exitswapPoolAmountIn, this.web3.utils.toWei(poolAmountIn), minTokenOutFormatted ) From f5f8fdd84b793c54152ad8ac416252443b2427f4 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 18:08:32 +0200 Subject: [PATCH 16/31] fix pool test amount error --- test/unit/pools/balancer/Pool.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 11f78373..05c3f901 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -85,7 +85,7 @@ describe('Pool unit test', () => { contracts.erc721FactoryAddress ) - assert(parseInt(allowCheck) >= 8000) + assert(parseInt(allowCheck) >= 2000) allowCheck = await allowance( web3, contracts.usdcAddress, From e2a4c32a65c47c07412d4faf28b1c63a0e62fa4c Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 23:37:54 +0200 Subject: [PATCH 17/31] use estimateGas() function in FixedRateExchange --- src/pools/fixedRate/FixedRateExchange.ts | 295 ++++++++++------------- 1 file changed, 128 insertions(+), 167 deletions(-) diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 5104789c..04048a00 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -9,7 +9,8 @@ import { configHelperNetworks, setContractDefaults, amountToUnits, - unitsToAmount + unitsToAmount, + estimateGas } from '../../utils' import { Config } from '../../models/index.js' import { PriceAndFees } from '../..' @@ -131,22 +132,16 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .buyDT( - datatokenAddress, - dtAmount, - maxBaseTokenAmount, - consumeMarketAddress, - consumeMarketFee - ) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.buyDT, + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) } /** @@ -178,8 +173,9 @@ export class FixedRateExchange { maxBaseTokenAmount ) - const estGas = await this.estBuyDT( + const estGas = await estimateGas( address, + this.contract.methods.buyDT, exchangeId, dtAmountFormatted, maxBtFormatted, @@ -227,22 +223,16 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .sellDT( - datatokenAddress, - dtAmount, - maxBaseTokenAmount, - consumeMarketAddress, - consumeMarketFee - ) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.sellDT, + datatokenAddress, + dtAmount, + maxBaseTokenAmount, + consumeMarketAddress, + consumeMarketFee + ) } /** @@ -273,8 +263,9 @@ export class FixedRateExchange { exchange.baseToken, minBaseTokenAmount ) - const estGas = await this.estBuyDT( + const estGas = await estimateGas( address, + this.contract.methods.sellDT, exchangeId, dtAmountFormatted, minBtFormatted, @@ -328,16 +319,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .setRate(exchangeId, await this.web3.utils.toWei(newRate)) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.setRate, + exchangeId, + await this.web3.utils.toWei(newRate) + ) } /** @@ -352,7 +340,12 @@ export class FixedRateExchange { exchangeId: string, newRate: string ): Promise { - const estGas = await this.estSetRate(address, exchangeId, newRate) + const estGas = await estimateGas( + address, + this.contract.methods.setRate, + exchangeId, + this.web3.utils.toWei(newRate) + ) const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(newRate)) .send({ @@ -378,16 +371,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .setRate(exchangeId, newAllowedSwapper) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.setRate, exchangeId, newAllowedSwapper) } /** @@ -402,7 +387,12 @@ export class FixedRateExchange { exchangeId: string, newAllowedSwapper: string ): Promise { - const estGas = await this.estSetAllowedSwapper(address, exchangeId, newAllowedSwapper) + const estGas = await estimateGas( + address, + this.contract.methods.setAllowedSwapper, + exchangeId, + newAllowedSwapper + ) const trxReceipt = await this.contract.methods .setAllowedSwapper(exchangeId, newAllowedSwapper) .send({ @@ -426,16 +416,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleExchangeState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } /** @@ -452,7 +434,11 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.active === true) return null - const estGas = await this.estActivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleExchangeState, + exchangeId + ) const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, gas: estGas + 1, @@ -474,16 +460,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleExchangeState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleExchangeState, exchangeId) } /** @@ -500,7 +478,11 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.active === false) return null - const estGas = await this.estDeactivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleExchangeState, + exchangeId + ) const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ from: address, @@ -711,16 +693,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleMintState(exchangeId, true) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleMintState, exchangeId, true) } /** @@ -737,7 +711,12 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.withMint === true) return null - const estGas = await this.estActivateMint(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleMintState, + exchangeId, + true + ) const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, true) .send({ @@ -761,16 +740,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .toggleMintState(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.toggleMintState, exchangeId) } /** @@ -787,7 +758,12 @@ export class FixedRateExchange { if (!exchange) return null if (exchange.withMint === false) return null - const estGas = await this.estDeactivate(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.toggleMintState, + exchangeId, + false + ) const trxReceipt = await this.contract.methods .toggleMintState(exchangeId, false) @@ -815,20 +791,11 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) - try { - estGas = await fixedRate.methods - .collectBT(exchangeId, amountWei) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, fixedRate.methods.collectBT, exchangeId, amountWei) } /** @@ -846,11 +813,18 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectBT(address, exchangeId, amount) const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.baseToken, amount) + + const estGas = await estimateGas( + address, + this.contract.methods.collectBT, + exchangeId, + amountWei + ) + const trxReceipt = await this.contract.methods.collectBT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, @@ -874,20 +848,12 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() + const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) - try { - estGas = await fixedRate.methods - .collectDT(exchangeId, amountWei) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(account, fixedRate.methods.collectDT, exchangeId, amountWei) } /** @@ -905,11 +871,18 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectDT(address, exchangeId, amount) const fixedrate: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() const amountWei = await this.amountToUnits(fixedrate.datatoken, amount) + + const estGas = await estimateGas( + address, + this.contract.methods.collectDT, + exchangeId, + amountWei + ) + const trxReceipt = await this.contract.methods.collectDT(exchangeId, amountWei).send({ from: address, gas: estGas + 1, @@ -931,16 +904,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .collectMarketFee(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } /** @@ -956,7 +921,11 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectMarketFee(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.collectMarketFee, + exchangeId + ) const trxReceipt = await this.contract.methods.collectMarketFee(exchangeId).send({ from: address, gas: estGas + 1, @@ -978,16 +947,8 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .collectMarketFee(exchangeId) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas(account, fixedRate.methods.collectMarketFee, exchangeId) } /** @@ -1003,7 +964,11 @@ export class FixedRateExchange { const exchange = await this.getExchange(exchangeId) if (!exchange) return null - const estGas = await this.estCollectOceanFee(address, exchangeId) + const estGas = await estimateGas( + address, + this.contract.methods.collectOceanFee, + exchangeId + ) const trxReceipt = await this.contract.methods.collectOceanFee(exchangeId).send({ from: address, gas: estGas + 1, @@ -1070,16 +1035,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .updateMarketFee(exchangeId, newMarketFee) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.updateMarketFee, + exchangeId, + newMarketFee + ) } /** @@ -1094,8 +1056,9 @@ export class FixedRateExchange { exchangeId: string, newMarketFee: string ): Promise { - const estGas = await this.estSetRate( + const estGas = await estimateGas( address, + this.contract.methods.updateMarketFee, exchangeId, this.web3.utils.toWei(newMarketFee) ) @@ -1124,16 +1087,13 @@ export class FixedRateExchange { contractInstance?: Contract ): Promise { const fixedRate = contractInstance || this.fixedRateContract - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await fixedRate.methods - .updateMarketFeeCollector(exchangeId, newMarketFeeCollector) - .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + + return estimateGas( + account, + fixedRate.methods.updateMarketFeeCollector, + exchangeId, + newMarketFeeCollector + ) } /** @@ -1148,8 +1108,9 @@ export class FixedRateExchange { exchangeId: string, newMarketFeeCollector: string ): Promise { - const estGas = await this.estUpdateMarketFeeCollector( + const estGas = await estimateGas( address, + this.contract.methods.updateMarketFeeCollector, exchangeId, newMarketFeeCollector ) From 8d9964448a1e44a09dc6e9b9db2fb4955f869d32 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 13:44:36 +0200 Subject: [PATCH 18/31] fix error in number of parameters in deployERC721Contract() --- src/factories/NFTFactory.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 465135bd..24660bf7 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -95,9 +95,7 @@ export class NftFactory { nftData.templateIndex, addressZERO, addressZERO, - nftData.tokenURI, - nftData.transferable, - nftData.owner + nftData.tokenURI ) } @@ -133,9 +131,7 @@ export class NftFactory { nftData.templateIndex, addressZERO, addressZERO, - nftData.tokenURI, - nftData.transferable, - nftData.owner + nftData.tokenURI ) // Invoke createToken function of the contract @@ -146,9 +142,7 @@ export class NftFactory { nftData.templateIndex, addressZERO, addressZERO, - nftData.tokenURI, - nftData.transferable, - nftData.owner + nftData.tokenURI ) .send({ from: address, From f9aa47c4d21f34aa31addd3a37054800f0e8fb6d Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 13:44:52 +0200 Subject: [PATCH 19/31] use estimateGas() function in Datatoken --- src/tokens/Datatoken.ts | 362 ++++++++++++++-------------------------- 1 file changed, 125 insertions(+), 237 deletions(-) diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index b63e911c..ecd2bd4e 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({ From 4cee7bb08cd11e38ecddbc0c14f6eebf88755c9b Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 14:17:03 +0200 Subject: [PATCH 20/31] use estimateGas() function in NFT --- src/tokens/NFT.ts | 364 ++++++++++++++-------------------------------- 1 file changed, 110 insertions(+), 254 deletions(-) diff --git a/src/tokens/NFT.ts b/src/tokens/NFT.ts index b0322e0b..3e115350 100644 --- a/src/tokens/NFT.ts +++ b/src/tokens/NFT.ts @@ -7,7 +7,8 @@ import { getFairGasPrice, generateDtName, setContractDefaults, - configHelperNetworks + configHelperNetworks, + estimateGas } from '../utils' import { Contract } from 'web3-eth-contract' import { MetadataProof } from '../../src/@types' @@ -75,22 +76,15 @@ export class Nft { new this.web3.eth.Contract(this.nftAbi, nftAddress), this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .createERC20( - templateIndex, - [name, symbol], - [minter, paymentCollector, mpFeeAddress, feeToken], - [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)], - [] - ) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas( + address, + nftContract.methods.createERC20, + templateIndex, + [name, symbol], + [minter, paymentCollector, mpFeeAddress, feeToken], + [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)], + [] + ) } /** @@ -137,19 +131,14 @@ export class Nft { this.config ) - const estGas = await this.estGasCreateErc20( - nftAddress, + const estGas = await estimateGas( address, - minter, - paymentCollector, - mpFeeAddress, - feeToken, - feeAmount, - cap, - name, - symbol, + nftContract.methods.createERC20, templateIndex, - nftContract + [name, symbol], + [minter, paymentCollector, mpFeeAddress, feeToken], + [this.web3.utils.toWei(cap), this.web3.utils.toWei(feeAmount)], + [] ) // Call createERC20 token function of the contract @@ -197,16 +186,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .addManager(manager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.addManager, manager) } /** @@ -226,7 +206,7 @@ export class Nft { throw new Error(`Caller is not NFT Owner`) } - const estGas = await this.estGasAddManager(nftAddress, address, manager, nftContract) + const estGas = await estimateGas(address, nftContract.methods.addManager, manager) // Invoke addManager function of the contract const trxReceipt = await nftContract.methods.addManager(manager).send({ @@ -258,16 +238,7 @@ export class Nft { new this.web3.eth.Contract(this.nftAbi, nftAddress), this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .removeManager(manager) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.removeManager, manager) } /** @@ -287,12 +258,7 @@ export class Nft { throw new Error(`Caller is not NFT Owner`) } - const estGas = await this.estGasRemoveManager( - nftAddress, - address, - manager, - nftContract - ) + const estGas = await estimateGas(address, nftContract.methods.removeManager, manager) // Invoke removeManager function of the contract const trxReceipt = await nftContract.methods.removeManager(manager).send({ @@ -324,17 +290,7 @@ export class Nft { new this.web3.eth.Contract(this.nftAbi, nftAddress), this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .addToCreateERC20List(erc20Deployer) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, nftContract.methods.addToCreateERC20List, erc20Deployer) } /** @@ -359,11 +315,10 @@ export class Nft { } // Estimate gas for addToCreateERC20List method - const estGas = await this.estGasAddErc20Deployer( - nftAddress, + const estGas = await estimateGas( address, - erc20Deployer, - nftContract + nftContract.methods.addToCreateERC20List, + erc20Deployer ) // Invoke addToCreateERC20List function of the contract @@ -399,17 +354,11 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .removeFromCreateErc20List(erc20Deployer) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + nftContract.methods.removeFromCreateERC20List, + erc20Deployer + ) } /** @@ -436,11 +385,10 @@ export class Nft { ) { throw new Error(`Caller is not Manager nor ERC20Deployer`) } - const estGas = await this.estGasRemoveErc20Deployer( - nftAddress, + const estGas = await estimateGas( address, - erc20Deployer, - nftContract + nftContract.methods.removeFromCreateERC20List, + erc20Deployer ) // Call removeFromCreateERC20List function of the contract @@ -476,16 +424,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .addToMetadataList(metadataUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.addToMetadataList, metadataUpdater) } /** @@ -509,11 +448,10 @@ export class Nft { throw new Error(`Caller is not Manager`) } - const estGas = await this.estGasAddMetadataUpdater( - nftAddress, + const estGas = await estimateGas( address, - metadataUpdater, - nftContract + nftContract.methods.addToMetadataList, + metadataUpdater ) // Call addToMetadataList function of the contract @@ -547,17 +485,11 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .removeFromMetadataList(metadataUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + address, + nftContract.methods.removeFromMetadataList, + metadataUpdater + ) } /** @@ -625,16 +557,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .addTo725StoreList(storeUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.addTo725StoreList, storeUpdater) } /** @@ -658,11 +581,10 @@ export class Nft { throw new Error(`Caller is not Manager`) } - const estGas = await this.estGasAddStoreUpdater( - nftAddress, + const estGas = await estimateGas( address, - storeUpdater, - nftContract + nftContract.methods.addTo725StoreList, + storeUpdater ) // Call addTo725StoreList function of the contract @@ -696,16 +618,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .removeFrom725StoreList(storeUpdater) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.removeFrom725StoreList, storeUpdater) } /** @@ -733,11 +646,10 @@ export class Nft { throw new Error(`Caller is not Manager nor storeUpdater`) } - const estGas = await this.estGasRemoveStoreUpdater( - nftAddress, + const estGas = await estimateGas( address, - storeUpdater, - nftContract + nftContract.methods.removeFrom725StoreList, + storeUpdater ) // Call removeFrom725StoreList function of the contract @@ -771,16 +683,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .cleanPermissions() - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, nftContract.methods.cleanPermissions) } /** @@ -806,7 +709,7 @@ export class Nft { throw new Error(`Caller is not NFT Owner`) } - const estGas = await this.estGasCleanPermissions(nftAddress, address, nftContract) + const estGas = await estimateGas(address, nftContract.methods.cleanPermissions) // Call cleanPermissions function of the contract const trxReceipt = await nftContract.methods.cleanPermissions().send({ @@ -841,19 +744,13 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .transferFrom(nftOwner, nftReceiver, tokenId) - .estimateGas({ from: nftOwner }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + nftOwner, + nftContract.methods.transferFrom, + nftOwner, + nftReceiver, + tokenId + ) } /** @@ -882,12 +779,12 @@ export class Nft { const tokenIdentifier = tokenId || 1 - const estGas = await this.estGasTransferNft( - nftAddress, + const estGas = await estimateGas( + nftOwner, + nftContract.methods.transferFrom, nftOwner, nftReceiver, - tokenIdentifier, - nftContract + tokenIdentifier ) // Call transferFrom function of the contract @@ -925,19 +822,13 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .safeTransferFrom(nftOwner, nftReceiver, tokenId) - .estimateGas({ from: nftOwner }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + nftOwner, + nftContract.methods.safeTransferFrom, + nftOwner, + nftReceiver, + tokenId + ) } /** @@ -966,12 +857,12 @@ export class Nft { const tokenIdentifier = tokenId || 1 - const estGas = await this.estGasSafeTransferNft( - nftAddress, + const estGas = await estimateGas( + nftOwner, + nftContract.methods.safeTransferFrom, nftOwner, nftReceiver, - tokenIdentifier, - nftContract + tokenIdentifier ) // Call transferFrom function of the contract @@ -1016,28 +907,17 @@ export class Nft { this.config ) if (!metadataProofs) metadataProofs = [] - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .setMetaData( - metadataState, - metadataDecryptorUrl, - metadataDecryptorAddress, - flags, - data, - metadataHash, - metadataProofs - ) - .estimateGas({ from: metadataUpdater }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - LoggerInstance.error('estGasSetMetadata error: ', e.message) - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + metadataUpdater, + nftContract.methods.setMetaData, + metadataState, + metadataDecryptorUrl, + metadataDecryptorAddress, + flags, + data, + metadataHash, + metadataProofs + ) } /** @@ -1066,17 +946,16 @@ export class Nft { if (!(await this.getNftPermissions(nftAddress, address)).updateMetadata) { throw new Error(`Caller is not Metadata updater`) } - const estGas = await this.estGasSetMetadata( - nftAddress, + const estGas = await estimateGas( address, + nftContract.methods.setMetaData, metadataState, metadataDecryptorUrl, metadataDecryptorAddress, flags, data, metadataHash, - metadataProofs, - nftContract + metadataProofs ) const trxReceipt = await nftContract.methods .setMetaData( @@ -1117,23 +996,15 @@ export class Nft { new this.web3.eth.Contract(this.nftAbi, nftAddress), this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas const sanitizedMetadataAndTokenURI = { ...metadataAndTokenURI, metadataProofs: metadataAndTokenURI.metadataProofs || [] } - try { - estGas = await nftContract.methods - .setMetaDataAndTokenURI(sanitizedMetadataAndTokenURI) - .estimateGas({ from: metadataUpdater }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + metadataUpdater, + nftContract.methods.setMetaDataAndTokenURI, + sanitizedMetadataAndTokenURI + ) } /** @@ -1155,16 +1026,15 @@ export class Nft { if (!(await this.getNftPermissions(nftAddress, metadataUpdater)).updateMetadata) { throw new Error(`Caller is not Metadata updater`) } - const estGas = await this.estGasSetMetadataAndTokenURI( - nftAddress, - metadataUpdater, - metadataAndTokenURI, - nftContract - ) const sanitizedMetadataAndTokenURI = { ...metadataAndTokenURI, metadataProofs: metadataAndTokenURI.metadataProofs || [] } + const estGas = await estimateGas( + metadataUpdater, + nftContract.methods.setMetaDataAndTokenURI, + sanitizedMetadataAndTokenURI + ) const trxReceipt = await nftContract.methods .setMetaDataAndTokenURI(sanitizedMetadataAndTokenURI) .send({ @@ -1197,19 +1067,11 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .setMetaDataState(metadataState) - .estimateGas({ from: metadataUpdater }, (err, estGas) => - err ? gasLimitDefault : estGas - ) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas( + metadataUpdater, + nftContract.methods.setMetaDataState, + metadataState + ) } /** @@ -1233,7 +1095,11 @@ export class Nft { throw new Error(`Caller is not Metadata updater`) } - const estGas = await this.estGasSetMetadataState(nftAddress, address, metadataState) + const estGas = await estimateGas( + address, + nftContract.methods.setMetaDataState, + metadataState + ) // Call transferFrom function of the contract const trxReceipt = await nftContract.methods.setMetaDataState(metadataState).send({ @@ -1261,17 +1127,7 @@ export class Nft { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await nftContract.methods - .setTokenURI('1', data) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - - return estGas + return estimateGas(address, nftContract.methods.setTokenURI, '1', data) } /** set TokenURI on an nft @@ -1290,7 +1146,7 @@ export class Nft { this.config ) - const estGas = await this.estSetTokenURI(nftAddress, address, data) + const estGas = await estimateGas(address, nftContract.methods.setTokenURI, '1', data) const trxReceipt = await nftContract.methods.setTokenURI('1', data).send({ from: address, gas: estGas + 1, From b3e3cdce7fc1072d897ef3a21c6738f2fade043a Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 14:18:36 +0200 Subject: [PATCH 21/31] remove GASLIMIT_DEFAULT constant from lib classes --- src/factories/NFTFactory.ts | 1 - src/pools/Router.ts | 1 - src/pools/balancer/Pool.ts | 1 - src/pools/dispenser/Dispenser.ts | 1 - src/pools/fixedRate/FixedRateExchange.ts | 1 - src/pools/ssContracts/SideStaking.ts | 1 - src/tokens/Datatoken.ts | 1 - src/tokens/NFT.ts | 1 - 8 files changed, 8 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 24660bf7..6eb5a61a 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -51,7 +51,6 @@ const addressZERO = '0x0000000000000000000000000000000000000000' * Provides an interface for NFT Factory contract */ export class NftFactory { - public GASLIMIT_DEFAULT = 1000000 public factory721Address: string public factory721Abi: AbiItem | AbiItem[] public web3: Web3 diff --git a/src/pools/Router.ts b/src/pools/Router.ts index 1852008d..81d4f6e2 100644 --- a/src/pools/Router.ts +++ b/src/pools/Router.ts @@ -16,7 +16,6 @@ import { Config } from '../models/index.js' * Provides an interface for FactoryRouter contract */ export class Router { - public GASLIMIT_DEFAULT = 1000000 public routerAddress: string public RouterAbi: AbiItem | AbiItem[] public web3: Web3 diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 802f128f..6c7680a3 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -38,7 +38,6 @@ const MaxUint256 = export class Pool { public poolAbi: AbiItem | AbiItem[] public web3: Web3 - public GASLIMIT_DEFAULT = 1000000 private config: Config constructor(web3: Web3, poolAbi: AbiItem | AbiItem[] = null, config?: Config) { diff --git a/src/pools/dispenser/Dispenser.ts b/src/pools/dispenser/Dispenser.ts index 87606427..4755ecd6 100644 --- a/src/pools/dispenser/Dispenser.ts +++ b/src/pools/dispenser/Dispenser.ts @@ -25,7 +25,6 @@ export interface DispenserToken { } export class Dispenser { - public GASLIMIT_DEFAULT = 1000000 public web3: Web3 = null public dispenserAddress: string public config: Config diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 04048a00..7af3906b 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -55,7 +55,6 @@ export enum FixedRateCreateProgressStep { /* eslint-enable no-unused-vars */ export class FixedRateExchange { - public GASLIMIT_DEFAULT = 1000000 /** Ocean related functions */ public oceanAddress: string = null public fixedRateAddress: string diff --git a/src/pools/ssContracts/SideStaking.ts b/src/pools/ssContracts/SideStaking.ts index 20a80e03..d49d1e9c 100644 --- a/src/pools/ssContracts/SideStaking.ts +++ b/src/pools/ssContracts/SideStaking.ts @@ -16,7 +16,6 @@ import { Config } from '../../models' export class SideStaking { public ssAbi: AbiItem | AbiItem[] public web3: Web3 - public GASLIMIT_DEFAULT = 1000000 public config: Config constructor(web3: Web3, ssAbi: AbiItem | AbiItem[] = null, config?: Config) { diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index ecd2bd4e..f9169349 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -45,7 +45,6 @@ export interface DispenserParams { } export class Datatoken { - public GASLIMIT_DEFAULT = 1000000 public factoryAddress: string public factoryABI: AbiItem | AbiItem[] public datatokensAbi: AbiItem | AbiItem[] diff --git a/src/tokens/NFT.ts b/src/tokens/NFT.ts index 3e115350..85a2606d 100644 --- a/src/tokens/NFT.ts +++ b/src/tokens/NFT.ts @@ -26,7 +26,6 @@ interface Roles { } export class Nft { - public GASLIMIT_DEFAULT = 1000000 public factory721Address: string public factory721Abi: AbiItem | AbiItem[] public nftAbi: AbiItem | AbiItem[] From ea74e4fc384e3da02d5ad53d9c90b8a06db03660 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 14:25:34 +0200 Subject: [PATCH 22/31] use ZERO_ADDRESS constant --- src/factories/NFTFactory.ts | 20 ++++++++++---------- src/pools/fixedRate/FixedRateExchange.ts | 7 ++++--- src/tokens/Datatoken.ts | 19 ++++++++----------- src/utils/ContractUtils.ts | 5 ++--- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 6eb5a61a..9f1cb4fa 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -12,7 +12,8 @@ import { getPoolCreationParams, configHelperNetworks, setContractDefaults, - estimateGas + estimateGas, + ZERO_ADDRESS } from '../utils' import { Config } from '../models/index.js' import { @@ -46,7 +47,6 @@ export interface NftCreateData { owner: string } -const addressZERO = '0x0000000000000000000000000000000000000000' /** * Provides an interface for NFT Factory contract */ @@ -92,8 +92,8 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - addressZERO, - addressZERO, + ZERO_ADDRESS, + ZERO_ADDRESS, nftData.tokenURI ) } @@ -128,8 +128,8 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - addressZERO, - addressZERO, + ZERO_ADDRESS, + ZERO_ADDRESS, nftData.tokenURI ) @@ -139,8 +139,8 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - addressZERO, - addressZERO, + ZERO_ADDRESS, + ZERO_ADDRESS, nftData.tokenURI ) .send({ @@ -271,7 +271,7 @@ export class NftFactory { if ((await this.getOwner()) !== address) { throw new Error(`Caller is not Factory Owner`) } - if (templateAddress === addressZERO) { + if (templateAddress === ZERO_ADDRESS) { throw new Error(`Template cannot be ZERO address`) } @@ -430,7 +430,7 @@ export class NftFactory { if ((await this.getOwner()) !== address) { throw new Error(`Caller is not Factory Owner`) } - if (templateAddress === addressZERO) { + if (templateAddress === ZERO_ADDRESS) { throw new Error(`Template cannot be address ZERO`) } diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 7af3906b..ea67fe61 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -10,7 +10,8 @@ import { setContractDefaults, amountToUnits, unitsToAmount, - estimateGas + estimateGas, + ZERO_ADDRESS } from '../../utils' import { Config } from '../../models/index.js' import { PriceAndFees } from '../..' @@ -158,7 +159,7 @@ export class FixedRateExchange { exchangeId: string, datatokenAmount: string, maxBaseTokenAmount: string, - consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', + consumeMarketAddress: string = ZERO_ADDRESS, consumeMarketFee: string = '0' ): Promise { const exchange = await this.getExchange(exchangeId) @@ -249,7 +250,7 @@ export class FixedRateExchange { exchangeId: string, datatokenAmount: string, minBaseTokenAmount: string, - consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', + consumeMarketAddress: string = ZERO_ADDRESS, consumeMarketFee: string = '0' ): Promise { const exchange = await this.getExchange(exchangeId) diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index f9169349..6d6a0fc5 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -11,7 +11,8 @@ import { setContractDefaults, configHelperNetworks, getFreOrderParams, - estimateGas + estimateGas, + ZERO_ADDRESS } from '../utils' import { ConsumeMarketFee, @@ -193,8 +194,7 @@ export class Datatoken { this.config ) - if (!fixedRateParams.allowedConsumer) - fixedRateParams.allowedConsumer = '0x0000000000000000000000000000000000000000' + if (!fixedRateParams.allowedConsumer) fixedRateParams.allowedConsumer = ZERO_ADDRESS const withMint = fixedRateParams.withMint ? 1 : 0 return estimateGas( @@ -237,8 +237,7 @@ export class Datatoken { if (!(await this.isERC20Deployer(dtAddress, address))) { throw new Error(`User is not ERC20 Deployer`) } - if (!fixedRateParams.allowedConsumer) - fixedRateParams.allowedConsumer = '0x0000000000000000000000000000000000000000' + if (!fixedRateParams.allowedConsumer) fixedRateParams.allowedConsumer = ZERO_ADDRESS const withMint = fixedRateParams.withMint ? 1 : 0 @@ -312,8 +311,7 @@ export class Datatoken { this.config ) - if (!dispenserParams.allowedSwapper) - dispenserParams.allowedSwapper = '0x0000000000000000000000000000000000000000' + if (!dispenserParams.allowedSwapper) dispenserParams.allowedSwapper = ZERO_ADDRESS if (!dispenserParams.withMint) dispenserParams.withMint = false @@ -351,8 +349,7 @@ export class Datatoken { this.config ) - if (!dispenserParams.allowedSwapper) - dispenserParams.allowedSwapper = '0x0000000000000000000000000000000000000000' + if (!dispenserParams.allowedSwapper) dispenserParams.allowedSwapper = ZERO_ADDRESS if (!dispenserParams.withMint) dispenserParams.withMint = false @@ -894,8 +891,8 @@ export class Datatoken { ) if (!consumeMarketFee) { consumeMarketFee = { - consumeMarketFeeAddress: '0x0000000000000000000000000000000000000000', - consumeMarketFeeToken: '0x0000000000000000000000000000000000000000', + consumeMarketFeeAddress: ZERO_ADDRESS, + consumeMarketFeeToken: ZERO_ADDRESS, consumeMarketFeeAmount: '0' } } diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 6386c303..9721bd08 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -11,7 +11,7 @@ import { import { Config } from '../models' import { minAbi } from './minAbi' import LoggerInstance from './Logger' -import { GASLIMIT_DEFAULT } from './Constants' +import { GASLIMIT_DEFAULT, ZERO_ADDRESS } from './Constants' export function setContractDefaults(contract: Contract, config: Config): Contract { if (config) { @@ -66,8 +66,7 @@ export function getFreOrderParams(freParams: FreOrderParams): any { } export function getFreCreationParams(freParams: FreCreationParams): any { - if (!freParams.allowedConsumer) - freParams.allowedConsumer = '0x0000000000000000000000000000000000000000' + if (!freParams.allowedConsumer) freParams.allowedConsumer = ZERO_ADDRESS const withMint = freParams.withMint ? 1 : 0 return { From 1874804d99ff0afa6ecd1e08c2ec55aac03ce416 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Wed, 6 Apr 2022 14:31:48 +0200 Subject: [PATCH 23/31] revert fix error in number of parameters in deployERC721Contract() --- src/factories/NFTFactory.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 9f1cb4fa..465135bd 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -12,8 +12,7 @@ import { getPoolCreationParams, configHelperNetworks, setContractDefaults, - estimateGas, - ZERO_ADDRESS + estimateGas } from '../utils' import { Config } from '../models/index.js' import { @@ -47,10 +46,12 @@ export interface NftCreateData { owner: string } +const addressZERO = '0x0000000000000000000000000000000000000000' /** * Provides an interface for NFT Factory contract */ export class NftFactory { + public GASLIMIT_DEFAULT = 1000000 public factory721Address: string public factory721Abi: AbiItem | AbiItem[] public web3: Web3 @@ -92,9 +93,11 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - ZERO_ADDRESS, - ZERO_ADDRESS, - nftData.tokenURI + addressZERO, + addressZERO, + nftData.tokenURI, + nftData.transferable, + nftData.owner ) } @@ -128,9 +131,11 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - ZERO_ADDRESS, - ZERO_ADDRESS, - nftData.tokenURI + addressZERO, + addressZERO, + nftData.tokenURI, + nftData.transferable, + nftData.owner ) // Invoke createToken function of the contract @@ -139,9 +144,11 @@ export class NftFactory { nftData.name, nftData.symbol, nftData.templateIndex, - ZERO_ADDRESS, - ZERO_ADDRESS, - nftData.tokenURI + addressZERO, + addressZERO, + nftData.tokenURI, + nftData.transferable, + nftData.owner ) .send({ from: address, @@ -271,7 +278,7 @@ export class NftFactory { if ((await this.getOwner()) !== address) { throw new Error(`Caller is not Factory Owner`) } - if (templateAddress === ZERO_ADDRESS) { + if (templateAddress === addressZERO) { throw new Error(`Template cannot be ZERO address`) } @@ -430,7 +437,7 @@ export class NftFactory { if ((await this.getOwner()) !== address) { throw new Error(`Caller is not Factory Owner`) } - if (templateAddress === ZERO_ADDRESS) { + if (templateAddress === addressZERO) { throw new Error(`Template cannot be address ZERO`) } From a1e9a42687ceef6be1b0ffb28b4b45b6cfce64ac Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 12 Apr 2022 10:05:22 +0200 Subject: [PATCH 24/31] fix error with GASLIMIT_DEFAULT --- src/factories/NFTFactory.ts | 1 - src/tokens/Datatoken.ts | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 7dff443d..feed296c 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -51,7 +51,6 @@ export interface NftCreateData { * Provides an interface for NFT Factory contract */ export class NftFactory { - public GASLIMIT_DEFAULT = 1000000 public factory721Address: string public factory721Abi: AbiItem | AbiItem[] public web3: Web3 diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index 40232b15..6d6a0fc5 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -194,8 +194,6 @@ export class Datatoken { this.config ) - const gasLimitDefault = this.GASLIMIT_DEFAULT - if (!fixedRateParams.allowedConsumer) fixedRateParams.allowedConsumer = ZERO_ADDRESS const withMint = fixedRateParams.withMint ? 1 : 0 From 52899969375295a8068c6d05473f18d4ac1b3328 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 28 Apr 2022 17:37:08 +0200 Subject: [PATCH 25/31] rewrite estimateGas for reuseOrder() --- src/tokens/Datatoken.ts | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index 16dce857..34ec66e4 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -949,17 +949,7 @@ export class Datatoken { this.config ) - // Estimate gas for reuseOrder method - const gasLimitDefault = this.GASLIMIT_DEFAULT - let estGas - try { - estGas = await dtContract.methods - .reuseOrder(orderTxId, providerFees) - .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) - } catch (e) { - estGas = gasLimitDefault - } - return estGas + return estimateGas(address, dtContract.methods.reuseOrder, orderTxId, providerFees) } /** Reuse Order: called by payer or consumer having a valid order, but with expired provider access. @@ -982,12 +972,11 @@ export class Datatoken { this.config ) try { - const estGas = await this.estGasReuseOrder( - dtAddress, + const estGas = await estimateGas( address, + dtContract.methods.reuseOrder, orderTxId, - providerFees, - dtContract + providerFees ) const trxReceipt = await dtContract.methods From 079a583041da44c02a1fccf84250b38d4a139b32 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 12 May 2022 19:02:44 +0200 Subject: [PATCH 26/31] console.log estimateGas() result --- src/utils/ContractUtils.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 17066603..84fb29f5 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -183,7 +183,15 @@ export async function estimateGas( { from: from }, - (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas) + (err, estGas) => { + if (err) { + console.log('ERROR ESTIMATING GAS: ' + err) + return GASLIMIT_DEFAULT + } else { + console.log('OK ESTIMATING GAS: ' + estGas) + return estGas + } + } ) } catch (e) { LoggerInstance.error(`ERROR: Estimate gas failed!`, e) From 6f74add54bb353dcdb045202a919b4ebafcd15b4 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Thu, 12 May 2022 21:33:16 +0200 Subject: [PATCH 27/31] revert console.log estimateGas() result --- src/utils/ContractUtils.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 84fb29f5..17066603 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -183,15 +183,7 @@ export async function estimateGas( { from: from }, - (err, estGas) => { - if (err) { - console.log('ERROR ESTIMATING GAS: ' + err) - return GASLIMIT_DEFAULT - } else { - console.log('OK ESTIMATING GAS: ' + estGas) - return estGas - } - } + (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas) ) } catch (e) { LoggerInstance.error(`ERROR: Estimate gas failed!`, e) From cab4d5b1a73e41bbf6eea74a2af9d7f52c56e447 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 16 May 2022 15:48:40 +0200 Subject: [PATCH 28/31] restore estApprove() function and add estTransfer() function --- src/utils/TokenUtils.ts | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index 22d20141..c8008c3f 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,4 +1,5 @@ import Decimal from 'decimal.js' +import { Contract } from 'web3-eth-contract' import { amountToUnits, estimateGas, @@ -10,6 +11,29 @@ import LoggerInstance from './Logger' import { TransactionReceipt } from 'web3-core' import Web3 from 'web3' +/** + * Estimate gas cost for approval function + * @param {String} account + * @param {String} tokenAddress + * @param {String} spender + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ +export async function estApprove( + web3: Web3, + account: string, + tokenAddress: string, + spender: string, + amount: string, + contractInstance?: Contract +): Promise { + const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) + + return estimateGas(account, tokenContract.methods.approve, spender, amount) +} + /** * Approve spender to spent amount tokens * @param {String} account @@ -58,6 +82,29 @@ export async function approve( return result } +/** + * Estimate gas cost for transfer function + * @param {String} account + * @param {String} tokenAddress + * @param {String} recipient + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ +export async function estTransfer( + web3: Web3, + account: string, + tokenAddress: string, + recipient: string, + amount: string, + contractInstance?: Contract +): Promise { + const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) + + return estimateGas(account, tokenContract.methods.transfer, recipient, amount) +} + /** * Moves amount tokens from the caller’s account to recipient. * @param {String} account From 6b02337869d717fafd8cbbc2575444034d8ef4ea Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 17 May 2022 12:01:15 +0300 Subject: [PATCH 29/31] Bump @types/node from 17.0.32 to 17.0.34 (#1471) Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 17.0.32 to 17.0.34. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) --- updated-dependencies: - dependency-name: "@types/node" dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 06f6d044..e5011c2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "@types/chai-spies": "^1.0.3", "@types/crypto-js": "^4.1.1", "@types/mocha": "^9.1.1", - "@types/node": "^17.0.32", + "@types/node": "^17.0.34", "@types/node-fetch": "^3.0.3", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", @@ -3021,9 +3021,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "17.0.32", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.32.tgz", - "integrity": "sha512-eAIcfAvhf/BkHcf4pkLJ7ECpBAhh9kcxRBpip9cTiO+hf+aJrsxYxBeS6OXvOd9WqNAJmavXVpZvY1rBjNsXmw==" + "version": "17.0.34", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.34.tgz", + "integrity": "sha512-XImEz7XwTvDBtzlTnm8YvMqGW/ErMWBsKZ+hMTvnDIjGCKxwK5Xpc+c/oQjOauwq8M4OS11hEkpjX8rrI/eEgA==" }, "node_modules/@types/node-fetch": { "version": "3.0.3", @@ -20079,9 +20079,9 @@ "dev": true }, "@types/node": { - "version": "17.0.32", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.32.tgz", - "integrity": "sha512-eAIcfAvhf/BkHcf4pkLJ7ECpBAhh9kcxRBpip9cTiO+hf+aJrsxYxBeS6OXvOd9WqNAJmavXVpZvY1rBjNsXmw==" + "version": "17.0.34", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.34.tgz", + "integrity": "sha512-XImEz7XwTvDBtzlTnm8YvMqGW/ErMWBsKZ+hMTvnDIjGCKxwK5Xpc+c/oQjOauwq8M4OS11hEkpjX8rrI/eEgA==" }, "@types/node-fetch": { "version": "3.0.3", diff --git a/package.json b/package.json index a4725d46..8951fc6e 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "@types/chai-spies": "^1.0.3", "@types/crypto-js": "^4.1.1", "@types/mocha": "^9.1.1", - "@types/node": "^17.0.32", + "@types/node": "^17.0.34", "@types/node-fetch": "^3.0.3", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", From bf060948a8c056bfac137c51b836fec019a5610a Mon Sep 17 00:00:00 2001 From: Alex Coseru Date: Tue, 17 May 2022 12:04:51 +0300 Subject: [PATCH 30/31] bump contracts to alpha.33 (#1472) * bump contracts * remove getOPCCollector --- package-lock.json | 14 +++++++------- package.json | 2 +- src/pools/balancer/Pool.ts | 19 ------------------- src/pools/fixedRate/FixedRateExchange.ts | 14 -------------- test/unit/pools/balancer/Pool.test.ts | 14 -------------- .../pools/fixedRate/FixedRateExchange.test.ts | 10 ---------- 6 files changed, 8 insertions(+), 65 deletions(-) diff --git a/package-lock.json b/package-lock.json index e5011c2a..bdca0a73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0-next.42", "license": "Apache-2.0", "dependencies": { - "@oceanprotocol/contracts": "^1.0.0-alpha.32", + "@oceanprotocol/contracts": "^1.0.0-alpha.33", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", @@ -2471,9 +2471,9 @@ } }, "node_modules/@oceanprotocol/contracts": { - "version": "1.0.0-alpha.32", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.32.tgz", - "integrity": "sha512-gCHw4ZVnNjSozzCVT/cQiDqLNP0Xvy4fe+g5PKy8rwMR/h2rRW65Txi6wkb4HfZiUPKCdHbzpUFOwYfJFbW0Jw==" + "version": "1.0.0-alpha.33", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.33.tgz", + "integrity": "sha512-jx8fg9src7fjeqMXB2ZYLdp0BoJA0G5vau6pa48YOjasDuPekJtPdT0oRy0PVkyJ2HtkYjAwKVyiFfAbZcug6A==" }, "node_modules/@octokit/auth-token": { "version": "2.5.0", @@ -19610,9 +19610,9 @@ } }, "@oceanprotocol/contracts": { - "version": "1.0.0-alpha.32", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.32.tgz", - "integrity": "sha512-gCHw4ZVnNjSozzCVT/cQiDqLNP0Xvy4fe+g5PKy8rwMR/h2rRW65Txi6wkb4HfZiUPKCdHbzpUFOwYfJFbW0Jw==" + "version": "1.0.0-alpha.33", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.33.tgz", + "integrity": "sha512-jx8fg9src7fjeqMXB2ZYLdp0BoJA0G5vau6pa48YOjasDuPekJtPdT0oRy0PVkyJ2HtkYjAwKVyiFfAbZcug6A==" }, "@octokit/auth-token": { "version": "2.5.0", diff --git a/package.json b/package.json index 8951fc6e..90c14291 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "web3": "^1.7.3" }, "dependencies": { - "@oceanprotocol/contracts": "^1.0.0-alpha.32", + "@oceanprotocol/contracts": "^1.0.0-alpha.33", "bignumber.js": "^9.0.2", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index e5a22a97..95096265 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -329,25 +329,6 @@ export class Pool { return result } - /** - * Get OPC Collector of this pool - * @param {String} poolAddress - * @return {String} - */ - async getOPCCollector(poolAddress: string): Promise { - const pool = setContractDefaults( - new this.web3.eth.Contract(this.poolAbi, poolAddress), - this.config - ) - let result = null - try { - result = await pool.methods._opcCollector().call() - } catch (e) { - LoggerInstance.error(`ERROR: Failed to get OPF Collector address: ${e.message}`) - } - return result - } - /** * Get if a token is bounded to a pool * Returns true if token is bound diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index c73f03fb..75c381cf 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -1020,20 +1020,6 @@ export class FixedRateExchange { return trxReceipt } - /** - * Get OPF Collector of fixed rate contract - * @return {String} - */ - async getOPCCollector(): Promise { - let result = null - try { - result = await this.contract.methods.opcCollector().call() - } catch (e) { - LoggerInstance.error(`ERROR: Failed to get OPC Collector address: ${e.message}`) - } - return result - } - /** * Get Router address set in fixed rate contract * @return {String} diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 68ce2b4e..1b2452c2 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -468,13 +468,6 @@ describe('Pool unit test', () => { assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) }) - it('#getOPCCollector- should get market fees for each token', async () => { - assert( - (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress - ) - }) - it('#collectCommunityFee- should get community fees for each token', async () => { const spotPriceBefore = await pool.getSpotPrice( poolAddress, @@ -953,13 +946,6 @@ describe('Pool unit test', () => { assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) }) - it('#getOPCCollector- should get market fees for each token', async () => { - assert( - (await pool.getOPCCollector(poolAddress)) === - contracts.opfCommunityFeeCollectorAddress - ) - }) - it('#getCurrentMarketFees- should get curent market fees for each token', async () => { const currentMarketFees: CurrentFees = await pool.getCurrentMarketFees(poolAddress) assert(currentMarketFees !== null) diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index 28e43472..58744acd 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -132,11 +132,6 @@ describe('Fixed Rate unit test', () => { it('#getOwner - should get exchange owner given an id', async () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) - it('#getOPFCollector - should get OPF collector', async () => { - expect(await fixedRate.getOPCCollector()).to.equal( - contracts.opfCommunityFeeCollectorAddress - ) - }) it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) }) @@ -453,11 +448,6 @@ describe('Fixed Rate unit test', () => { it('#getOwner - should get exchange owner given an id', async () => { expect(await fixedRate.getExchangeOwner(exchangeId)).to.equal(exchangeOwner) }) - it('#getOPFCollector - should get OPF collector', async () => { - expect(await fixedRate.getOPCCollector()).to.equal( - contracts.opfCommunityFeeCollectorAddress - ) - }) it('#getRouter - should get Router address', async () => { expect(await fixedRate.getRouter()).to.equal(contracts.routerAddress) }) From 41e68657b905b106b34184cde5526bef170d79a5 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Tue, 17 May 2022 02:11:12 -0700 Subject: [PATCH 31/31] Release 1.0.0-next.43 --- CHANGELOG.md | 14 ++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 595058c0..d4d4d10b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,25 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v1.0.0-next.43](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.42...v1.0.0-next.43) + +- bump contracts to alpha.33 [`#1472`](https://github.com/oceanprotocol/ocean.js/pull/1472) +- Bump @types/node from 17.0.32 to 17.0.34 [`#1471`](https://github.com/oceanprotocol/ocean.js/pull/1471) +- Add transfer() function to TokenUtils [`#1387`](https://github.com/oceanprotocol/ocean.js/pull/1387) +- Issue-#1391: Add generic estimateGas() function [`#1394`](https://github.com/oceanprotocol/ocean.js/pull/1394) +- Bump @types/node from 17.0.31 to 17.0.32 [`#1465`](https://github.com/oceanprotocol/ocean.js/pull/1465) +- use estimateGas() function in NFT [`4cee7bb`](https://github.com/oceanprotocol/ocean.js/commit/4cee7bb08cd11e38ecddbc0c14f6eebf88755c9b) +- use estimateGas() function in Datatoken [`f9aa47c`](https://github.com/oceanprotocol/ocean.js/commit/f9aa47c4d21f34aa31addd3a37054800f0e8fb6d) +- use estimateGas() function in NFTFactory [`00fc5d2`](https://github.com/oceanprotocol/ocean.js/commit/00fc5d21e9e819e0ef47254dd5e4721b52c9d2fa) + #### [v1.0.0-next.42](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.41...v1.0.0-next.42) +> 9 May 2022 + - bump contracts to alpha.32 [`#1464`](https://github.com/oceanprotocol/ocean.js/pull/1464) - simple compute flow [`#1458`](https://github.com/oceanprotocol/ocean.js/pull/1458) - add optional decimals parameter at joinswapExternAmountIn() and exitswapPoolAmountIn() [`#1461`](https://github.com/oceanprotocol/ocean.js/pull/1461) +- Release 1.0.0-next.42 [`56f992e`](https://github.com/oceanprotocol/ocean.js/commit/56f992e760712a88f4c9a236d75d784d6dd1c4b1) #### [v1.0.0-next.41](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.40...v1.0.0-next.41) diff --git a/package-lock.json b/package-lock.json index bdca0a73..32efbef6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@oceanprotocol/lib", - "version": "1.0.0-next.42", + "version": "1.0.0-next.43", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@oceanprotocol/lib", - "version": "1.0.0-next.42", + "version": "1.0.0-next.43", "license": "Apache-2.0", "dependencies": { "@oceanprotocol/contracts": "^1.0.0-alpha.33", diff --git a/package.json b/package.json index 90c14291..7e2405d3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@oceanprotocol/lib", "source": "./src/index.ts", - "version": "1.0.0-next.42", + "version": "1.0.0-next.43", "description": "JavaScript client library for Ocean Protocol", "main": "./dist/lib.js", "umd:main": "dist/lib.umd.js",