简体   繁体   English

估算 ERC20 传输的气体

[英]Estimating gas of an ERC20 transfer

I'd like to estimate the gas of a simple ERC20 transfer between two addresses.我想估计两个地址之间的简单 ERC20 传输的气体。 The web3.js docs on estimateGas are admittedly confusing:无可否认, 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');
});

The myMethod(123) is what I find confusing. myMethod(123)让我感到困惑。 What is that for?那个有什么用? The following is what I'm currently thinking, but I'm getting TypeError: contract.methods.send is not a function .以下是我目前的想法,但我收到TypeError: contract.methods.send is not a function What should I substitute for myMethod(123) ?我应该用什么代替myMethod(123)

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

send() refers to a contract method called send . send()指的是一个名为send的合约方法。 You do not have send in your Solidity Contract source code.你没有send你的 Solidity 合约源代码。

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

A bit late, but you need to look for the function in the smartcontract you want to call and replace myMethod(123) for the function in the contract you are calling.有点晚了,但是您需要在要调用的智能合约中查找 function 并在您正在调用的合约中将myMethod(123)替换为 function。

For example, if you are working with the pancakeswap contract and check in the code the function function deposit(uint256 _pid, uint256 _amount) public {...} (line 1674), you will need to do this:例如,如果您正在使用 pancakeswap 合约并检查代码 function function deposit(uint256 _pid, uint256 _amount) public {...} (第 1674 行),您需要这样做:

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

You need to pass the args to deposit in order to make it work.您需要传递 args 以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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