![](/img/trans.png)
[英]Estimating ETH transfer and ERC20 transfer gas always returns same value
[英]Estimating gas of an ERC20 transfer
我想估計兩個地址之間的簡單 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
的合約方法。 你沒有send
你的 Solidity 合約源代碼。
相反,請嘗試contract.methods.myMethod.send
有點晚了,但是您需要在要調用的智能合約中查找 function 並在您正在調用的合約中將myMethod(123)
替換為 function。
例如,如果您正在使用 pancakeswap 合約並檢查代碼 function function deposit(uint256 _pid, uint256 _amount) public {...}
(第 1674 行),您需要這樣做:
const estimatedGas = await contract.methods.deposit(123, 0).estimateGas({from: "0x345})
您需要傳遞 args 以使其工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.