1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

estimate gas without using the function signature

This commit is contained in:
Miquel A. Cabot 2022-04-05 10:24:27 +02:00
parent 2fe89b476a
commit 605812ee7f

View File

@ -1,5 +1,4 @@
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { Contract } from 'web3-eth-contract'
import { amountToUnits, getFairGasPrice, unitsToAmount } from './ContractUtils' import { amountToUnits, getFairGasPrice, unitsToAmount } from './ContractUtils'
import { minAbi } from './minAbi' import { minAbi } from './minAbi'
import LoggerInstance from './Logger' import LoggerInstance from './Logger'
@ -9,32 +8,28 @@ import { GASLIMIT_DEFAULT } from '.'
/** /**
* Estimates the gas used when a function would be executed on chain * 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} 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 * @param {...any[]} args arguments of the function
* @return {Promise<number>} gas cost of the function * @return {Promise<number>} gas cost of the function
*/ */
export async function estimateGas( export async function estimateGas(
tokenContract: Contract,
from: string, from: string,
functionSignature: string, functionToEstimateGas: Function,
...args: any[] ...args: any[]
): Promise<number> { ): Promise<number> {
const gasLimitDefault = GASLIMIT_DEFAULT let estimatedGas = GASLIMIT_DEFAULT
let estGas
try { try {
estGas = await tokenContract.methods[functionSignature].apply(null, args).estimateGas( estimatedGas = await functionToEstimateGas.apply(null, args).estimateGas(
{ {
from: from from: from
}, },
(err, estGas) => (err ? gasLimitDefault : estGas) (err, estGas) => (err ? GASLIMIT_DEFAULT : estGas)
) )
} catch (e) { } catch (e) {
estGas = gasLimitDefault LoggerInstance.error(`ERROR: Estimate gas failed!`, e)
LoggerInstance.error(`ERROR: Estimate gas failed for ${functionSignature}!`, e)
} }
return estGas return estimatedGas
} }
/** /**
@ -63,9 +58,8 @@ export async function approve(
let result = null let result = null
const amountFormatted = await amountToUnits(web3, tokenAddress, amount) const amountFormatted = await amountToUnits(web3, tokenAddress, amount)
const estGas = await estimateGas( const estGas = await estimateGas(
tokenContract,
account, account,
'approve(address,uint256)', tokenContract.methods.approve,
spender, spender,
amountFormatted amountFormatted
) )
@ -104,9 +98,8 @@ export async function transfer(
let result = null let result = null
const amountFormatted = await amountToUnits(web3, tokenAddress, amount) const amountFormatted = await amountToUnits(web3, tokenAddress, amount)
const estGas = await estimateGas( const estGas = await estimateGas(
tokenContract,
account, account,
'transfer(address,uint256)', tokenContract.methods.transfer,
recipient, recipient,
amountFormatted amountFormatted
) )