Giter Site home page Giter Site logo

Receiving 'Error: nonce has already been used' when deploying multiple unrelated contracts with ethers v6 and hardhat about ethers.js HOT 14 CLOSED

claasahl avatar claasahl commented on June 12, 2024 2
Receiving 'Error: nonce has already been used' when deploying multiple unrelated contracts with ethers v6 and hardhat

from ethers.js.

Comments (14)

applewil avatar applewil commented on June 12, 2024 8

Wrap your signer in a NonceManager to ensure sequential nonces for concurrent transactions

import { NonceManager, Signer } from 'ethers'
const signer: Signer = NonceManger(wallet);

from ethers.js.

tab00 avatar tab00 commented on June 12, 2024

I got error "ProviderError: Nonce too low. Expected nonce to be 1 but got 0" when in a local hardhat node I tried to send a raw transaction that has been signed by some other account and whose nonce is 0.

I had to fund that account first (to prevent "insufficient funds error"), then send the raw transaction:

  let txRec = await signer.sendTransaction({ to: otherAddress, value: ethers.parseUnits("0.1", "ether") })
  await txRec.wait()

  const signedRawTx = "0x..."
  const txHash = await ethers.provider.send("eth_sendRawTransaction", [ signedRawTx ])

from ethers.js.

zemse avatar zemse commented on June 12, 2024

How is the signer object created? Is the signer connected to the node provider? You can use signer.connect(provider) or provider.getSigner(addr) to have a signer with provider.

This enables the signer to infer the nonce otherwise it will use zero as the nonce while preparing the transaction.

from ethers.js.

claasahl avatar claasahl commented on June 12, 2024

How is the signer object created? Is the signer connected to the node provider? You can use signer.connect(provider) or provider.getSigner(addr) to have a signer with provider.

This enables the signer to infer the nonce otherwise it will use zero as the nonce while preparing the transaction.

Thanks for your reply. The signer is connected to a provider

https://github.com/claasahl/ethers-v6-with-hardhat/blob/1fa7e130d701f07b4e5c182bd79dd83c109ae856/src/example1.ts#L24C5-L24C5

from ethers.js.

claasahl avatar claasahl commented on June 12, 2024

I got error "ProviderError: Nonce too low. Expected nonce to be 1 but got 0" when in a local hardhat node I tried to send a raw transaction that has been signed by some other account and whose nonce is 0.

Thanks for your reply. In my case, one signer is used (not multiple).

https://github.com/claasahl/ethers-v6-with-hardhat/blob/main/src/example1.ts

from ethers.js.

claasahl avatar claasahl commented on June 12, 2024

That did the trick. Thanks @applewil

from ethers.js.

Aziz87 avatar Aziz87 commented on June 12, 2024

Wrap your signer in a NonceManager to ensure sequential nonces for concurrent transactions

import { NonceManager, Signer } from 'ethers'
const signer: Signer = NonceManger(wallet);

NonceManager not stable

from ethers.js.

PerminovEugene avatar PerminovEugene commented on June 12, 2024

@Aziz87 Does it mean that it's not good to use it in deployment script for main net?

from ethers.js.

ricmoo avatar ricmoo commented on June 12, 2024

What do you mean NonceManager is not stable? The nonce is managed internally, so there shouldn't be any problems regarding nonce management.

from ethers.js.

pkieltyka avatar pkieltyka commented on June 12, 2024

we've recently been upgrading from ethers v5 to v6 (finally), and we're hitting these issues too. What has changed from v5 to v6 related to nonce management..? its a bit strange we even need to wrap the wallet with a NonceManager .. doesn't feel like the cleanest approach or DX

from ethers.js.

ricmoo avatar ricmoo commented on June 12, 2024

@pkieltyka Nothing should have changed in ethers. In both v5 and v6, the getTransactionCount(address, "pending") is used.

Or are you using the NonceManager? I still donโ€™t think anything in the NonceManager (behaviour-wise) should have changed though.

Are there other libraries that could have affected behaviour in some way, like Hardhat?

from ethers.js.

pkieltyka avatar pkieltyka commented on June 12, 2024

I'm not working on the upgrade first hand myself, but my engineering team let me know they've been hitting the problem and then as I read the thread with suggestion to use the NonceManager to wrap the wallet, that looked very strange/unnecessary. But we'll definitely report back with our findings, and yea perhaps its something with hardhat.

Glad to hear between v5 and v6 its the same getTransactionCount(address, "pending")

from ethers.js.

ricmoo avatar ricmoo commented on June 12, 2024

Yeah, both Wallet and NonceManager should behave identically between v5 and v6. But I think Hardhat did a bunch of new things?

Regardless, have any engineer reach out to me on GitHub or Discord. I usually respond immediately to Discord assuming Iโ€™m awake, and catch up once awake if Iโ€™m asleep. :)

from ethers.js.

pkieltyka avatar pkieltyka commented on June 12, 2024

btw, you're right... I just tried to instead of using hardhat provider inline in the tests, I setup a JsonRpcProvider and pointed it at http://localhost:8545, running anvil .. and tests passed! the nonce has already been used issue has gone away

strange.. we're using the latest version of hardhat too, maybe something weird with the config there.. I'll investigate as I'm sure hardhat should work just fine too.

thanks very much for the offer to help and being so quick to respond! hope you're well buddy!!

from ethers.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.