1

I'm using rainbowkit wallet which offers various wallet options such as walletconnect and coinbase.

My question is, if I'm connected via metamask then my web3 instance can use window.ethereum as provider.

But if I'm connected to other wallets like walletconnect or coinbase, can I still go with window.ethereum as provider?

i.e. let web3 = new Web3(window.ethereum);

if not, for other wallets where can I get its provider? is there an option to grab currently connected provider using wagmi?

3 Answers 3

4

Figured it out.

I had to use signer object to get provider.

import { useSigner } from 'wagmi';

const {data: signer} = useSigner();

const web3 = new Web3((signer?.provider as any).provider);
2

Use the useProvider() hook as detailed in the docs 😉

3
  • Thank you, but it seems like provider cannot be just passed as an argument. const provider = useProvider() const web3 = new Web3(provider); <-- error
    – bbusdriver
    Aug 22, 2022 at 15:44
  • Wagmi uses Ethers under the hood, so provider you get via the useProvider() hook is already an Ethers provider. All the hooks provided by Wagmi are there so you don't have to manually instantiate a provider. If you want to send a transaction, you can use the useSendTransaction hook. If you want to execute read functions from a contract, use the useContractReads hook. And to use write contract functions, you can use the useContractWrite hook
    – Antonio U
    Aug 23, 2022 at 11:06
  • thanks, this is all because i'm using biconomy where transactions are sent to their api As you can see in the example, it requires web3 instance with provider being passed
    – bbusdriver
    Aug 24, 2022 at 20:50
0

In this example below, I'm using the wallet provider (browser wallet, walletconnect, etc) to get data from a smart contract.

import { useContractRead, useWalletClient } from "wagmi";

const { data: walletClient } = useWalletClient();
  const contractRead = useContractRead({
    address: contractAddress,
    abi: contractABI,
    functionName: "getAllProjects",
    walletClient: walletClient,
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.