massalabs / massa-standards

Massa standards
MIT License
25 stars 75 forks source link

Implement ERC721 enumerable extension #148

Open leoloco opened 8 months ago

leoloco commented 8 months ago

Context

Currently, the NFT standard only allows retrieving a particular token based on its tokenId.

The Enumerable extension for ERC721 tokens, typically represented in smart contracts by interfaces such as IERC721Enumerable, adds functionality for discovering and navigating through all tokens in a contract, as well as querying tokens owned by a particular account. This extension enhances the ERC721 standard by providing a way to enumerate over all tokens or all tokens owned by a user, which is particularly useful for contracts that need to display or operate on a collection of NFTs.

User flow

For NFT Owners and Collectors:

  1. Viewing Owned NFTs:

    • A collector can view a list of all NFTs they own within a particular collection. This is made possible by the tokenOfOwnerByIndex(address owner, uint256 index) function, which returns the token ID owned by a user at a given index within their list of owned tokens.
    • This enables user interfaces (UIs) to iterate over all NFTs owned by a user and display them, even if the total number is unknown beforehand.
  2. Exploring the Entire Collection:

    • Users can explore the entire collection of NFTs issued by the contract. The totalSupply() function returns the total number of tokens in existence, and tokenByIndex(uint256 index) allows enumeration over all tokens based on their indices.
    • This feature supports the creation of gallery views or marketplaces where users can browse through every NFT in a collection, facilitating discovery and trading.

For Developers and Marketplaces:

  1. Building Dynamic Interfaces:

    • Developers can leverage the Enumerable extension to build dynamic and responsive interfaces that automatically update to reflect the total supply and ownership distribution of NFTs in real-time.
    • This includes creating features like leaderboards, galleries, and personalized user dashboards that showcase NFT collections.
  2. Implementing Advanced Querying and Filtering:

    • With the ability to enumerate tokens, developers can implement advanced querying and filtering logic to allow users to search for NFTs based on various criteria (e.g., rarity, attributes) within a collection.
    • This enhances the user experience by making it easier to find specific NFTs in potentially large and diverse collections.
  3. Facilitating Efficient Transactions and Interactions:

    • The Enumerable extension makes it easier for contracts, such as marketplaces or games, to interact with NFT collections by providing structured ways to access and iterate over tokens.
    • This can lead to more efficient batch processing or aggregation operations, improving transaction efficiency and reducing costs.

Technical details

Based on openzeplin's implementation https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721Enumerable.sol Implement

SlnPons commented 5 days ago

IMPORTANT NOTE: on top of enumerable, add METADATA extension to the ERC721 contract as part of this issue please. (see for more details: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC721/extensions)