15

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.

5 Answers 5

27

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.

3
  • 1
    This is the correct answer, the enumerable extension was created explicitly for this purpose. The only correction is that it's tokenOfOwnerByIndex not byAddress. Mar 16, 2019 at 23:36
  • Cheers for the help :) Mar 17, 2019 at 5:32
  • what is an .md file? What application front-end do you type balanceOf into?
    – user610620
    Jan 16, 2022 at 14:20
3

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
  • 1
    See also my answer. I believe most tokens implement the optional "enumeration extension" which does allow for efficiently finding a user's tokens.
    – user19510
    Mar 16, 2019 at 23:32
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).

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

Unfortunately the best practices way is not a 1-liner.

You'll need to comb through the events that involved the account in question.

Here's some code that would work. Disclaimer: i'm only grabbing the Transfer events that involve incoming transfers your address in question. If tokens were transferred out of this address then you'll need to comb through those transfer events as well.

I hope this helps. I think all of us have asked this question at one point or another. I wish the answer was a little more straight forward.

let toAddr = '0xBc25A51F63AA4Db0FFff0C34467c8EE6DCb2d0FC';
let incomingTokenTransferEvents = await book.getPastEvents('Transfer', { filter: {'to': toAddr}, fromBlock: 0, toBlock: 'latest'})
incomingTokenTransferEvents.forEach( (event) => console.log(event.returnValues.tokenId));
0

This can be done by query the logs. Ususally When an nft is minted, the Transfer event is triggered from 0 address to msg.sender. Using this, we can query the blockchain for the specific address containing the required address that you want to get.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Apr 5, 2023 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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