ERC-721 Enumerable Transfer Module
Internal ERC721 token transfer logic
- Internal functions for token transfers, designed for facet composition.
- Utilizes diamond storage for token ownership tracking.
- Includes
safeTransferFromfor compatibility checks with ERC721Receiver interfaces. - No external dependencies, ensuring composability within a diamond.
This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.
Overview
This module provides internal functions for managing ERC721 token transfers within a diamond. It leverages diamond storage to track token ownership and ensures safe transfers by checking receiver compatibility. Changes made through this module are immediately visible to all facets accessing the shared ERC721 storage.
Storage
ERC721EnumerableStorage
storage-location: erc8042:erc721.enumerable
ERC721Storage
storage-location: erc8042:erc721
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc721.enumerable")) |
ERC721_STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc721")) |
Functions
getERC721Storage
Returns a pointer to the ERC-721 storage struct. Uses inline assembly to access the storage slot defined by STORAGE_POSITION.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC721Storage | The ERC721Storage struct in storage. |
getStorage
Returns the storage struct used by this facet.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC721EnumerableStorage | The ERC721Enumerable storage struct. |
function safeTransferFrom
Safely transfers a token, checking for receiver contract compatibility. Safely transfers a token with additional data.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | - |
_to | address | - |
_tokenId | uint256 | - |
_data | bytes memory | - |
transferFrom
Internal function to transfer ownership of a token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The address sending the token. |
_to | address | The address receiving the token. |
_tokenId | uint256 | The token ID being transferred. |
Events
Errors
Best Practices
- Ensure caller owns the token and the token exists before calling
transferFromorsafeTransferFrom. - Verify receiver contract compatibility before calling
safeTransferFromto prevent unexpected reverts. - Handle custom errors like
ERC721IncorrectOwner,ERC721InvalidReceiver, andERC721NonexistentTokenin calling facets.
Integration Notes
This module interacts with diamond storage via the STORAGE_POSITION slot, identified by keccak256(\"erc721.enumerable\"). It reads and writes to the ERC721EnumerableStorage struct. All state changes made through transferFrom and safeTransferFrom are immediately reflected in this shared storage, making them visible to any other facet that accesses the same storage slot.