簡體   English   中英

web3js處理多個事務,但是出現nonce錯誤

[英]web3js processes multiple transactions, but there was a nonce error

我發起了兩筆交易,但出現錯誤“tx 沒有正確的 nonce”。 account has nonce of: 213 tx has nonce of: 212 ',我應該如何處理這種情況? 可以跟蹤 nonce 值嗎?

web3.eth.getTransactionCount(ENV.account).then((txCount) => {
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(800000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
      to: ENV.contractAddress,
      data: contract.methods.auth(ENV.deviceName, ENV.serialNumber).encodeABI()
    };
    const raw = toToRaw(txObject);
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
      if (err) {
        console.error("error: ", err);
      } else {
        console.log("txHash: ", txHash);
        console.log("authenticate successfully!");
      }
    });
});

web3.eth.getTransactionCount(ENV.account,  (err, txCount) => {
  const txObject = {
    nonce: web3.utils.toHex(txCount),
    gasLimit: web3.utils.toHex(800000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
    to: ENV.contractAddress,
    data: contract.methods.createAbstract(
      "2022",
      web3.utils.utf8ToHex(ENV.serialNumber),
      contexts.ipfsHash,
      web3.utils.utf8ToHex(soilData),
      "xx"
    ).encodeABI()
  };
  const raw = txToRaw(txObject);
  web3.eth.sendSignedTransaction(raw, (err, txHash) => {
    if (err) {
      console.error("error: ", err);
    } else {
      console.log("txHash: ", txHash);
      console.log("create certification successfully!");
    }
  });

});

我應該如何處理這種情況? 可以跟蹤 nonce 值嗎? 我連接到甘納許。

使用“待定”對我不起作用。 web3.eth.getTransactionCount(ENV.account, 'pending', (err, txCount) => {

現在我用async function解決了這個問題,但是代碼很難看

(async function main() {
  await web3.eth.getTransactionCount(ENV.account,(err, txCount) => {
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(800000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
      to: ENV.contractAddress,
      data: contract.methods.auth(ENV.deviceName, ENV.serialNumber).encodeABI()
    };
    const raw = txToRaw(txObject);
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
      if (err) {
        console.error("error: ", err);
      } else {
        console.log("txHash: ", txHash);
        console.log("authenticate successfully!");
      }
    });

  });

  soilData = generateData();
  saveDataToIPFS(soilData).then((hash) => {
    console.log("IPFS HASH: ", hash);
    contexts.ipfsHash = hash;
  });

  web3.eth.getTransactionCount(ENV.account,(err, txCount) => {
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(800000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
      to: ENV.contractAddress,
      data: contract.methods.createAbstract(
        "2022",
        web3.utils.utf8ToHex(ENV.serialNumber),
        contexts.ipfsHash,
        web3.utils.utf8ToHex(soilData),
        "xx"
      ).encodeABI()
    };
    const raw = txToRaw(txObject);
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
      if (err) {
        console.error("error: ", err);
      } else {
        console.log("txHash: ", txHash);
        console.log("create certification successfully!");
      }
    });

  });


})()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM