binance-smart-chainbep20

How get USDT balance BEP-20? web3.eth.getBalance


Good day! Can you please tell me how you can get the balance of the USDT address? I found a way to get BNB balance (https://docs.binance.org/smart-chain/developer/BEP20.html), but there is no example for USDT


Solution

  • Token balance of an address is not a property of the address. It is stored in each of the token's contract. So you need to call the balanceOf() function of the token contract, passing it the holder address as a parameter.

    For example the BUSD token:

    const busdAddress = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
    const holderAddress = "0x8894e0a0c962cb723c1976a4421c95949be2d4e3";
    
    // just the `balanceOf()` is sufficient in this case
    const abiJson = [
        {"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
    ];
    
    const contract = new web3.eth.Contract(abiJson, busdAddress);
    const balance = await contract.methods.balanceOf(holderAddress).call();
    // note that this number includes the decimal places (in case of BUSD, that's 18 decimal places)
    console.log(balance);