ERC-20 Approve Module
Manages ERC-20 token allowances for spenders
- Exposes
internalfunctions for use within custom facets. - Manages ERC-20 allowances using the diamond storage pattern (EIP-8042).
- Emits an
Approvalevent upon successful allowance updates. - Includes a custom error
ERC20InvalidSpenderfor invalid spender addresses.
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
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_spender | address | The address to approve for spending. |
_value | uint256 | The amount of tokens to approve. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True 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.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC20Storage | The ERC20 storage struct reference. |
Events
Errors
Best Practices
- Ensure the caller has sufficient tokens before calling
approveif simulating a full ERC-20 behavior. - Handle the
ERC20InvalidSpendererror if the spender address is zero. - Verify access control is properly managed by the calling facet before invoking
approve.
Integration Notes
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.