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 covers ERC4524, a proposal that checks whether a recipient contract can handle ERC20 tokens when an ERC20 transfer is made.

This article translates, summarizes, and supplements the material below.

I have also covered various other EIPs.

Overview

ERC4524 proposes integrating EIP165 into conventional ERC20 tokens. With EIP165, callers can determine which interfaces a contract implements by calling its supportsInterface function.

The proposal also incorporates functions for validating and processing token receipts, similar to those used by the NFT standard ERC721 and the multi-token standard ERC1155, into ERC20. This ensures that the receiving contract properly implements token receipt functionality.

For more information about EIP165, see the following article.

[ERC165] How to Check Whether a Contract Supports a Particular Interface

Motivation

Difficulty Detecting Extensions

ERC20 defines only token transfers and receipts. Applications and other contracts have no standard external mechanism for determining whether an implementation supports token permits, such as ERC2612, which enables approval and transfer in a single transaction.

For more information about ERC2612, see the following article.

[ERC2612] How to Execute ERC20 Approve and TransferFrom in One Transaction

Encouraging Modular Design

Using supportsInterface from EIP165, developers and contracts can dynamically determine whether a particular EIP is implemented. This makes it easier to select behavior automatically based on the features each token supports and helps developers build flexible, extensible platforms.

Maintaining Consistency Across Standards

ERC721 and ERC1155 already incorporate EIP165, which is widely used for NFTs and multi-token applications. Introducing the same mechanism into ERC20 improves interoperability with future extension standards and provides a consistent development experience across the community.

Specification

  • Integrate EIP165 (interface detection) into ERC20 tokens so that supportsInterface can identify the interfaces a contract implements.
  • Introduce the safeTransfer functionality used by ERC721 and ERC1155 into ERC20 to ensure that receiving contracts can handle the tokens correctly.

Implementation Requirements

  • ERC20-compliant contracts inherit IERC20 and IERC165 and implement the interface below.
  • The EIP165 interfaceId for this interface is 0x534f5876.
js
pragma solidity 0.8.10;

import './IERC20.sol';
import './IERC165.sol';

// the EIP-165 interfaceId for this interface is 0x534f5876

interface SaferERC-20 is IERC20, IERC165 {
  function safeTransfer(address to, uint256 amount) external returns(bool);
  function safeTransfer(address to, uint256 amount, bytes memory data) external returns(bool);
  function safeTransferFrom(address from, address to, uint256 amount) external returns(bool);
  function safeTransferFrom(address from, address to, uint256 amount, bytes memory data) external returns(bool);
}

safeTransfer and safeTransferFrom must support the following behavior.

  • Transfers to EOAs (externally owned accounts) succeed as before.
  • Transfers to contracts implementing ERC20Receiver that return 0x4fc35859 when onERC20Received is called succeed.
  • If the recipient contract does not implement ERC20Receiver or returns an incorrect selector, the transfer reverts.

A contract accepting safeTrransfer must implement the following interface and return 0x4fc35859 from onERC20Received.

js
pragma solidity 0.8.10;

import './IERC165.sol';

interface ERC20Receiver is IERC165 {
  function onERC20Received(
    address _operator,
    address _from,
    uint256 _amount,
    bytes _data
  ) external returns(bytes4);
}

Functions

safeTransfer

js
function safeTransfer(address to, uint256 amount) external returns(bool);

Transfers the specified amount of tokens to an EOA or a contract implementing ERC20Receiver whose onERC20Received function returns 0x4fc35859. If the recipient does not implement ERC20Receiver or returns an incorrect selector, the transfer reverts.

Parameters

  • to
    • Address receiving the tokens.
  • amount
    • Number of tokens to transfer.

Return value

  • bool
    • Returns true when the transfer succeeds.

safeTransfer

js
function safeTransfer(address to, uint256 amount, bytes memory data) external returns(bool);

Transfers amount tokens to the address specified by the first parameter, to, together with data. Transfers succeed for EOAs and contracts implementing ERC20Receiver, and revert if the interface is not implemented or the return value is incorrect.

Parameters

  • to
    • Address receiving the tokens.
  • amount
    • Number of tokens to transfer.
  • data
    • Byte sequence containing additional information.

Return value

  • bool
    • Returns true when the transfer succeeds.

safeTransferFrom

js
function safeTransferFrom(address from, address to, uint256 amount) external returns(bool);

Transfers amount tokens from a third party's from address to the to address. Transfers succeed for EOAs and contracts implementing ERC20Receiver, and revert if the interface is not implemented or the return value is incorrect.

Parameters

  • from
    • Address from which tokens are debited.
  • to
    • Address receiving the tokens.
  • amount
    • Number of tokens to transfer.

Return value

  • bool
    • Returns true when the transfer succeeds.

safeTransferFrom

js
function safeTransferFrom(address from, address to, uint256 amount, bytes memory data) external returns(bool);

Transfers amount tokens and data from from to to. Transfers succeed for EOAs and contracts implementing ERC20Receiver, and revert if the interface is not implemented or the return value is incorrect.

Parameters

  • from
    • Address from which tokens are debited.
  • to
    • Address receiving the tokens.
  • amount
    • Number of tokens to transfer.
  • data
    • Byte sequence containing additional information.

Return value

  • bool
    • Returns true when the transfer succeeds.

onERC20Received

js
function onERC20Received(
  address _operator,
  address _from,
  uint256 _amount,
  bytes _data
) external returns(bytes4);

As part of the ERC20Receiver interface, this function is called by safeTransfer or safeTransferFrom. The calling contract checks that the return value is 0x4fc35859 and reverts the transfer if the value is incorrect.

Parameters

  • _operator
    • Address of the contract that executed the transfer.
  • _from
    • Address of the original token owner.
  • _amount
    • Number of tokens transferred.
  • _data
    • Byte sequence containing additional information supplied with the transfer.

Return value

  • bytes4
    • Returns the success selector 0x4fc35859.

Additional Considerations

Minimal Implementation

ERC4524 is designed to minimize the extra code required to integrate EIP165 into ERC20. The amount of additional code is small, so the main disadvantage is a slight increase in contract size.

Naming and Benefits of the safeTransfer Functions

safeTransfer and safeTransferFrom retain the names already widely used by ERC721 and ERC1155. By preserving this naming convention, developers can easily check in advance whether a contract supports safe token transfers, reducing the risk of unintentionally losing tokens.

Why Metadata URIs Are Omitted

ERC721 and ERC1155 use metadata URIs to retrieve token information, but ERC4524 deliberately omits them for two reasons. First, the proposal aims for a minimal implementation. Second, ERC1046 already adequately covers token metadata URI functionality. Avoiding overlapping specifications keeps the design simple.

For more information about ERC1046, see the following article.

[ERC1046] How to Make ERC20 Metadata Work Like ERC721 Metadata

Backward Compatibility

There are no compatibility issues.

Security

onERC20Received is a callback function. Callbacks have been exploited in the past, so implementations must take care not to introduce vulnerabilities.

Citation

William Schwab (@wschwab), "ERC-4524: Safer ERC-20 [DRAFT]," Ethereum Improvement Proposals, no. 4524, December 2021. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-4524.

Closing Remarks

This article covered ERC4524, a proposal for checking whether a recipient contract can handle ERC20 tokens when an ERC20 transfer is made. I hope it was helpful.

If you have any questions, feel free to reach out via Twitter DM.

Twitter @cardene777

I also publish on other platforms, which you can explore below.

https://chaldene.net/