我在智能合约中有一个名为'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)
})
})发布于 2019-11-06 11:51:59
我已经有了解决方案。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)
})发布于 2020-08-25 09:58:03
@Chitranshu answer非常正确(工作正常),但只要包含回调函数,就可以将参数放在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。
https://stackoverflow.com/questions/58682257
复制相似问题