【问题标题】:The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameterMetaMask Web3 对象不支持没有回调参数的同步方法,例如 eth_sendTransaction
【发布时间】:2019-11-03 16:33:22
【问题描述】:

我在名为“gameDeposit”的智能合约中有一个支付功能,用户需要存入 eth 才能参与游戏,但是当我使用 web3 javascript api 调用它时 它给了我未捕获的错误

inpage.js:1 Uncaught (in promise) Error: The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameter
        const Abi = [{ABI}];
        const contractAbi = web3.eth.contract(Abi);
        const myContract = contractAbi.at("0x3....");
        const amountEth = '0.01';
        console.log(myContract);
        const gameID = '10';
        myContract.gameDeposit(gameID).send({
            from: web3.eth.accounts[0],
            value: web3.toWei(amountEth, 'ether')
        },(error , result) => {
            if(!error)
                console.log(result);
            else
                console.error(error)
        })
    })

【问题讨论】:

  • 我在单击按钮时调用此函数
  • 这里有什么问题?

标签: javascript ethereum smartcontracts web3


【解决方案1】:

我找到了解决方案。 gameDeposit 函数中不应该有参数,参数应该在 .sendTransaction() 中。

     myContract.gameDeposit.sendTransaction(gameID,{
            from:   web3.eth.accounts[0],
             value: 1000000000000000
         },function(error , result){
             if(!error)
                 console.log(result);
             else
                 console.log(error.code)
        })

【讨论】:

    【解决方案2】:

    @Chitranshu 的回答非常正确(工作得很好),但是只要您在其中包含回调函数,您就可以在 gameDeposit 上使用该参数。见下文:

    myContract.gameDeposit(gameID,{
            from:   web3.eth.accounts[0],
             value: 1000000000000000
         },function(error, result){
             if(!error)
                 console.log(result);
             else
                 console.error(error);
        });
    

    这样它会根据方法类型自动确定使用 call 或 sendTransaction。

    【讨论】:

      猜你喜欢
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2022-07-08
      • 2020-08-29
      • 2012-10-05
      相关资源
      最近更新 更多