6

I am trying to connect to the metamask from my dapp. following the docs: https://docs.metamask.io/guide/getting-started.html#connecting-to-metamask

but when I call eth_requestAccounts method it's opening coinbase popup as well.

how to work around this? any suggestions?

ethereum.request({ method: 'eth_requestAccounts' });

enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
Xaarth
  • 1,021
  • 3
  • 12
  • 34

2 Answers2

10

Issue being that you have more than one provider installed, if you want to only target MetaMask you need to pick that provider alone.

// Find MetaMask Provider

const provider = window.ethereum.providers.find((provider) => provider.isMetaMask);
provider.request({ method: 'eth_requestAccounts' });

You can also try using metamask provider detector but it didn't work for me. https://docs.metamask.io/guide/ethereum-provider.html#ethereum-provider-api

Pancho DL
  • 116
  • 1
  • 4
  • 2
    This isn't working for me on the chrome as `window.ethereum.providers` is `undefined`. Any other solution? – Usman Pervaiz Feb 01 '22 at 21:12
  • @UsmanPervaiz Kindly check if you have any ethereum provider (browser extension like metamask) installed in your browser, this should even work if you type it in your browser console. – Pancho DL Feb 08 '22 at 18:29
  • 1
    Yes, I had installed the meta mask extension on the chrome. But I have figured out the issue. We get `window.ethereum.providers` when `Coinbase wallet extension` is installed in the chrome. I was actually trying to resolve the conflict between Metamask and Math Wallet. Thanks for the support :) – Usman Pervaiz Feb 23 '22 at 08:37
  • yeah that package is pretty useless. Even if you pass mustBeMetaMask option to it still both will show up – ihor.eth Aug 04 '22 at 20:46
1

Easily, you can switch providers (similar other wallets):

let provider;
    switch (providerName) {
        case ConnectorName.CoinbaseWallet:
            provider = ethereum.providers.find(({ isCoinbaseWallet }: any) => isCoinbaseWallet);
            break;
        case ConnectorName.Injected:
            provider = ethereum.providers.find(({ isMetaMask }: any) => isMetaMask);
            break;
        default:
            break;
    }

    if (provider) {
        ethereum.setSelectedProvider(provider);
    }
Alex Lai
  • 53
  • 6