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>
326 lines
6.6 KiB
Markdown
326 lines
6.6 KiB
Markdown
# ERC6909
|
|
|
|
Simple EIP-6909 implementation.
|
|
|
|
|
|
<b>Note:</b>
|
|
|
|
The ERC6909 standard allows minting and transferring to and from the zero address,
|
|
minting and transferring zero tokens, as well as self-approvals.
|
|
For performance, this implementation WILL NOT revert for such actions.
|
|
Please add any checks with overrides if desired.
|
|
|
|
<b>If you are overriding:</b>
|
|
- Make sure all variables written to storage are properly cleaned
|
|
(e.g. the bool value for `isOperator` 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
|
|
|
|
### InsufficientBalance()
|
|
|
|
```solidity
|
|
error InsufficientBalance()
|
|
```
|
|
|
|
Insufficient balance.
|
|
|
|
### InsufficientPermission()
|
|
|
|
```solidity
|
|
error InsufficientPermission()
|
|
```
|
|
|
|
Insufficient permission to perform the action.
|
|
|
|
### BalanceOverflow()
|
|
|
|
```solidity
|
|
error BalanceOverflow()
|
|
```
|
|
|
|
The balance has overflowed.
|
|
|
|
## Events
|
|
|
|
### Transfer(address,address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
event Transfer(
|
|
address by,
|
|
address indexed from,
|
|
address indexed to,
|
|
uint256 indexed id,
|
|
uint256 amount
|
|
)
|
|
```
|
|
|
|
Emitted when `by` transfers `amount` of token `id` from `from` to `to`.
|
|
|
|
### OperatorSet(address,address,bool)
|
|
|
|
```solidity
|
|
event OperatorSet(
|
|
address indexed owner, address indexed operator, bool approved
|
|
)
|
|
```
|
|
|
|
Emitted when `owner` enables or disables `operator` to manage all of their tokens.
|
|
|
|
### Approval(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
event Approval(
|
|
address indexed owner,
|
|
address indexed spender,
|
|
uint256 indexed id,
|
|
uint256 amount
|
|
)
|
|
```
|
|
|
|
Emitted when `owner` approves `spender` to use `amount` of `id` token.
|
|
|
|
## ERC6909 Metadata
|
|
|
|
### tokenURI(uint256)
|
|
|
|
```solidity
|
|
function tokenURI(uint256 id) public view virtual returns (string memory);
|
|
```
|
|
|
|
Returns the Uniform Resource Identifier (URI) for token `id`.
|
|
|
|
## ERC6909
|
|
|
|
### balanceOf(address,uint256)
|
|
|
|
```solidity
|
|
function balanceOf(address owner, uint256 id)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 amount)
|
|
```
|
|
|
|
Returns the amount of token `id` owned by `owner`.
|
|
|
|
### allowance(address,address,uint256)
|
|
|
|
```solidity
|
|
function allowance(address owner, address spender, uint256 id)
|
|
public
|
|
view
|
|
virtual
|
|
returns (uint256 amount)
|
|
```
|
|
|
|
Returns the amount of token `id` that `spender` can spend on behalf of `owner`.
|
|
|
|
### isOperator(address,address)
|
|
|
|
```solidity
|
|
function isOperator(address owner, address spender)
|
|
public
|
|
view
|
|
virtual
|
|
returns (bool status)
|
|
```
|
|
|
|
Checks if a `spender` is approved by `owner` to manage all of their tokens.
|
|
|
|
### transfer(address,uint256,uint256)
|
|
|
|
```solidity
|
|
function transfer(address to, uint256 id, uint256 amount)
|
|
public
|
|
payable
|
|
virtual
|
|
returns (bool)
|
|
```
|
|
|
|
Transfers `amount` of token `id` from the caller to `to`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- caller must at least have `amount`.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### transferFrom(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function transferFrom(address from, address to, uint256 id, uint256 amount)
|
|
public
|
|
payable
|
|
virtual
|
|
returns (bool)
|
|
```
|
|
|
|
Transfers `amount` of token `id` from `from` to `to`.
|
|
|
|
<b>Note:</b>
|
|
|
|
- Allowance is NOT deducted if it is `type(uint256).max`.
|
|
- Allowance is NOT deducted if `from` is an operator.
|
|
- For efficiency, allowance is deducted even if `from` is the caller.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `from` must at least have `amount` of token `id`.
|
|
- The caller must have at least `amount` of allowance to transfer the
|
|
tokens of `from` or approved as an operator.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### approve(address,uint256,uint256)
|
|
|
|
```solidity
|
|
function approve(address spender, uint256 id, uint256 amount)
|
|
public
|
|
payable
|
|
virtual
|
|
returns (bool)
|
|
```
|
|
|
|
Sets `amount` as the allowance of `spender` for the caller for token `id`.
|
|
|
|
Emits a `Approval` event.
|
|
|
|
### setOperator(address,bool)
|
|
|
|
```solidity
|
|
function setOperator(address operator, bool approved)
|
|
public
|
|
payable
|
|
virtual
|
|
returns (bool)
|
|
```
|
|
|
|
Sets whether `operator` is approved to manage the tokens of the caller.
|
|
|
|
Emits `OperatorSet` 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`.
|
|
|
|
## Internal Functions
|
|
|
|
### _mint(address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _mint(address to, uint256 id, uint256 amount) internal virtual
|
|
```
|
|
|
|
Mints `amount` of token `id` to `to`.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _burn(address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _burn(address from, uint256 id, uint256 amount) internal virtual
|
|
```
|
|
|
|
Burns `amount` token `id` from `from`.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _transfer(address,address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _transfer(
|
|
address by,
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount
|
|
) internal virtual
|
|
```
|
|
|
|
Transfers `amount` of token `id` from `from` to `to`.
|
|
|
|
<b>Note:</b>
|
|
|
|
- Allowance is NOT deducted if it is `type(uint256).max`.
|
|
- Allowance is NOT deducted if `from` is an operator.
|
|
- For efficiency, allowance is deducted even if `from` is `by`.
|
|
|
|
<b>Requirements:</b>
|
|
|
|
- `from` must at least have `amount` of token `id`.
|
|
- If `by` is not the zero address,
|
|
it must have at least `amount` of allowance to transfer the
|
|
tokens of `from` or approved as an operator.
|
|
|
|
Emits a `Transfer` event.
|
|
|
|
### _approve(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _approve(
|
|
address owner,
|
|
address spender,
|
|
uint256 id,
|
|
uint256 amount
|
|
) internal virtual
|
|
```
|
|
|
|
Sets `amount` as the allowance of `spender` for `owner` for token `id`.
|
|
|
|
Emits a `Approval` event.
|
|
|
|
### _setOperator(address,address,bool)
|
|
|
|
```solidity
|
|
function _setOperator(address owner, address operator, bool approved)
|
|
internal
|
|
virtual
|
|
```
|
|
|
|
Sets whether `operator` is approved to manage the tokens of `owner`.
|
|
|
|
Emits `OperatorSet` event.
|
|
|
|
## Hooks To Override
|
|
|
|
### _beforeTokenTransfer(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _beforeTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount
|
|
) internal virtual
|
|
```
|
|
|
|
Hook that is called before any transfer of tokens.
|
|
This includes minting and burning.
|
|
|
|
### _afterTokenTransfer(address,address,uint256,uint256)
|
|
|
|
```solidity
|
|
function _afterTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount
|
|
) internal virtual
|
|
```
|
|
|
|
Hook that is called after any transfer of tokens.
|
|
This includes minting and burning. |