ERC-4524 An ERC-20 safe transfer extension (Safer ERC-20)

Stagnant · Tokens · created 2021-12-05

Adds EIP-165 and safeTransfer to ERC-20 tokens, so that a transfer only completes after confirming the receiving contract is ready to accept the token.

Why it is needed

Because existing ERC-20 lacks EIP-165, there is no way for an app or another contract to check in advance which extension standards a given token implements. For example, even if a token supports a feature like ERC-2612 permit that combines approval and transfer in one step, an integrator has no standardized way to find out whether it is supported. In addition, ERC-20's basic transfer function completes the transfer regardless of whether the receiving side is ready to handle the token. ERC-721 and ERC-1155 already solved this problem with safeTransfer, but ERC-20 lacks the same safeguard, which can result in tokens being sent to a contract that cannot handle them and becoming permanently stuck.

What to implement

In depth

EIP-165 support and interface identification

ERC-20 contracts following this standard must implement the SaferERC-20 interface, which combines IERC20 and IERC165. The EIP-165 interfaceId for this interface is 0x534f5876. This lets other contracts or apps call supportsInterface in advance to check whether a given token implements this standard.

The four safeTransfer / safeTransferFrom functions

The standard requires two versions each of safeTransfer and safeTransferFrom, one with a data parameter and one without, for a total of four functions. These play the same role as ERC-20's existing transfer and transferFrom, but add a step that checks whether the recipient is ready to receive the token before the transfer completes.

Transfer rules for EOAs and contracts

safeTransfer and safeTransferFrom must transfer as usual, just like standard ERC-20, when the recipient address is an EOA (an externally owned account controlled directly by a person). When the recipient address is a contract, that contract must implement ERC20Receiver; if it does not, or if it fails to return the specified function selector when called, the transfer must revert.

The ERC20Receiver callback and function selector

A contract that wants to safely receive tokens must implement onERC20Received(address _operator, address _from, uint256 _amount, bytes _data) from the ERC20Receiver interface, and this function must return the function selector 0x4fc35859. If this value is not returned, the token contract rejects the transfer and reverts.

Why no metadata URI was included

Adding a metadata URI pointing to a logo or similar, as in ERC-721 and ERC-1155, was also considered, but it was left out to keep this EIP minimal. The text also notes that EIP-1046 already sufficiently covers metadata URIs for ERC-20 tokens.

Backward compatibility

The original text states that this EIP adds functionality while keeping the full existing ERC-20 function set intact, so there are no backward compatibility issues.

Proposal status

This EIP is currently in Stagnant status, meaning it is no longer actively discussed or progressed and has stalled.

Things to watch out for

Original ERC · ← ERCs