question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] useProvider isn't returning the correct provider when using Hardhat locally

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Package Version

0.3.2

Current Behavior

When you connect your Metamask wallet to a local hardhat node (running on localhost:8545), and then connect this wallet to a React app using wagmi, the output of the useProvider() hook returns a provider connected to homestead, instead of the local hardhat node.

Furthermore, useContractRead & useToken also use the default homestead-connected provider (instead of a provider connected to the local hardhat node), whereas useContractWrite correctly uses a provider/signer connected to the local hardhat node.

Expected Behavior

When you connect a Metamask wallet (connected to a localhost hardhat node) with wagmi:

  1. The useProvider hook should return a provider connected to this local hardhat node, and not a provider connected to homestead.

  2. Hooks like useToken & useContractRead should use a provider connected to the local hardhat node, and not one connected to homestead.

Steps To Reproduce

I’ve created a mini repro example showing this behavior at: https://codesandbox.io/s/cool-platform-0u6n2z?file=/src/index.tsx

Basically, once you connect a Metamask wallet (that’s connected to a local hardhat node), you’ll see that the useProvider() hook returns a provider that’s connected to homestead instead.

If Codesandbox shows an error with importing import { InjectedConnector } from "wagmi/connectors/injected";, simply comment out this import line (line 3 @ App.tsx), save file (cmd/ctrl+s), uncomment this import line, save file (cmd/ctrl+s), and everything should work again.

Link to Minimal Reproducible Example (CodeSandbox, StackBlitz, etc.)

https://codesandbox.io/s/cool-platform-0u6n2z?file=/src/index.tsx

Anything else?

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thecooltechguycommented, May 12, 2022

After some debugging, it appears that the root cause for this bug is that inside the useChainId hook (https://github.com/tmm/wagmi/blob/8a5f6c209049c35f16f9c6a86cd4ea71106e84a6/packages/react/src/hooks/utils/useChainId.ts#L7), it returns provider.network.chainId – however, provider.network is undefined. As a quick test, I edited the useChainId hook to simply return the input chainId without querying the provider via useProvider, and my code began to work again (ie., read-based smart contract calls worked, etc.).

From looking at the ethers docs & code, it appears that the network for a provider can be obtained via provider.getNetwork() (instead of provider.network), which returns a promise: https://github.com/ethers-io/ethers.js/blob/01aea705ce60b1c42d2f465b162cb339a0e94392/packages/providers/src.ts/base-provider.ts#L1135

@tmm So perhaps, should the useChainId hook be updated to fetch the network via the provider’s getNetwork() function? Or, am I maybe misunderstanding something here?

0reactions
thecooltechguycommented, May 27, 2022

@jamiew Thanks for sharing this! Using the new API you mentioned + Rainbowkit has solved the issue for me.

@jxom I’m no longer running into these issues so I’ll close this issue. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[bug] useProvider isn't returning the correct provider when ...
When you connect your Metamask wallet to a local hardhat node (running on localhost:8545), and then connect this wallet to a React app...
Read more >
Error: Could not find the correct Provider<UserProvider ...
Ensure the correct context is being used. I want to know the problem why using Provider.of(context) does not work import 'package ...
Read more >
Hardhat: networks and providers
Hardhat : networks and providers. An Ethereum node can be used to read the state of the blockchain or to send transactions that...
Read more >
How to use web3-react - tonisives
Tutorial about how to use web3-react and hardhat to read and execute Solidity contracts.
Read more >
Build Your First DApp Using Flutter | Web3 | Solidity | Blockchain
Two main alternatives to Truffle are Hardhat,which comes built-in with Hardhat Network, a local Ethereum network and Remix IDE, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found