ERC-3589 NFT token bundling multiple assets (Assemble assets into NFTs)
Stagnant · NFTs & multi-token · created 2021-05-24
A standard that bundles ETH, ERC-20, ERC-721, and ERC-1155 assets into a single ERC-721 token (assembly token) so they can be transferred or exchanged all at once.
Why it is needed
Until now, collectors wanting to exchange NFT collectibles with each other have had only two options. One is listing each person's NFT on a marketplace and waiting for the other party to buy it, but exchanging multiple items this way incurs gas costs per trade and is a cumbersome process. The other is entrusting both sides' assets to a third party found on social media or in a chat room and asking them to carry out the exchange, which carries the risk that the third party disappears with the assets. To safely perform a batch swap of multiple assets at once, this needs to be converted into a one-to-one atomic exchange, which first requires a way to bundle assets such as ETH, ERC-20, ERC-721, and ERC-1155 together.
What to implement
event AssemblyAsset(address indexed firstHolder, uint256 indexed tokenId, uint256 salt, address[] addresses, uint256[] numbers);: An event that records which assets and quantities are included when assets are bundled into a single token.function hash(uint256 _salt, address[] memory _addresses, uint256[] memory _numbers) external pure returns (uint256 tokenId);: Combines the salt with the list of asset addresses and amounts to create the signature used as the token ID.function mint(address _to, address[] memory _addresses, uint256[] memory _numbers) payable external returns(uint256 tokenId);: Mints a single ERC-721 token by gathering assets that incur no loss on transfer.function safeMint(address _to, address[] memory _addresses, uint256[] memory _numbers) payable external returns(uint256 tokenId);: Mints while accounting for tokens that lose value on transfer due to taxes and so on, calculating and recording the amount actually received.function burn(address _to, uint256 _tokenId, uint256 _salt, address[] calldata _addresses, uint256[] calldata _numbers) external;: Burns the token and returns the assets bundled inside it to a specified address.
In depth
How to represent assets using two arrays
_addresses is an array that concatenates the ERC-20, ERC-721, and ERC-1155 addresses in that order. _numbers is an array containing the ETH amount, the number of ERC-20 addresses, the number of ERC-721 addresses, the number of ERC-1155 addresses, followed by the ERC-20 transfer amounts, ERC-721 token IDs, and ERC-1155 token IDs and amounts. The layout in the reference implementation is eth | erc20.length | erc721.length | erc1155.length | erc20 amounts | erc721 ids | erc1155 ids | erc1155 amounts.
The hash function and token ID generation
_salt is a value used to prevent hash collisions and may (MAY) be derived from user input or an incrementing nonce in the contract, but the token ID must (MUST) be generated via the hash function. hash chains _salt, _addresses, and _numbers in order through keccak256 to produce a signature, which is used as the token ID.
The difference between mint and safeMint
mint is recommended (SHOULD) for use with ordinary ERC-20 tokens and the like that incur no loss on transfer. safeMint must (MUST) be implemented for lossy tokens such as the PIG token that charge a tax on transfer, and it records the difference in the contract's token balance before and after transfer as the amount actually received, adjusting the numbers array accordingly.
Restoring assets via burn
burn can only be called by the token owner, and it recomputes hash from the supplied _salt, _addresses, and _numbers to verify that it matches _tokenId. If it matches, the token is burned and the ETH, ERC-20, ERC-721, and ERC-1155 assets are returned in order to the _to address.
The role of the AssemblyAsset event
When mint or safeMint is executed, it emits an AssemblyAsset event containing firstHolder, tokenId, salt, addresses, and numbers. This lets a wallet show the user what assets and how much of each are contained in a given token ID just from the token ID.
Why ERC-721 was chosen as the wrapper
ERC-721 is already widely used and well supported by NFT wallets, and an assembly token itself can be reassembled into further tokens. An assembly token is also easier for smart contracts to handle than a bundle of assets in situations like batch transfers, batch swaps, and collection swaps.
Proposal status
This proposal is currently in Stagnant status. Discussion has stalled with no further progress, so the specification may change or may never be adopted.
Things to watch out for
- The risk of pausable tokens: If a token is bundled and one of the underlying tokens is later paused, the burn function may fail to execute correctly. Platforms using this standard should maintain an allowlist or blocklist to avoid this situation.