[Ethereum] the tx doesn’t have the correct nonce. account has nonce of: 12 tx has nonce of: 3 – Ganache

contract-invocationganachenodejsnonceweb3js

I am trying to run my simple contract with different parameters (in the form of encoded signatures) and intends to put multiple transaction into ONE block. If I set Ganache GUI with automine option, it works perfectly but with one transaction in ONE block. However, if I set timedelay (i.e. say 2/3 sec) to enable multiple trxs into one block, then i am facing problem situations;
1. If I send my transaction with await keyword, then it works perfectly (but put one trx in one block, which i don't want) .
2. If i send my transactions without await keyword, then i put multiple trxs into one block, however then it give captioned error.

Here is code:

async function finalDeploy(r){ // this function deploy my contracts
  for(i=0; i<constructorParams.length; i++) {
// console.log("constructorParams", constructorParams[0])
// console.log("myBin", myBin[0])
    const options = {
      data: '0x' + myBin[r]+constructorParams[i],
      gas: 5000000,
  };
  const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
  const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Contract: " +r+" is deployed at  " +receipt.contractAddress);
  contractAddressess.push(receipt.contractAddress)

  }

}


async function finalSettersFunctions(){ // here i am calling setter functions of already deployed contracts 
  var myFunc; 
  var  myContAddr ;
  for(i=0; i < contractAddressess.length; i++) {
      myContAddr = contractAddressess[i]
    for(j=1; j<finalSetters[i].length; j++) {
          myFunc = finalSetters[i][j];
          console.log("function name: ", myFunc);
            try {
              //await web3.eth.sendTransaction( // if i enable this then ONE trx/ONE block
                 web3.eth.sendTransaction( // this makes multiple trxs/ONE block, but gives error
                {from:account1,
                to:myContAddr,
                data: myFunc
                    }, function(err, receipt) {console.log("Normal Setters: ",receipt)});
            } catch (error) {
              console.log(" Normal Setters: ERROR !"); 
            }
    }
  }
}


async function main() {


  for(r=0; r<contractFile.length; r++) {
      var myReceipt = await finalDeploy(r);

        console.log("============All  Setter Functions of Contract No. ", r);
        var myget = await finalSettersFunctions();

    console.log("Contract no. : "+r+ "  completed !")
  }
  }

  main().then(() => {
    console.log("ALL has been DONE !")
  })

Here is error:

UnhandledPromiseRejectionWarning: Error: Returned error: the tx doesn't have the correct nonce. account has nonce of: 12 tx has nonce of: 3

Best Answer

You can set the nonce with {nonce: <nonce>} but the bigger question is WHY?

The nonce should be managed by the client and there are valid reasons to track it yourself. Have a look over here at "Candidate 3" that I would recommend most of the time. Concurrency patterns for account nonce

However, this is not a valid design.

intends to put multiple transactions into ONE block

I'm not sure why you want to do that or how it might relate to the emergence of nonce problems, but I'm confident this is a bad direction.

There is no practical way to ensure that two transactions land in the same block, and AFAIK, there is no valid reason to WANT to do that. In testing environments, you might create the illusion that it is possible but the result will be misleading. It makes me wonder what you are hoping to achieve because it also certainly will not do what you hope it does.

Hope it helps.