[Ethereum] Error: nonce too low

web3js

I try to send eth from one address to another, but I get an error:Error: Returned error: nonce too low
What could be the problem?

const Web3 = require('web3')


const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io'))   
var Tx = require('ethereumjs-tx').Transaction;
var privateKey = Buffer.from('private_key', 'hex');

var rawTx = {
  nonce: '0x000',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: 'to_address',
  value: '0x00',
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

Best Answer

Error: Returned error: nonce too low

A nonce is basically the number of transactions that have been performed from a particular account address. In order to fix the above error:

const accountNonce =
  '0x' + (web3.eth.getTransactionCount(ethereum_account_address) + 1).toString(16)

Replace the nonce value in the rawTx with the above one.

var rawTx = {
  nonce: accountNonce,
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: 'to_address',
  value: '0x00',
}

This will get rid of the nonce too low error.

Related Topic