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