ERC721EnumerableFacet
ERC-721 token ownership and metadata management
- 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
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("compose.erc721.enumerable")) |
Functions
name
Returns the name of the token collection.
Returns:
| Property | Type | Description |
|---|---|---|
- | string | The token collection name. |
symbol
Returns the symbol of the token collection.
Returns:
| Property | Type | Description |
|---|---|---|
- | string | The token symbol. |
tokenURI
Provide the metadata URI for a given token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | tokenID of the NFT to query the metadata from |
Returns:
| Property | Type | Description |
|---|---|---|
- | string | the URI providing the detailed metadata of the specified tokenID |
totalSupply
Returns the total number of tokens in existence.
Returns:
| Property | Type | Description |
|---|---|---|
- | uint256 | The total supply of tokens. |
balanceOf
Returns the number of tokens owned by an address.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | The address to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | uint256 | The balance (number of tokens owned). |
ownerOf
Returns the owner of a given token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | The token ID to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | address | The address of the token owner. |
tokenOfOwnerByIndex
Returns a token ID owned by a given address at a specific index.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | The address to query. |
_index | uint256 | The index of the token. |
Returns:
| Property | Type | Description |
|---|---|---|
- | uint256 | The token ID owned by _owner at _index. |
getApproved
Returns the approved address for a given token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | The token ID to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | address | The approved address for the token. |
isApprovedForAll
Returns whether an operator is approved for all tokens of an owner.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | The token owner. |
_operator | address | The operator address. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if approved for all, false otherwise. |
approve
Approves another address to transfer a specific token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_to | address | The address being approved. |
_tokenId | uint256 | The token ID to approve. |
setApprovalForAll
Approves or revokes an operator to manage all tokens of the caller.
Parameters:
| Property | Type | Description |
|---|---|---|
_operator | address | The operator address. |
_approved | bool | True to approve, false to revoke. |
transferFrom
Transfers a token from one address to another.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The current owner of the token. |
_to | address | The recipient address. |
_tokenId | uint256 | The token ID to transfer. |
safeTransferFrom
Safely transfers a token, checking for receiver contract compatibility.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The current owner of the token. |
_to | address | The recipient address. |
_tokenId | uint256 | The token ID to transfer. |
safeTransferFrom
Safely transfers a token with additional data.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The current owner of the token. |
_to | address | The recipient address. |
_tokenId | uint256 | The token ID to transfer. |
_data | bytes | Additional data to send to the receiver contract. |
Events
Errors
Best Practices
- Initialize the
name,symbol, andbaseURIduring diamond setup. - Enforce access control on state-changing functions like
approveandtransferFromif custom logic requires it. - Ensure storage compatibility when upgrading to new versions of this facet.
Security Considerations
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.