[Ethereum] How to read all token balances from wallet


The following code works for me to read Ether value from wallet:

var web3Instance =  new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

export async function readAccountEtherValue(address: string): Promise<number> {
    var balance = await web3Instance.eth.getBalance(address);
    return web3Instance.utils.fromWei(balance, "ether");

How can I read the values of all tokens from the wallet?

Best Answer

Token balances are not stored in a user's wallet. They are stored in the corresponding Token contract. Assuming the token you want to read the balance from is an ERC20 token, then you would have to instantiate the token contract and then call balanceOf(address) to get the address' balance. For example:

let token = await ERC20Basic.at(_tokenAddress);
let balance = await token.balanceOf(accounts[0]);