我正在处理这样的交易:
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)
});
}
});
目前,如果发件人进行了 1 笔交易,这会起作用,但如果发件人在挖掘之前进行了两笔交易,随机数是相同的,因此会导致错误。
我已经阅读了关于链接的 web3js 文档,但我不确定这是否是我正在寻找的内容。
我将如何更改我现有的代码以便在多笔交易被开采之前为它们工作?
最佳答案
您的应用应管理nonce
并保持最新值,然后在发送交易中使用它。
但也许 pending
交易计数可以解决您的问题
web3.eth.getTransactionCount(sender_address, 'pending', (err, txCount) =>{...
关于javascript - 如何在 nodejs 中处理多个 web3 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65349603/