ERC-721 Transfer Module
Manage ERC-721 token transfers within a diamond
- All functions are
internalfor use within facets. - Uses the diamond storage pattern (EIP-8042) for state management.
- Emits a
Transferevent upon successful token transfer. - Includes specific errors for common ERC-721 transfer failures.
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 to manage ERC-721 token transfers using diamond storage. Facets can import this module to perform token ownership changes, ensuring consistency across the diamond. State modifications are immediately visible to all facets accessing the shared ERC-721 storage.
Storage
ERC721Storage
Storage layout for ERC-721 token management. Defines ownership, balances, approvals, and operator mappings per ERC-721 standard. storage-location: erc8042:erc721
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc721")) |
Functions
getStorage
Returns the ERC-721 storage struct from its predefined slot. Uses inline assembly to access diamond storage location.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC721Storage | The storage reference for ERC-721 state variables. |
transferFrom
Transfers ownership of a token ID from one address to another. Validates ownership, approval, and receiver address before updating state.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The current owner of the token. |
_to | address | The address that will receive the token. |
_tokenId | uint256 | The ID of the token being transferred. |
Events
Errors
Best Practices
- Ensure caller is the owner or has been approved for the token before calling
transferFrom. - Handle
ERC721IncorrectOwner,ERC721InvalidReceiver, andERC721NonexistentTokenerrors when interacting withtransferFrom. - Verify that the
ERC721Storagestruct definition remains compatible with the diamond's storage layout upon upgrades.
Integration Notes
This module reads and writes to diamond storage at the slot designated by keccak256(\"erc721\"). The ERC721Storage struct defines the layout for token ownership and related metadata. Changes made via the transferFrom function are immediately reflected in this shared storage, making them visible to all facets that access the same storage position. The getStorage function provides a read-only view of this state using inline assembly.