From c936c47260b84a191cd7d7717d04c025c24043a7 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Tue, 5 Apr 2022 16:21:43 +0200 Subject: [PATCH] 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