Skip to main content

ERC-721 Enumerable Mint Module

Mint ERC-721 tokens and manage enumeration lists

Key Features
  • Internal functions for minting and enumeration management.
  • Uses diamond storage pattern (EIP-8042) for shared state.
  • Enforces ERC-721 token existence checks.
  • Emits Transfer event upon successful minting.
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 to mint ERC-721 tokens and maintain enumeration lists within a diamond. Facets can import this module to handle token creation and ensure that all minted tokens are tracked, making them accessible to other facets via shared diamond storage. This facilitates consistent state management across the diamond.

Storage

ERC721EnumerableStorage

storage-location: erc8042:erc721.enumerable

Definition
struct ERC721EnumerableStorage {
mapping(address owner => mapping(uint256 index => uint256 tokenId)) ownerTokens;
mapping(uint256 tokenId => uint256 ownerTokensIndex) ownerTokensIndex;
uint256[] allTokens;
mapping(uint256 tokenId => uint256 allTokensIndex) allTokensIndex;
}

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.enumerable"))
ERC721_STORAGE_POSITIONbytes32Diamond 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.

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

Returns:

PropertyTypeDescription
sERC721StorageThe ERC721Storage struct in storage.

getStorage

Returns the storage struct used by this facet.

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

Returns:

PropertyTypeDescription
sERC721EnumerableStorageThe ERC721Enumerable storage struct.

mint

Mints a new ERC-721 token to the specified address, adding it to enumeration lists. Reverts if the receiver address is zero or if the token already exists.

function mint(address _to, uint256 _tokenId) ;

Parameters:

PropertyTypeDescription
_toaddressThe address that will own the newly minted token.
_tokenIduint256The ID of the token to mint.

Events

Errors

Best Practices

Best Practice
  • Ensure the to address is not the zero address before calling mint.
  • Verify that the tokenId does not already exist in storage to prevent re-minting.
  • Call mint only after confirming necessary access controls are met by the calling facet.

Integration Notes

Shared Storage

This module interacts with diamond storage at the STORAGE_POSITION defined by keccak256(\"erc721.enumerable\"). The mint function reads from and writes to the ERC721EnumerableStorage struct, ensuring that newly minted tokens are added to internal enumeration lists. Changes to this storage are immediately visible to all facets operating on the same diamond storage layout.

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.