估计ERC20输送的气体



我想估计两个地址之间的简单ERC20传输的气体。estimateGas上的web3.js文档确实令人困惑:

// using the callback
myContract.methods.myMethod(123).estimateGas({gas: 5000000}, function(error, gasAmount){
if(gasAmount == 5000000)
console.log('Method ran out of gas');
});

myMethod(123)让我感到困惑。那是干什么的?以下是我目前的想法,但我得到了TypeError: contract.methods.send is not a function。我应该用什么来代替myMethod(123)

try {
await contract.methods
.send("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe")
.estimateGas({ gas: 60000 }, (error, gasAmount) => {
return gasAmount;
});
} catch (err) {
console.log(err);
}

send()指的是一种称为send的合约方法。您的Solidity Contract源代码中没有send

相反,请尝试contract.methods.myMethod.send

有点晚了,但您需要在要调用的智能合约中查找函数,并将myMethod(123)替换为正在调用的合约中的函数。

例如,如果您正在使用pancakeswap合约,并在代码中检查函数function deposit(uint256 _pid, uint256 _amount) public {...}(第1674行(,则需要执行以下操作:

const estimatedGas = await contract.methods.deposit(123, 0).estimateGas({from: "0x345})

您需要将args传递给deposit才能使其工作。

最新更新