javascript - 为什么estimateGas总是返回null或零?

标签 javascript ethereum web3js metamask ether

我遇到了以下问题...可能是我的代码,或者可能只是交易不需要天然气?

它们总是返回 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?

最佳答案

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);
    }
 });

关于javascript - 为什么estimateGas总是返回null或零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837197/

相关文章:

javascript - amCharts LabelFunction 不工作

javascript - 来自 Node.js 上的 HTTP 请求的 getaddrinfo ENOENT 错误

javascript - Angular: utils/util.js -> Uncaught ReferenceError: process is not defined

c# - System.Runtime.Numerics BigHexInteger 不工作

javascript - 将 MATIC 与 Web3 和 MetaMask 结合使用 : "Error: Returned error: unknown account"

ethereum - 使用 web3js 从交易哈希中获取 token 传输详细信息

node.js - Web3.js sendSignedTransaction 给出 "Error: Failed to check for transaction receipt"

.net - 在更新面板中提交表单后运行 javascript?

javascript - Cufon.refresh() 在 IE8 中不起作用

ethereum - 如何为 Cairo 程序生成证明并验证它?