Understanding and Using Nonce in EVM Transactions

·

In blockchain technology, especially on Ethereum Virtual Machine (EVM) compatible chains, a nonce is a critical component for transaction ordering and security. It is a number that represents the number of transactions sent from a specific address. This article explains how to query the next available nonce and the pending nonce from the mempool using a Web3 API, ensuring your transactions are broadcast correctly and avoid failures due to nonce issues.

What is a Nonce?

A nonce, an abbreviation for "number used once," is a unique number assigned to each transaction from a given Ethereum address. It serves two primary purposes:

  1. Transaction Ordering: It ensures transactions from the same address are processed in the correct order. The first transaction has a nonce of 0, the next 1, and so on.
  2. Preventing Replay Attacks: It prevents the same transaction from being executed multiple times.

For instance, if the last confirmed on-chain transaction from an address has a nonce of 10, the next transaction must use a nonce of 11. If transactions with nonces 11 and 12 are already broadcast but waiting in the mempool to be mined, the next available nonce to use for a new transaction would be 13. This is crucial information for developers to build seamless wallet and dApp experiences.

How to Query Nonce Information via API

To assist developers in obtaining accurate nonce data, robust wallet APIs provide endpoints specifically for querying this information. This helps in constructing transactions that are less likely to fail due to incorrect nonce usage.

API Request Path

The GET request is made to the following endpoint to fetch the nonce details:
https://web3.okx.com/api/v5/wallet/pre-transaction/nonce

Required Request Parameters

Two parameters are mandatory for this request:

ParameterTypeRequiredDescription
chainIndexStringYesThe unique identifier of the chain.
addressStringYesThe wallet address to query.

Understanding the Response Parameters

The API returns a response containing two key pieces of information:

ParameterTypeDescription
nonceStringThe next available nonce that can be used for a new transaction to be included in a block.
pendingNonceStringThe next nonce value that follows the highest nonce currently present in the mempool for the given address.

Example API Request

Here is a practical example of how to structure a cURL request to this endpoint:

curl --location --request GET 'https://web3.okx.com/api/v5/wallet/pre-transaction/nonce?chainIndex=1&address=0x1ucda' \
--header 'Content-Type: application/json' \
--header 'OK-ACCESS-PROJECT: 86af********d1bc' \
--header 'OK-ACCESS-KEY: 37c541a1-****-****-****-10fe7a038418' \
--header 'OK-ACCESS-SIGN: leaV********3uw=' \
--header 'OK-ACCESS-PASSPHRASE: 1****6' \
--header 'OK-ACCESS-TIMESTAMP: 2023-10-18T12:21:41.274Z'

Example API Response

A successful API call will return a response in the following JSON format:

{
  "code": "0",
  "data": [
    {
      "nonce": "15",
      "pendingNonce": "21"
    }
  ],
  "msg": "success"
}

In this example, the next available nonce to use for a new transaction is 15. The pendingNonce of 21 indicates that the mempool already contains transactions from this address with nonces 15 through 20, which are waiting to be mined. Therefore, a transaction with a nonce of 21 would be the next in line after those pending.

Best Practices for Managing Nonces

Managing nonces correctly is vital for a smooth user experience. Here are some best practices:

Frequently Asked Questions

What happens if I use a nonce that is too high?
If you use a nonce that is significantly higher than the next expected one (e.g., skipping several numbers), the transaction will be considered invalid and will not be processed by the network. It will remain in the mempool until all previous nonces are filled, which may never happen.

Can two transactions have the same nonce from one address?
No. Each transaction from a single address must have a unique, sequential nonce. If you broadcast two transactions with the same nonce, the network will typically only mine the one that pays the higher gas fee, and the other will be dropped.

Why is my transaction stuck with a pending status?
A transaction can get stuck if the gas price set is too low for current network conditions, causing miners to prioritize other transactions. It can also happen if a previous transaction with a lower nonce is also stuck or failed, blocking all subsequent transactions.

How does the 'pendingNonce' help me?
The pendingNonce value shows you the next nonce that is expected after all currently pending transactions. This helps you understand the queue and avoid broadcasting a transaction with a nonce that is already occupied in the mempool, preventing immediate failure.

What should I do if a previous transaction fails?
If a transaction with a specific nonce fails, you often need to resend a new transaction with the same nonce but a higher gas price to replace it. This is known as "replacing by fee." Once that nonce is cleared, you can proceed with the next ones.

Is nonce management the same on all EVM-compatible chains?
Yes, the fundamental concept of a nonce is consistent across all Ethereum and EVM-compatible blockchains like BNB Smart Chain, Polygon, and Avalanche C-Chain. The nonce is tracked per address per chain. For advanced management techniques across multiple chains, you can explore more strategies offered by professional API services.