繁体   English   中英

估算 ERC20 传输的气体

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM