Skip to main content

ERC-721 Enumerable Data Facet

Enumerable ERC-721 token data retrieval

Key Features
  • Exposes external functions for enumerable ERC-721 data retrieval via diamond proxy.
  • Reads from shared diamond storage without modifying state.
  • Supports querying total supply and specific tokens by index or owner.
  • Utilizes inline assembly for efficient storage access.

Overview

This facet provides external functions for retrieving enumerable ERC-721 token data from a diamond. It accesses shared storage to return total token supply and specific token IDs by index or owner. Developers add this facet to enable querying of token ownership and enumeration within a diamond.

Storage

ERC721EnumerableStorage

Definition
struct ERC721EnumerableStorage {
mapping(address owner => mapping(uint256 index => uint256 tokenId)) ownerTokens;
mapping(uint256 tokenId => uint256 ownerTokensIndex) ownerTokensIndex;
uint256[] allTokens;
}

ERC721Storage

Definition
struct ERC721Storage {
mapping(uint256 tokenId => address owner) ownerOf;
mapping(address owner => uint256 balance) balanceOf;
}

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc721.enumerable"))
ERC721_STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc721"))

Functions

totalSupply

Returns the total number of tokens in existence.

function totalSupply() external view returns (uint256);

Returns:

PropertyTypeDescription
-uint256The total supply of tokens.

tokenOfOwnerByIndex

Returns a token ID owned by a given address at a specific index.

function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);

Parameters:

PropertyTypeDescription
_owneraddressThe address to query.
_indexuint256The index of the token.

Returns:

PropertyTypeDescription
-uint256The token ID owned by _owner at _index.

tokenByIndex

Enumerate valid NFTs Throws if _index >= totalSupply().

function tokenByIndex(uint256 _index) external view returns (uint256);

Parameters:

PropertyTypeDescription
_indexuint256A counter less than totalSupply()

Returns:

PropertyTypeDescription
-uint256The token identifier for the _indexth NFT,

exportSelectors

Exports the function selectors of the ERC721EnumerableDataFacet This function is use as a selector discovery mechanism for diamonds

function exportSelectors() external pure returns (bytes memory);

Returns:

PropertyTypeDescription
-bytesselectors The exported function selectors of the ERC721EnumerableDataFacet

Errors

Best Practices

Best Practice
  • Ensure the ERC721EnumerableDataFacet is added to the diamond and its selectors are correctly registered.
  • When calling tokenOfOwnerByIndex or tokenByIndex, validate the provided index against totalSupply() to prevent ERC721OutOfBoundsIndex errors.
  • This facet only reads from shared storage; ensure other facets managing token state maintain storage invariants.

Security Considerations

Security

The tokenByIndex function reverts if _index is out of bounds, indicated by the ERC721OutOfBoundsIndex error. All functions are view and do not pose reentrancy risks. Access control is implicitly handled by the diamond proxy routing calls to this facet.

Was this helpful?
Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.