Skip to main content

ERC-20 Transfer Module

Internal ERC-20 token transfer functions

Key Features
  • 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 Transfer events upon successful transfers.
Module Usage

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

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

getStorage

Returns a pointer to the ERC-20 storage struct. Uses inline assembly to bind the storage struct to the fixed storage position.

function getStorage() pure returns (ERC20Storage storage s);

Returns:

PropertyTypeDescription
sERC20StorageThe ERC-20 storage struct.

transfer

Transfers tokens from the caller to another address. Updates balances directly without allowance mechanism.

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

Parameters:

PropertyTypeDescription
_toaddressThe address to send tokens to.
_valueuint256The number of tokens to transfer.

Returns:

PropertyTypeDescription
-boolTrue if the transfer was successful.

transferFrom

Transfers tokens from one address to another using an allowance. Deducts the spender's allowance and updates balances.

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

Parameters:

PropertyTypeDescription
_fromaddressThe address to send tokens from.
_toaddressThe address to send tokens to.
_valueuint256The number of tokens to transfer.

Returns:

PropertyTypeDescription
-boolTrue if the transfer was successful.

Events

Errors

Best Practices

Best Practice
  • Ensure the caller has sufficient balance before calling transfer.
  • Verify the spender has sufficient allowance before calling transferFrom.
  • Handle potential errors such as ERC20InvalidReceiver or ERC20InsufficientBalance.
  • Use the getStorage() function to access the storage pointer for direct inspection if necessary.

Integration Notes

Shared Storage

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.

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.