0

The user has a wallet installed and but they are not logged in

the below function does not prompt the user login to their wallet, it just returns an empty array.

  const getAccount = async () => {
    try {
      const acc = await window.ethereum.request({
        method: 'eth_accounts',
      });
      console.log(acc);
    } catch (err) {
      console.log('err');
    }
  };

but if they don't have the chain I want to use installed I do get a prompt, why?

  const installAndChangeToNet = async (): Promise<void> => {
    try {
      await window.ethereum.request({
        method: 'wallet_addEthereumChain',
        params: [
          {
            chainId: chainId,
            chainName: chainName,
            nativeCurrency: {
              name: name,
              symbol: symbol,
              decimals: decimals,
            },
            rpcUrls: [rpcUrls],
            blockExplorerUrls: [blockExplorerUrls],
          },
        ],
      });
      getAccount();
    } catch (err: any) {
      alert('err');
    }
  };

1 Answer 1

0

Good question, I think the problem is that you are calling the wrong method. If you look in the Metamask documentation, you can find this:

Metamask Docs

const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const accounts = accounts[0];

So, with the first line, you request accounts from the connected wallet, and with the second line you already select the desired one.

That is, calling eth_requestAccounts gives access to the wallet and addresses.

Now, in the second one you call wallet_addEthereumChain. This method is used to add some particular network to the wallet, you can read more here in the docs

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.