Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethereum Nonce Management. error nonce too low

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

like image 800

People also ask

How do I edit nonce on MetaMask?

How do I customize the nonce when sending a transaction? Click the 'Custom nonce' field to enter the number you need, and, when you're ready to submit the transaction, 'Confirm'. Press 'Save' to confirm your choice.

What does a nonce of 0 mean?

A Nonce of "0" means that it is the first transaction executed from a particular wallet. The Nonce value is incremented in the order of "1" in each sequential transaction.

What is Uniswap nonce?

A nonce is the number of the transaction of the sender's address. Every transaction from an address is numbered sequentially, beginning with 0 for the first transaction. For example, if the nonce of a transaction is 10, it is the 11th outgoing transaction sent from the sender's address.


2 Answers

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

like image 181
ehanoc Avatar answered Oct 07 '22 06:10

ehanoc


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.

like image 26
user618677 Avatar answered Oct 07 '22 05:10

user618677