2

How can I get the provider from a Wallet when using ethersjs?

I have tried getting the provider through wallet.provider but this is different from the Ethereum provider which has the send method.

I have also tried the following:

const network = await wallet.provider.getNetwork();
const provider = new providers.JsonRpcProvider(network);

The JsonRpcProvider in this case should return a provider object which has the send method that is different from wallet.provider. However, the above still doesn't work because I'm not sure how I can convert from the wallet to the provider without losing any information.

2 Answers 2

2

If your wallet is connected to the provider you should be able to get the providers object using wallet.provider

if you are trying to access the send function of the provider try creating a provider with JsonRpcProvider like:

  • create a provider with new ethers. providers.JsonRpcProvider(<networkUrl>)
  • but I guess this send function is used to fetch blockchain with raw data

if you are trying to send transaction using ethers provider directly. use provider.sendTransaction(<signedTransaction>) this will send your signed Transaction to blockchain

3
  • Am I able to get the netowrkUrl from wallet.provider? As much as possible, I'm trying to convert whatever wallet information provided by the user into the Provider object.
    – xenon
    Commented Aug 24, 2021 at 3:38
  • wallet and provider are two different things. provider only determines which network you are connected to. whereas wallet can be connected to any network so, you should always have a wallet connected to a provider/network your app is working on. so, if your wallet is not connected with provider- wallet.provider will be null. and to create a provider networkUrl is the gateway to blockchain network. in case on ethereum you can get it from infura like services Commented Aug 24, 2021 at 4:39
  • So does it mean even if a wallet is connected to a provider, I won’t be able to get the networkUrl of the provider from wallet.provider? This could be useful because if I could get the networkUrl from the wallet’s provider, I could then pass it into JsonRpcProvider with something like this: new ethers.providers.JsonRpcProvider(wallet.provider.networkUrl)
    – xenon
    Commented Aug 24, 2021 at 4:55
0
const provider = new ethers.providers.Web3Provider( window.ethereum, "any" );
const network = await provider.getNetwork();
const chainId = network.chainId;

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.