V
V
vladislovev2022-03-30 18:29:54
JavaScript
vladislovev, 2022-03-30 18:29:54

How to view the balance of the most famous ERC20 tokens of a user?

const web3 = new Web3(Web3.givenProvider);

    const tokenAddresses = [
      '0x',
      '0x',
    ];

    const myAddress = "0x";
  
    for (let tokenAddress of tokenAddresses) {
        const contract = new web3.eth.Contract(erc20AbiJson, tokenAddress);
        const tokenBalance = await contract.methods.balanceOf(myAddress).call();
        console.log(tokenBalance);
    }


I have the total Abi of the token. I need to fill an array with contracts addresses, I saw somewhere that there are about 2000 addresses in one place, but I lost the link and out of spite I can’t find it. If you have something like that, please post it. Also, the function displays just numbers. How can I complete the code, what to add so that it displays: "Token: 500" - either a symbol or a name - it doesn't matter

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Step, 2022-04-01
@kellas

Addresses of token contracts can be viewed here https://etherscan.io/tokens - there are about 1000 of the most popular

const erc20Contracts = [
  { address: '0xdac17f958d2ee523a2206206994597c13d831ec7', name:'Tether USD' , ticker:'USDT'}
    ];

for (let token of erc20Contracts) {
        const contract = new web3.eth.Contract(erc20AbiJson, token.address);
        const tokenBalance = await contract.methods.balanceOf(myAddress).call();
        console.log(token.name+': '+  web3.utils.fromWei(tokenBalance));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question