ERC-20 Permit Facet
Implements EIP-2612 permit for ERC-20 within a diamond
- Implements EIP-2612 permit for off-chain approvals.
- Manages nonces within diamond storage for replay protection.
- Exposes
DOMAIN_SEPARATORfor signature encoding. - Includes
exportSelectorsfor diamond facet discovery.
Overview
This facet implements EIP-2612 permit functionality, allowing users to grant allowances via signed messages. It integrates with diamond storage for nonce management and provides the necessary domain separator for signature validation. Developers add this facet to enable off-chain approval for token transfers.
Storage
ERC20MetadataStorage
ERC20Storage
NoncesStorage
State Variables
| Property | Type | Description |
|---|---|---|
ERC20_METADATA_STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc20.metadata")) |
ERC20_STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc20")) |
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("nonces")) |
Functions
nonces
Returns the current nonce for an owner. This value changes each time a permit is used.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | The address of the owner. |
Returns:
| Property | Type | Description |
|---|---|---|
- | uint256 | The current nonce. |
DOMAIN_SEPARATOR
Returns the domain separator used in the encoding of the signature for permit. This value is unique to a contract and chain ID combination to prevent replay attacks.
Returns:
| Property | Type | Description |
|---|---|---|
- | bytes32 | The domain separator. |
permit
Sets the allowance for a spender via a signature. This function implements EIP-2612 permit functionality.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | The address of the token owner. |
_spender | address | The address of the spender. |
_value | uint256 | The amount of tokens to approve. |
_deadline | uint256 | The deadline for the permit (timestamp). |
_v | uint8 | The recovery byte of the signature. |
_r | bytes32 | The r value of the signature. |
_s | bytes32 | The s value of the signature. |
exportSelectors
Exports the function selectors of the ERC20PermitFacet This function is use as a selector discovery mechanism for diamonds
Returns:
| Property | Type | Description |
|---|---|---|
- | bytes | selectors The exported function selectors of the ERC20PermitFacet |
Events
Errors
Best Practices
- Initialize the ERC20PermitMod module before calling functions from this facet.
- Ensure the correct domain separator is used when generating signatures off-chain.
- Verify that the nonce for the owner has been incremented after a successful permit operation.
Security Considerations
The permit function validates signatures using owner nonces and the domain separator to prevent replay attacks. Ensure off-chain signature generation uses the correct parameters. The nonces function increments the nonce after a successful permit call, which is critical for subsequent permit operations. Follow standard Solidity security practices for input validation and access control.