Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// from: https://medium.com/@piyopiyo/how-to-get-erc20-token-balance-with-web3-js-206df52f2561
let tokenAddress = "REPLACE_WITH_ERC20_TOKEN_ADDRESS";
let walletAddress = "REPLACE_WITH_WALLET_ADDRESS";
// The minimum ABI to get ERC20 Token balance
let minABI = [
// balanceOf
{
"constant":true,
"inputs":[{"name":"_owner","type":"address"}],
"name":"balanceOf",
"outputs":[{"name":"balance","type":"uint256"}],
"type":"function"
},
// decimals
{
"constant":true,
"inputs":[],
"name":"decimals",
"outputs":[{"name":"","type":"uint8"}],
"type":"function"
}
];
// Get ERC20 Token contract instance
let contract = web3.eth.contract(minABI).at(tokenAddress);
// Call balanceOf function
contract.balanceOf(walletAddress, (error, balance) => {
// Get decimals
contract.decimals((error, decimals) => {
// calculate a balance
balance = balance.div(10**decimals);
console.log(balance.toString());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.