ERC-20 Transfer Facet
ERC-20 token transfers within a diamond
- Exposes external
transferandtransferFromfunctions for diamond interaction. - Manages token balances and allowances using shared diamond storage.
- Emits standard
Transferevents for off-chain monitoring. - Utilizes custom errors for clear revert reasons.
Overview
This facet implements ERC-20 token transfer functionality, exposing external functions for token movement within a diamond. It accesses shared diamond storage to manage balances and allowances, adhering to the ERC-2535 standard for upgradeability. Developers integrate this facet to enable token operations via the diamond proxy.
Storage
ERC20Storage
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("erc20")) |
Functions
transfer
Transfers tokens to another address. Emits a Transfer event.
Parameters:
| Property | Type | Description |
|---|---|---|
_to | address | The address to receive the tokens. |
_value | uint256 | The amount of tokens to transfer. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the transfer was successful. |
transferFrom
Transfers tokens on behalf of another account, provided sufficient allowance exists. Emits a Transfer event and decreases the spender's allowance.
Parameters:
| Property | Type | Description |
|---|---|---|
_from | address | The address to transfer tokens from. |
_to | address | The address to transfer tokens to. |
_value | uint256 | The amount of tokens to transfer. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the transfer was successful. |
exportSelectors
Exports the function selectors of the ERC20TransferFacet This function is use as a selector discovery mechanism for diamonds
Returns:
| Property | Type | Description |
|---|---|---|
- | bytes | selectors The exported function selectors of the ERC20TransferFacet |
Events
Errors
Best Practices
- Ensure the ERC20Storage struct is correctly initialized in diamond storage.
- Verify that allowances are managed correctly before calling
transferFrom. - Use the
exportSelectorsfunction during diamond upgrades to discover facet selectors.
Security Considerations
Functions are protected by Compose's access control mechanisms. Input validation is performed for recipient addresses and transfer values. The transferFrom function requires sufficient allowance to be set by the token owner. Reentrancy is mitigated by adhering to the checks-effects-interactions pattern.