ERC1155Facet
Manages ERC-1155 fungible and non-fungible tokens within a diamond
- Exposes standard ERC-1155 functions via diamond routing.
- Supports both single and batch transfers and balance checks.
- Integrates with diamond storage for persistent state.
- Emits standard ERC-1155 events for off-chain tracking.
Overview
This facet implements ERC-1155 token functionality within a diamond proxy. It exposes external functions for transfers, approvals, and balance checks, routing calls through the diamond's orchestration layer. Developers add this facet to enable ERC-1155 token operations, leveraging the diamond's upgradeability and modularity.
Storage
ERC1155Storage
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("compose.erc1155")) |
Functions
uri
Returns the URI for token type _id. If a token-specific URI is set in tokenURIs[_id], returns the concatenation of baseURI and tokenURIs[_id]. Note that baseURI is empty by default and must be set explicitly if concatenation is desired. If no token-specific URI is set, returns the default URI which applies to all token types. The default URI may contain the substring {id} which clients should replace with the actual token ID.
Parameters:
| Property | Type | Description |
|---|---|---|
_id | uint256 | The token ID to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | string | The URI for the token type. |
balanceOf
Returns the amount of tokens of token type id owned by account.
Parameters:
| Property | Type | Description |
|---|---|---|
_account | address | The address to query the balance of. |
_id | uint256 | The token type to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | uint256 | The balance of the token type. |
balanceOfBatch
Batched version of balanceOf.
Parameters:
| Property | Type | Description |
|---|---|---|
_accounts | address[] | The addresses to query the balances of. |
_ids | uint256[] | The token types to query. |
Returns:
| Property | Type | Description |
|---|---|---|
balances | uint256[] | The balances of the token types. |
setApprovalForAll
Grants or revokes permission to operator to transfer the caller's tokens. Emits an ApprovalForAll event.
Parameters:
| Property | Type | Description |
|---|---|---|
_operator | address | The address to grant/revoke approval to. |
_approved | bool | True to approve, false to revoke. |
isApprovedForAll
Returns true if operator is approved to transfer account's tokens.
Parameters:
| Property | Type | Description |
|---|---|---|
_account | address | The token owner. |
_operator | address | The operator to query. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the operator is approved, false otherwise. |
safeTransferFrom
Transfers value amount of token type id from from to to. Emits a TransferSingle event.
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 to transfer. |
_data | bytes | Additional data with no specified format. |
safeBatchTransferFrom
Batched version of safeTransferFrom. Emits a TransferBatch event.
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 to transfer. |
_data | bytes | Additional data with no specified format. |
Events
Errors
Best Practices
- Initialize the
baseURIandurimappings during diamond setup if custom URIs are required. - Use
setApprovalForAllto grant operator permissions before executing transfers on behalf of another account. - Ensure all token IDs and values are validated before calling transfer functions to prevent unexpected behavior.
Security Considerations
Follow standard Solidity security practices. Input validation is crucial for token IDs, values, sender, and receiver addresses. The safeTransferFrom and safeBatchTransferFrom functions include checks for sufficient balance and operator approval. Reentrancy is mitigated by using the checks-effects-interactions pattern.