EIPs Explained

ERC-4524 · Stagnant · ERC · 2021-12-05

ERC-4524: Safer ERC-20

Proposed ERC-20 extension that adds ERC-165 interface detection and ERC-721-style safeTransfer functions, so tokens cannot be sent to contracts that are not ready to receive them.

What problem does it solve?

Plain ERC-20 transfer will happily send tokens to any address, including a contract that has no way to move them out again. Tokens sent that way are stuck forever. NFT standards solved this long ago: ERC-721 and ERC-1155 have safeTransferFrom, which calls a hook on the receiving contract and reverts if the receiver does not confirm.

ERC-20 also lacks ERC-165 supportsInterface, so an app cannot ask a token which extensions (for example ERC-2612 permits) it implements. EIP-4524 brings both features back to fungible tokens.

How it works

A compliant token is a normal ERC-20 that also implements ERC-165 and four new functions:

Function Purpose
safeTransfer(to, amount) Like transfer, with a receiver check
safeTransfer(to, amount, data) Same, passing data to the receiver
safeTransferFrom(from, to, amount) Like transferFrom, with a receiver check
safeTransferFrom(from, to, amount, data) Same, passing data

The spec gives the interface ID as 0x534f5876.

Rules for the safe functions:

  • Transfers to an EOA work as usual.
  • Transfers to a contract only succeed if that contract implements ERC20Receiver.onERC20Received(operator, from, amount, data) and returns its selector, 0x4fc35859.
  • Otherwise they must revert.

All original ERC-20 functions stay unchanged, so existing integrations keep working.

Sample code

A receiving vault and the token-side check (simplified):

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

interface ERC20Receiver {
    function onERC20Received(address operator, address from, uint256 amount, bytes calldata data)
        external returns (bytes4);
}

contract Vault is ERC20Receiver {
    // token => depositor => amount (msg.sender is the token contract)
    mapping(address => mapping(address => uint256)) public deposited;

    function onERC20Received(address, address from, uint256 amount, bytes calldata)
        external returns (bytes4)
    {
        deposited[msg.sender][from] += amount;
        return 0x4fc35859;           // accept
    }
}

// Inside the token:
function _checkReceiver(address operator, address from, address to, uint256 amount, bytes memory data) internal {
    if (to.code.length == 0) return; // EOA
    bytes4 ret = ERC20Receiver(to).onERC20Received(operator, from, amount, data);
    require(ret == 0x4fc35859, "receiver rejected");
}

Things to know

  • Status: Stagnant. It never became a widely adopted standard.
  • onERC20Received is a callback into another contract, which opens a re-entrancy path. Update balances before calling the hook and consider a re-entrancy guard.
  • Integrators cannot assume any given ERC-20 has safeTransfer; check supportsInterface first, and remember that most deployed tokens do not implement ERC-165 at all.
  • ERC-1363 (transferAndCall) is a Final standard that also adds receiver callbacks to ERC-20 tokens.
  • Do not confuse this with OpenZeppelin's SafeERC20 library, which is a caller-side wrapper for tokens that do not return bool, not a token interface.

#token #erc20 #safe-transfer #erc165 #callback

Details

Authors
William Schwab (@wschwab)
Status
Stagnant
Type
Standards Track / ERC
Created
2021-12-05
Requires

Machine-readable: Markdown · JSON