Skip to main content

ERC-20 Permit Facet

Implements EIP-2612 permit for ERC-20 within a diamond

Key Features
  • Implements EIP-2612 permit for off-chain approvals.
  • Manages nonces within diamond storage for replay protection.
  • Exposes DOMAIN_SEPARATOR for signature encoding.
  • Includes exportSelectors for 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

Definition
struct ERC20MetadataStorage {
string name;
}

ERC20Storage

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

NoncesStorage

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

State Variables

PropertyTypeDescription
ERC20_METADATA_STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc20.metadata"))
ERC20_STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc20"))
STORAGE_POSITIONbytes32Diamond 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.

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.

exportSelectors

Exports the function selectors of the ERC20PermitFacet This function is use as a selector discovery mechanism for diamonds

function exportSelectors() external pure returns (bytes memory);

Returns:

PropertyTypeDescription
-bytesselectors The exported function selectors of the ERC20PermitFacet

Events

Errors

Best Practices

Best Practice
  • 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

Security

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.

Was this helpful?
Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.