Reputation: 59
I have a payable function in smart contract named 'gameDeposit' where user need to deposit eth to participate in game but when I call it using web3 javascript api it gives me uncaught error
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)
})
})
Upvotes: 2
Views: 3846
Reputation: 9888
@Chitranshu answer is quite right (works just fine), but you can have the parameter on gameDeposit as long as you include the callback function within. See below:
myContract.gameDeposit(gameID,{
from: web3.eth.accounts[0],
value: 1000000000000000
},function(error, result){
if(!error)
console.log(result);
else
console.error(error);
});
This way it automatically determines the use of call or sendTransaction based on the method type.
Upvotes: 1
Reputation: 59
I've got the solution. there should no parameter in gameDeposit function , parameter should be in .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)
})
Upvotes: 3