mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
create a generic estimateGas() function
This commit is contained in:
parent
0735a4b4e3
commit
f34573be44
@ -8,34 +8,31 @@ import Web3 from 'web3'
|
|||||||
import { GASLIMIT_DEFAULT } from '.'
|
import { GASLIMIT_DEFAULT } from '.'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estimate gas cost for approval function
|
* Estimates the gas used when a function would be executed on chain
|
||||||
* @param {String} account
|
* @param {Contract} tokenContract token contract instance
|
||||||
* @param {String} tokenAddress
|
* @param {string} from account that calls the function
|
||||||
* @param {String} spender
|
* @param {string} functionSignature signature of the function
|
||||||
* @param {String} amount
|
* @param {...any[]} args arguments of the function
|
||||||
* @param {String} force
|
* @return {Promise<number>} gas cost of the function
|
||||||
* @param {Contract} contractInstance optional contract instance
|
|
||||||
* @return {Promise<number>}
|
|
||||||
*/
|
*/
|
||||||
export async function estApprove(
|
export async function estimateGas(
|
||||||
web3: Web3,
|
tokenContract: Contract,
|
||||||
account: string,
|
from: string,
|
||||||
tokenAddress: string,
|
functionSignature: string,
|
||||||
spender: string,
|
...args: any[]
|
||||||
amount: string,
|
|
||||||
contractInstance?: Contract
|
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress)
|
|
||||||
|
|
||||||
const gasLimitDefault = GASLIMIT_DEFAULT
|
const gasLimitDefault = GASLIMIT_DEFAULT
|
||||||
let estGas
|
let estGas
|
||||||
try {
|
try {
|
||||||
estGas = await tokenContract.methods
|
estGas = await tokenContract.methods[functionSignature].apply(null, args).estimateGas(
|
||||||
.approve(spender, amount)
|
{
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
from: from
|
||||||
|
},
|
||||||
|
(err, estGas) => (err ? gasLimitDefault : estGas)
|
||||||
|
)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
estGas = gasLimitDefault
|
estGas = gasLimitDefault
|
||||||
LoggerInstance.error('estimate gas failed for approve!', e)
|
LoggerInstance.error(`ERROR: Estimate gas failed for ${functionSignature}!`, e)
|
||||||
}
|
}
|
||||||
return estGas
|
return estGas
|
||||||
}
|
}
|
||||||
@ -65,13 +62,12 @@ 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,
|
tokenContract,
|
||||||
account,
|
account,
|
||||||
tokenAddress,
|
'approve(address,uint256)',
|
||||||
spender,
|
spender,
|
||||||
amountFormatted,
|
amountFormatted
|
||||||
tokenContract
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -88,39 +84,6 @@ export async function approve(
|
|||||||
return result
|
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<number>}
|
|
||||||
*/
|
|
||||||
export async function estTransfer(
|
|
||||||
web3: Web3,
|
|
||||||
account: string,
|
|
||||||
tokenAddress: string,
|
|
||||||
recipient: string,
|
|
||||||
amount: string,
|
|
||||||
contractInstance?: Contract
|
|
||||||
): Promise<number> {
|
|
||||||
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.
|
* Moves amount tokens from the caller’s account to recipient.
|
||||||
* @param {String} account
|
* @param {String} account
|
||||||
@ -140,13 +103,12 @@ 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 estTransfer(
|
const estGas = await estimateGas(
|
||||||
web3,
|
tokenContract,
|
||||||
account,
|
account,
|
||||||
tokenAddress,
|
'transfer(address,uint256)',
|
||||||
recipient,
|
recipient,
|
||||||
amountFormatted,
|
amountFormatted
|
||||||
tokenContract
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user