0

How to get the current selected account in Metamask?

I've seen things like this as the answer to this..

const accounts = await web3.eth.getAccounts();
const account = accounts[0];

But doesn't this just select the first account? Not the current selected account?

GN.
  • 643
  • 2
  • 8
  • 20

1 Answers1

2

metamask ethereum provider now recommends to use the following method for getting the current enabled account:

var accountsOnEnable = await ethereum.request({method: 'eth_requestAccounts'});

Variable ethereum is now existing since the MetaMask version update. Method await web3.eth.getAccounts() is also working to get the current enabled account, but since the metamask provider recommends to use the other way, I would go with the example above.

Miroslav Nedelchev
  • 3,519
  • 2
  • 10
  • 12
  • var accountsOnEnable = await ethereum.request({method: 'eth_requestAccounts'}); this is giving the list of accounts there in the metamask web3.eth.getAccounts() not working it says there is no function like that – sarangkkl Dec 20 '21 at 04:50
  • I have also gone through the files of web3.js there is no function getAccounts() – sarangkkl Dec 20 '21 at 04:56