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>
7.4 KiB
ERC20
Simple ERC20 + EIP-2612 implementation.
Note:
- The ERC20 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.
- The
permitfunction uses the ecrecover precompile (0x1).
If you are overriding:
- NEVER violate the ERC20 invariant:
the total sum of all balances must be equal to
totalSupply(). - 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
TotalSupplyOverflow()
error TotalSupplyOverflow()
The total supply has overflowed.
AllowanceOverflow()
error AllowanceOverflow()
The allowance has overflowed.
AllowanceUnderflow()
error AllowanceUnderflow()
The allowance has underflowed.
InsufficientBalance()
error InsufficientBalance()
Insufficient balance.
InsufficientAllowance()
error InsufficientAllowance()
Insufficient allowance.
InvalidPermit()
error InvalidPermit()
The permit is invalid.
PermitExpired()
error PermitExpired()
The permit has expired.
Permit2AllowanceIsFixedAtInfinity()
error Permit2AllowanceIsFixedAtInfinity()
The allowance of Permit2 is fixed at infinity.
Events
Transfer(address,address,uint256)
event Transfer(address indexed from, address indexed to, uint256 amount)
Emitted when amount tokens is transferred from from to to.
Approval(address,address,uint256)
event Approval(
address indexed owner, address indexed spender, uint256 amount
)
Emitted when amount tokens is approved by owner to be used by spender.
Constants
_PERMIT2
address internal constant _PERMIT2 =
0x000000000022D473030F116dDEE9F6B43aC78BA3
The canonical Permit2 address.
For signature-based allowance granting for single transaction ERC20 transferFrom.
Enabled by default. To disable, override _givePermit2InfiniteAllowance().
Github
Etherscan
ERC20
totalSupply()
function totalSupply() public view virtual returns (uint256 result)
Returns the amount of tokens in existence.
balanceOf(address)
function balanceOf(address owner)
public
view
virtual
returns (uint256 result)
Returns the amount of tokens owned by owner.
allowance(address,address)
function allowance(address owner, address spender)
public
view
virtual
returns (uint256 result)
Returns the amount of tokens that spender can spend on behalf of owner.
approve(address,uint256)
function approve(address spender, uint256 amount)
public
virtual
returns (bool)
Sets amount as the allowance of spender over the caller's tokens.
Emits a Approval event.
transfer(address,uint256)
function transfer(address to, uint256 amount)
public
virtual
returns (bool)
Transfer amount tokens from the caller to to.
Requirements:
frommust at least haveamount.
Emits a Transfer event.
transferFrom(address,address,uint256)
function transferFrom(address from, address to, uint256 amount)
public
virtual
returns (bool)
Transfers amount tokens from from to to.
Note: Does not update the allowance if it is the maximum uint256 value.
Requirements:
frommust at least haveamount.- The caller must have at least
amountof allowance to transfer the tokens offrom.
Emits a Transfer event.
EIP-2612
_constantNameHash()
function _constantNameHash()
internal
view
virtual
returns (bytes32 result)
For more performance, override to return the constant value
of keccak256(bytes(name())) if name() will never change.
_versionHash()
function _versionHash() internal view virtual returns (bytes32 result)
If you need a different value, override this function.
_incrementNonce(address)
function _incrementNonce(address owner) internal virtual
For inheriting contracts to increment the nonce.
nonces(address)
function nonces(address owner)
public
view
virtual
returns (uint256 result)
Returns the current nonce for owner.
This value is used to compute the signature for EIP-2612 permit.
permit(address,address,uint256,uint256,uint8,bytes32,bytes32)
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual
Sets value as the allowance of spender over the tokens of owner,
authorized by a signed approval by owner.
Emits a Approval event.
DOMAIN_SEPARATOR()
function DOMAIN_SEPARATOR() public view virtual returns (bytes32 result)
Returns the EIP-712 domain separator for the EIP-2612 permit.
Internal Mint Functions
_mint(address,uint256)
function _mint(address to, uint256 amount) internal virtual
Mints amount tokens to to, increasing the total supply.
Emits a Transfer event.
Internal Burn Functions
_burn(address,uint256)
function _burn(address from, uint256 amount) internal virtual
Burns amount tokens from from, reducing the total supply.
Emits a Transfer event.
Internal Transfer Functions
_transfer(address,address,uint256)
function _transfer(address from, address to, uint256 amount)
internal
virtual
Moves amount of tokens from from to to.
Internal Allowance Functions
_spendAllowance(address,address,uint256)
function _spendAllowance(address owner, address spender, uint256 amount)
internal
virtual
Updates the allowance of owner for spender based on spent amount.
_approve(address,address,uint256)
function _approve(address owner, address spender, uint256 amount)
internal
virtual
Sets amount as the allowance of spender over the tokens of owner.
Emits a Approval event.
Hooks To Override
_beforeTokenTransfer(address,address,uint256)
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
virtual
Hook that is called before any transfer of tokens.
This includes minting and burning.
_afterTokenTransfer(address,address,uint256)
function _afterTokenTransfer(address from, address to, uint256 amount)
internal
virtual
Hook that is called after any transfer of tokens.
This includes minting and burning.
Permit2
_givePermit2InfiniteAllowance()
function _givePermit2InfiniteAllowance()
internal
view
virtual
returns (bool)
Returns whether to fix the Permit2 contract's allowance at infinity.
This value should be kept constant after contract initialization,
or else the actual allowance values may not match with the Approval events.
For best performance, return a compile-time constant for zero-cost abstraction.