ERC-1155 Transfer Module
Manages ERC-1155 token transfers within a diamond
- Implements
safeTransferFromandsafeBatchTransferFromadhering to EIP-1155. - Uses diamond storage for persistent token balance management.
- Emits
TransferSingleandTransferBatchevents upon successful transfers. - Includes robust error checking for invalid inputs and state conditions.
This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.
Overview
This module implements core ERC-1155 transfer logic, enabling facets to move multiple token types between addresses. It leverages diamond storage for token balances, ensuring consistency across all facets. By adhering to EIP-1155 safe transfer requirements, it provides a secure foundation for complex token interactions within a diamond.
Storage
ERC1155Storage
ERC-8042 compliant storage struct for ERC-1155 token data. storage-location: erc8042:erc1155
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc1155")) |
Functions
getStorage
Returns the ERC-1155 storage struct from the predefined diamond storage slot. Uses inline assembly to set the storage slot reference.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC1155Storage | The ERC-1155 storage struct reference. |
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.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The address to transfer from. |
_to | address | The address to transfer to. |
_ids | uint256[] | The token types to transfer. |
_values | uint256[] | The amounts of tokens to transfer for each type. |
_operator | address | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The address to transfer from. |
_to | address | The address to transfer to. |
_id | uint256 | The token type to transfer. |
_value | uint256 | The amount of tokens to transfer. |
_operator | address | The address initiating the transfer (may be owner or approved operator). |
Events
Errors
Best Practices
- Ensure necessary approvals are granted using
ERC1155ApproveModbefore calling transfer functions. - Verify that the
_toaddress is a valid receiver, especially if it's a contract, to prevent unexpected behavior. - Handle the
ERC1155InsufficientBalance,ERC1155InvalidArrayLength,ERC1155InvalidReceiver,ERC1155InvalidSender, andERC1155MissingApprovalForAllerrors appropriately.
Integration Notes
This module interacts with diamond storage at the slot identified by keccak256(\"erc1155\"). The getStorage() function provides direct access to the ERC1155Storage struct, which holds all token balance information. Any modifications to balances made by safeTransferFrom or safeBatchTransferFrom are immediately reflected in this shared storage, visible to all facets accessing the same storage slot.