Skip to main content

ERC-721 Approve Module

Manages ERC-721 token approvals and operator status

Key Features
  • Provides internal functions for ERC-721 token approvals and operator management.
  • Utilizes the diamond storage pattern (EIP-8042) for shared state.
  • No external dependencies, promoting composability within a diamond.
  • Emits Approval and ApprovalForAll events upon successful state changes.
Module Usage

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 ERC-721 token approvals and operator status within a diamond. Facets can import this module to interact with shared ERC-721 storage, ensuring consistent state management across the diamond. Changes to approvals and operator statuses are immediately visible to all facets accessing the same diamond storage.

Storage

ERC721Storage

storage-location: erc8042:erc721

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

State Variables

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

Functions

approve

Approves another address to transfer the given token ID.

function approve(address _to, uint256 _tokenId) ;

Parameters:

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

getStorage

Returns a pointer to the ERC-721 storage struct. Uses inline assembly to access the storage slot defined by STORAGE_POSITION.

function getStorage() pure returns (ERC721Storage storage s);

Returns:

PropertyTypeDescription
sERC721StorageThe ERC721Storage struct in storage.

setApprovalForAll

Approves or revokes permission for an operator to manage all users's assets.

function setApprovalForAll(address user, address _operator, bool _approved) ;

Parameters:

PropertyTypeDescription
_operatoraddressThe operator address to set approval for.
_approvedboolTrue to approve, false to revoke.

Events

Errors

Best Practices

Best Practice
  • Call approve or setApprovalForAll only after verifying caller permissions if required by your facet's logic.
  • Ensure the ERC721Storage struct definition in your facet matches the one defined in this module to prevent storage collisions.
  • Handle potential errors like ERC721NonexistentToken or ERC721InvalidOperator when calling the module's functions.

Integration Notes

Shared Storage

This module interacts with diamond storage at the slot identified by STORAGE_POSITION, which is defined as keccak256(\"erc721\"). It uses the ERC721Storage struct to manage approval data. All state modifications made through this module are written directly to this shared storage slot, making them immediately visible to any other facet within the same diamond that reads from or writes to this storage position.

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.