Codementor Events

My USD Coin Balance Check By Web3

Published May 17, 2019

This is proxy contract, which means that it delegates actual work to another contract, probably to this one: 0x0882477e7895bdc5cea7cb1552ed914ab157fe56, which in turn is ERC-20 compliant. Proxy contract allows its administrator to change address of the contract actual work is delegated to, effectively changing smart contract logic. Proxy contracts like this one are implemented via DELEGATECALL opcode that executes code of another contract on behalf of this contract, i.e. called code has access to the storage and balance of calling contract.

async function getTokenBalance(address) {
    const contractABI = require(baseDir + '/bin/contracts/' +  config.CONTRACT_ABI_FILE_NAME.USDC);
    const web3 = new Web3(new Web3.providers.HttpProvider(config.INFURA_NETWORK_URL));

    const USDContractInstance = await new web3.eth.Contract(contractABI, config.CONTRACT_ADDRESS.PROXY);
    let balance = await USDContractInstance.methods.balanceOf(address).call();
    balance = web3.utils.hexToNumber(balance) / Math.pow(10, 6);
    return balance;
}

exports.getTokenBalance = getTokenBalance;

This will give you a token balance of your wallet address like 1000.
Hope this will be a good help for someone who is finding a way.
Thank you for reading my post.

Discover and read more posts from Teodor Malinas
get started