6

I use web3 and provider mainnet. I do 2 transactions by contract. first is approve method and another transaction is multitransfer. I store second signature in database. if first transaction is success I send second transaction/. second transaction almost always error nonce too low`. how i can solve this problem

2 Answers2

12

For proper nonce management you have 2 options :

  • Request the nr of transactions confirmed for your address with web3.eth.getTransactionCount(ethAddress), increment, send and wait for receipt before processing the next one. This is very slow if you need to have high-throughput and you are relying on a specific node to be available and to be synced.

  • You maintain your own local counter persisted at the level of the database. Use the DB's access locks to handle possible concurrent requests and return correct values every time. You do not want to keep this counters in memory as they will be lost if your application crashes or restarts. This is very efficient as you do not need the node and you can send as many transactions as you can dish out. If something goes wrong... (nonce too low) reset to the value of web3.eth.getTransactionCount(ethAddress).

Important Note: You might wonder why not to use web3.eth.getTransactionCount(ethAddress, 'pending'). This is because the 'pending' option makes the call unreliable as its hard for the nodes to have an accurate number of transaction in the queue & the memory pool.

For a better understanding how the nodes look at your message's nonce. Check this answer here : https://ethereum.stackexchange.com/questions/2808/what-happens-when-a-transaction-nonce-is-too-high/2809#2809

And this one too : Send Raw Transaction Ethereum infura nodejs npm

ehanoc
  • 2,187
  • 18
  • 23
  • Could you show examples of what you meant with "increment, send and wait for receipt before processing the next one" please? – Dorklord Dec 03 '22 at 09:28
2

There may be two issues(solutions) here from my understanding; 1) You may have to manually increment nonce for the gas estimation of the multitransfer operations. 2) Some server are pretty slow these days, so either you fetch transactionReceipt(poll) of first transaction before doing the second to be sure that it has been mined. This way you would likely have the right nonce for second transaction. However, if you are lazy to do this, just are reasonable delay between the two transactions.

user618677
  • 4,909
  • 6
  • 23
  • 24
  • look, for example: I have 2 signature with nonce 6 and 7. I send first signature to node and wait for mined. if it was mined I send second signature. it is ok. BUT between transaction if someone do transaction from this wallet then this transaction aslso took nonce number 7. come out I have 2 transactions with same none, therefore I got error `nonce too low` – Максаткалиулы Адилет Jun 09 '19 at 12:25