Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ethers JSON RPC provider doesn't return correct nonce when used with hardhat #4549

Open
valeriivanchev opened this issue Jan 17, 2024 · 1 comment
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@valeriivanchev
Copy link

Ethers Version

6.8.1

Search Terms

nonce, provider doesn't get correct nonce,

Describe the Problem

Hi EthersJs Team,
We have a hardhat deployed inside a docker locally and we have tests that are connecting to the deployed hardhat and issue transactions. The provider we are using is JsonRpcProvider, when we issue multiple transaction one after the other sometimes the nonce is not correct. We also checked if we request the nonce from the hardhat network using the JSON-RPC request ("eth_getTransactionCount") and request the transaction count using the provider we get different outcomes.
Do you have some cache setup, because we are getting the nonce from the prev transaction.

Code Snippet

No response

Contract ABI

No response

Errors

No response

Environment

No response

Environment (Other)

No response

@valeriivanchev valeriivanchev added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Jan 17, 2024
@valeriivanchev valeriivanchev changed the title Add Bug Title Here Ethers JSON RPC provider doesn't return correct nonce when used with hardhat Jan 17, 2024
@ricmoo
Copy link
Member

ricmoo commented Jan 18, 2024

There is a caching layer which can affect providers that behave synchronously or with near-immediate results.

The GanacheProvider includes code to disable the caching which may be useful as a base for creating a sub-class, but basically when instantiating the provider you can use:

class MyProvider extends JsonRpcApiProvider {
  constructor(network: Network) {
    super(network, {
      staticNetwork: true,     // Ensure we never ask for eth_chainId; optional
      batchStallTime: 0,       // Don't batch requests, send them immediately
      cacheTimeout: -1         // Do not employ result caching
    });
  }
}

You can of course pass the same parameters along to a JsonRpcProvider without sub-classing. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants