-
Notifications
You must be signed in to change notification settings - Fork 66
Get Token Balance
A script that gets the token balance of an address using the address and the token contract address, then shows the result in the console.
Note: This script will only get the tokens from the contract we specify, be sure your queried address has tokens from the contract you specify.
For this example we're going to use the script located here.
var Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/YOUR-API-TOKEN-HERE'));
Please read the Basic Required Setup page for this first bit, you'll see it a lot.
console.log('Getting contract tokens balance.....');
var addr = ('YOUR-ADDRESS-WITH-TOKENS');
console.log("Address: " + addr);
var contractAddr = ('TOKEN-CONTRACT-ADDRESS');
The console.log
at the top isn't necessary, but it looks pretty. Then we define addr
as the address with some tokens, and log it in the console. Then we define the contract address as contractAddr
, this is the contract that created those tokens.
So for example, if your friend Bob created "Bob Tokens" and he sent you some, you would need the address of his token contract. Bob would have to either have to tell you this or list it publically on website.
You can easily find the token contract address on a blockchain explorer like Etherscan.io. You can also use this handy json file from MyEtherWallet's github repo which has all of them nicely organized in an array.
var tknAddress = (addr).substring(2);
var contractData = ('0x70a08231000000000000000000000000' + tknAddress);
Now things get interesting... First we want to remove the "0x" from the tknAddress
. We do this because that identifier is not necessary for completing the call to the contract. Now we define the ContractData
, which is composed of the function of the contract we want to call in hex, a zero buffer, and the now shortened tknAddress
.
Back to our example for a second, because Bob was a good developer he made his token contract according to the ERC20 Token Standard, which means that he used "balanceOf()" as the function that will get the balance of the address.
Almost all tokens on the Ethereum blockchain use this standard so that we can query them easily without having to read their own documentation for interacting with their token contract(s). But why do we turn "balanceOf()" into hex value? When we want to make calls we have to use hex values, not plain text which is why "balanceOf()" becomes "0x70a08231".
web3.eth.call({
to: contractAddr,
data: contractData
}, function(err, result) {
if (result) {
var tokens = web3.utils.toBN(result).toString();
console.log('Tokens Owned: ' + web3.utils.fromWei(tokens, 'ether'));
}
else {
console.log(err); // Dump errors here
}
});
Ok now we need to build the call to the contract. So we start with the Web3 function eth.call
which requires two things, who (to) and data (what). The "to" is the token contract address previously stated as contractAddr
. The "what" is the hex value of balanceOf(), with a zero buffer and the address we want to search within that contract.
That will return likely either an error, or a result. The result is however a big number and javascript hates those, so we use the Web3 function utils.toBN
to convert that into something usable, then use .toString
to clean it up some more. The resulting number is in Wei, which is too big (19 decimal places) for us humans to read, so as a final step we just convert that to Ether using utils.fromWei
.
If all is fine and dandy it should show up in the console like this:
Getting contract tokens balance.....
Address: YOUR-ADDRESS-WITH-TOKENS
Tokens Owned: X.xxxxxxxxxxxxxxxxxx
Just like the Get Balance script you could try to import the current fiat rate of the tokens and multiply them to get the value. Or you could really get into the weeds and try to execute other contract functions the same way!