ERC-20 Permit Module
Manage ERC-2612 permit signatures and domain separators
- Provides internal functions for ERC-2612
permitfunctionality. - Manages unique domain separators to prevent replay attacks.
- Interacts with diamond storage for allowance updates.
- No external dependencies, promoting composability.
This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.
Overview
This module manages ERC-2612 permit logic, including domain separators and signature validation for allowance approvals. Facets can use this module to implement the permit functionality, ensuring unique domain separators per contract and chain ID to prevent replay attacks. Changes to allowances made via permit are recorded in diamond storage and are visible to all facets.
Storage
ERC20MetadataStorage
storage-location: erc8042:erc20.metadata
ERC20Storage
storage-location: erc8042:erc20
NoncesStorage
storage-location: erc8042:nonces
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
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. |
getERC20MetadataStorage
getERC20Storage
getPermitStorage
permit
Validates a permit signature and sets allowance. Emits Approval event; must be emitted by the calling facet/contract.
Parameters:
| Property | Type | Description |
|---|---|---|
_owner | address | Token owner. |
_spender | address | Token spender. |
_value | uint256 | Allowance value. |
_deadline | uint256 | Permit's time deadline. |
Events
Errors
Best Practices
- Ensure the calling facet correctly emits the
Approvalevent after a successfulpermitcall. - Verify that the
DOMAIN_SEPARATORused by the module is correctly initialized and unique to the contract and chain ID. - Handle the
ERC2612InvalidSignatureandERC20InvalidSpendererrors to provide appropriate feedback to users.
Integration Notes
This module interacts with diamond storage to manage permit-related state, specifically related to allowances and nonces. The permit function updates allowance values directly in the shared diamond storage. The DOMAIN_SEPARATOR function is crucial for signature verification and ensures that permits are specific to the contract instance and chain. Changes made via the permit function are immediately visible to any other facet that reads the allowance state from the diamond storage.