Solvedweb3.js getTransactionCount() returns incorrect nonce when transactions are pending

The situation I have is this:

  • The correct nonce of (208) is reported by https://rinkeby.etherscan.io/.
  • An incorrect nonce of (206) is returned from web3.eth.getTransactionCount()
  • I have 2 pending transactions on rinkeby.

so I assume the difference of 2 comes from my 2 pending transactions?

I've only tested this on rinkeby.

To reproduce the error you'll need to submit a transaction that sits too long with a pending status.

I'm fortunate/unfortunate enough to be able to reproduce this consistently when trying to deploy a contract on rinkeby. (at the time of this writing, my contract deployments are taking up to two days to confirm regardless of gwei).

If you want to try and reproduce, here is my rough code and hopefully your transactions pend awhile ;)

// I've already compiled using solc and have the abi and bytecode 

let contract = new web3.eth.Contract(abi);

let txn = contract.deploy({data: '0x'+bytecode}); 

let txn_object = {
  gas: 270000,
  gasPrice: '55', 
  from: from, 
  data: txn.encodeABI()
};

web3.eth.getTransactionCount(from)
  .then(nonce => {
    txn_object.nonce = nonce;

    let raw_txn = new require('ethereum-tx')(txn_object);
    raw_txn.sign(private_key);
    let serialized = raw_txn.serialize();
    let signed_txn = '0x'+serialized.toString('hex');

    web3.eth.sendSignedTransaction(signed_txn).on('receipt', (result) => console.log(result));
  })

running this the second time results in transaction already known error because getTransactionCount() is returning previous nonce.

I know that I could reproduce using txn.send({from:from}) instead of going through the trouble of manually signing the txn, but I wanted to report exactly what I'm doing.

15 Answers

✔️Accepted Answer

I believe you can call web3.eth.getTransactionCount(address, 'pending') to include pending transactions.

Other Answers:

Any update on this? This is still an issue on Quorum v2.2.4 using web3 1.0.0-beta.55.

We're retrieving the nonce like this: eth.getTransactionCount(this.account.address, 'pending');. However, if we send a lot of transactions at the same time, some of them fail:

Transaction has been reverted by the EVM:
{
  "blockHash": "0x004092e938498bbdc9ce609a854ab588a39cc69da54201b572f61873157684e2",
  "blockNumber": 1769,
  "contractAddress": null,
  "cumulativeGasUsed": 29919,
  "from": "0xeddf42a34b24d37bc6f4b8aac1d11eb8a72f99ae",
  "gasUsed": 29919,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": "0x353b7ffc2eaa9ca5d5a55b377956d201ef100645",
  "transactionHash": "0x05d2611ce819bbeb3de934a0f9537c32dc78eb6559532725a39e6938930c77b8",
  "transactionIndex": 0
}

This does not happen if we manually increment the nonce for each transaction.

More Issues: