【问题标题】:Estimating gas of an ERC20 transfer估算 ERC20 传输的气体
【发布时间】:2020-07-28 00:44:42
【问题描述】:

我想估算一下在两个地址之间进行简单 ERC20 转账的 gas。 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);
  }

【问题讨论】:

    标签: ethereum web3js


    【解决方案1】:

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

    试试contract.methods.myMethod.send

    【讨论】:

      【解决方案2】:

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

      例如,如果您正在使用 pancakeswap contract 并在代码中签入函数 function deposit(uint256 _pid, uint256 _amount) public {...}(第 1674 行),您将需要这样做:

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

      您需要将 args 传递到存款才能使其工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-31
        • 2022-12-18
        • 1970-01-01
        • 1970-01-01
        • 2023-02-10
        • 2021-09-26
        • 1970-01-01
        • 2018-04-10
        相关资源
        最近更新 更多