Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-223, a token transaction model based on ERC-20 and inspired by native ETH transfers.

This article translates, summarizes, and expands on the following specification.

More ERC explainers:

Overview

The proposal adds a receiver callback called tokenReceived to the token transfer model. A recipient contract can be notified when it receives tokens, creating behavior analogous to native ETH transfers.

For an introduction to ERC-20, see the following article.

https://chaldene.net/erc20

Motivation

The proposal provides a communication model for interactions between tokens and contracts.

  1. Notify a recipient contract when a token transfer takes place.
  2. Improve the gas efficiency of depositing tokens into a contract.
  3. Associate additional data with a token transfer.

This model aims to simplify token-contract interactions and provide a consistent way to handle transfer-related information.

Specification

Contracts receiving ERC-223 tokens must support the tokenReceived callback or the applicable fallback behavior described in the proposal. A transfer that cannot be handled must fail rather than silently deposit tokens into an incompatible recipient.

This is intended to reduce accidental deposits, not to eliminate every possible security risk.

Token contract

totalSupply

javascript
function totalSupply() view returns (uint256)

Overview

Returns the total token supply, with the same purpose as in ERC-20.

Returns

  • totalSupply — the total supply.

name

javascript
function name() view returns (string memory)

Overview

Returns the token name, with the same purpose as in ERC-20.

Returns

  • name — the token name.

symbol

javascript
function symbol() view returns (string memory)

Overview

Returns the token symbol, with the same purpose as in ERC-20.

Returns

  • symbol — the token symbol.

decimals

javascript
function decimals() view returns (uint8)

Overview

Returns the number of decimal places used for displaying token amounts, as in ERC-20.

Returns

  • decimals — the number of decimal places.

standard

javascript
function standard() view returns (string memory)

Overview

Returns an identifier for the token standard. For ERC-223, the identifier is 223.

Returns

  • standard — the standard identifier, represented as the string 223 for ERC-223.

balanceOf

javascript
function balanceOf(address _owner) view returns (uint256)

Overview

Returns the token balance of the specified owner, as in ERC-20.

Parameters

  • _owner — the account to query.

Returns

  • balance — the account's token balance.

transfer

javascript
function transfer(address _to, uint _value) returns (bool);
function transfer(address _to, uint _value, bytes calldata _data) returns (bool);

Overview

Transfers tokens to the specified address.

Details

The function sends _value tokens to _to. When the recipient is a contract, the token contract must invoke its tokenReceived handler, subject to the fallback behavior described below. An unsupported recipient must cause the transaction to fail.

For an EOA recipient, the tokens are sent without invoking that callback. The transfer can also carry optional _data, which may be empty and adds gas costs when used.

Parameters

  • _to — the recipient address.
  • _value — the amount of tokens to transfer.

Returns

  • bool — whether the transfer succeeded; an unsuccessful operation may revert.

Events

Transfer

javascript
event Transfer(address indexed _from, address indexed _to, uint256 _value, bytes _data)

Overview

Emitted when tokens are transferred. It serves a purpose similar to the ERC-20 Transfer event, but the additional data parameter changes the event signature and therefore prevents direct event-topic compatibility.

Parameters

  • _from — the sending address.
  • _to — the recipient address.
  • _value — the transferred amount.
  • _data — optional additional information associated with the transfer.

ERC-223 Token Receiver

tokenReceived

javascript
function tokenReceived(address _from, uint _value, bytes calldata _data) returns (bytes4)

Overview

A callback invoked by the token contract when tokens are transferred to a recipient contract.

Details

Inside this function, msg.sender is the token contract, allowing the receiver to filter accepted tokens by their contract address.

The callback returns the value 0x8943ec02 in the described interface. The original proposal also describes a fallback-based handling path where that specific return value is not required.

Parameters

  • _from — the sending address.
  • _value — the transferred token amount.
  • _data — data associated with the transfer.

Returns

  • bytes4 — the specified acceptance value or the appropriate value under the applicable receiver behavior.

Additional explanation

The proposed communication model requires a handler to run at the receiving contract during a transfer. A transfer to a contract that cannot handle it must fail.

ERC-223 follows a push transaction model: the sender initiates the transfer, and the receiver processes it. A deposit can be completed in one transaction, whereas a conventional ERC-20 deposit often requires both approval and a transferFrom call.

The original article provides these illustrative gas figures:

  • ERC-20 deposit
    • approve — approximately 46 gas as printed in the original; this appears to omit the thousands unit and should not be treated as a verified measurement.
    • transferFrom — approximately 75K gas.
  • ERC-223 deposit
    • Transaction and receiver processing — approximately 54K gas.

Actual gas consumption depends on the implementation and execution context.

The receiver can process a transfer or reject it when it is inappropriate. This lets applications use the same transfer interface for EOAs and contracts, reducing the chance of choosing an incorrect deposit mechanism.

ERC-20 offers direct transfer and the approve-plus-transferFrom pattern. A direct ERC-20 transfer does not automatically notify a recipient contract. Consequently, the recipient's application-level accounting may not recognize the deposit, potentially leaving tokens inaccessible.

ERC-223 aims to reduce that risk by handling the recipient interaction automatically. Its deposit pattern resembles an ETH deposit, using a single transfer call instead of a two-step approval-and-transfer sequence.

The optional bytes calldata _data parameter also carries information with the transaction. Applications can use this data to describe additional receiver-side operations or record information in public transaction logs when needed.

Compatibility

The interface is based on ERC-20, with many functions serving the same purpose. However, the overload transfer(address, uint256, bytes calldata) is not part of the ERC-20 interface, and applications should not assume complete ERC-20 compatibility.

ERC-20 tokens can be sent directly to an EOA. Contracts commonly accept deposits through the approve-plus-transferFrom pattern. A direct transfer to a contract may not be recognized by its deposit logic.

The following ERC-20 example accepts tokenA through its deposit function but cannot prevent other ERC-20 tokens from being transferred directly to its address. If tokenA is sent directly using transfer, the sender's balance decreases without incrementing the receiver's deposits mapping.

The source reports that, as of May 9, 2023, approximately $210 million worth of tokens across 50 ERC-20 tokens on Ethereum mainnet had been lost in this manner. This is a historical figure, not a current estimate.

javascript
contract ERC20Receiver
{
    address tokenA;
    mapping (address => uint256) deposits;
    function deposit(uint _value, address _token) public
    {
        require(_token == tokenA);
        IERC20(_token).transferFrom(msg.sender, address(this), _value);
        deposits[msg.sender] += _value;
    }
}

ERC-223 uses the transfer method for both EOAs and contracts. The following receiver filters incoming tokens, accepting tokenA and rejecting other ERC-223 tokens.

javascript
contract ERC223Receiver
{
    address tokenA;
    mapping (address => uint256) deposits;
    function tokenReceived(address _from, uint _value, bytes memory _data) public returns (bytes4)
    {
        require(msg.sender == tokenA);
        deposits[_from] += _value;
        return 0x8943ec02;
    }
}

This provides a different transfer model from ERC-20 by requiring consistent handling between the sender and receiver.

Security considerations

The transfer model resembles native ETH transfers, including external calls to receiver contracts. The original article refers to replay attacks here, but the linked example concerns reentrancy, which is the relevant concern when a receiver callback can call back into the token contract.

For a discussion of reentrancy attacks, see:

https://chaldene.net/solidity-reentrancy-attack

Reference implementation

javascript
pragma solidity ^0.8.19;

library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * This test is non-exhaustive, and there may be false-negatives: during the
     * execution of a contract's constructor, its address will be reported as
     * not containing a contract.
     *
     * > It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies in extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }
}

abstract contract IERC223Recipient {
/**
 * @dev Standard ERC-223 receiving function that will handle incoming token transfers.
 *
 * @param _from  Token sender address.
 * @param _value Amount of tokens.
 * @param _data  Transaction metadata.
 */
    function tokenReceived(address _from, uint _value, bytes memory _data) public virtual returns (bytes4);
}

/**
 * @title Reference implementation of the ERC223 standard token.
 */
contract ERC223Token {

     /**
     * @dev Event that is fired on successful transfer.
     */
    event Transfer(address indexed from, address indexed to, uint value, bytes data);

    string  private _name;
    string  private _symbol;
    uint8   private _decimals;
    uint256 private _totalSupply;
    
    mapping(address => uint256) private balances; // List of user balances.

    /**
     * @dev Sets the values for {name} and {symbol}, initializes {decimals} with
     * a default value of 18.
     *
     * To select a different value for {decimals}, use {_setupDecimals}.
     *
     * All three of these values are immutable: they can only be set once during
     * construction.
     */
     
    constructor(string memory new_name, string memory new_symbol, uint8 new_decimals)
    {
        _name     = new_name;
        _symbol   = new_symbol;
        _decimals = new_decimals;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view returns (string memory)
    {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view returns (string memory)
    {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5,05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC223} uses, unless {_setupDecimals} is
     * called.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC223-balanceOf} and {IERC223-transfer}.
     */
    function decimals() public view returns (uint8)
    {
        return _decimals;
    }

    /**
     * @dev See {IERC223-totalSupply}.
     */
    function totalSupply() public view returns (uint256)
    {
        return _totalSupply;
    }

    /**
     * @dev See {IERC223-standard}.
     */
    function standard() public view returns (string memory)
    {
        return "223";
    }

    
    /**
     * @dev Returns balance of the `_owner`.
     *
     * @param _owner   The address whose balance will be returned.
     * @return balance Balance of the `_owner`.
     */
    function balanceOf(address _owner) public view returns (uint256)
    {
        return balances[_owner];
    }
    
    /**
     * @dev Transfer the specified amount of tokens to the specified address.
     *      Invokes the `tokenFallback` function if the recipient is a contract.
     *      The token transfer fails if the recipient is a contract
     *      but does not implement the `tokenFallback` function
     *      or the fallback function to receive funds.
     *
     * @param _to    Receiver address.
     * @param _value Amount of tokens that will be transferred.
     * @param _data  Transaction metadata.
     */
    function transfer(address _to, uint _value, bytes calldata _data) public returns (bool success)
    {
        // Standard function transfer similar to ERC20 transfer with no _data .
        // Added due to backwards compatibility reasons .
        balances[msg.sender] = balances[msg.sender] - _value;
        balances[_to] = balances[_to] + _value;
        if(Address.isContract(_to)) {
            IERC223Recipient(_to).tokenReceived(msg.sender, _value, _data);
        }
        emit Transfer(msg.sender, _to, _value, _data);
        return true;
    }
    
    /**
     * @dev Transfer the specified amount of tokens to the specified address.
     *      This function works the same with the previous one
     *      but doesn't contain `_data` param.
     *      Added due to backwards compatibility reasons.
     *
     * @param _to    Receiver address.
     * @param _value Amount of tokens that will be transferred.
     */
    function transfer(address _to, uint _value) public returns (bool success)
    {
        bytes memory _empty = hex"00000000";
        balances[msg.sender] = balances[msg.sender] - _value;
        balances[_to] = balances[_to] + _value;
        if(Address.isContract(_to)) {
            IERC223Recipient(_to).tokenReceived(msg.sender, _value, _empty);
        }
        emit Transfer(msg.sender, _to, _value, _empty);
        return true;
    }
}

Reference: https://eips.ethereum.org/EIPS/eip-223

Citation

Dexaran (@Dexaran) dexaran@ethereumclassic.org, "ERC-223: Token with transaction handling model," Ethereum Improvement Proposals, no. 223, May 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-223.

Conclusion

ERC-223 proposes an ERC-20-inspired token transaction model that behaves more like native ETH transfers by supporting recipient-side handling.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish material on the following platforms.

https://chaldene.net/