Skip to main content

ERC-20 Approve Facet

Approves token spending on behalf of an owner

Key Features
  • Exposes approve function for managing token allowances.
  • Emits Approval event for off-chain tracking.
  • Interacts with diamond's shared storage for approval state.
  • Functions are callable through the diamond proxy.

Overview

This facet exposes external functions for approving token spending within a diamond. It interacts with shared diamond storage to manage approvals and emits events for off-chain consumption. Developers add this facet to enable token allowance functionality in a composable manner.

Storage

ERC20Storage

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 up to a certain amount of tokens on behalf of the caller. Emits an Approval event.

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

Parameters:

PropertyTypeDescription
_spenderaddressThe address approved to spend tokens.
_valueuint256The number of tokens to approve.

Returns:

PropertyTypeDescription
-boolTrue if the approval was successful.

exportSelectors

Exports the function selectors of the ERC20ApproveFacet 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 ERC20ApproveFacet

Events

Errors

Best Practices

Best Practice
  • Ensure the ERC20ApproveFacet is properly initialized within the diamond.
  • Verify that the caller has the necessary permissions to approve token spending.
  • Listen for Approval events to track token allowances off-chain.

Security Considerations

Security

The approve function is protected by access control, ensuring only the token owner can set allowances. Input validation for the _spender address is performed, reverting with ERC20InvalidSpender if it is the zero address. The function follows the checks-effects-interactions pattern to mitigate reentrancy risks. Developers must ensure the diamond's access control mechanisms are correctly configured for this facet.

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.