Skip to main content

ERC-20 Approve Module

Manages ERC-20 token allowances for spenders

Key Features
  • Exposes internal functions for use within custom facets.
  • Manages ERC-20 allowances using the diamond storage pattern (EIP-8042).
  • Emits an Approval event upon successful allowance updates.
  • Includes a custom error ERC20InvalidSpender for invalid spender addresses.
Module Usage

This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.

Overview

This module provides internal functions to manage ERC-20 token allowances. Facets can import this module to approve spenders on behalf of token owners using shared diamond storage. Changes to allowances are immediately visible to all facets interacting with the same storage slot.

Storage

ERC20Storage

ERC-8042 compliant storage struct for ERC20 token data. storage-location: erc8042:erc20

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

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("erc20"))

Functions

approve

Approves a spender to transfer tokens on behalf of the caller. Sets the allowance for the spender.

function approve(address _spender, uint256 _value) returns (bool);

Parameters:

PropertyTypeDescription
_spenderaddressThe address to approve for spending.
_valueuint256The amount of tokens to approve.

Returns:

PropertyTypeDescription
-boolTrue if the approval was successful.

getStorage

Returns the ERC20 storage struct from the predefined diamond storage slot. Uses inline assembly to set the storage slot reference.

function getStorage() pure returns (ERC20Storage storage s);

Returns:

PropertyTypeDescription
sERC20StorageThe ERC20 storage struct reference.

Events

Errors

Best Practices

Best Practice
  • Ensure the caller has sufficient tokens before calling approve if simulating a full ERC-20 behavior.
  • Handle the ERC20InvalidSpender error if the spender address is zero.
  • Verify access control is properly managed by the calling facet before invoking approve.

Integration Notes

Shared Storage

This module utilizes the diamond storage pattern at STORAGE_POSITION, keyed by keccak256("erc20"). The approve function modifies the allowance mapping within this shared storage. The getStorage function provides direct access to the ERC20Storage struct, allowing other facets to inspect the allowance state. All modifications are immediately visible to any facet that reads from the same storage slot.

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.