I am handling a transaction like this:
web3.eth.getTransactionCount(sender_address, (err, txCount) =>{
console.log(txCount)
if(err){
console.log(err);
}else{
const txObject = {
nonce: web3.utils.toHex(txCount),
to: master_address,
value: web3.utils.toHex(web3.utils.toWei("0.1", "ether")),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei("10", "gwei"))
}
const tx = new Tx(txObject);
tx.sign(sender_private_key);
const serialized_tx = tx.serialize();
const raw = '0x' + serialized_tx.toString('hex');
web3.eth.sendSignedTransaction(raw, (err, txHash) =>{
if(err){
console.log(err);
}else{
console.log("txHash:", txHash);
}
}).then(function(receipt) {
//Insert amir
if(receipt["status"] == true){
console.log("Tx has been mined")
}else{
}
console.log(receipt)
});
}
});
At the moment this works if the sender makes 1 transaction, but if the sender makes two transactions before they are mined, the nonce is the same, and will therefore cause an error.
I have read in web3js documentation about chaining, but i am not sure if this is what i am looking for.
How would i change my existing code in order to work for multiple transactions before they are mined?
Your app should manage nonce
and keep latest value, then use it in send transaction.
But maybe pending
transactions count solve your problem
web3.eth.getTransactionCount(sender_address, 'pending', (err, txCount) =>{...