Skip to main content

ERC-721 Mint Module

Mint ERC-721 tokens using diamond storage

Key Features
  • Exposes internal mint function for facet integration.
  • Utilizes diamond storage pattern (EIP-8042) for state management.
  • Emits Transfer event upon successful minting.
  • No external dependencies, ensuring minimal on-chain footprint.
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 new ERC-721 tokens. Facets can import this module to manage token creation, leveraging shared diamond storage for consistency. Changes made via this module are immediately visible to all facets sharing the same storage.

Storage

ERC721Storage

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

getStorage

Returns the ERC-721 storage struct from its predefined slot. Uses inline assembly to access diamond storage location.

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

Returns:

PropertyTypeDescription
sERC721StorageThe storage reference for ERC-721 state variables.

mint

Mints a new ERC-721 token to the specified address. 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 calling facet enforces access control before invoking mint.
  • Verify the to address is not the zero address and the tokenId does not already exist before calling mint.
  • Handle ERC721InvalidReceiver and other potential errors gracefully.

Integration Notes

Shared Storage

This module accesses shared diamond storage at the position identified by keccak256(\"erc721\") to manage ERC-721 token state. The mint function reads and writes to this shared storage. Any facet that also accesses this storage slot will observe changes made by this module immediately. The ERC721Storage struct, though empty in this definition, reserves the storage slot for future ERC-721 specific state.

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.