【问题标题】:estimateGas always null or zero估计气体总是为空或为零
【发布时间】:2018-02-17 02:00:30
【问题描述】:

在使用 web3 时,我遇到了以下问题。

可能是我的代码,或者可能只是交易不需要gas?

总是返回 null 或零。

可能是我缺乏理解和建议。

var gas = 0;
const eth = new Eth(web3.currentProvider);
const contract = new EthContract(eth);
const myContract= contract(abi);
var me = myContract.at(contractAddress);
eth.estimateGas({
        from: eth.accounts[0], 
        to: "0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", 
        amount: web3.toWei(1, "ether")}, function(d){
        var gas = web3.toBigNumber(gas).toString();
        console.log(gas);                            
        if(gas.toString() != "null"){
                   gas = d; 
                    console.log("Gas: " + d);
       }
 });

总是返回零.... 还是 null?这是我的代码错误吗?或者这个交易不需要gas??新的和学习的,谢谢。

【问题讨论】:

    标签: ethereum contract web3 ether metamask


    【解决方案1】:

    Web3 API 使用error first style callbacks

    您的电话应如下所示:

    eth.estimateGas({
        from: eth.accounts[0], 
        to: "0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", 
        value: web3.toWei(1, "ether")
      }, 
      function(e, d) {
        var gas = web3.toBigNumber(gas).toString();
        console.log(gas);
    
        if (gas.toString() != "null") {
          gas = d; 
          console.log("Gas: " + d);
        }
     });
    

    【讨论】:

    • 哦该死的谢谢,所以据我所知,它只是将 null 作为零错误发送回......谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 2011-05-07
    • 1970-01-01
    • 2014-11-15
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多