EIPs Explained

ERC-223 · Final · ERC · 2017-05-03

ERC-223: Token with transaction handling model

A fungible token standard that calls a tokenReceived hook on recipient contracts and reverts if they cannot handle tokens, so deposits work in one transaction and tokens are not lost in contracts.

What problem does it solve?

With ERC-20, a plain transfer to a contract gives the contract no notice. If the contract was not built to take tokens that way, the tokens simply sit there with no way out. Contracts that do accept deposits need the two-step approve + transferFrom pattern, which costs two transactions. Which method to use is left to the user, and choosing wrong loses funds.

ERC-223 makes tokens behave more like ether. There is one transfer for everyone. If the recipient is a contract, it is told about the deposit, and it can reject it.

How it works

  • The token keeps the usual totalSupply, balanceOf, and optional name/symbol/decimals with ERC-20 meaning.
  • transfer(to, value) and transfer(to, value, data) move tokens. If to has code, the token then calls:
function tokenReceived(address from, uint256 value, bytes calldata data) external returns (bytes4);
  • If the recipient does not implement tokenReceived, the whole transfer reverts. Transfers to externally owned accounts (no code) do not call any hook.
  • tokenReceived should return the magic value 0x8943ec02. msg.sender inside it is the token contract, so the receiver can check which token arrived.
  • The hook must be called after balances are updated, to limit re-entrancy problems.
  • data works like msg.data on an ether payment. It can encode an instruction for the receiver or just carry a note.
  • The Transfer event has an extra bytes data field.

Sample code

A vault that accepts only one ERC-223 token and records deposits in a single call:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Vault223 {
    address public immutable token;
    mapping(address => uint256) public deposits;

    constructor(address _token) { token = _token; }

    function tokenReceived(address from, uint256 value, bytes calldata)
        external returns (bytes4)
    {
        require(msg.sender == token, "unsupported token");
        deposits[from] += value;
        return 0x8943ec02;
    }
}

The user deposits with one call to IERC223(token).transfer(address(vault), amount). No prior approve is needed.

Things to know

  • ERC-223 is only partly compatible with ERC-20. It has no approve/transferFrom, and transfer(address,uint256,bytes) is new. Tools that expect ERC-20 may not handle it fully. ERC-20 remains by far the dominant fungible token standard.
  • Older ERC-223 drafts and implementations named the hook tokenFallback. The Final text uses tokenReceived, so check which one a given token calls.
  • The "is it a contract?" check uses code size, which is zero for a contract that is still in its constructor.
  • Any receive hook gives the recipient control during the transfer. Follow checks-effects-interactions and use re-entrancy guards.
  • ERC-777 solves the same problem differently, by looking up hooks in the ERC-1820 registry.

#token #fungible #standard #callbacks

Details

Authors
Dexaran (@Dexaran) <[email protected]>
Status
Final
Type
Standards Track / ERC
Created
2017-05-03
Required by

Machine-readable: Markdown · JSON