the tx doesn't have the correct nonce. account has nonce of: 5 tx has nonce of: 15

2.5k Views Asked by At

Using truffle (3.4.6), metamask (3.9.2) and testrpc (4.0.1), I call a transaction and get "Error: the tx doesn't have the correct nonce. account has nonce of: 5 tx has nonce of: 15". I have cut down the body of my contract method to something trivial and I still get this. Any idea what causes it?

contract MyContract {
  mapping (address => bool) authorized;

  function myMethod (uint element, uint price) whenNotPaused returns (bool) {
     if (!authorized[msg.sender]) throw;
     return true;
  }
}

I call the method like this (using truffle):

  MyContract.deployed().then((instance) => {
      instance.myMethod (id, price, {from: account}).then (...)
3

There are 3 best solutions below

0
On BEST ANSWER

In this thread i saw a suggestion of switching networks back and forth and the error self correcting.

¯\_(ツ)_/¯

0
On

I ran into this error using etherjs. As of ethers v6, you can use the getNonce method on your signer to find the Nonce and then pass that Nonce to the deploy method on your contract. Full example:

const wallet = new ethers.Wallet(
    process.env.PRIVATE_KEY,
    provider
  );

 const nonce = await wallet.getNonce();

const contract = await contractFactory.deploy(
    {   
        nonce: nonce,
        gasLimit: 1000000,
        gasPrice: 2000000000,
        value: 0,
    }
  );
0
On

@okwme is right, but you can also change your configuration of the network to fix the issue. Assume you're running a dev network on localhost with a HDWallet provider, then you can fix the error by commenting out that config property and add host and port properties to it. It looks like follows:

development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
      gas: 6721975
      // networkCheckTimeout: 10000,
      // provider: function() {
      //   return new HDWalletProvider(mnemonic, 'http://127.0.0.1:8545/', 0, 10);
      // },
      // network_id: '*',
      // gas: 6721975,
    },
  },