I'm trying to call the leafPicked() function in my contract from web3 which launches Metamask as intended. After pressing accept the transaction shows as failed along with the error message pictured below.

What am I doing wrong?

Contract Function:

function leafPicked(uint numPicked) public returns (uint, uint){
    require(numPicked < remaining_leaves);
    uint number_before_picking = remaining_leaves;
    remaining_leaves -= numPicked;
    leafWasPicked(remaining_leaves, numPicked);
    return (remaining_leaves, number_before_picking);
}

Web3 function:

  web3.eth.getAccounts(function(error, accounts) {
    if (error) {
    console.log(error);
    }

    var account = accounts[0];
    console.log(account);

    App.contracts.Plant.deployed().then(function(instance) {
      plantInstance = instance;

      // This function is not executing as intended
      return plantInstance.leafPicked(parseInt(leafNum), {from: account});

    }).then(function(result) {
      console("Result from leaf picking: ", result);
      return App.SetContractValues();
    }).catch(function(err) {
      console.log(err.message);
    });
  });

enter image description here

  • According to the screenshot you have the wrong nonce in your transaction, rather than an out of gas error. You can check if you are you using in the code the same account as MetaMask. – gisdev_p Nov 16 '17 at 14:56
  • How can you retrieve the nonce? – Charles.fried Nov 16 '17 at 15:06
  • web3.eth.getTransactionCount(account) gives you the nonce for the next transaction – gisdev_p Nov 16 '17 at 15:30
  • Ok. so web3.eth.getTransactionCount(account); does give 4 which is inline with the error. The tx however is set to 15 - how do I get them to match? – Charles.fried Nov 16 '17 at 15:46
  • Question already at: stackoverflow.com/questions/45585735/… – Muhammad Altabba Jul 22 at 9:59
up vote 1 down vote accepted

I had the same issue with ganache, obviously re-installing metamask is not trivial, thank goodness changing the networkId of ganache put a stop to the nonce issue in metamask.

  • 1
    for me also changing from localhost to ropsten and back to localhost solved the issue – CodingYourLife Aug 30 at 1:57

There are two common problems people have when using MetaMask on private networks.

If you're running a private network, you have to be sure to set your chainId to the same as your networkId. This is for EIP 155 compatibility.

If you're running a test blockchain that you've shut down and restarted from a blank state with MetaMask connected to it, you can get MetaMask confused, because it caches some information about the network it is currently connected to, including completed transactions, which it uses to derive the correct nonce. You can clear this cache by selecting a different network in MetaMask, and then your local server again.

  • 2
    Hi Dan, I keep getting this error when connecting to the truffle RPC. Switching server does not solve it, only reinstalling Metamask works. Do you have another solution I can try? – Charles.fried Nov 17 '17 at 9:21
  • Try starting TestRPC with a different Network ID. MetaMask uses the current network ID to keep track of successful transactions, and uses those to compute its nonce. If you keep re-using the same network ID, MetaMask will assume it's still on the same network. We'll have it auto-fix this soon, as discussed in this issue: github.com/MetaMask/metamask-extension/issues/1999 – DanF Nov 18 '17 at 0:04

There is a new feature in Metamask for this:

https://consensys.zendesk.com/hc/en-us/articles/360004177531-Resetting-an-Account-New-UI-

  • worked like a charm! – dmi3y Jun 30 at 4:46
  • link not working – Arash Kiani Aug 22 at 20:48
  • Worked perfectly! Thanks! – David T Aug 27 at 13:20

It seems this is an ongoing Metamask issue when testing on the private networks.

It is described here: Nonce calculation is broken for private networks

And here: Truffle develop static network id interferes with MetaMask

The current solution is to reinstall Metamask - it worked for me.

  • You can also simply disable and re-enable Metamask in Chrome. – Stan James Jan 6 at 19:04

Genash uses the same networkId as ropsten network, and for costum url's metamask uses the same network params as the last network, so change your network to ropsten1 and back to localhost.

Thanks CodingYourLife for the solution.


1 or whatever network your local client is running on.

New contributor
Ohad Cohen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You should reset your metamask account by going to setting option and reset account. Please check the metamask docs.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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