I am using the web3modal
import { useWeb3Modal } from "@web3modal/react";
const foo = () => {
const { open, close } = useWeb3Modal();
async function connect_wallet() {
try {
await open(); // Open the web3modal provider selection modal
console.log("web3 and provider")
// provider
} catch (error) {
console.error("Error connecting to provider:", error);
}
}
return(
<div>
<button onClick={connect_wallet}>
Connect Wallet
</button>
</div>
)
}
so the web3modal pops up fine and gets connected, but how to grab the values of provider and web3 from it ?
Okay, A little background, I needed to work on a project, just to realize that there have been a lot of changes to the @web3mnodal library, I was frustrated trying to get the connected provider from the
open
method but failed to.Now, when setting web3modal up, I chose the web3modal+wagmi setup (not the ethers setup), the reason being that most of my ethers code was using v6, and the web3modal ether version is stuck in v5(meaning I'd have to switch my codebase back to 5), the good thing about wagmi set up is that it is connected to the web3modal in some way.
I imported some hooks from the
wagmi
library i.eimport {useAccount, useWalletClient} from 'wagmi'
The useAccount hook gives you the state of the account, like the address, the current connector(if connected else null), the connected state, and the
connecting
state (if it's connecting at the moment)The useWalletClient on the other hand gets the actual connector/walletCient (this is what you need, as this has more information unlike the connector gotten from the useAccount hook, you could do a
connector.getWalletClient()
tho)This was the easy part, the hard part was trying to convert the connector/walletCliend to an ethers Provider and/or Signer, I had to create a helper function for that.
Code would look like this sample
Hope this helps.