Skip to main content

ERC1155Facet

Manages ERC-1155 fungible and non-fungible tokens within a diamond

Key Features
  • 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

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

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.

function uri(uint256 _id) external view returns (string memory);

Parameters:

PropertyTypeDescription
_iduint256The token ID to query.

Returns:

PropertyTypeDescription
-stringThe URI for the token type.

balanceOf

Returns the amount of tokens of token type id owned by account.

function balanceOf(address _account, uint256 _id) external view returns (uint256);

Parameters:

PropertyTypeDescription
_accountaddressThe address to query the balance of.
_iduint256The token type to query.

Returns:

PropertyTypeDescription
-uint256The balance of the token type.

balanceOfBatch

Batched version of balanceOf.

function balanceOfBatch(address[] calldata _accounts, uint256[] calldata _ids)
external
view
returns (uint256[] memory balances);

Parameters:

PropertyTypeDescription
_accountsaddress[]The addresses to query the balances of.
_idsuint256[]The token types to query.

Returns:

PropertyTypeDescription
balancesuint256[]The balances of the token types.

setApprovalForAll

Grants or revokes permission to operator to transfer the caller's tokens. Emits an ApprovalForAll event.

function setApprovalForAll(address _operator, bool _approved) external;

Parameters:

PropertyTypeDescription
_operatoraddressThe address to grant/revoke approval to.
_approvedboolTrue to approve, false to revoke.

isApprovedForAll

Returns true if operator is approved to transfer account's tokens.

function isApprovedForAll(address _account, address _operator) external view returns (bool);

Parameters:

PropertyTypeDescription
_accountaddressThe token owner.
_operatoraddressThe operator to query.

Returns:

PropertyTypeDescription
-boolTrue if the operator is approved, false otherwise.

safeTransferFrom

Transfers value amount of token type id from from to to. Emits a TransferSingle event.

function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes calldata _data) external;

Parameters:

PropertyTypeDescription
_fromaddressThe address to transfer from.
_toaddressThe address to transfer to.
_iduint256The token type to transfer.
_valueuint256The amount to transfer.
_databytesAdditional data with no specified format.

safeBatchTransferFrom

Batched version of safeTransferFrom. Emits a TransferBatch event.

function safeBatchTransferFrom(
address _from,
address _to,
uint256[] calldata _ids,
uint256[] calldata _values,
bytes calldata _data
) external;

Parameters:

PropertyTypeDescription
_fromaddressThe address to transfer from.
_toaddressThe address to transfer to.
_idsuint256[]The token types to transfer.
_valuesuint256[]The amounts to transfer.
_databytesAdditional data with no specified format.

Events

Errors

Best Practices

Best Practice
  • Initialize the baseURI and uri mappings during diamond setup if custom URIs are required.
  • Use setApprovalForAll to 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

Security

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.

Was this helpful?
Last updated: