mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
use estimateGas() function in TokenUtils
This commit is contained in:
parent
c936c47260
commit
1380661c97
@ -1,6 +1,6 @@
|
|||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
import { Contract } from 'web3-eth-contract'
|
import { Contract } from 'web3-eth-contract'
|
||||||
import { amountToUnits, getFairGasPrice, unitsToAmount } from './ContractUtils'
|
import { amountToUnits, estimateGas, getFairGasPrice, unitsToAmount } from './ContractUtils'
|
||||||
import { minAbi } from './minAbi'
|
import { minAbi } from './minAbi'
|
||||||
import LoggerInstance from './Logger'
|
import LoggerInstance from './Logger'
|
||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from 'web3-core'
|
||||||
@ -27,17 +27,7 @@ export async function estApprove(
|
|||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress)
|
const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress)
|
||||||
|
|
||||||
const gasLimitDefault = GASLIMIT_DEFAULT
|
return estimateGas(account, tokenContract.methods.approve, spender, amount)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,13 +55,11 @@ 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 estApprove(
|
const estGas = await estimateGas(
|
||||||
web3,
|
|
||||||
account,
|
account,
|
||||||
tokenAddress,
|
tokenContract.methods.approve,
|
||||||
spender,
|
spender,
|
||||||
amountFormatted,
|
amountFormatted
|
||||||
tokenContract
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user