This topic explains how nonces work in Ethereum Virtual Machine (EVM) transactions. It also gives you tips for managing nonces when you handle many transactions at once.
Before you read this, you should know the basics about EVM wallets and how transactions are signed.
In EVM-based blockchains, a wallet nonce is a counter. It tracks how many transactions you sent from a specific wallet address. Each transaction must use a different nonce. You must use nonces in order, one after another.
Key points:
Problems can happen when you create many transactions at the same time from one wallet address.
This problem often happens when many users create transactions at once, and your system uses the same EVM wallet for all these requests.
When signing transactions, multiple threads in your system might try to get the wallet's current nonce at the same time. If this race condition occurs, multiple transactions might get assigned the same nonce number. When this occurs, only the first transaction works. The others fail.
Another common problem happens when transactions get stuck in the blockchain's mempool and block later transactions. Here's an example:
Your integration may vary based on your systems. Here are some general tips for managing nonces:
Set up one central system to track nonces for each wallet. This system should safely handle nonce assignment when multiple threads request to sign transactions at once. Make sure each nonce gets assigned only once to prevent duplicates. You can use a locking system or a queue system to manage nonce assignment when handling multiple operations.
Keep your system in sync with the blockchain to track the latest nonce numbers. Your local nonce tracking should reset its counter if it gets out of sync with the blockchain's nonce state. Set up regular health checks to find nonce gaps.
You should sync with the blockchain: