Skip to main content

ERC20PermitFacet

EIP-2612 permit functionality for ERC-20 tokens

Key Features
  • Implements EIP-2612 permit function for off-chain approvals.
  • Exposes nonces and DOMAIN_SEPARATOR for signature verification.
  • Integrates with Compose diamond storage pattern.

Overview

This facet implements EIP-2612 permit functionality, enabling off-chain approvals for ERC-20 token transfers within a Compose diamond. It exposes the permit function to set allowances via signatures and provides nonces and DOMAIN_SEPARATOR for signature validation. Developers integrate this facet to allow users to grant token spending permissions without direct on-chain transactions.


Storage

ERC20Storage

Definition
struct ERC20Storage {
mapping(address owner => uint256 balance) balanceOf;
uint256 totalSupply;
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
uint8 decimals;
string name;
}

ERC20PermitStorage

Definition
struct ERC20PermitStorage {
mapping(address owner => uint256) nonces;
}

State Variables

PropertyTypeDescription
ERC20_STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("compose.erc20"))
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("compose.erc20.permit"))

Functions

nonces

Returns the current nonce for an owner. This value changes each time a permit is used.

function nonces(address _owner) external view returns (uint256);

Parameters:

PropertyTypeDescription
_owneraddressThe address of the owner.

Returns:

PropertyTypeDescription
-uint256The 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.

function DOMAIN_SEPARATOR() external view returns (bytes32);

Returns:

PropertyTypeDescription
-bytes32The domain separator.

permit

Sets the allowance for a spender via a signature. This function implements EIP-2612 permit functionality.

function permit(
address _owner,
address _spender,
uint256 _value,
uint256 _deadline,
uint8 _v,
bytes32 _r,
bytes32 _s
) external;

Parameters:

PropertyTypeDescription
_owneraddressThe address of the token owner.
_spenderaddressThe address of the spender.
_valueuint256The amount of tokens to approve.
_deadlineuint256The deadline for the permit (timestamp).
_vuint8The recovery byte of the signature.
_rbytes32The r value of the signature.
_sbytes32The s value of the signature.

Events

Errors

Best Practices

Best Practice
  • Integrate the ERC20PermitFacet during diamond initialization.
  • Ensure DOMAIN_SEPARATOR is correctly configured for the specific chain and diamond.
  • Validate signature parameters (_v, _r, _s) before relaying them to the permit function.

Security Considerations

Security

The permit function allows setting allowances via signatures. Ensure correct implementation of signature verification logic by consumers of this facet. Reentrancy is not applicable as permit only modifies storage. Input validation for spender and value is handled internally. Follow standard Solidity security practices.

Was this helpful?
Last updated: