ERC-20 Transfer Module
Internal ERC-20 token transfer functions
- Internal functions for token transfers, designed for use within custom facets.
- Utilizes diamond storage (EIP-8042) for state management.
- No external dependencies, promoting composability.
- Emits
Transferevents upon successful transfers.
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 for executing ERC-20 token transfers and transfers from an allowance. Facets can import and utilize these functions to manage token balances and operations within the diamond storage pattern. Changes to balances are immediately reflected across all facets accessing the same storage.
Storage
ERC20Storage
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc20")) |
Functions
getStorage
Returns a pointer to the ERC-20 storage struct. Uses inline assembly to bind the storage struct to the fixed storage position.
Returns:
| Property | Type | Description |
|---|---|---|
s | ERC20Storage | The ERC-20 storage struct. |
transfer
Transfers tokens from the caller to another address. Updates balances directly without allowance mechanism.
Parameters:
| Property | Type | Description |
|---|---|---|
_to | address | The address to send tokens to. |
_value | uint256 | The number of tokens to transfer. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the transfer was successful. |
transferFrom
Transfers tokens from one address to another using an allowance. Deducts the spender's allowance and updates balances.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The address to send tokens from. |
_to | address | The address to send tokens to. |
_value | uint256 | The number of tokens to transfer. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the transfer was successful. |
Events
Errors
Best Practices
- Ensure the caller has sufficient balance before calling
transfer. - Verify the spender has sufficient allowance before calling
transferFrom. - Handle potential errors such as
ERC20InvalidReceiverorERC20InsufficientBalance. - Use the
getStorage()function to access the storage pointer for direct inspection if necessary.
Integration Notes
This module reads and writes to diamond storage at the STORAGE_POSITION, identified by keccak256("erc20"). The ERC20Storage struct, containing fields like totalSupply, is managed here. All functions operate directly on this shared storage, ensuring that balance updates are immediately visible to any facet that accesses the same storage slot. The getStorage() function provides a direct pointer to this struct via inline assembly.