Skip to content

Instantly share code, notes, and snippets.

@wtfaremyinitials
Last active December 3, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtfaremyinitials/42b60a8eb9119f24a11cbbb5c2453e5a to your computer and use it in GitHub Desktop.
Save wtfaremyinitials/42b60a8eb9119f24a11cbbb5c2453e5a to your computer and use it in GitHub Desktop.
// 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