[Ethereum] how get to nonce that need to send in ethereum transaction

ethernodesweb3js

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);
                            tx.sign(privateKey);

                            var serializedTx = tx.serialize();

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

Best Answer

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.