0

TL;DR: wanted: a method that receives an address as a parameter and returns an array of token names/contracts/balances held by address.

I'm aware of the fact that Etherscan API has a method allowing you to see the balance of an address, when you supply the contract taddress of a token. My question is: how do I get a list of tokens an address hold?

Case in point: when you go to Etherscan, there's a dropdown, showing a list of tokens and balances. Surely it is not the case (I hope) that Etherscan iterates through all known token contracts to look for the address?

How can I build such a list?

1

That's exactly what Etherscan does - they have a list of tokens that they track. You'll then have to do a balanceOf query on each token with the address you want to get balance for.

There is no central repository of token balances, since they are all their own individual contracts that just contain user balances.

Edit: If you're querying balances directly from the chain, you can also use this balance checker library. You can query the contract directly here. Just pass a list of accounts and tokens and it will automatically return the list of the balances for each account on each token with one API call.

| improve this answer | |
  • But such a list is (potentially) infinite (ok, no infinite-infinite, but a very large possible number). Am I expected to keep a huge list of contract addresses and iterate through all of them, calling the API for each? – Traveling Tech Guy Jul 8 at 20:36
  • Yup, that's the only way. Etherscan of course has all this data cached, so they don't have to query every token every time you view your account on etherscan – flygoing Jul 9 at 17:13
  • Shame, but I guess that's life :) I'll just maintain a list of major token contracts, and allow users to add more. Of course, that's now one extra API call per token... – Traveling Tech Guy Jul 9 at 20:19
  • There is also an easy way around the "one API call per token", see my edit – flygoing Jul 14 at 22:03
  • I believe it just does the same thing: it still needs to query n accounts with m1...mn contracts, and check the blockchain m1+m2+...mn times. I don't think there's any "magic" shortcut. I ended up writing a module that accepts a list of addresses, goes over all transactions, gets a unique list of contracts and checks the balances of each on the account. Realizing that Etherscan limits you to 5 calls/second was a fun part of the development process... – Traveling Tech Guy Jul 15 at 23:17

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.