1

I'm new around here and would appreciate some intuitive help.

I wrote a contract and made a UI for it, and it's all working fine with ganache-cli. However, now that I've launched it on Ropsten and on an actual website, the code is not working with MetaMask. I've done my research and realize that MetaMask needs callback parameters, but after trying different ways, I still haven't managed to get it right.

My code is:

   $("#button1").click(function(resolve, reject) {
       address =  account.toString(); 
       MyContract.function1({from: address, value: '1000000000000000000'});
    })

    $("#button2").click(function(web3) {
       address =  account.toString();  
       var spender = $("#useraddress")[0].value;
       MyContract.function2(spender, "1", {from: address});
    })

    $("#button3").click(function (web3) {
       address =  account.toString(); 
       var tokenreceiver = $("#newowner")[0].value;
       MyContract.function3(tokenreceiver, "1", {from: address});
    })

    $("#button4").click(function(web3) {
       address =  account.toString(); 
       MyContract.function4({from: address, value: '0', gas: >'3000000'});
    })

The error I'm getting is:

Uncaught Error: The MetaMask Web3 object does not support synchronous methods like eth_sendTransaction without a callback parameter.

How should I set my callback parameters for the functions above?

P.S. I'm new to programming so please excuse possible mistakes with the code.

Thanks in advance.

marked as duplicate by Ismael, Richard Horrocks, Achala Dissanayake, Henk, Raghav Sood Aug 21 '18 at 11:41

This question was marked as an exact duplicate of an existing question.

0

Take a look at this solution as an answer to your problem.

The general idea is that you are trying to make a synchronous call and are expecting to receive a promise. The docs explain it very clearly and provide an example.

Take a look at the example below:

web3.eth.getBlock(48, function(error, result){
    if(!error)
        console.log(JSON.stringify(result));
    else
        console.error(error);
})
  • 1
    Checked it out and using the code above as example I managed to get the sync with MetaMask. However, now it automatically calls the function as soon as I load the website, instead of at the click of the button. Any suggestions? – Yakko Majuri Aug 16 '18 at 21:50
0

It's solved. @shanefontaine's comment and this were the main sources of help. As for why functions were being automatically called when the website loaded, it was a syntax error from my side. The JavaScript function was self-invoking, and hence was calling the contract function automatically. The final code looks like this for a button + function:

$("#button").click(function(web3) {     
 var tx = MyContract.function(parameter1, parameter2, {  
  from: address,   
  gas: "300000",   
  to: "CONTRACTADDRESS",   
  value: "0",   
  data: "" }, 
function(err, transactionHash) {  
   if (!err)
console.log(transactionHash);    
}) })

Thanks for the help.

Not the answer you're looking for? Browse other questions tagged or ask your own question.