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>
645 lines
14 KiB
Markdown
645 lines
14 KiB
Markdown
# ERC721
|
|
|
|
Simple ERC721 implementation with storage hitchhiking.
|
|
|
|
|
|
<b>Note:</b>
|
|
|
|
- The ERC721 standard allows for self-approvals.
|
|
For performance, this implementation WILL NOT revert for such actions.
|
|
Please add any checks with overrides if desired.
|
|
- For performance, methods are made payable where permitted by the ERC721 standard.
|
|
- The `safeTransfer` functions use the identity precompile (0x4)
|
|
to copy memory internally.
|
|
|
|
<b>If you are overriding:</b>
|
|
- NEVER violate the ERC721 invariant:
|
|
the balance of an owner MUST always be equal to their number of ownership slots.
|
|
The transfer functions do not have an underflow guard for user token balances.
|
|
- 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 -->
|
|
|
|
## Constants
|
|
|
|
### _MAX_ACCOUNT_BALANCE
|
|
|
|
```solidity
|
|
uint256 internal constant _MAX_ACCOUNT_BALANCE = 0xffffffff
|
|
```
|
|
|
|
An account can hold up to 4294967295 tokens.
|
|
|
|
## Custom Errors
|
|
|
|
### NotOwnerNorApproved()
|
|
|
|
```solidity
|
|
error NotOwnerNorApproved()
|
|
```
|
|
|
|
Only the token owner or an approved account can manage the token.
|
|
|
|
### TokenDoesNotExist()
|
|
|
|
```solidity
|
|
error TokenDoesNotExist()
|
|
```
|
|
|
|
The token does not exist.
|
|
|
|
### TokenAlreadyExists()
|
|
|
|
```solidity
|
|
error TokenAlreadyExists()
|
|
```
|
|
|
|
The token already exists.
|
|
|
|
### BalanceQueryForZeroAddress()
|
|
|
|
```solidity
|
|
error BalanceQueryForZeroAddress()
|
|
```
|
|
|
|
Cannot query the balance for the zero address.
|
|
|
|
### TransferToZeroAddress()
|
|
|
|
```solidity
|
|
error TransferToZeroAddress()
|
|
```
|
|
|
|
Cannot mint or transfer to the zero address.
|
|
|
|
### TransferFromIncorrectOwner()
|
|
|
|
```solidity
|
|
error TransferFromIncorrectOwner()
|
|
```
|
|
|
|
The token must be owned by `from`.
|
|
|
|
### AccountBalanceOverflow()
|
|
|
|
```solidity
|
|
error AccountBalanceOverflow()
|
|
```
|
|
|
|
The recipient's balance has overflowed.
|
|
|
|
### TransferToNonERC721ReceiverImplementer()
|
|
|
|
```solidity
|
|
error TransferToNonERC721ReceiverImplementer()
|
|
```
|
|
|
|
Cannot safely transfer to a contract that does not implement
|
|
the ERC721Receiver interface.
|
|
|
|
## Events
|
|
|
|
### Transfer(address,address,uint256)
|
|
|
|
```solidity
|
|
event Transfer(
|
|
address indexed from, address indexed to, uint256 indexed id
|
|
)
|
|
```
|
|
|
|
Emitted when token `id` is transferred from `from` to `to`.
|
|
|
|
### Approval(address,address,uint256)
|
|
|
|
```solidity
|
|
event Approval(
|
|
address indexed owner, address indexed account, uint256 indexed id
|
|
)
|
|
```
|
|
|
|
Emitted when `owner` enables `account` to manage the `id` token.
|
|
|
|
### 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.
|
|
|
|
## ERC721
|
|
|
|
### ownerOf(uint256)
|
|
|
|
```solidity
|
|
function ownerOf(uint256 id) public view virtual returns (address result)
|
|
```
|
|
|
|
Returns the owner of token `id`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
|
|
### balanceOf(address)
|
|
|
|
```solidity
|
|
function balanceOf(address owner)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 result)
|
|
```
|
|
|
|
Returns the number of tokens owned by `owner`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `owner` must not be the zero address.
|
|
|
|
### getApproved(uint256)
|
|
|
|
```solidity
|
|
function getApproved(uint256 id)
|
|
public
|
|
view
|
|
virtual
|
|
returns (address result)
|
|
```
|
|
|
|
Returns the account approved to manage token `id`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
|
|
### approve(address,uint256)
|
|
|
|
```solidity
|
|
function approve(address account, uint256 id) public payable virtual
|
|
```
|
|
|
|
Sets `account` as the approved account to manage token `id`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- The caller must be the owner of the token,
|
|
or an approved operator for the token owner.
|
|
|
|
Emits an `Approval` event.
|
|
|
|
### 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 an `ApprovalForAll` event.
|
|
|
|
### transferFrom(address,address,uint256)
|
|
|
|
```solidity
|
|
function transferFrom(address from, address to, uint256 id)
|
|
public
|
|
payable
|
|
virtual
|
|
```
|
|
|
|
Transfers token `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- `from` must be the owner of the token.
|
|
- `to` cannot be the zero address.
|
|
- The caller must be the owner of the token, or be approved to manage the token.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### safeTransferFrom(address,address,uint256)
|
|
|
|
```solidity
|
|
function safeTransferFrom(address from, address to, uint256 id)
|
|
public
|
|
payable
|
|
virtual
|
|
```
|
|
|
|
Equivalent to `safeTransferFrom(from, to, id, "")`.
|
|
|
|
### safeTransferFrom(address,address,uint256,bytes)
|
|
|
|
```solidity
|
|
function safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
bytes calldata data
|
|
) public payable virtual
|
|
```
|
|
|
|
Transfers token `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- `from` must be the owner of the token.
|
|
- `to` cannot be the zero address.
|
|
- The caller must be the owner of the token, or be approved to manage the token.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`IERC721Receiver-onERC721Received`, which is called upon a safe transfer.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### 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 Query Functions
|
|
|
|
### _exists(uint256)
|
|
|
|
```solidity
|
|
function _exists(uint256 id) internal view virtual returns (bool result)
|
|
```
|
|
|
|
Returns if token `id` exists.
|
|
|
|
### _ownerOf(uint256)
|
|
|
|
```solidity
|
|
function _ownerOf(uint256 id)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (address result)
|
|
```
|
|
|
|
Returns the owner of token `id`.
|
|
Returns the zero address instead of reverting if the token does not exist.
|
|
|
|
## Internal Data Hitchhiking Functions
|
|
|
|
For performance, no events are emitted for the hitchhiking setters.
|
|
Please emit your own events if required.
|
|
|
|
### _getAux(address)
|
|
|
|
```solidity
|
|
function _getAux(address owner)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (uint224 result)
|
|
```
|
|
|
|
Returns the auxiliary data for `owner`.
|
|
Minting, transferring, burning the tokens of `owner` will not change the auxiliary data.
|
|
Auxiliary data can be set for any address, even if it does not have any tokens.
|
|
|
|
### _setAux(address,uint224)
|
|
|
|
```solidity
|
|
function _setAux(address owner, uint224 value) internal virtual
|
|
```
|
|
|
|
Set the auxiliary data for `owner` to `value`.
|
|
Minting, transferring, burning the tokens of `owner` will not change the auxiliary data.
|
|
Auxiliary data can be set for any address, even if it does not have any tokens.
|
|
|
|
### _getExtraData(uint256)
|
|
|
|
```solidity
|
|
function _getExtraData(uint256 id)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (uint96 result)
|
|
```
|
|
|
|
Returns the extra data for token `id`.
|
|
Minting, transferring, burning a token will not change the extra data.
|
|
The extra data can be set on a non-existent token.
|
|
|
|
### _setExtraData(uint256,uint96)
|
|
|
|
```solidity
|
|
function _setExtraData(uint256 id, uint96 value) internal virtual
|
|
```
|
|
|
|
Sets the extra data for token `id` to `value`.
|
|
Minting, transferring, burning a token will not change the extra data.
|
|
The extra data can be set on a non-existent token.
|
|
|
|
## Internal Mint Functions
|
|
|
|
### _mint(address,uint256)
|
|
|
|
```solidity
|
|
function _mint(address to, uint256 id) internal virtual
|
|
```
|
|
|
|
Mints token `id` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must not exist.
|
|
- `to` cannot be the zero address.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _mintAndSetExtraDataUnchecked(address,uint256,uint96)
|
|
|
|
```solidity
|
|
function _mintAndSetExtraDataUnchecked(address to, uint256 id, uint96 value)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Mints token `id` to `to`, and updates the extra data for token `id` to `value`.
|
|
Does NOT check if token `id` already exists (assumes `id` is auto-incrementing).
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `to` cannot be the zero address.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _safeMint(address,uint256)
|
|
|
|
```solidity
|
|
function _safeMint(address to, uint256 id) internal virtual
|
|
```
|
|
|
|
Equivalent to `_safeMint(to, id, "")`.
|
|
|
|
### _safeMint(address,uint256,bytes)
|
|
|
|
```solidity
|
|
function _safeMint(address to, uint256 id, bytes memory data)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Mints token `id` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must not exist.
|
|
- `to` cannot be the zero address.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`IERC721Receiver-onERC721Received`, which is called upon a safe transfer.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
## Internal Burn Functions
|
|
|
|
### _burn(uint256)
|
|
|
|
```solidity
|
|
function _burn(uint256 id) internal virtual
|
|
```
|
|
|
|
Equivalent to `_burn(address(0), id)`.
|
|
|
|
### _burn(address,uint256)
|
|
|
|
```solidity
|
|
function _burn(address by, uint256 id) internal virtual
|
|
```
|
|
|
|
Destroys token `id`, using `by`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- If `by` is not the zero address,
|
|
it must be the owner of the token, or be approved to manage the token.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
## Internal Approval Functions
|
|
|
|
### _isApprovedOrOwner(address,uint256)
|
|
|
|
```solidity
|
|
function _isApprovedOrOwner(address account, uint256 id)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (bool result)
|
|
```
|
|
|
|
Returns whether `account` is the owner of token `id`, or is approved to manage it.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
|
|
### _getApproved(uint256)
|
|
|
|
```solidity
|
|
function _getApproved(uint256 id)
|
|
internal
|
|
view
|
|
virtual
|
|
returns (address result)
|
|
```
|
|
|
|
Returns the account approved to manage token `id`.
|
|
Returns the zero address instead of reverting if the token does not exist.
|
|
|
|
### _approve(address,uint256)
|
|
|
|
```solidity
|
|
function _approve(address account, uint256 id) internal virtual
|
|
```
|
|
|
|
Equivalent to `_approve(address(0), account, id)`.
|
|
|
|
### _approve(address,address,uint256)
|
|
|
|
```solidity
|
|
function _approve(address by, address account, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Sets `account` as the approved account to manage token `id`, using `by`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- If `by` is not the zero address, `by` must be the owner
|
|
or an approved operator for the token owner.
|
|
|
|
Emits a `Approval` event.
|
|
|
|
### _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 an `ApprovalForAll` event.
|
|
|
|
## Internal Transfer Functions
|
|
|
|
### _transfer(address,address,uint256)
|
|
|
|
```solidity
|
|
function _transfer(address from, address to, uint256 id) internal virtual
|
|
```
|
|
|
|
Equivalent to `_transfer(address(0), from, to, id)`.
|
|
|
|
### _transfer(address,address,address,uint256)
|
|
|
|
```solidity
|
|
function _transfer(address by, address from, address to, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Transfers token `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- `from` must be the owner of the token.
|
|
- `to` cannot be the zero address.
|
|
- If `by` is not the zero address,
|
|
it must be the owner of the token, or be approved to manage the token.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _safeTransfer(address,address,uint256)
|
|
|
|
```solidity
|
|
function _safeTransfer(address from, address to, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Equivalent to `_safeTransfer(from, to, id, "")`.
|
|
|
|
### _safeTransfer(address,address,uint256,bytes)
|
|
|
|
```solidity
|
|
function _safeTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
bytes memory data
|
|
) internal virtual
|
|
```
|
|
|
|
Transfers token `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- `from` must be the owner of the token.
|
|
- `to` cannot be the zero address.
|
|
- The caller must be the owner of the token, or be approved to manage the token.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`IERC721Receiver-onERC721Received`, which is called upon a safe transfer.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _safeTransfer(address,address,address,uint256)
|
|
|
|
```solidity
|
|
function _safeTransfer(address by, address from, address to, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Equivalent to `_safeTransfer(by, from, to, id, "")`.
|
|
|
|
### _safeTransfer(address,address,address,uint256,bytes)
|
|
|
|
```solidity
|
|
function _safeTransfer(
|
|
address by,
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
bytes memory data
|
|
) internal virtual
|
|
```
|
|
|
|
Transfers token `id` from `from` to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- Token `id` must exist.
|
|
- `from` must be the owner of the token.
|
|
- `to` cannot be the zero address.
|
|
- If `by` is not the zero address,
|
|
it must be the owner of the token, or be approved to manage the token.
|
|
- If `to` refers to a smart contract, it must implement
|
|
`IERC721Receiver-onERC721Received`, which is called upon a safe transfer.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
## Hooks For Overriding
|
|
|
|
### _beforeTokenTransfer(address,address,uint256)
|
|
|
|
```solidity
|
|
function _beforeTokenTransfer(address from, address to, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Hook that is called before any token transfers, including minting and burning.
|
|
|
|
### _afterTokenTransfer(address,address,uint256)
|
|
|
|
```solidity
|
|
function _afterTokenTransfer(address from, address to, uint256 id)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Hook that is called after any token transfers, including minting and burning. |