ERC-20 Approve Facet
Approves token spending on behalf of an owner
- Exposes
approvefunction for managing token allowances. - Emits
Approvalevent 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
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 up to a certain amount of tokens on behalf of the caller. Emits an Approval event.
Parameters:
| Property | Type | Description |
|---|---|---|
_spender | address | The address approved to spend tokens. |
_value | uint256 | The number of tokens to approve. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the approval was successful. |
exportSelectors
Exports the function selectors of the ERC20ApproveFacet This function is use as a selector discovery mechanism for diamonds
Returns:
| Property | Type | Description |
|---|---|---|
- | bytes | selectors The exported function selectors of the ERC20ApproveFacet |
Events
Errors
Best Practices
- Ensure the
ERC20ApproveFacetis properly initialized within the diamond. - Verify that the caller has the necessary permissions to approve token spending.
- Listen for
Approvalevents to track token allowances off-chain.
Security Considerations
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.