0

i'm running a script using web3js in which one i have my accounts[0] that is running 2 transactions.

When i do: await web3.eth.getTransactionCount(accounts[0]) , i receive X (number) as a response.

Then submitting first transaction, till here all is ok!

I run again: await web3.eth.getTransactionCount(accounts[0]), now i receive X+1 as a response. All good till here.

Now here is the problem, i submit the 2nd Tx with accounts[0]. And it failed with "nonce too low" as an error message. The same transaction is ok when running with another account, like accounts[2] or whatever.

I read a lot about that I need to increase nonce of accounts[0], cause it seems that web3js keep in memory the same nonce for the whole script (so same nonce for every transaction of the same account in ths script). but i don't find how to do so with web3js.

//transaction 1: 

const receipt = await web3.eth .sendSignedTransaction("0x" + serializedTx.toString("hex")) .catch((err) => console.log(err));

const nonceUpdated = await web3.eth.getTransactionCount(myAddress); console.log("nonceUpdated =>", nonceUpdated); 

//transaction 2: 

await myDeFiProject .foo(accounts[1], amountFooFunction, { from: accounts[0] }) .catch((err) => console.log(err));

Here is the problem transaction 2 going to failed with "nonce too low" despite on my console.log i have an updated nonce. Looks like web3js keep the same nonce for all the transaction related to accounts[0] so there must be a way to increment it?

Thanks

4
  • You could get the nonce at the script start and then increase it when creating a transaction. That way you will only call getTransactionCount once at the beginning.
    – Ismael
    Jul 24 at 18:37
  • thanks Ismael, the thing is that i don't know how to increase it. Let me show you how it goes in my code: ` //transaction 1: const receipt = await web3.eth .sendSignedTransaction("0x" + serializedTx.toString("hex")) .catch((err) => console.log(err)); const nonceUpdated = await web3.eth.getTransactionCount(myAddress); console.log("nonceUpdated =>", nonceUpdated); //transaction 2: await myDeFiProject .foo(accounts[1], amountFooFunction, { from: accounts[0] }) .catch((err) => console.log(err)); `
    – Wallace
    Jul 25 at 19:47
  • It will look better if you edit the question to add that snippet of code. In javascript numbers are usually wrapped around libraries like BN o BigNumber because they don't fit in javascript's native numbers. That libraries usually provide function to increase them like add or something similar.
    – Ismael
    Jul 25 at 20:58
  • updated Ismael, thanks, sorry i'm new here
    – Wallace
    Jul 26 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.