Skip to main content

ERC-20 Transfer Facet

ERC-20 token transfers within a diamond

Key Features
  • Exposes external transfer and transferFrom functions for diamond interaction.
  • Manages token balances and allowances using shared diamond storage.
  • Emits standard Transfer events 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

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

transfer

Transfers tokens to another address. Emits a Transfer event.

function transfer(address _to, uint256 _value) external returns (bool);

Parameters:

PropertyTypeDescription
_toaddressThe address to receive the tokens.
_valueuint256The amount of tokens to transfer.

Returns:

PropertyTypeDescription
-boolTrue 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.

function transferFrom(address _from, address _to, uint256 _value) external returns (bool);

Parameters:

PropertyTypeDescription
_fromaddressThe address to transfer tokens from.
_toaddressThe address to transfer tokens to.
_valueuint256The amount of tokens to transfer.

Returns:

PropertyTypeDescription
-boolTrue if the transfer was successful.

exportSelectors

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

Events

Errors

Best Practices

Best Practice
  • Ensure the ERC20Storage struct is correctly initialized in diamond storage.
  • Verify that allowances are managed correctly before calling transferFrom.
  • Use the exportSelectors function during diamond upgrades to discover facet selectors.

Security Considerations

Security

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.

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.