[Ethereum] Total metamask balance

balanceshd-walletsmetamask

I am curious how to get the total current balance of my metamask wallet with as many "accounts" inside it as it bears in the moment?

As I can understand metamask is the HD wallet and all my addresses in it are derived from my seed phrase and xpub/private key.

So is it possible to find a service or (preferably) to write a script to calculate total balance of my metamask wallet using my xpub or another root info? Please show me the right direction. Thank you.

Best Answer

You can get the balance of a single account via:

web3.eth.getBalance(address);

By default you will get all accounts of metamask. (this code uses await and therefore has to be placed within an async function)

const accounts = await web3.eth.getAccounts();

What you get is an array with all the accounts. Now it is up to you to loop through them and sum up the balance.

Related Topic