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
});
}
});
});
web3.eth.getTransactionReceiptstatus and then find the nonce usinggetTransactionCount– Crissi Mariam Robert Jun 07 '18 at 11:22