Nonce - What is it and optional use

Modified on Wed, 24 May 2023 at 09:04 AM

TABLE OF CONTENTS


In blockchain technology, the nonce is a transaction counter that helps to prevent double-spending and replay attacks on a blockchain network. This value is attached to each Ethereum and EVM-compatible chain sender addresses.


Ethereum and other EVM-compatible blockchains require the nonce value for each transaction to ensure that the same transaction is not executed twice, and no one can replay a previously sent transaction.


With Tatum, when the nonce is not specified when making a transaction on Ethereum and other EVM-compatible chains, our Engine automatically adds it based on the sender's address. 


The Tatum engine may have a delay when automatically calculating the nonce. Users with high transaction broadcast volumes should manually apply the nonce value to their transactions. Failure to do so may result in the transactions being rejected by the blockchain.


Finding the nonce value

Via API request

Users can get the current nonce value for their Ethereum sender address by making an API call.

Example cURL:

curl --location 'https://api.tatum.io/v3/ethereum/transaction/count/0x271ebc3C939Db4d0######' \
--header 'x-api-key: ##Mainnet_API_KEY'
//response:
4887 // Current nonce value in decimals

Via RPC node request

Users can also obtain the current nonce value using an Ethereum node's RPC call.


Example cURL:

curl --location 'https://api.tatum.io/v3/blockchain/node/ETH/' \
--header 'x-api-key: ##Mainnet_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "jsonrpc":"2.0",
    "method":"eth_getTransactionCount",
    "params":[
        "0x271ebc3C939Db4d0######'",
        "latest"
        ],
    "id":1
    }'
//response:
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": "0x1316" //needs conversion to decimal
}

Nonce value and dropped transactions

Single transaction

If a transaction with nonce value X is dropped or doesn't get included in a block, the nonce value for that account doesn't increase. The nonce is only increased once a transaction is successfully included in a block.


Multiple transactions 

If you try to send multiple transactions at the same time, each transaction must have nonce+1, nonce+2, nonce+3, nonce+4, etc. If for some reason the transaction with nonce 2 gets dropped (low gas price, network congestion, etc.), the transactions with nonce values 3 and 4 will be stuck in the mempool and won't be mined until the transaction with nonce 2 is mined. 


To solve this, you would need to re-send the transaction with nonce 2 with enough gas for it to be mined. Once the transaction with nonce 2 would be mined, any subsequent transactions with higher nonce value should be able to be mined too.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article