13

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.

10

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.

  • Thanks, but how can I find the code on Solidity. For example there balanseOf function for ether, but I need balans tokens. – M.Sheremain Jan 28 '17 at 11:30
  • Updated answer with an example in Solidity – Bokky WeAreAllAssange PooBah Jan 28 '17 at 12:54
5

Etherscan exposes an API which can be used to find the balance instead.

Get ERC20-Token Account Balance for TokenContractAddress

https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0x57d90b64a1a57749b0f932f1a3395792e12e7055&address=0xe04f27eb70e025b78871a2ad7eabe85e61212761&tag=latest&apikey=YourApiKeyToken

Where address is the person's address.

And contractaddress is the token address in this case Elcoin

  • Is there anyway to list holders of a specified ERC20 contract? – yuyue007 Aug 15 '18 at 7:52
0

With RPC:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to": "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0", "data":"0x70a082310000000000000000000000000b88516a6d22bf8e0d3657effbd41577c5fd4cb7"}, "latest"],"id":67}' -H "Content-Type: application/json" http://127.0.0.1:8545/

For details refer my answer here

0

It's more complicated if you want to query a token you deployed for testing. You can either:

  1. Use the hex version of the balanceOf(address) of the ERC20-compliant tokens in a js file.
  2. Recompile your sol file with solc or truffle, either programmatically or from the terminal, and then load your *.abi files using web3.eth.contract(ABI).at(address);
  3. RPC with etherscan.io, as @garg10may explained above

I personally chose 1, you can find some sample code here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.