Replies: 4 comments 4 replies
-
Don't think this is an "issue" related to wagmi, so I am going to convert this one to a discussion thread for the community to answer! Sorry, I don't think I have a solution to this as I am not a web3.js user. |
Beta Was this translation helpful? Give feedback.
-
Solved by using useSigner()
Typecasting to any because provider does not exist in provider according to typescript, except it does! Can't use the top-level provider in web3. |
Beta Was this translation helpful? Give feedback.
-
This is basically how I did it //ethersHooks.js
import * as React from "react";
import { useWalletClient } from "wagmi";
import { providers } from "ethers";
import Web3 from "web3";
export function walletClientToSigner(walletClient) {
const { account, chain, transport } = walletClient;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
const provider = new providers.Web3Provider(transport, network);
const signer = provider.getSigner(account.address);
return signer;
}
/** Hook to convert a viem Wallet Client to an ethers.js Signer. */
export function useEthersSigner({ chainId } = {}) {
const { data: walletClient } = useWalletClient({ chainId });
return React.useMemo(
() => (walletClient ? walletClientToSigner(walletClient) : undefined),
[walletClient]
);
}
export function useWeb3Signer({ chainId } = {}) {
const { data: walletClient } = useWalletClient({ chainId });
return React.useMemo(
() =>
walletClient
? new Web3(walletClientToSigner(walletClient).provider.provider)
: undefined,
[walletClient]
);
} Then you can just use it doing this // We get web3 signer (not provider) from this
const web3 = useWeb3Signer(); This is ethers v5. If you are using v6 you probably need to change it accordingly using https://wagmi.sh/react/ethers-adapters |
Beta Was this translation helpful? Give feedback.
-
I've created PR to this issue "How to use Web3js with Wagmi". |
Beta Was this translation helpful? Give feedback.
-
Is there an existing issue for this?
Package Version
0.10.15
Current Behavior
So the background is I have a complex dapp where we use web3.js to interact with all contracts and do transactions (over 200 different calls). Previously used web3modal v.1 to set the provider as such:
Now upgraded to web3modal 2.0 and the only way to get hands on the provider is via wagmi (I think). Hence I found the useProvider()
https://wagmi.sh/react/hooks/useProvider
So I'm trying this:
But that provider is not typescript compatible with what Web3 expects.
And forcing it with (provider as any) will make the web3 calls to fail. Would looks like this for example:
Reference (the version used is 1.8.1):
https://web3js.readthedocs.io/en/v1.8.1/getting-started.html#adding-web3-js
Expected Behavior
I don't know if it's supposed to work like this, but the expected behaviour would be Web3 just accepting the provider as it is or with some middleware. I would be ok with anything to just get this to work.
Steps To Reproduce
Make any web3 contract call or transaction
Link to Minimal Reproducible Example (CodeSandbox, StackBlitz, etc.)
No response
Anything else?
A workaround is to do
new Web3(Web3.givenProvider)
But that only works when using metamask on desktop. Connecting any mobile wallet via WalletConnect will fail since the provider is then still trying to using metamask. Or I'm doing this all wrong, please enlighten me. I understand that we can use wagmi hooks for all contract calls, but I want to avoid that if possible due to all changes and research required.
Edit:
It seems that what I'm looking for is a way to convert the ethers provider to a web3 provider. Not sure if it's possible or where to find clues.
Beta Was this translation helpful? Give feedback.
All reactions