3

There is a balanceOf function, but it only displays one uint (the token identifier) I see most ERC721 tokens doing this so I am confused on how to view all owned tokens.

3

Per https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md, there's an optional "enumeration extension". If this is implemented for the token you're interested in, then you can just call balanceOf to get the number of tokens owned by the account, followed by tokenOfOwnerByIndex in a loop to get each owned token ID.

If it isn't implemented, then there's no well-defined way to discover all of a user's owned tokens, but there may be a non-standard way to do it for the specific token you're interested in.

  • This is the correct answer, the enumerable extension was created explicitly for this purpose. The only correction is that it's tokenOfOwnerByIndex not byAddress. – AnAllergyToAnalogy Mar 16 at 23:36
  • Thanks for the correction! I edited my answer. – smarx Mar 16 at 23:50
  • Cheers for the help :) – BlockchainBoy Mar 17 at 5:32
2

The function balanceOf actually returns the number of owned tokens and not the identifier of an owned token.

Therefore I'd assume the process could go something like this:

1) Find out how many tokens an address has (balanceOf). If this is more than zero, continue the process.

2) Find out which tokens exist in the contract. The standard offers no direct functionality for this so this depends on the implementation. Or, as is probably the case, simply start enumerating from zero upwards hoping that the tokens are enumerable in that way (and not assigned for example to random numbers). So you would check "does someone own token 0? does someone own token 1? .."

3) Once you have found all the tokens owned by the address you can stop the process.

  • 1
    See also my answer. I believe most tokens implement the optional "enumeration extension" which does allow for efficiently finding a user's tokens. – smarx Mar 16 at 23:32
  • Thanks for the heads-up :) – Lauri Peltonen Mar 17 at 7:53
1

There is basically no way to easily view all owned tokens. My Ethereum wallets own probably lots of random worthless tokens which someone has airdropped and I don't even know about it.

Some services (wallets) display many of the owned tokens. I'm unsure how exactly they do that but most likely they simply have a database of known token contracts and they check whether a certain address owns those known tokens.

If I now created a new token and donated some of it to random wallets nobody would probably know about it unless I somehow tell wallet creators "please include my token contract in your checks".

At least myetherwallet used to display all (known) ERC20 tokens still about a year ago but the quickly they gave up and last I checked you had to manually enter the contract address to see those tokens. The amount of different token contracts exploded a year ago and it was probably too much hassle to try to keep track of them (to store them all in their database).

  • I am looking at a specific token, I don't see a easy way to check this from the token's contract – BlockchainBoy Mar 16 at 18:31
  • Ah, sorry, I had misunderstood the question. My answer is not so relevant in that case. – Lauri Peltonen Mar 16 at 18:34

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.