Skip to main content

ERC1155Mod

Handles ERC-1155 token minting, burning, and transfers

Key Features
  • Internal functions for minting, burning, and transfers.
  • Supports safe transfers for ERC-1155 token contracts.
  • Manages token URIs with setBaseURI and setTokenURI functions.
  • Utilizes diamond storage for state management.
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 core ERC-1155 functionality including minting, burning, and safe transfers. Facets can integrate this module to manage ERC-1155 tokens within the diamond, leveraging shared diamond storage for token balances and URIs. It ensures interoperability by adhering to EIP-1155 standards for safe transfers.


Storage

ERC1155Storage

ERC-8042 compliant storage struct for ERC-1155 token data. storage-location: erc8042:compose.erc1155

Definition
struct ERC1155Storage {
mapping(uint256 id => mapping(address account => uint256 balance)) balanceOf;
mapping(address account => mapping(address operator => bool)) isApprovedForAll;
string uri;
string baseURI;
mapping(uint256 tokenId => string) tokenURIs;
}

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("compose.erc1155"))

Functions

burn

Burns a single token type from an address. Decreases the balance and emits a TransferSingle event. Reverts if the account has insufficient balance.

function burn(address _from, uint256 _id, uint256 _value) ;

Parameters:

PropertyTypeDescription
_fromaddressThe address whose tokens will be burned.
_iduint256The token type to burn.
_valueuint256The amount of tokens to burn.

burnBatch

Burns multiple token types from an address in a single transaction. Decreases balances for each token type and emits a TransferBatch event. Reverts if the account has insufficient balance for any token type.

function burnBatch(address _from, uint256[] memory _ids, uint256[] memory _values) ;

Parameters:

PropertyTypeDescription
_fromaddressThe address whose tokens will be burned.
_idsuint256[]The token types to burn.
_valuesuint256[]The amounts of tokens to burn for each type.

getStorage

Returns the ERC-1155 storage struct from the predefined diamond storage slot. Uses inline assembly to set the storage slot reference.

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

Returns:

PropertyTypeDescription
sERC1155StorageThe ERC-1155 storage struct reference.

mint

Mints a single token type to an address. Increases the balance and emits a TransferSingle event. Performs receiver validation if recipient is a contract.

function mint(address _to, uint256 _id, uint256 _value, bytes memory _data) ;

Parameters:

PropertyTypeDescription
_toaddressThe address that will receive the tokens.
_iduint256The token type to mint.
_valueuint256The amount of tokens to mint.

mintBatch

Mints multiple token types to an address in a single transaction. Increases balances for each token type and emits a TransferBatch event. Performs receiver validation if recipient is a contract.

function mintBatch(address _to, uint256[] memory _ids, uint256[] memory _values, bytes memory _data) ;

Parameters:

PropertyTypeDescription
_toaddressThe address that will receive the tokens.
_idsuint256[]The token types to mint.
_valuesuint256[]The amounts of tokens to mint for each type.

safeBatchTransferFrom

Safely transfers multiple token types from one address to another in a single transaction. Validates ownership, approval, and receiver address before updating balances for each token type. Performs ERC1155Receiver validation if recipient is a contract (safe transfer). Complies with EIP-1155 safe transfer requirements.

function safeBatchTransferFrom(
address _from,
address _to,
uint256[] memory _ids,
uint256[] memory _values,
address _operator
) ;

Parameters:

PropertyTypeDescription
_fromaddressThe address to transfer from.
_toaddressThe address to transfer to.
_idsuint256[]The token types to transfer.
_valuesuint256[]The amounts of tokens to transfer for each type.
_operatoraddressThe address initiating the transfer (may be owner or approved operator).

safeTransferFrom

Safely transfers a single token type from one address to another. Validates ownership, approval, and receiver address before updating balances. Performs ERC1155Receiver validation if recipient is a contract (safe transfer). Complies with EIP-1155 safe transfer requirements.

function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, address _operator) ;

Parameters:

PropertyTypeDescription
_fromaddressThe address to transfer from.
_toaddressThe address to transfer to.
_iduint256The token type to transfer.
_valueuint256The amount of tokens to transfer.
_operatoraddressThe address initiating the transfer (may be owner or approved operator).

setBaseURI

Sets the base URI prefix for token-specific URIs. The base URI is concatenated with token-specific URIs set via setTokenURI. Does not affect the default URI used when no token-specific URI is set.

function setBaseURI(string memory _baseURI) ;

Parameters:

PropertyTypeDescription
_baseURIstringThe base URI string to prepend to token-specific URIs.

setTokenURI

Sets the token-specific URI for a given token ID. Sets tokenURIs[_tokenId] to the provided string and emits a URI event with the full computed URI. The emitted URI is the concatenation of baseURI and the token-specific URI.

function setTokenURI(uint256 _tokenId, string memory _tokenURI) ;

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to set the URI for.
_tokenURIstringThe token-specific URI string to be concatenated with baseURI.

Events

Errors

Best Practices

Best Practice
  • Call safeTransferFrom or safeBatchTransferFrom for standard ERC-1155 transfers to ensure receiver contract compatibility.
  • Use mint and burn functions for internal token lifecycle management.
  • Ensure correct _operator is passed for transfers to maintain accurate approval tracking.

Integration Notes

Shared Storage

This module utilizes the diamond storage pattern, storing ERC-1155 state within a dedicated slot identified by keccak256(\"compose.erc1155\"). The ERC1155Storage struct, containing uri and baseURI fields, is managed at this position. Functions like mint, burn, and transfers directly interact with this shared storage, ensuring all facets accessing this storage see consistent token balances and URI information.

Was this helpful?
Last updated: