Last active
December 3, 2018 07:19
-
-
Save wtfaremyinitials/42b60a8eb9119f24a11cbbb5c2453e5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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