Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
12 KiB
ERC1155
Simple ERC1155 implementation.
Note:
- The ERC1155 standard allows for self-approvals. For performance, this implementation WILL NOT revert for such actions. Please add any checks with overrides if desired.
- The transfer functions use the identity precompile (0x4) to copy memory internally.
If you are overriding:
- Make sure all variables written to storage are properly cleaned
(e.g. the bool value for
isApprovedForAllMUST be either 1 or 0 under the hood). - Check that the overridden function is actually used in the function you want to change the behavior of. Much of the code has been manually inlined for performance.
Custom Errors
ArrayLengthsMismatch()
error ArrayLengthsMismatch()
The lengths of the input arrays are not the same.
TransferToZeroAddress()
error TransferToZeroAddress()
Cannot mint or transfer to the zero address.
AccountBalanceOverflow()
error AccountBalanceOverflow()
The recipient's balance has overflowed.
InsufficientBalance()
error InsufficientBalance()
Insufficient balance.
NotOwnerNorApproved()
error NotOwnerNorApproved()
Only the token owner or an approved account can manage the tokens.
TransferToNonERC1155ReceiverImplementer()
error TransferToNonERC1155ReceiverImplementer()
Cannot safely transfer to a contract that does not implement
the ERC1155Receiver interface.
Events
TransferSingle(address,address,address,uint256,uint256)
event TransferSingle(
address indexed operator,
address indexed from,
address indexed to,
uint256 id,
uint256 amount
)
Emitted when amount of token id is transferred
from from to to by operator.
TransferBatch(address,address,address,uint256[],uint256[])
event TransferBatch(
address indexed operator,
address indexed from,
address indexed to,
uint256[] ids,
uint256[] amounts
)
Emitted when amounts of token ids are transferred
from from to to by operator.
ApprovalForAll(address,address,bool)
event ApprovalForAll(
address indexed owner, address indexed operator, bool isApproved
)
Emitted when owner enables or disables operator to manage all of their tokens.
URI(string,uint256)
event URI(string value, uint256 indexed id)
Emitted when the Uniform Resource Identifier (URI) for token id
is updated to value. This event is not used in the base contract.
You may need to emit this event depending on your URI logic.
See: https://eips.ethereum.org/EIPS/eip-1155#metadata
ERC1155 Metadata
uri(uint256)
function uri(uint256 id) public view virtual returns (string memory);
Returns the URI for token id.
You can either return the same templated URI for all token IDs,
(e.g. "https://example.com/api/id.json"),
or return a unique URI for each id.
See: https://eips.ethereum.org/EIPS/eip-1155#metadata
ERC1155
balanceOf(address,uint256)
function balanceOf(address owner, uint256 id)
public
view
virtual
returns (uint256 result)
Returns the amount of id owned by owner.
isApprovedForAll(address,address)
function isApprovedForAll(address owner, address operator)
public
view
virtual
returns (bool result)
Returns whether operator is approved to manage the tokens of owner.
setApprovalForAll(address,bool)
function setApprovalForAll(address operator, bool isApproved)
public
virtual
Sets whether operator is approved to manage the tokens of the caller.
Emits a ApprovalForAll event.
safeTransferFrom(address,address,uint256,uint256,bytes)
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) public virtual
Transfers amount of id from from to to.
Requirements:
tocannot be the zero address.frommust have at leastamountofid.- If the caller is not
from,
it must be approved to manage the tokens offrom. - If
torefers to a smart contract, it must implement
ERC1155-onERC1155Received, which is called upon a batch transfer.
Emits a TransferSingle event.
safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)
function safeBatchTransferFrom(
address from,
address to,
uint256[] calldata ids,
uint256[] calldata amounts,
bytes calldata data
) public virtual
Transfers amounts of ids from from to to.
Requirements:
tocannot be the zero address.frommust have at leastamountofid.idsandamountsmust have the same length.- If the caller is not
from,
it must be approved to manage the tokens offrom. - If
torefers to a smart contract, it must implement
ERC1155-onERC1155BatchReceived, which is called upon a batch transfer.
Emits a TransferBatch event.
balanceOfBatch(address[],uint256[])
function balanceOfBatch(address[] calldata owners, uint256[] calldata ids)
public
view
virtual
returns (uint256[] memory balances)
Returns the amounts of ids for `owners.
Requirements:
ownersandidsmust have the same length.
supportsInterface(bytes4)
function supportsInterface(bytes4 interfaceId)
public
view
virtual
returns (bool result)
Returns true if this contract implements the interface defined by interfaceId.
See: https://eips.ethereum.org/EIPS/eip-165
This function call must use less than 30000 gas.
Internal Mint Functions
_mint(address,uint256,uint256,bytes)
function _mint(address to, uint256 id, uint256 amount, bytes memory data)
internal
virtual
Mints amount of id to to.
Requirements:
tocannot be the zero address.- If
torefers to a smart contract, it must implement
ERC1155-onERC1155Received, which is called upon a batch transfer.
Emits a TransferSingle event.
_batchMint(address,uint256[],uint256[],bytes)
function _batchMint(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual
Mints amounts of ids to to.
Requirements:
tocannot be the zero address.idsandamountsmust have the same length.- If
torefers to a smart contract, it must implement
ERC1155-onERC1155BatchReceived, which is called upon a batch transfer.
Emits a TransferBatch event.
Internal Burn Functions
_burn(address,uint256,uint256)
function _burn(address from, uint256 id, uint256 amount) internal virtual
Equivalent to _burn(address(0), from, id, amount).
_burn(address,address,uint256,uint256)
function _burn(address by, address from, uint256 id, uint256 amount)
internal
virtual
Destroys amount of id from from.
Requirements:
frommust have at leastamountofid.- If
byis not the zero address, it must be eitherfrom,
or approved to manage the tokens offrom.
Emits a TransferSingle event.
_batchBurn(address,uint256[],uint256[])
function _batchBurn(
address from,
uint256[] memory ids,
uint256[] memory amounts
) internal virtual
Equivalent to _batchBurn(address(0), from, ids, amounts).
_batchBurn(address,address,uint256[],uint256[])
function _batchBurn(
address by,
address from,
uint256[] memory ids,
uint256[] memory amounts
) internal virtual
Destroys amounts of ids from from.
Requirements:
idsandamountsmust have the same length.frommust have at leastamountsofids.- If
byis not the zero address, it must be eitherfrom,
or approved to manage the tokens offrom.
Emits a TransferBatch event.
Internal Approval Functions
_setApprovalForAll(address,address,bool)
function _setApprovalForAll(address by, address operator, bool isApproved)
internal
virtual
Approve or remove the operator as an operator for by,
without authorization checks.
Emits a ApprovalForAll event.
Internal Transfer Functions
_safeTransfer(address,address,uint256,uint256,bytes)
function _safeTransfer(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) internal virtual
Equivalent to _safeTransfer(address(0), from, to, id, amount, data).
_safeTransfer(address,address,address,uint256,uint256,bytes)
function _safeTransfer(
address by,
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) internal virtual
Transfers amount of id from from to to.
Requirements:
tocannot be the zero address.frommust have at leastamountofid.- If
byis not the zero address, it must be eitherfrom,
or approved to manage the tokens offrom. - If
torefers to a smart contract, it must implement
ERC1155-onERC1155Received, which is called upon a batch transfer.
Emits a TransferSingle event.
_safeBatchTransfer(address,address,uint256[],uint256[],bytes)
function _safeBatchTransfer(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual
Equivalent to _safeBatchTransfer(address(0), from, to, ids, amounts, data).
_safeBatchTransfer(address,address,address,uint256[],uint256[],bytes)
function _safeBatchTransfer(
address by,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual
Transfers amounts of ids from from to to.
Requirements:
tocannot be the zero address.idsandamountsmust have the same length.frommust have at leastamountsofids.- If
byis not the zero address, it must be eitherfrom,
or approved to manage the tokens offrom. - If
torefers to a smart contract, it must implement
ERC1155-onERC1155BatchReceived, which is called upon a batch transfer.
Emits a TransferBatch event.
Hooks For Overriding
_useBeforeTokenTransfer()
function _useBeforeTokenTransfer() internal view virtual returns (bool)
Override this function to return true if _beforeTokenTransfer is used.
This is to help the compiler avoid producing dead bytecode.
_beforeTokenTransfer(address,address,uint256[],uint256[],bytes)
function _beforeTokenTransfer(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual
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 are 1.
_useAfterTokenTransfer()
function _useAfterTokenTransfer() internal view virtual returns (bool)
Override this function to return true if _afterTokenTransfer is used.
This is to help the compiler avoid producing dead bytecode.
_afterTokenTransfer(address,address,uint256[],uint256[],bytes)
function _afterTokenTransfer(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual
Hook that is called after 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 are 1.