ERC-4521 · Stagnant · ERC · 2021-12-13
ERC-4521: 721/20-compatible transfer
Adds an ERC-20-style transfer(to, tokenId) to ERC-721 NFTs so wallets and contracts built for fungible tokens can move NFTs instead of locking them.
What problem does it solve?
ERC-721 NFTs move with transferFrom() and safeTransferFrom(), but they have no transfer(), the function every ERC-20 token exposes. Many wallets and contracts only know how to call transfer(to, amount). If an NFT ends up in a contract that was written for ERC-20s (a treasury, a multisig module, a vault), that contract may have no way to send it back out, so the NFT is effectively stuck.
ERC-4521 recommends adding one extra function to NFT contracts so the same transfer() call works for both fungible and non-fungible tokens.
How it works
The NFT contract adds:
function transfer(address to, uint256 tokenId) external returns (bool success);
- The signature is the same shape as ERC-20's
transfer(address,uint256), so it has the same function selector. Code that callsIERC20(token).transfer(to, x)will work against an NFT, withxinterpreted as the token id. - Only the current owner may call it; it moves the token from
msg.sendertoto. - It must emit the standard ERC-721
Transfer(from, to, tokenId)event, so indexers see an ordinary NFT transfer. - It must return
true. Many ERC-20-handling contracts check the return value and would revert without it.
Nothing else in ERC-721 changes: approvals, safeTransferFrom and metadata work as before.
Sample code
An extension to a simple ERC-721 storage layout, based on the reference implementation:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
abstract contract ERC721Transferable {
event Transfer(address indexed from, address indexed to, uint256 indexed id);
mapping(uint256 => address) public ownerOf;
mapping(address => uint256) public balanceOf;
mapping(uint256 => address) public getApproved;
/// ERC-4521: ERC-20-compatible transfer for NFTs.
function transfer(address to, uint256 tokenId) public virtual returns (bool) {
require(msg.sender == ownerOf[tokenId], "NOT_OWNER");
require(to != address(0), "ZERO_ADDRESS");
unchecked {
balanceOf[msg.sender]--;
balanceOf[to]++;
}
delete getApproved[tokenId]; // clear any single-token approval
ownerOf[tokenId] = to;
emit Transfer(msg.sender, to, tokenId);
return true;
}
}
Things to know
- Status: Stagnant. It is a small recommendation, not a widely required interface; most wallets and marketplaces still use
safeTransferFrom. - Unlike
safeTransferFrom,transfer()does not callonERC721Received, so sending to a contract that cannot handle NFTs is not blocked. That is the point of the extension for ERC-20-aware contracts, but it removes a safety net for other recipients. - Because the selector matches ERC-20, tools that guess a token's type from function selectors may misclassify such NFTs. Use ERC-165
supportsInterfaceto detect ERC-721. - ERC-1155 is a different route to handling fungible and non-fungible tokens with one interface.