question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERC721Enumerable - Add back `_tokensOfOwner()`

See original GitHub issue

tokensOfOwner() was first suggest on #1512 and implemented on #1522 and was removed in a bulk edit that makes me wonder if it was removed accidentally. https://github.com/OpenZeppelin/openzeppelin-contracts/commit/bd0778461da82364aebd2d763bb338795c5f9fa7

This function helps when coding web3 apps. Instead of calling the RPC multiple times to get all NFTs owned by the user, it can be done at once by calling tokensOfOwner()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
imsyscommented, Jan 15, 2022

So, I just tested this, and it works!

I know it is an unbounded loop, but it doesn’t seem to be a problem for a RPC.

pragma solidity ^0.8.0;

// SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";

contract ListNFT {

    function listUserNFTs(address contractAddress, address owner) external view returns (uint[] memory) {

        uint balance = IERC721Enumerable(contractAddress).balanceOf(owner);

        uint[] memory tokens = new uint[](balance);

        for (uint i=0; i<balance; i++) {
            tokens[i] = (IERC721Enumerable(contractAddress).tokenOfOwnerByIndex(owner, i));
        }

        return tokens;
    }
}
0reactions
imsyscommented, Jan 23, 2022

For me this can be closed then, unless someone else has anything else to add.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose tokensOfOwner method on ERC721Enumerable
Hello, I was wondering why the ERC721Enumerable smart contract only exposes a tokenOfOwnerByIndex which returns only one of the owned token ...
Read more >
Best way to list all tokens of user in Solidity
The most obvious way is to iterate through all the tokens in the contract and check if the owner of the token is...
Read more >
The ERC721 enumerable - Mastering Blockchain ... - O'Reilly
tokenOfOwnerByIndex() : The function returns the tokenId of an NFT that is stored at a particular index of the owner's list of tokens....
Read more >
Writing an NFT Collectible Smart Contract - DEV Community ‍ ‍
wait() // Get all token IDs of the owner let tokens = await contract.tokensOfOwner(owner.address) console.log("Owner has tokens: ", tokens); } ...
Read more >
Ethereum Contract Diff Checker - Etherscan
totalSupply(), "ERC721Enumerable: global index out of bounds"); ... @param tokenId uint256 ID of the token to be added to the tokens list */...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found