Skip to main content

ERC-1155 Transfer Facet

ERC-1155 token transfers within a diamond

Key Features
  • Implements safeTransferFrom and safeBatchTransferFrom for ERC-1155 token handling.
  • Accesses shared diamond storage via getStorage() for balance management.
  • Emits TransferSingle and TransferBatch events for off-chain tracking.
  • Exports its function selectors via exportSelectors() for diamond integration.

Overview

This facet implements ERC-1155 token transfers as external functions callable through a diamond proxy. It accesses shared diamond storage to manage token balances and emits standard events for off-chain consumption. Developers integrate this facet to provide core ERC-1155 transfer capabilities while retaining diamond's upgradeability.

Storage

ERC1155Storage

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

State Variables

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

Functions

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.

exportSelectors

Exports the function selectors of the ERC1155TransferFacet This function is use as a selector discovery mechanism for diamonds

function exportSelectors() external pure returns (bytes memory);

Returns:

PropertyTypeDescription
-bytesselectors The exported function selectors of the ERC1155TransferFacet

Events

Errors

Best Practices

Best Practice
  • Ensure the ERC1155Storage struct is correctly initialized in diamond storage before deploying this facet.
  • Verify that the ERC1155ApproveFacet is also deployed to manage approvals required for transfers.
  • Call exportSelectors() during diamond deployment to register the facet's functions.

Security Considerations

Security

The safeTransferFrom and safeBatchTransferFrom functions perform checks before state changes and external interactions, mitigating reentrancy risks. Input validation is performed for sender, receiver, and array lengths, reverting with custom errors like ERC1155InvalidSender and ERC1155InvalidArrayLength. Ensure that the caller has the necessary approvals via ERC1155ApproveFacet before invoking transfer functions.

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.