1

I am trying to configure ethers.js together with metamask.

Here is how I configure my provider:

  initProvider() {
    const ethereum = window['ethereum'];
    ethereum.enable();
    this.provider = new ethers.providers.Web3Provider(ethereum);//Metamask is used but only one account is listed...
  }

I have Ganache running with a number of accounts. However, when I try to retrieve/list the accounts like so: this.provider.listAccounts(), I only get one element in the array...

Note that I can see all accounts when I use the following configuration for my provider:

const provider = new ethers.providers.JsonRpcProvider(url);//Metamask is left out but I can list all accounts.

However, I need both metamask and the full list of accounts. What am I getting wrong? Do I need two separate providers?

edit: note that metamask itself lists all Ganache accounts properly.

1
+50

TL:DR

yes, you need two


That's because the Web3Provider does not have the ganache accounts "connected". The Metamask account is the only signer. The JsonRpcProvider on the other hand has all the ganache accounts connected as signers. If you want to perform transactions using the ganache accounts using Metamask, you'll need to import their individual private keys into metamask. However, as far as I know, you can't connect all the available metamask wallets as signers of a single Web3Provider object. This is because the way the Web3Provider works is by wrapping a web3.js provider – i.e. window.ethereum.

You can read the ethers.js documentation for reference.

2
  • Thanks for your reply. However, it seems one the application can connect to Metamask to select which accounts to select. See: github.com/ethers-io/ethers.js/discussions/…. What are your thoughts?
    – balteo
    Sep 27 at 10:56
  • Any thoughts about the provided link above and the wallet_requestPermissions method?
    – balteo
    Sep 28 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.