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>
515 lines
12 KiB
Markdown
515 lines
12 KiB
Markdown
# ERC1155
|
|
|
|
Simple ERC1155 implementation.
|
|
|
|
|
|
<b>Note:</b>
|
|
|
|
- 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.
|
|
|
|
<b>If you are overriding:</b>
|
|
- Make sure all variables written to storage are properly cleaned
|
|
(e.g. the bool value for `isApprovedForAll` MUST 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.
|
|
|
|
|
|
|
|
<!-- customintro:start --><!-- customintro:end -->
|
|
|
|
## Custom Errors
|
|
|
|
### ArrayLengthsMismatch()
|
|
|
|
```solidity
|
|
error ArrayLengthsMismatch()
|
|
```
|
|
|
|
The lengths of the input arrays are not the same.
|
|
|
|
### TransferToZeroAddress()
|
|
|
|
```solidity
|
|
error TransferToZeroAddress()
|
|
```
|
|
|
|
Cannot mint or transfer to the zero address.
|
|
|
|
### AccountBalanceOverflow()
|
|
|
|
```solidity
|
|
error AccountBalanceOverflow()
|
|
```
|
|
|
|
The recipient's balance has overflowed.
|
|
|
|
### InsufficientBalance()
|
|
|
|
```solidity
|
|
error InsufficientBalance()
|
|
```
|
|
|
|
Insufficient balance.
|
|
|
|
### NotOwnerNorApproved()
|
|
|
|
```solidity
|
|
error NotOwnerNorApproved()
|
|
```
|
|
|
|
Only the token owner or an approved account can manage the tokens.
|
|
|
|
### TransferToNonERC1155ReceiverImplementer()
|
|
|
|
```solidity
|
|
error TransferToNonERC1155ReceiverImplementer()
|
|
```
|
|
|
|
Cannot safely transfer to a contract that does not implement
|
|
the ERC1155Receiver interface.
|
|
|
|
## Events
|
|
|
|
### TransferSingle(address,address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
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[])
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
function balanceOf(address owner, uint256 id)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 result)
|
|
```
|
|
|
|
Returns the amount of `id` owned by `owner`.
|
|
|
|
### isApprovedForAll(address,address)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
function safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes calldata data
|
|
) public virtual
|
|
```
|
|
|
|
Transfers `amount` of `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- `from` must have at least `amount` of `id`.
|
|
- If the caller is not `from`,
|
|
it must be approved to manage the tokens of `from`.
|
|
- If `to` refers 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)
|
|
|
|
```solidity
|
|
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`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- `from` must have at least `amount` of `id`.
|
|
- `ids` and `amounts` must have the same length.
|
|
- If the caller is not `from`,
|
|
it must be approved to manage the tokens of `from`.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`ERC1155-onERC1155BatchReceived`, which is called upon a batch transfer.
|
|
|
|
Emits a `TransferBatch` event.
|
|
|
|
### balanceOfBatch(address[],uint256[])
|
|
|
|
```solidity
|
|
function balanceOfBatch(address[] calldata owners, uint256[] calldata ids)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256[] memory balances)
|
|
```
|
|
|
|
Returns the amounts of `ids` for `owners.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `owners` and `ids` must have the same length.
|
|
|
|
### supportsInterface(bytes4)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
function _mint(address to, uint256 id, uint256 amount, bytes memory data)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Mints `amount` of `id` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- If `to` refers 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)
|
|
|
|
```solidity
|
|
function _batchMint(
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual
|
|
```
|
|
|
|
Mints `amounts` of `ids` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- `ids` and `amounts` must have the same length.
|
|
- If `to` refers 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)
|
|
|
|
```solidity
|
|
function _burn(address from, uint256 id, uint256 amount) internal virtual
|
|
```
|
|
|
|
Equivalent to `_burn(address(0), from, id, amount)`.
|
|
|
|
### _burn(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _burn(address by, address from, uint256 id, uint256 amount)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Destroys `amount` of `id` from `from`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `from` must have at least `amount` of `id`.
|
|
- If `by` is not the zero address, it must be either `from`,
|
|
or approved to manage the tokens of `from`.
|
|
|
|
Emits a `TransferSingle` event.
|
|
|
|
### _batchBurn(address,uint256[],uint256[])
|
|
|
|
```solidity
|
|
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[])
|
|
|
|
```solidity
|
|
function _batchBurn(
|
|
address by,
|
|
address from,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts
|
|
) internal virtual
|
|
```
|
|
|
|
Destroys `amounts` of `ids` from `from`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `ids` and `amounts` must have the same length.
|
|
- `from` must have at least `amounts` of `ids`.
|
|
- If `by` is not the zero address, it must be either `from`,
|
|
or approved to manage the tokens of `from`.
|
|
|
|
Emits a `TransferBatch` event.
|
|
|
|
## Internal Approval Functions
|
|
|
|
### _setApprovalForAll(address,address,bool)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- `from` must have at least `amount` of `id`.
|
|
- If `by` is not the zero address, it must be either `from`,
|
|
or approved to manage the tokens of `from`.
|
|
- If `to` refers 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)
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
- `ids` and `amounts` must have the same length.
|
|
- `from` must have at least `amounts` of `ids`.
|
|
- If `by` is not the zero address, it must be either `from`,
|
|
or approved to manage the tokens of `from`.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`ERC1155-onERC1155BatchReceived`, which is called upon a batch transfer.
|
|
|
|
Emits a `TransferBatch` event.
|
|
|
|
## Hooks For Overriding
|
|
|
|
### _useBeforeTokenTransfer()
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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()
|
|
|
|
```solidity
|
|
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)
|
|
|
|
```solidity
|
|
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. |