Skip to main content

StructERC1155

Git Source

Authors: Struct Finance, Added _beforeTransferHook to Solmate's ERC1155 impl (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC1155.sol)

Minimalist and gas efficient standard ERC1155 implementation with _beforeTransfer hook.

State Variables

balanceOf

mapping(address => mapping(uint256 => uint256)) public balanceOf;

isApprovedForAll

mapping(address => mapping(address => bool)) public isApprovedForAll;

Functions

uri

function uri(uint256 id) public view virtual returns (string memory);

setApprovalForAll

function setApprovalForAll(address operator, bool approved) public virtual;

safeTransferFrom

function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) public virtual;

safeBatchTransferFrom

function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public virtual;

balanceOfBatch

function balanceOfBatch(address[] memory owners, uint256[] memory ids)
public
view
virtual
returns (uint256[] memory balances);

supportsInterface

function supportsInterface(bytes4 interfaceId) public pure virtual returns (bool);

_mint

function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal;

_batchMint

function _batchMint(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal;

_batchBurn

function _batchBurn(address from, uint256[] memory ids, uint256[] memory amounts) internal;

_burn

function _burn(address from, uint256 id, uint256 amount) internal;

_beforeTokenTransfer

Hook that is called before any token transfer. This includes minting and burning, as well as batched variants. The same hook is called on both single and batched variants. For single transfers, the length of the id and amount arrays will be 1. Calling conditions (for each id and amount pair):

  • When from and to are both non-zero, amount of from's tokens of token type id will be transferred to to.
  • When from is zero, amount tokens of token type id will be minted for to.
  • when to is zero, amount of from's tokens of token type id will be burned.
  • from and to are never both zero.
  • ids and amounts have the same, non-zero length. To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual;

_asSingletonArray

function _asSingletonArray(uint256 element) private pure returns (uint256[] memory);

Events

TransferSingle

event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 amount);

TransferBatch

event TransferBatch(
address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] amounts
);

ApprovalForAll

event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

URI

event URI(string value, uint256 indexed id);