Solidity Token Balance – How to Find Out the Token Balance of an Address

soliditytokens

How can I find out the token balance of some address using Solidity?

For example, I have the address of another person, and I need to know the number of tokens on his account.

Best Answer

There are 2 ways using https://etherscan.io/ :

  1. Navigate to https://etherscan.io/ . Enter the account in the search box on the top right, the click Go. If there are tokens associated with the account, you will see the Token Tracker dropdow. Clicking this will show you the number of tokens owned by the account. For example: https://etherscan.io/address/0xc49cee55a099349bc5a67a6a454dbad3833e7b14 :

enter image description here

  1. If you know the token, navigate to https://etherscan.io/ . Select the menu Token -> View Tokens. Then select the token in the list. In this example, we will look at Golem. Enter the address of interest in the Filter By Address field and click Apply. The following screen shows the details for the same address in the example above:

enter image description here



Update Responding To The More Specific Question On Getting The Balance Using Solidity

Here is an example using GNT, where I got the ABI from the GNT contract details at 0xa74476443119A942dE498590Fe1f2454d7D4aC0d:

> var gntABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"golemFactory","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_master","type":"address"}],"name":"setMigrationMaster","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"migrate","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finalize","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"migrationMaster","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationCap","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_agent","type":"address"}],"name":"setMigrationAgent","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"migrationAgent","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fundingEndBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalMigrated","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationMin","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"funding","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"tokenCreationRate","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fundingStartBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"create","outputs":[],"payable":true,"type":"function"},{"inputs":[{"name":"_golemFactory","type":"address"},{"name":"_migrationMaster","type":"address"},{"name":"_fundingStartBlock","type":"uint256"},{"name":"_fundingEndBlock","type":"uint256"}],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Migrate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Refund","type":"event"}];
undefined
> var gntAddress = "0xa74476443119A942dE498590Fe1f2454d7D4aC0d";
undefined
> var address = "0xc49cee55a099349bc5a67a6a454dbad3833e7b14"; // Address in examples above
undefined
> var gntContract = eth.contract(gntABI).at(gntAddress);
undefined
> var balance = gntContract.balanceOf(address).div(1e18);
undefined
> balance
23771.112437704491608715



Update With Script To Find GNT Balances For Multiple Accounts

The getGNTBalances script from https://github.com/bokkypoobah/TokenTrader/tree/master/scripts will produce the following type of output:

  #     Account                                                             GNT                        ETH
------- ------------------------------------------ ---------------------------- --------------------------
  0     0x4319c142f7b6cd722fc3a49289b8a22a7a51ca1e 180000000.000000000000000000       0.000000000000000000
  1     0x168ae36b4386cea14475faa41498a0ea63c67dd7 100000999.999999991611392000   11717.218877544008288507
  2     0xd39379d7887c6a9ebd01007e2b96efc774652047  57459993.000000000000000000  106160.984854218882216845
------- ------------------------------------------ ---------------------------- --------------------------
  3     Total                                      337460992.999999991611392000  117878.203731762890505352

Just change the address in the script to any ERC20 compliant token to view the balances for that token.

Related Topic