16

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 '18 at 9:59
7

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.

I believe you can also "reset" your account in metamask. This will set your nonce back to Zero. You can find this option under settings.

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

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
13

There is a new feature in Metamask for this:

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

  • 2
    link not working – Arash Kiani Aug 22 '18 at 20:48
5

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 '18 at 19:04
3

Starting from MetaMask v3.14.1 you can reset your account as follow:

Resetting an Account In the Settings menu, MetaMask has a "Reset Account" button. This button wipes the current account's transaction history, which is used to calculate the current account nonce.

enter image description here

Normal users should never have a reason to use this feature.

This is useful for developers who reset a test network but want to continue using the same account, forcing MetaMask to believe this network ID is a clean network in a fresh state.

Ref: http://metamask.helpscoutdocs.com/article/36-resetting-an-account

Copied from https://stackoverflow.com/a/47429502/8303489

3

I answered this in a stackoverflow post, however, since this post comes up on top in google search, so here we go

Check your truffle.js or truffle-config.js

if you are using the HDWalletProvider like below, it seems that this is what causing the error

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "your mnemonic";

module.exports = {
  networks: {
    development: {
      provider: function() {
        return new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/", 0, 50);
      },
      network_id: '*',
      gas: 9999999
    }
  },
  compilers: {
    solc: {
      version: "^0.4.25"
    }
  }
};

remove the HDWalletProvider like below, it solves mine

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost
      port: 8545,            // Standard Ganache UI port
      network_id: "*", 
      gas: 4600000
    }
  },
  compilers: {
    solc: {
      version: "0.5.2"
    }
  }
};
  • This question is unrelated to Truffle. – Ismael May 16 at 16:18
2

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.

0

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

0

I had the same problem solved by:

  1. As advised by Phillip Gibb in the above, I changed the Ganache network id to 5778. Ganache will restart.

  2. Re-connect MetaMask to Ganache.

    check your account. If only Account 1, this is a good sign. You can manually import other accounts. But if more than one account still exist, that's not a good sign. You may have to change to other Ganache network id and reconnect MetaMask.

  3. Migrate contract

truffle migrate --reset --network development

  1. Contract deployed. Test new transactions ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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