如何在 nodejs 中处理多个 web3 事务

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) =>{...