[英]How to handle multiple web3 transactions in nodejs
我正在處理這樣的交易:
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) =>{...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.