nonce. It is an important piece of information, especially to ensure that a transaction is not sent twice.
Get you own node endpoint todayStart for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.
Parameters
-
address— the address to retrieve the transaction count. -
quantity or tag— the integer of a block encoded as hexadecimal or the string with:latest— the most recent block in the blockchain and the current state of the blockchain at the most recent blockearliest— the earliest available or genesis blockpending— the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.
See the default block parameter.
Response
quantity— an integer value identifying the number of transactions sent from an address at the specified block.
eth_getTransactionCount code examples
Use case
One of the most common use cases foreth_getTransactionCountis to create the transaction object built in a script designed to send a transaction. The nonce field is required, and it is retrieved using the eth_getTransactionCount method.
The following code shows how to build a rawTransction using web3.js and ethereumjs-tx.
Security noticeYou need your private key to sign the transaction; never share your private key with anyone.On a side note, the private key in this case must be imported without ‘0x’ at the beginning of the string.
index.js
nonce, gasPrice, and gasLimit, builds a transaction object, and signs it using a private key.
First, the createRawTransaction function calls validateInputs to ensure that the fromAddress and toAddress parameters are valid addresses. If either of these addresses is invalid, the function throws an error with a descriptive message.
Next, the function makes a call to web3.eth.getTransactionCount with the fromAddress as a parameter. This method returns the number of transactions sent from the fromAddress, and is used as the nonce for the transaction.
The function retrieves the gasPrice and gasLimit for the transaction using web3.eth.getGasPrice and web3.eth.estimateGas, respectively. The eth_gasPrice is the amount of gas that the transaction sender is willing to pay per unit of gas consumed by the transaction, while the eth_estimateGas is an estimate of the maximum amount of gas that the transaction will consume.
With the nonce, gasPrice, and gasLimit values, the function builds a transaction object, which includes the toAddress, gasPrice, gasLimit, nonce, and value of the transaction. The value is the amount transferred from the fromAddress to the toAddress.
The transaction object is then passed to a new Tx instance, which is signed using the private key. The signed transaction is then serialized and returned as a rawTransaction, which can be broadcasted to the network to execute the transaction.Body
application/json