a_lucky_guy
a_lucky_guy

Reputation: 23

web3js processes multiple transactions, but there was a nonce error

I initiated two transactions, but there's an error 'the tx doesn't have the correct nonce. account has nonce of: 213 tx has nonce of: 212 ',How should I handle this situation? Can nonce values be tracked?

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!");
    }
  });

});

How should I handle this situation? Can nonce values be tracked? I'm connected to the ganache.

use 'pending' is not work for me. web3.eth.getTransactionCount(ENV.account, 'pending', (err, txCount) => {

Upvotes: 1

Views: 506

Answers (1)

a_lucky_guy
a_lucky_guy

Reputation: 23

Now I solved this problem with async function, but the code is ugly

(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!");
      }
    });

  });


})()

Upvotes: 1

Related Questions