I have written down node js code to send eth using web3. I am getting nonce value using getTransactionCount which is working fine but as all know it can not count pending transaction so if i send another transaction before the first one confirmed it generates an error. here is my code

                    var pkey1 = xxxxxxxxxxxxxxx;
                    var privateKey = new Buffer(pkey1, 'hex');

                    web3.eth.getTransactionCount(fromAccount, function(err, nonce) {
                        web3.eth.getGasPrice(function(err, gasPrice) {
                            var gasLimit = 31500;
                            gasPriceHex = web3.utils.toHex(gasPrice);
                            gasLimitHex = web3.utils.toHex(gasLimit);
                            nonceHex = web3.utils.toHex(nonce);
                            amountHex = web3.utils.toHex(amount);
                            var rawTx = {
                                nonce: nonceHex,
                                gasPrice: gasPriceHex,
                                gas: gasLimitHex,
                                to: toAccount,
                                from: fromAccount,
                                value: amountHex,
                            var tx = new Tx(rawTx);

                            var serializedTx = tx.serialize();

                            try {
                                web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
                                    if (err) {
                                            status: false,
                                            msg: "Please try after sometime",
                                            httpcode: 400
                                    } else {
                                            status: true,
                                            msg: "Transaction Done Successfully",
                                            txid: hash,
                                            httpcode: 200
                            } catch (e) {
                                    status: false,
                                    msg: "insufficient funds",
                                    httpcode: 400

2 Answers 2


It is the sender's responsibility to know their account nonce. You can't inspect the node for pending transactions as a substitute for tracking account nonce-not reliably.

Generally, a wallet keeps track of the nonce so the user doesn't need to worry about it. You need to be aware of a few details to reliably send transactions at scale.

See this answer for a conceptual overview of a reliable process and exception handling with a link to a technical discussion of alternative approaches: How to send a lot of transactions to ethereum, using js

Hope it helps.


Although miners can generally sort your transactions by nonces in their mempool and include them in the right order, this practically does not happen. So it is strongly recommended to verify that your previous transaction was included in a block (and maybe also verified at least for 2 or 3 blocks) before sending out a transaction with the next nonce.

  • 1
    but this code i am using in public app i can not restrict user to wait untill previous transaction confirmed Jun 6, 2018 at 21:52
  • Then it's certainly time for a code redesign, as the Ethereum network will probably not willing to adapt to that app ;) Jun 7, 2018 at 8:52
  • 1
    @AnilBaweja For the same case,I have used web3.eth.getTransactionReceipt status and then find the nonce using getTransactionCount Jun 7, 2018 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.