Skip to main content

ERC721EnumerableFacet

ERC-721 token ownership and metadata management

Key Features
  • Implements core ERC-721 enumerable functionality.
  • Exposes external functions for diamond routing.
  • Utilizes diamond storage for state management.
  • Compatible with ERC-2535 diamond standard.

Overview

This facet implements ERC-721 token functionality, including ownership tracking and metadata retrieval, within a diamond proxy. It exposes standard ERC-721 functions externally, allowing interaction with token data via the diamond's unified interface. Developers integrate this facet to provide NFT capabilities while leveraging the diamond's upgradeability and composability.


Storage

ERC721EnumerableStorage

Definition
struct ERC721EnumerableStorage {
mapping(uint256 tokenId => address owner) ownerOf;
mapping(address owner => uint256[] ownerTokens) ownerTokens;
mapping(uint256 tokenId => uint256 ownerTokensIndex) ownerTokensIndex;
uint256[] allTokens;
mapping(uint256 tokenId => uint256 allTokensIndex) allTokensIndex;
mapping(address owner => mapping(address operator => bool approved)) isApprovedForAll;
mapping(uint256 tokenId => address approved) approved;
string name;
string symbol;
string baseURI;
}

State Variables

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

Functions

name

Returns the name of the token collection.

function name() external view returns (string memory);

Returns:

PropertyTypeDescription
-stringThe token collection name.

symbol

Returns the symbol of the token collection.

function symbol() external view returns (string memory);

Returns:

PropertyTypeDescription
-stringThe token symbol.

tokenURI

Provide the metadata URI for a given token ID.

function tokenURI(uint256 _tokenId) external view returns (string memory);

Parameters:

PropertyTypeDescription
_tokenIduint256tokenID of the NFT to query the metadata from

Returns:

PropertyTypeDescription
-stringthe URI providing the detailed metadata of the specified tokenID

totalSupply

Returns the total number of tokens in existence.

function totalSupply() external view returns (uint256);

Returns:

PropertyTypeDescription
-uint256The total supply of tokens.

balanceOf

Returns the number of tokens owned by an address.

function balanceOf(address _owner) external view returns (uint256);

Parameters:

PropertyTypeDescription
_owneraddressThe address to query.

Returns:

PropertyTypeDescription
-uint256The balance (number of tokens owned).

ownerOf

Returns the owner of a given token ID.

function ownerOf(uint256 _tokenId) public view returns (address);

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to query.

Returns:

PropertyTypeDescription
-addressThe address of the token owner.

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.

getApproved

Returns the approved address for a given token ID.

function getApproved(uint256 _tokenId) external view returns (address);

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to query.

Returns:

PropertyTypeDescription
-addressThe approved address for the token.

isApprovedForAll

Returns whether an operator is approved for all tokens of an owner.

function isApprovedForAll(address _owner, address _operator) external view returns (bool);

Parameters:

PropertyTypeDescription
_owneraddressThe token owner.
_operatoraddressThe operator address.

Returns:

PropertyTypeDescription
-boolTrue if approved for all, false otherwise.

approve

Approves another address to transfer a specific token ID.

function approve(address _to, uint256 _tokenId) external;

Parameters:

PropertyTypeDescription
_toaddressThe address being approved.
_tokenIduint256The token ID to approve.

setApprovalForAll

Approves or revokes an operator to manage all tokens of the caller.

function setApprovalForAll(address _operator, bool _approved) external;

Parameters:

PropertyTypeDescription
_operatoraddressThe operator address.
_approvedboolTrue to approve, false to revoke.

transferFrom

Transfers a token from one address to another.

function transferFrom(address _from, address _to, uint256 _tokenId) external;

Parameters:

PropertyTypeDescription
_fromaddressThe current owner of the token.
_toaddressThe recipient address.
_tokenIduint256The token ID to transfer.

safeTransferFrom

Safely transfers a token, checking for receiver contract compatibility.

function safeTransferFrom(address _from, address _to, uint256 _tokenId) external;

Parameters:

PropertyTypeDescription
_fromaddressThe current owner of the token.
_toaddressThe recipient address.
_tokenIduint256The token ID to transfer.

safeTransferFrom

Safely transfers a token with additional data.

function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes calldata _data) external;

Parameters:

PropertyTypeDescription
_fromaddressThe current owner of the token.
_toaddressThe recipient address.
_tokenIduint256The token ID to transfer.
_databytesAdditional data to send to the receiver contract.

Events

Errors

Best Practices

Best Practice
  • Initialize the name, symbol, and baseURI during diamond setup.
  • Enforce access control on state-changing functions like approve and transferFrom if custom logic requires it.
  • Ensure storage compatibility when upgrading to new versions of this facet.

Security Considerations

Security

Follow standard Solidity security practices. Input validation for token IDs and addresses is critical. The safeTransferFrom functions include checks for receiver contract compatibility. Ensure that any custom access control added to state-changing functions is robust.

Was this helpful?
Last updated: