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?

1 Answer 1

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.

2
  • 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, 2021 at 4:50
  • I have also gone through the files of web3.js there is no function getAccounts()
    – sarangkkl
    Dec 20, 2021 at 4:56

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.