EIPs Explained

ERC-7590 · Review · ERC · 2024-01-05

ERC-7590: ERC-20 Holder Extension for NFTs

An ERC-721 extension that lets each NFT hold its own ERC-20 balances, so fungible tokens can be bundled inside an NFT and travel with it when it is sold or transferred.

What problem does it solve?

Games and apps often want an NFT to carry fungible value with it: a character with its gold, a membership pass preloaded with reward points, a bundle sold as one item. With plain ERC-721, the tokens sit in the owner's wallet, not "in" the NFT, so they do not move when the NFT changes hands.

ERC-7590 adds a small interface so an NFT contract can track ERC-20 balances per token id. Whoever owns (or is approved for) the NFT controls the tokens inside it.

How it works

The NFT contract itself custodies the ERC-20s and keeps an internal ledger per (erc20Contract, tokenId):

  • transferERC20ToToken(erc20, tokenId, amount, data): pulls amount from msg.sender (who must have approved the NFT contract) and credits it to tokenId. Emits ReceivedERC20.
  • transferHeldERC20FromToken(erc20, tokenId, to, amount, data): sends tokens out of an NFT. Only the NFT owner or an approved operator may call it. Emits TransferredERC20 and increments the token's nonce.
  • balanceOfERC20(erc20, tokenId): the balance held by that NFT.
  • erc20TransferOutNonce(tokenId): a counter that goes up on every transfer out.

The design uses a pull mechanism because ERC-20 has no standard transfer callback: there is no way for a contract receiving a plain transfer to know which NFT it was meant for. The authors also chose a granular ERC-20-only interface rather than a generic holder for every token type, since ERC-721 and ERC-1155 already have safe-transfer receiver hooks.

Sample code

Interface:

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface IERC7590 /* is IERC165, IERC721 */ {
    event ReceivedERC20(address indexed erc20Contract, uint256 indexed toTokenId, address indexed from, uint256 amount);
    event TransferredERC20(address indexed erc20Contract, uint256 indexed fromTokenId, address indexed to, uint256 amount);

    function balanceOfERC20(address erc20Contract, uint256 tokenId) external view returns (uint256);
    function transferHeldERC20FromToken(address erc20Contract, uint256 tokenId, address to, uint256 amount, bytes memory data) external;
    function transferERC20ToToken(address erc20Contract, uint256 tokenId, uint256 amount, bytes memory data) external;
    function erc20TransferOutNonce(uint256 tokenId) external view returns (uint256);
}

Loading tokens into NFT #7 (ethers v6):

await (await erc20.approve(nftAddress, amount)).wait();
await (await nft.transferERC20ToToken(erc20Address, 7n, amount, "0x")).wait();
console.log(await nft.balanceOfERC20(erc20Address, 7n));

Things to know

  • Status is Review, not yet Final.
  • ERC-20 tokens sent directly to the NFT contract with a plain transfer are not credited to any NFT and are lost. Always use transferERC20ToToken.
  • Implementations must pull from msg.sender, never from an arbitrary from, or approvals could be abused to move someone else's tokens into a different NFT.
  • Marketplaces must record erc20TransferOutNonce when an NFT is listed and revert the sale if it changed, otherwise a seller can drain the bundled tokens just before the sale executes.
  • Fee-on-transfer tokens can make credited and actual balances diverge; measure the contract's balance before and after.
  • A different approach is ERC-6551, which gives each NFT its own smart-contract account that can hold any asset.

#nft #erc20 #composability #token-holder #gaming

Details

Authors
Steven Pineda (@steven2308), Jan Turk (@ThunderDeliverer)
Status
Review
Type
Standards Track / ERC
Created
2024-01-05
Requires

Machine-readable: Markdown · JSON