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.

how can i know if a provider is connected or not?

See original GitHub issue

i need to get a cached provider used by our user previously. if that provider is connected, i will get first user account and display it on the page. or if not, i don’t want to invoke connect process by default, unless user click on the connect button.

some methods of web3js can detect whether a wallet connected to network or not. but i need to use web3modal.connect() to get a provider to construct a web3 instance.

some code snippet:

// use connect method to get a cached provider will invoke connect process and metamask chrome plugin will popup.
const provider = await web3Modal.connect();
const web3 = new Web3(provider);

so, is there any way to get a cached provider without calling the connect function? or any way to detect a cached provider is connected to network or not?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

5reactions
4skinSkywalkercommented, Oct 8, 2021

First thing: @vanvantsyan thank you for the downvote.

You can get providers the same way Web3Modal gets them (see web3modal connectors). I do this all the time. Web3Modal just abstracts away all the low-level logic to extract providers from the various sources (most of them are different properties in the window object) It’s no voodoo magic.

Unless you take the provider you don’t have a meaningful way to see if that provider was injected and unless you istantiate it via ethers or web3 you don’t have a meaningful way to know what account is connected and on what network. If you don’t want to spawn the dialog with .connect() then simply try to get the provider you want and check it’s state by yourself.

Following some example on how you can get can providers:

Get MetaMask provider

if (typeof win.ethereum !== 'undefined') {
  let provider = win.ethereum;
  provider = await provider.request({ method: 'eth_requestAccounts' });
}

(This may also trigger other injected providers (e.g. Coinbase) but you can filter MetaMask if you want).

Get Binance Wallet provider

if (typeof win.BinanceChain!== 'undefined') {
  let provider = win.BinanceChain;
  provider = await provider.request({ method: 'eth_requestAccounts' });
}

Get Coinbase Wallet provider

let walletLink = new WalletLink({ appName });
let provider = walletLink.makeWeb3Provider(networkUrl, chainId);
let provider = await provider.enable();

Get Fortmatic provider

let fm = new this.Fortmatic(FORTMATIC_APIKEY, { rpcUrl, chainId });
let provider = provider: fm.getProvider();

What I’ve just shown you is obviously painful and it’s just to show you how you can do it manually! To do it without spawning the modal you’ll have to import connectors from Web3Modal library and evaluate them. (see web3modal connectors).

2reactions
4skinSkywalkercommented, Sep 30, 2021

So if I understood you want the provider without the modal showing up, right?

(async function() {
    // Get the cached provider from LocalStorage
    const cachedProviderName = JSON.parse(localStorage.getItem("WEB3_CONNECT_CACHED_PROVIDER"));
    
    // Get the connector for the cachedProviderName
    const connector = web3Modal.providerController.providerOptions[cachedProviderName].connector;

    // Evaluate connector() which returns a Proxy in the case of MetaMask
    const proxy = await connector(); // Some connector may need providerPackage and opts
    console.log("Proxy", proxy);

    // Get the working provider from your favorite library (ethers, web3, ...)
    const provider = new ethers.providers.Web3Provider(proxy); // If you use web3, then const web3 = new Web3(proxy);
    console.log("Provider", provider);

    // You can list the connected accounts without launching Web3Modal
    console.log("Accounts", await provider.listAccounts()); // If you use web3, then await web3.eth.getAccounts();
})();

With the above code you should be able to leverage your Web3Modal configurations and get a working provider and feed it in your library of choice without the need of calling the connect method.

I’ve tested the above code and had the expected result: image

Ah, you may need to put some if-clauses to guard from undefined, infact if there’s no cached provider you get the following error: image

Keep in mind I’m not part of the team, I’m not even an expert of this library. I just read their source code and come up with the above snippet.

Let’s ask the guys from the project itself if my snippet is reliable: @miohtama, @jamesmorgan, @miguelmota, @ptescher and @arronhunt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check if a doctor is in-network | HealthPartners Blog
Call your insurance company using the member services line, which you can usually find on your insurance card. HealthPartners members can also find...
Read more >
How Can I Verify if a Provider is in My Coverage Network?
You did the right thing by calling your doctor's office and using Aetna's provider lookup tool. Follow up by calling your insurance company...
Read more >
Does my doctor know what providers are in my network?
No, you are responsible to check if a provider is in your network. Every insurance company has different benefit plans and network requirements....
Read more >
How to Verify If Your Provider Is in Your Coverage Network
The next step is to contact your insurance provider and ask what physicians or other medical professionals are covered under your specific plan....
Read more >
How do I find out if my doctor is in my health insurance plan's ...
How do I find out if my doctor is in my health insurance plan's network? That's a common question we hear in our...
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