Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article examines ERC998, a proposal that extends ERC721 to enable tokens to own other ERC721 and ERC20 tokens.
The explanation below translates, summarizes, and supplements the material in the following proposal.
As of October 28, 2023, 998 was in the Draft stage.
I have also covered various other ERCs.
Overview
This standard allows ERC721 tokens to own other ERC721 and ERC20 tokens. It also allows ERC20 and ERC223 tokens to be owned by ERC721 tokens. This gives rise to four types of composable tokens.
- ERC998ERC721 Top-Down Composable
- Tokens that allow an ERC721 token to own, hold, and transfer ERC721 tokens.
- ERC998ERC20 Top-Down Composable
- Tokens that allow an ERC721 token to own, hold, and transfer ERC20 tokens.
- ERC998ERC721 Bottom-Up Composable
- Tokens that allow an ERC721 token to be attached to another ERC721 token.
- ERC998ERC20 Bottom-Up Composable
- Tokens that allow an ERC20 token to be attached to an ERC721 token.
For more on ERC20, see the following article.
For more on ERC721, see the following article.
Here is a brief explanation of the characteristics of these tokens.
- ERC998ERC721 Top-Down Composable extends ERC721 tokens so that they can own other ERC721 tokens.
- This enables one ERC721 token to own other ERC721 tokens.
- ERC998ERC20 Top-Down Composable extends ERC721 tokens so that they can own ERC20 tokens.
- This enables one ERC721 token to own ERC20 tokens.
- ERC998ERC721 Bottom-Up Composable extends ERC721 tokens so that they can be attached to other ERC721 tokens.
- This enables an ERC721 token to be associated with another ERC721 token.
- ERC998ERC20 Bottom-Up Composable extends ERC20 tokens so that they can be attached to ERC721 tokens.
- This enables an ERC20 token to be associated with an ERC721 token.
Composable tokens make it possible to connect ERC721 and ERC20 tokens through ownership and create hierarchical structures. The entire structure can be managed under a single owner address and transferred in one transaction.
Different composable approaches (Top-Down and Bottom-Up) have their own advantages and disadvantages (see the Additional considerations section for details). A token may implement one or more composable token types.
Non-fungible tokens conforming to this EIP must implement one or more of the following interfaces.
ERC998ERC721TopDownERC998ERC20TopDownERC998ERC721BottomUpERC998ERC20BottomUp
Specification
An ERC721 contract must implement the ERC721 interface to work with ERC998ERC721TopDown, ERC998ERC20TopDown, and the bottom-up composable contract ERC998ERC721BottomUp.
This requirement ensures that these composable contracts provide standard ERC721 token functionality.
For ERC20 contracts, the bottom-up composable contract ERC998ERC20BottomUp must implement the ERC20 interface.
This ensures that such contracts provide the usual ERC20 token functionality.
Furthermore, each contract using an ERC998 interface must implement the ERC165 standard. ERC165 is a standard for detecting whether a contract supports a particular interface. It therefore makes it possible to verify that a contract with ERC998 composable functionality can interact correctly with other contracts and users.
For more on ERC165, see the following article.
[ERC165] How to Check Whether a Contract Supports a Particular Interface
An ERC721 contract must implement the ERC721 interface, an ERC20 contract must implement the ERC20 interface, and an ERC998 contract must implement ERC165 to provide the appropriate functionality.
Authorization
This process determines whether a user or contract can perform a specific action on a composable token of type ERC998ERC721TopDown or ERC998ERC721BottomUp.
It works in the same way in both cases.
The rootOwner (root owner) is the owner address at the top of a tree of composable and ERC721 tokens.
Authorization within a composable token works as follows.
- Find the root owner.
- Compare the root owner with
msg.sender(the address of the user or contract executing the transaction), check the return value ofgetApproved(tokenId)(the address approved for the specified token ID), and check the return value ofisApprovedForAll(rootOwner, msg.sender)(the approval status between the root owner andmsg.sender). - If an applicable authorization check passes, authorization succeeds. Otherwise, authorization fails and the contract throws an error.
The following is an example of authorization code.
address rootOwner = address(rootOwnerOf(_tokenId));
require(
rootOwner == msg.sender ||
isApprovedForAll(rootOwner, msg.sender) ||
getApproved(tokenId) == msg.sender
);approve(address _approved, uint256 _tokenId) and getApproved(uint256 _tokenId) are ERC721 functions implemented with the root owner in mind.
This makes it possible to transfer an entire composable token tree to a new root owner without having to worry about which addresses have been approved within child composable tokens.
This is because previous approvals are only usable in relation to the previous root owner.
Authorization is the process of identifying the root owner and checking token approvals to determine whether a particular action is permitted. This ensures that the composable token tree is operated on appropriately.
The following is an example implementation.
function approve(address _approved, uint256 _tokenId) external {
address rootOwner = address(rootOwnerOf(_tokenId));
require(rootOwner == msg.sender || isApprovedForAll(rootOwner,msg.sender));
rootOwnerAndTokenIdToApprovedAddress[rootOwner][_tokenId] = _approved;
emit Approval(rootOwner, _approved, _tokenId);
}
function getApproved(uint256 _tokenId) public view returns (address) {
address rootOwner = address(rootOwnerOf(_tokenId));
return rootOwnerAndTokenIdToApprovedAddress[rootOwner][_tokenId];
}Traversal
The root owner of a composable token is retrieved by calling rootOwnerOf(uint256 _tokenId) or rootOwnerOfChild(address _childContract, uint256 _childTokenId).
These functions allow top-down and bottom-up composable tokens to traverse a tree of composable and ERC721 tokens to find the root owner.
The ERC998ERC721 top-down and bottom-up composable token types are compatible with each other.
A top-down composable token can own a bottom-up composable token, or it can own an ERC721 token that in turn owns a bottom-up token.
In any configuration, calling rootOwnerOf(uint256 _tokenID) on the composable token returns the root owner's address at the top of the ownership tree.
The traversal logic of rootOwnerOf is the same regardless of whether the composable token is bottom-up or top-down.
The logic of rootOwnerOf(uint256 _tokenId) can be described as follows.
- First, check whether the token is a bottom-up composable token. If a parent token exists, call
rootOwnerOfon the parent token.- If the call succeeds, the returned address is the root owner.
- If the call fails, call
rootOwnerOfChildon the parent token.- If the call succeeds, the returned address is the root owner.
- If the call fails, the token owner's address is the root owner.
- If the token is not a bottom-up composable token, call
rootOwnerOfChildfor the token.- If the call succeeds, the returned address is the root owner.
- If the call fails, the token owner's address is the root owner.
This logic identifies the root owner regardless of which tokens are bottom-up or top-down composable tokens.
The logic for calling rootOwnerOfChild for a token is as follows.
// Logic for calling rootOwnerOfChild for a tokenId
address tokenOwner = ownerOf(tokenId);
address childContract = address(this);
bytes32 rootOwner = ERC998ERC721(tokenOwner).rootOwnerOfChild(childContract, tokenId);- First, retrieve the token owner using a function such as
ownerOf. - Obtain the address of the contract itself.
- Use the retrieved owner and contract address to call
rootOwnerOfChildthrough theERC998ERC721interface.
The actual call must be made using assembly.
This makes it possible to detect whether the call succeeded or failed and to use the staticcall opcode to ensure that the call does not modify contract state.
This approach enables composable tokens and contracts to identify the owners of parent or child tokens within a token tree.
Moving composable tokens
All functions related to moving composable tokens adopt the same parameter pattern. The pattern is from : to : what (source, destination, and item being transferred).
For example, getChild(address _from, uint256 _tokenId, address _childContract, uint256 _childTokenId) transfers an ERC721 token from an address to a top-down composable token.
Here, _from is the source (from), _tokenId is the destination (to), and address _childContract and uint256 _childTokenId describe the item being transferred (what).
Another example is safeTransferChild(uint256 _fromTokenId, address _to, address _childContract, uint256 _childTokenId).
In this case, _fromTokenId is the source (from), _to is the destination (to), and the _childContract and _childTokenId parameters identify the item being transferred (what).
This consistent parameter pattern makes the functions for moving composable tokens easier to understand and clarifies their roles.
Transferring tokens owned by tokens
In bottom-up and top-down composable contracts, transferFrom and safeTransferFrom must throw an error if an attempt is made to transfer a token owned by another token directly.
This restriction exists because token ownership cannot be reliably determined when these functions attempt to transfer a token without explicitly identifying its owning token.
Accordingly, transferFrom and safeTransferFrom are designed to operate normally only when a token is owned by a particular address.
In other words, if a token is owned by another token, these functions cannot be called directly to transfer it and will throw an error.
ERC-721 Top-Down Composable
An ERC721 top-down composable contract acts as a container for ERC721 tokens. In other words, it is itself an ERC721 token that can receive, hold, and transfer ERC721 tokens.
There are two ways to transfer an ERC721 token into an ERC721 top-down composable contract.
-
Use
safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data).- The
_toparameter specifies the address of the top-down composable contract. - The
bytes dataparameter contains the integer token ID of the top-down composable token receiving the ERC721 token.
- The
-
Call
approveon the ERC721 token contract to approve the top-down composable contract.- Then call
getChildon the composable contract to perform the transfer.
- Then call
The first method is suitable for ERC721 contracts that provide safeTransferFrom. The second is used for contracts that do not provide this function, such as CryptoKitties.
The following example transfers ERC721 token 3 from a specified address to top-down composable token 6.
uint256 tokenId = 6;
bytes memory tokenIdBytes = new bytes(32);
assembly { mstore(add(tokenIdBytes, 32), tokenId) }
ERC721(contractAddress).safeTransferFrom(userAddress, composableAddress, 3, tokenIdBytes);uint256 tokenId = 6;- Declare
tokenIdand set it to6, the ID of the destination ERC721 token.
- Declare
bytes memory tokenIdBytes = new bytes(32);- Create a byte array named
tokenIdByteswith a length of32. - This byte array will later store the ERC721 token ID.
- Create a byte array named
assembly { mstore(add(tokenIdBytes, 32), tokenId) }- Store the token ID in the byte array inside an assembly block.
- Specifically, store
tokenIdat the position beginning at byte32oftokenIdBytes. - The purpose of this assembly code is to convert the integer token ID into a byte sequence.
- An ERC721 token ID is usually a
256-bit integer, but it must be converted into bytes for the transfer.
ERC721(contractAddress).safeTransferFrom(userAddress, composableAddress, 3, tokenIdBytes);- Call
safeTransferFromon a contract implementing the ERC721 interface (the ERC721 token contract specified bycontractAddress). - This transfers the ERC721 token from
userAddresstocomposableAddress(the top-down composable contract). - The function arguments are the source address (
userAddress), destination address (composableAddress), token ID3, andtokenIdBytes, the destination token ID converted into a byte array.
- Call
This procedure transfers the ERC721 token to the specified top-down composable contract, targeting token ID 6.
An ERC721 top-down composable contract must implement the ERC998ERC721TopDown interface to conform to the standard.
The contract may also implement the ERC998ERC721TopDownEnumerable or ERC998ERC20TopDownEnumerable interface, but this is optional.
pragma solidity ^0.4.24;
/// @title `ERC998ERC721` Top-Down Composable Non-Fungible Token
/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-998.md
/// Note: the ERC-165 identifier for this interface is 0xcde244d9
interface ERC998ERC721TopDown {
/// @dev This emits when a token receives a child token.
/// @param _from The prior owner of the token.
/// @param _toTokenId The token that receives the child token.
event ReceivedChild(
address indexed _from,
uint256 indexed _toTokenId,
address indexed _childContract,
uint256 _childTokenId
);
/// @dev This emits when a child token is transferred from a token to an address.
/// @param _fromTokenId The parent token that the child token is being transferred from.
/// @param _to The new owner address of the child token.
event TransferChild(
uint256 indexed _fromTokenId,
address indexed _to,
address indexed _childContract,
uint256 _childTokenId
);
/// @notice Get the root owner of tokenId.
/// @param _tokenId The token to query for a root owner address
/// @return rootOwner The root owner at the top of tree of tokens and ERC-998 magic value.
function rootOwnerOf(uint256 _tokenId) public view returns (bytes32 rootOwner);
/// @notice Get the root owner of a child token.
/// @param _childContract The contract address of the child token.
/// @param _childTokenId The tokenId of the child.
/// @return rootOwner The root owner at the top of tree of tokens and ERC-998 magic value.
function rootOwnerOfChild(
address _childContract,
uint256 _childTokenId
)
public
view
returns (bytes32 rootOwner);
/// @notice Get the parent tokenId of a child token.
/// @param _childContract The contract address of the child token.
/// @param _childTokenId The tokenId of the child.
/// @return parentTokenOwner The parent address of the parent token and ERC-998 magic value
/// @return parentTokenId The parent tokenId of _tokenId
function ownerOfChild(
address _childContract,
uint256 _childTokenId
)
external
view
returns (
bytes32 parentTokenOwner,
uint256 parentTokenId
);
/// @notice A token receives a child token
/// @param _operator The address that caused the transfer.
/// @param _from The owner of the child token.
/// @param _childTokenId The token that is being transferred to the parent.
/// @param _data Up to the first 32 bytes contains an integer which is the receiving parent tokenId.
function onERC721Received(
address _operator,
address _from,
uint256 _childTokenId,
bytes _data
)
external
returns(bytes4);
/// @notice Transfer child token from top-down composable to address.
/// @param _fromTokenId The owning token to transfer from.
/// @param _to The address that receives the child token
/// @param _childContract The ERC-721 contract of the child token.
/// @param _childTokenId The tokenId of the token that is being transferred.
function transferChild(
uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
)
external;
/// @notice Transfer child token from top-down composable to address.
/// @param _fromTokenId The owning token to transfer from.
/// @param _to The address that receives the child token
/// @param _childContract The ERC-721 contract of the child token.
/// @param _childTokenId The tokenId of the token that is being transferred.
function safeTransferChild(
uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
)
external;
/// @notice Transfer child token from top-down composable to address.
/// @param _fromTokenId The owning token to transfer from.
/// @param _to The address that receives the child token
/// @param _childContract The ERC-721 contract of the child token.
/// @param _childTokenId The tokenId of the token that is being transferred.
/// @param _data Additional data with no specified format
function safeTransferChild(
uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId,
bytes _data
)
external;
/// @notice Transfer bottom-up composable child token from top-down composable to other ERC-721 token.
/// @param _fromTokenId The owning token to transfer from.
/// @param _toContract The ERC-721 contract of the receiving token
/// @param _toTokenId The receiving token
/// @param _childContract The bottom-up composable contract of the child token.
/// @param _childTokenId The token that is being transferred.
/// @param _data Additional data with no specified format
function transferChildToParent(
uint256 _fromTokenId,
address _toContract,
uint256 _toTokenId,
address _childContract,
uint256 _childTokenId,
bytes _data
)
external;
/// @notice Get a child token from an ERC-721 contract.
/// @param _from The address that owns the child token.
/// @param _tokenId The token that becomes the parent owner
/// @param _childContract The ERC-721 contract of the child token
/// @param _childTokenId The tokenId of the child token
function getChild(
address _from,
uint256 _tokenId,
address _childContract,
uint256 _childTokenId
)
external;
}rootOwnerOf
// Get the root owner of tokenId.
function rootOwnerOf(uint256 _tokenId) public view returns (bytes32 rootOwner);Overview
A function that retrieves the root owner address of the specified token ID.
Details
Traverses token ownership to identify the root owner address of the specified token ID.
The first 4 bytes of rootOwner contain the ERC998 magic value 0xcd740db5, and the final 20 bytes contain the root owner address.
The magic value is used to determine whether a call to a contract whose support for rootOwnerOf is unknown has returned a valid value.
When support is unknown, the first 4 bytes of the returned rootOwner must be compared with 0xcd740db5.
0xcd740db5 is calculated as follows.
0xcd740db5 = this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^ this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;Parameters
_tokenId- Token ID to query.
Return value
rootOwner- A byte sequence containing the ERC998 magic value (first
4bytes) and root owner address (final20bytes).
- A byte sequence containing the ERC998 magic value (first
rootOwnerOfChild
// Get the root owner of a child token.
function rootOwnerOfChild(
address _childContract,
uint256 _childTokenId
)
public
view
returns (bytes32 rootOwner);Overview
A function that retrieves the root owner address of the specified child token.
Details
This function traverses token ownership to identify the root owner address of the specified child token.
The first 4 bytes of rootOwner contain the ERC998 magic value 0xcd740db5, and the final 20 bytes contain the root owner address.
The magic value is used to determine whether a call to a contract whose support for rootOwnerOfChild is unknown has returned a valid value.
When support is unknown, the first 4 bytes of the returned rootOwner must be compared with 0xcd740db5.
0xcd740db5 is calculated as follows:
0xcd740db5 = this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^ this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;Parameters
_childContract- Contract address of the child token.
_childTokenId- Token ID of the child token.
Return value
rootOwner- A byte sequence containing the ERC998 magic value (first
4bytes) and root owner address (final20bytes).
- A byte sequence containing the ERC998 magic value (first
ownerOfChild
// Get the parent tokenId of a child token and the owner address of the parent token.
function ownerOfChild(
address _childContract,
uint256 _childTokenId
)
external
view
returns (
address parentTokenOwner,
uint256 parentTokenId
);Overview
A function that retrieves the parent token ID and the address of the parent token's owner for a child token.
Details
Retrieves the parent token ID and the address of the parent token's owner for the specified child token.
The first 4 bytes of parentTokenOwner contain the ERC998 magic value 0xcd740db5, and the final 20 bytes contain the parent token owner's address.
The magic value is used to determine whether a call to a contract whose support for ownerOfChild is unknown has returned a valid value.
When support is unknown, the first 4 bytes of the returned parentTokenOwner must be compared with 0xcd740db5.
0xcd740db5 is calculated as follows.
0xcd740db5 = this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^ this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;Parameters
_childContract- Contract address of the child token.
_childTokenId- Token ID of the child token.
Return value
parentTokenOwner- A byte sequence containing the ERC998 magic value (first
4bytes) and the parent token owner's address (final20bytes).
- A byte sequence containing the ERC998 magic value (first
parentTokenId- Token ID of the parent token.
onERC721Received
// A token receives a child token
function onERC721Received(
address _operator,
address _from,
uint256 _childTokenId,
bytes _data
)
external
returns(bytes4);Overview
A function that notifies a recipient that an ERC721 token has been transferred and identifies the parent token ID to which it belongs.
Details
A function defined by the ERC721 standard.
This function is invoked when safeTransferFrom is called on an ERC721 contract.
The _data parameter contains an integer between 1 and 32 bytes long representing the ID of the parent token receiving the ERC721 token.
The return value of onERC721Received is the magic value 0x150b7a02, which equals bytes4(keccak256(abi.encodePacked("onERC721Received(address,address,uint256,bytes)"))).
Parameters
_operator- Address that initiated the token transfer.
_from- Address of the previous owner of the child token.
_childTokenId- ID of the child token being transferred to the parent token.
_data- The first
32bytes, at most, contain the parent token ID.
- The first
Return value
bytes4- Returns the magic value
0x150b7a02to indicate acceptance.
- Returns the magic value
transferChild
// Transfer child token from top-down composable to address.
function transferChild(
uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
)
external;Overview
A function that authorizes msg.sender and transfers a child token from a top-down composable contract to another address.
Details This function includes the following call.
ERC721(_childContract).transferFrom(this, _to, _childTokenId);This call transfers the child token identified by _childTokenId through _childContract from this (the composable contract itself) to the _to address.
The function transfers a child token from a top-down composable contract to another address.
Parameters
_fromTokenId- ID of the owning token from which the transfer is made.
_to- Address receiving the child token.
_childContract- Address of the child token's ERC721 contract.
_childTokenId- ID of the token being transferred.
safeTransferChild
Overview
A function that transfers a child token from a top-down composable contract to another address.
/// @notice Transfer child token from top-down composable to address.
/// @param _fromTokenId The owning token to transfer from.
/// @param _to The address that receives the child token
/// @param _childContract The ERC-721 contract of the child token.
/// @param _childTokenId The tokenId of the token that is being transferred.
function safeTransferChild(
uint256 _fromTokenId,
address _to,
address _childContract,
uint256 _childTokenId
)
external;Details
Authorizes msg.sender and transfers a child token from a top-down composable contract to another address.
It makes the following call internally.
ERC721(_childContract).safeTransferFrom(this, _to, _childTokenId);This transfers the child token identified by _childTokenId through _childContract from this (the composable contract itself) to the _to address.
This function can be used to transfer ownership of the child token.
Parameters
_fromTokenId- ID of the owning token from which the transfer is made.
_to- Address receiving the child token.
_childContract- Address of the child token's ERC721 contract.
_childTokenId- ID of the token being transferred.
safeTransferChild
Overview
A function that transfers a child token from a top-down composable contract to another address or another top-down composable contract.
Details
Authorizes msg.sender and transfers a child token from a top-down composable contract to another address or another top-down composable contract.
If the destination is another top-down composable contract, specify the parent token ID in _data.
It makes the following call internally.
ERC721(_childContract).safeTransferFrom(this, _to, _childTokenId, _data);This transfers the child token identified by _childTokenId through _childContract from this (the composable contract itself) to the _to address or another top-down composable contract.
This function can be used to transfer ownership of the child token.
Parameters
_fromTokenId- ID of the owning token from which the transfer is made.
_to- Address receiving the child token or address of another top-down composable contract.
_childContract- Address of the child token's ERC721 contract.
_childTokenId- ID of the token being transferred.
_data- Additional data with no prescribed format.
- Can be used to specify the destination parent token ID.
transferChildToParent
Overview
A function that transfers a child token (a token of a bottom-up composable contract) from a top-down composable contract to another ERC721 token.
Details
This function authorizes msg.sender and transfers a token of a bottom-up composable contract from a top-down composable contract to another ERC721 token.
It can only be used when the child token is a bottom-up composable token.
It is designed to transfer a token from a top-down composable contract to an ERC721 token using bottom-up composition.
It makes the following call internally.
ERC998ERC721BottomUp(_childContract).transferToParent(address(this), _toContract, _toTokenId, _childTokenId, _data);This transfers the child token identified by _childTokenId through _childContract from this (the composable contract itself) to the ERC721 token identified by _toTokenId at _toContract.
This function allows a bottom-up composable token to be transferred from a top-down composable contract to another ERC721 token.
Parameters
_fromTokenId- ID of the owning token from which the transfer is made.
_toContract- Contract address of the receiving ERC721 token.
_toTokenId- ID of the receiving ERC721 token.
_childContract- Address of the ERC721 contract for the child token (the token of the bottom-up composable contract).
_childTokenId- ID of the child token being transferred.
_data- Additional data with no specified format.
getChild
Overview
A function that transfers an ERC721 token when the ERC721 contract does not provide safeTransferChild(uint256 _fromTokenId, address _to, address _childContract, uint256 _childTokenId, bytes _data).
Details
The transfer takes place as follows.
- The ERC721 token owner calls
approveorsetApprovalForAllto approve the top-down composable contract. - The ERC721 token owner calls
getChildon the ERC721 top-down composable contract.
The getChild function must authorize msg.sender by verifying that the caller owns the ERC721 token, is approved for that token, or is an operator in the token's ERC721 contract.
Parameters
_from- Address of the child token owner.
_tokenId- Token that will become the parent owner.
_childContract- Address of the child token's ERC721 contract.
_childTokenId- ID of the child token.
ERC-721 Top-Down Composable Enumeration
An optional interface for enumerating top-down composable tokens.
/// @dev The ERC-165 identifier for this interface is 0xa344afe4
interface ERC998ERC721TopDownEnumerable {
/// @notice Get the total number of child contracts with tokens that are owned by tokenId.
/// @param _tokenId The parent token of child tokens in child contracts
/// @return uint256 The total number of child contracts with tokens owned by tokenId.
function totalChildContracts(uint256 _tokenId) external view returns(uint256);
/// @notice Get child contract by tokenId and index
/// @param _tokenId The parent token of child tokens in child contract
/// @param _index The index position of the child contract
/// @return childContract The contract found at the tokenId and index.
function childContractByIndex(
uint256 _tokenId,
uint256 _index
)
external
view
returns (address childContract);
/// @notice Get the total number of child tokens owned by tokenId that exist in a child contract.
/// @param _tokenId The parent token of child tokens
/// @param _childContract The child contract containing the child tokens
/// @return uint256 The total number of child tokens found in child contract that are owned by tokenId.
function totalChildTokens(
uint256 _tokenId,
address _childContract
)
external
view
returns(uint256);
/// @notice Get child token owned by tokenId, in child contract, at index position
/// @param _tokenId The parent token of the child token
/// @param _childContract The child contract of the child token
/// @param _index The index position of the child token.
/// @return childTokenId The child tokenId for the parent token, child token and index
function childTokenByIndex(
uint256 _tokenId,
address _childContract,
uint256 _index
)
external
view
returns (uint256 childTokenId);
}ERC-20 Top-Down Composable
An ERC20 top-down composable contract is a type of ERC721 token that acts as a container for receiving, holding, and transferring ERC20 tokens.
This contract represents an ERC721 token with the ability to receive, hold, and transfer ERC20 tokens.
There are two ways to transfer ERC20 tokens into an ERC20 top-down composable contract.
-
Use
transfer(address _to, uint256 _value, bytes _data)on an ERC20 contract supporting ERC223.- Here,
_tois the address of the ERC20TopDown composable contract, and_valuespecifies the amount of ERC20 tokens to transfer. - The
bytesparameter contains an integer representing the ID of the token in the ERC20TopDown composable contract that receives the transferred tokens.
- Here,
-
Call
approveon the ERC20 contract to approve the ERC20TopDown composable contract, then callgetERC20(address _from, uint256 _tokenId, address _erc20Contract, uint256 _value)on the ERC20TopDown composable contract.
The first method can be used when the ERC20 contract supports ERC223. The second is used for ERC20 contracts that do not support it.
The ERC20TopDown composable contract implements an interface that enables interaction with ERC20 tokens.
/// @title `ERC998ERC20` Top-Down Composable Non-Fungible Token
/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-998.md
/// Note: the ERC-165 identifier for this interface is 0x7294ffed
interface ERC998ERC20TopDown {
/// @dev This emits when a token receives ERC-20 tokens.
/// @param _from The prior owner of the token.
/// @param _toTokenId The token that receives the ERC-20 tokens.
/// @param _erc20Contract The ERC-20 contract.
/// @param _value The number of ERC-20 tokens received.
event ReceivedERC20(
address indexed _from,
uint256 indexed _toTokenId,
address indexed _erc20Contract,
uint256 _value
);
/// @dev This emits when a token transfers ERC-20 tokens.
/// @param _tokenId The token that owned the ERC-20 tokens.
/// @param _to The address that receives the ERC-20 tokens.
/// @param _erc20Contract The ERC-20 contract.
/// @param _value The number of ERC-20 tokens transferred.
event TransferERC20(
uint256 indexed _fromTokenId,
address indexed _to,
address indexed _erc20Contract,
uint256 _value
);
/// @notice A token receives ERC-20 tokens
/// @param _from The prior owner of the ERC-20 tokens
/// @param _value The number of ERC-20 tokens received
/// @param _data Up to the first 32 bytes contains an integer which is the receiving tokenId.
function tokenFallback(address _from, uint256 _value, bytes _data) external;
/// @notice Look up the balance of ERC-20 tokens for a specific token and ERC-20 contract
/// @param _tokenId The token that owns the ERC-20 tokens
/// @param _erc20Contract The ERC-20 contract
/// @return The number of ERC-20 tokens owned by a token from an ERC-20 contract
function balanceOfERC20(
uint256 _tokenId,
address _erc20Contract
)
external
view
returns(uint256);
/// @notice Transfer ERC-20 tokens to address
/// @param _tokenId The token to transfer from
/// @param _value The address to send the ERC-20 tokens to
/// @param _erc20Contract The ERC-20 contract
/// @param _value The number of ERC-20 tokens to transfer
function transferERC20(
uint256 _tokenId,
address _to,
address _erc20Contract,
uint256 _value
)
external;
/// @notice Transfer ERC-20 tokens to address or ERC-20 top-down composable
/// @param _tokenId The token to transfer from
/// @param _value The address to send the ERC-20 tokens to
/// @param _erc223Contract The `ERC-223` token contract
/// @param _value The number of ERC-20 tokens to transfer
/// @param _data Additional data with no specified format, can be used to specify tokenId to transfer to
function transferERC223(
uint256 _tokenId,
address _to,
address _erc223Contract,
uint256 _value,
bytes _data
)
external;
/// @notice Get ERC-20 tokens from ERC-20 contract.
/// @param _from The current owner address of the ERC-20 tokens that are being transferred.
/// @param _tokenId The token to transfer the ERC-20 tokens to.
/// @param _erc20Contract The ERC-20 token contract
/// @param _value The number of ERC-20 tokens to transfer
function getERC20(
address _from,
uint256 _tokenId,
address _erc20Contract,
uint256 _value
)
external;
}tokenFallback
function tokenFallback(address _from, uint256 _value, bytes _data) external;Overview
A function that notifies an ERC20TopDown composable contract that one of its tokens has received ERC20 tokens.
Details
This function originates from ERC223, an extension of the ERC20 standard.
When ERC20 tokens are transferred, the sending token contract calls this function on the receiving contract.
The _data parameter specifies which token received the ERC20 tokens.
As part of the ERC-223 standard, this function handles receipt of ERC20 tokens.
Parameters
_from- Address of the previous owner of the ERC20 tokens.
_value- Amount of ERC20 tokens received.
_data- Integer representing the ID of the receiving token.
balanceOfERC20
function balanceOfERC20(uint256 _tokenId, address _erc20Contract) external view returns(uint256);Overview
A function that retrieves the ERC20 token balance held by a particular token for an ERC20 contract.
Details
Retrieves the balance of ERC20 tokens held by a particular token from the specified ERC20 contract.
Parameters
_tokenId- Token ID.
_erc20Contract- ERC20 contract address.
Return value
- Returns the amount of ERC20 tokens.
transferERC20
function transferERC20(uint256 _tokenId, address _to, address _erc20Contract, uint256 _value) external;Overview
Used to transfer ERC20 tokens from a token to an address.
This function calls ERC20(_erc20Contract).transfer(_to, _value).
Details
This function is used to transfer ERC20 tokens from a token to an address.
The caller (msg.sender) must be authorized.
Parameters
_tokenId- Token ID.
_to- Address to which the ERC20 tokens are transferred.
_erc20Contract- Contract address of the ERC20 tokens.
_value- Amount of ERC20 tokens to transfer.
transferERC223
function transferERC223(
uint256 _tokenId,
address _to,
address _erc223Contract,
uint256 _value,
bytes _data
)
external;Overview
A function based on ERC223 for transferring ERC20 tokens from a token to an address or another token.
Details
This function originates from ERC223 and is used to transfer ERC20 tokens from a token to an address or another token.
The _data parameter can contain an integer token ID to specify the transfer destination.
The caller (msg.sender) must be authorized.
Parameters
_tokenId- Token ID.
_to- Destination address for the ERC20 tokens.
_erc223Contract- Address of the ERC223 token contract.
_value- Amount of ERC20 tokens to transfer.
_data- Additional data with no specified format.
- Typically used to specify a token ID.
getERC20
function getERC20(
address _from,
uint256 _tokenId,
address _erc20Contract,
uint256 _value
)
external;Overview
A function that transfers ERC20 tokens into an ERC-20TopDown composable contract.
Details
Used when the ERC20 contract does not provide transferERC223(uint256 _tokenId, address _to, address _erc223Contract, uint256 _value, bytes _data).
Before using this function, the ERC20 contract must approve the address of the ERC20 top-down composable contract to transfer tokens.
The caller (msg.sender) must be authorized by verifying that it matches _from or is approved in the ERC20 contract.
Parameters
_from- Address of the current owner of the ERC20 tokens.
_tokenId- Token ID.
_erc20Contract- Address of the ERC20 token contract.
_value- Amount of ERC20 tokens to transfer.
ERC-20 Top-Down Composable Enumeration
An optional interface for enumerating top-down composable tokens.
/// @dev The ERC-165 identifier for this interface is 0xc5fd96cd
interface ERC998ERC20TopDownEnumerable {
/// @notice Get the number of ERC-20 contracts that token owns ERC-20 tokens from
/// @param _tokenId The token that owns ERC-20 tokens.
/// @return uint256 The number of ERC-20 contracts
function totalERC20Contracts(uint256 _tokenId) external view returns(uint256);
/// @notice Get an ERC-20 contract that token owns ERC-20 tokens from by index
/// @param _tokenId The token that owns ERC-20 tokens.
/// @param _index The index position of the ERC-20 contract.
/// @return address The ERC-20 contract
function erc20ContractByIndex(
uint256 _tokenId,
uint256 _index
)
external
view
returns(address);
}ERC-721 Bottom-Up Composable
An ERC721BottomUp composable contract represents ERC721 tokens that can be attached to other ERC721 tokens. These contracts can associate tokens with other ERC721 tokens.
An ERC721BottomUp composable contract stores the token owner's address and, when the token is owned by a parent token, that parent's token ID (identifier). This enables management of token ownership and hierarchical structures and establishes relationships between tokens.
/// @title `ERC998ERC721` Bottom-Up Composable Non-Fungible Token
/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-998.md
/// Note: the ERC-165 identifier for this interface is 0xa1b23002
interface ERC998ERC721BottomUp {
/// @dev This emits when a token is transferred to an ERC-721 token
/// @param _toContract The contract the token is transferred to
/// @param _toTokenId The token the token is transferred to
/// @param _tokenId The token that is transferred
event TransferToParent(
address indexed _toContract,
uint256 indexed _toTokenId,
uint256 _tokenId
);
/// @dev This emits when a token is transferred from an ERC-721 token
/// @param _fromContract The contract the token is transferred from
/// @param _fromTokenId The token the token is transferred from
/// @param _tokenId The token that is transferred
event TransferFromParent(
address indexed _fromContract,
uint256 indexed _fromTokenId,
uint256 _tokenId
);
/// @notice Get the root owner of tokenId.
/// @param _tokenId The token to query for a root owner address
/// @return rootOwner The root owner at the top of tree of tokens and ERC-998 magic value.
function rootOwnerOf(uint256 _tokenId) external view returns (bytes32 rootOwner);
/// @notice Get the owner address and parent token (if there is one) of a token
/// @param _tokenId The tokenId to query.
/// @return tokenOwner The owner address of the token
/// @return parentTokenId The parent owner of the token and ERC-998 magic value
/// @return isParent True if parentTokenId is a valid parent tokenId and false if there is no parent tokenId
function tokenOwnerOf(
uint256 _tokenId
)
external
view
returns (
bytes32 tokenOwner,
uint256 parentTokenId,
bool isParent
);
/// @notice Transfer token from owner address to a token
/// @param _from The owner address
/// @param _toContract The ERC-721 contract of the receiving token
/// @param _toToken The receiving token
/// @param _data Additional data with no specified format
function transferToParent(
address _from,
address _toContract,
uint256 _toTokenId,
uint256 _tokenId,
bytes _data
)
external;
/// @notice Transfer token from a token to an address
/// @param _fromContract The address of the owning contract
/// @param _fromTokenId The owning token
/// @param _to The address the token is transferred to.
/// @param _tokenId The token that is transferred
/// @param _data Additional data with no specified format
function transferFromParent(
address _fromContract,
uint256 _fromTokenId,
address _to,
uint256 _tokenId,
bytes _data
)
external;
/// @notice Transfer a token from a token to another token
/// @param _fromContract The address of the owning contract
/// @param _fromTokenId The owning token
/// @param _toContract The ERC-721 contract of the receiving token
/// @param _toToken The receiving token
/// @param _tokenId The token that is transferred
/// @param _data Additional data with no specified format
function transferAsChild(
address _fromContract,
uint256 _fromTokenId,
address _toContract,
uint256 _toTokenId,
uint256 _tokenId,
bytes _data
)
external;
}rootOwnerOf
function rootOwnerOf(uint256 _tokenId) public view returns (bytes32 rootOwner);Overview
A function that retrieves the root owner address of the specified token.
Details
Traverses token ownership until it finds the root owner address of the specified token.
The first 4 bytes of rootOwner contain the ERC998 magic value 0xcd740db5, and the final 20 bytes contain the root owner address.
This magic value is returned when the function is called on a contract whose support for rootOwnerOf is unknown.
The magic value is used to verify that such a call returned a valid result.
If it is unknown whether the contract implements rootOwnerOf, the first 4 bytes of the returned rootOwner must be compared with 0xcd740db5.
0xcd740db5 is equivalent to the following expression.
this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^
this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;An example return value for retrieving the root owner is 0xcd740db50000000000000000e5240103e1ff986a2c8ae6b6728ffe0d9a395c59.
Parameters
_tokenId- ID of the token whose root owner address is being queried.
Return value
rootOwner- The root owner address of the token, together with the ERC998 magic value.
tokenOwnerOf
function tokenOwnerOf(uint256 _tokenId) external view returns (bytes32 tokenOwner, uint256 parentTokenId, bool isParent);Overview
A function that retrieves the token owner's address and the parent token ID, if any.
Details
If isParent is true, tokenOwner is the address of the owning ERC721 contract, and parentTokenId is a valid parent token ID.
If isParent is false, tokenOwner is a user address, and parentTokenId does not contain a valid parent token ID and must be ignored.
The first 4 bytes of tokenOwner contain the ERC998 magic value 0xcd740db5, and the final 20 bytes contain the token owner's address.
This magic value is returned when the function is called on a contract whose support for tokenOwnerOf is unknown.
The magic value is used to verify that such a call returned a valid result.
If it is unknown whether the contract implements tokenOwnerOf, the first 4 bytes of the returned tokenOwner must be compared with 0xcd740db5.
Parameters
_tokenId- ID of the token being queried.
Return value
tokenOwner- Contains the token owner's address and the ERC998 magic value.
parentTokenId- ID of the parent token.
isParenttrueifparentTokenIdis a valid parent token ID; otherwisefalse.
transferToParent
function transferToParent(address _from, address _toContract, uint256 _toTokenId, uint256 _tokenId, bytes _data) external;Overview
A function that transfers a token from an address to another token.
Overview
msg.sender must be authorized.
This function must check whether _toToken exists in _toContract and throw an exception if it does not.
Parameters
_from- Address of the token owner.
_toContract- ERC721 contract of the receiving token.
_toTokenId- ID of the receiving token.
_tokenId- ID of the token being transferred.
_data- Additional data with the specified format.
transferFromParent
function transferFromParent(address _fromContract, uint256 _fromTokenId, address _to, uint256 _tokenId, bytes _data) external;Overview
A function that transfers a token from another token to an address.
Details
msg.sender must be authorized.
This function checks whether _fromContract and _fromTokenId identify the owner of _tokenId and throws an error if they do not.
Parameters
_fromContract- Address of the contract containing the owning token.
_fromTokenId- ID of the source token.
_to- Destination address for the transferred token.
_tokenId- ID of the token being transferred.
_data- Additional data with no specified format.
transferAsChild
function transferAsChild(address _fromContract, uint256 _fromTokenId, address _toContract, uint256 _toTokenId, uint256 _tokenId, bytes _data) external;Overview
A function that transfers a token from one token to another token.
Details
msg.sender must be authorized.
This function checks that _toToken exists in _toContract and throws an error if it does not.
It also checks whether _fromContract and _fromTokenId identify the owner of _tokenId and throws an error if they do not.
Parameters
_fromContract- Address of the contract containing the owning token.
_fromTokenId- ID of the source token.
_toContract- Address of the destination ERC-721 token contract.
_toTokenId- ID of the destination token.
_tokenId- ID of the token being transferred.
_data- Additional data with no specified format.
ERC-721 Bottom-Up Composable Enumeration
An optional interface for enumerating bottom-up composable tokens.
/// @dev The ERC-165 identifier for this interface is 0x8318b539
interface ERC998ERC721BottomUpEnumerable {
/// @notice Get the number of ERC-721 tokens owned by parent token.
/// @param _parentContract The contract the parent ERC-721 token is from.
/// @param _parentTokenId The parent tokenId that owns tokens
// @return uint256 The number of ERC-721 tokens owned by parent token.
function totalChildTokens(
address _parentContract,
uint256 _parentTokenId
)
external
view
returns (uint256);
/// @notice Get a child token by index
/// @param _parentContract The contract the parent ERC-721 token is from.
/// @param _parentTokenId The parent tokenId that owns the token
/// @param _index The index position of the child token
/// @return uint256 The child tokenId owned by the parent token
function childTokenByIndex(
address _parentContract,
uint256 _parentTokenId,
uint256 _index
)
external
view
returns (uint256);
}ERC-20 Bottom-Up Composable
An ERC20 bottom-up composable contract is a type of ERC20 token. These tokens can be attached to ERC721 tokens or held by user addresses in the same way as ordinary ERC20 tokens.
When attached to an ERC721 token, an ERC20BottomUp composable contract stores the owner's address and the parent token ID, if one exists. The contract also adds several methods to the ERC20 or ERC223 interfaces, enabling parent token balance queries, transfers to parent tokens, and transfers between parent tokens.
Implementing this functionality requires an additional mapping to track balances held by tokens alongside the ordinary mapping used to track balances held by user addresses.
/// @dev This mapping tracks standard ERC20/`ERC-223` ownership, where an address owns
/// a particular amount of tokens.
mapping(address => uint) userBalances;
/// @dev This additional mapping tracks ERC-998 ownership, where an ERC-721 token owns
/// a particular amount of tokens. This tracks contractAddres => tokenId => balance
mapping(address => mapping(uint => uint)) nftBalances;The complete interface is shown below.
/// @title `ERC998ERC20` Bottom-Up Composable Fungible Token
/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-998.md
/// Note: The ERC-165 identifier for this interface is 0xffafa991
interface ERC998ERC20BottomUp {
/// @dev This emits when a token is transferred to an ERC-721 token
/// @param _toContract The contract the token is transferred to
/// @param _toTokenId The token the token is transferred to
/// @param _amount The amount of tokens transferred
event TransferToParent(
address indexed _toContract,
uint256 indexed _toTokenId,
uint256 _amount
);
/// @dev This emits when a token is transferred from an ERC-721 token
/// @param _fromContract The contract the token is transferred from
/// @param _fromTokenId The token the token is transferred from
/// @param _amount The amount of tokens transferred
event TransferFromParent(
address indexed _fromContract,
uint256 indexed _fromTokenId,
uint256 _amount
);
/// @notice Get the balance of a non-fungible parent token
/// @param _tokenContract The contract tracking the parent token
/// @param _tokenId The ID of the parent token
/// @return amount The balance of the token
function balanceOfToken(
address _tokenContract,
uint256 _tokenId
)
external
view
returns (uint256 amount);
/// @notice Transfer tokens from owner address to a token
/// @param _from The owner address
/// @param _toContract The ERC-721 contract of the receiving token
/// @param _toToken The receiving token
/// @param _amount The amount of tokens to transfer
function transferToParent(
address _from,
address _toContract,
uint256 _toTokenId,
uint256 _amount
)
external;
/// @notice Transfer token from a token to an address
/// @param _fromContract The address of the owning contract
/// @param _fromTokenId The owning token
/// @param _to The address the token is transferred to
/// @param _amount The amount of tokens to transfer
function transferFromParent(
address _fromContract,
uint256 _fromTokenId,
address _to,
uint256 _amount
)
external;
/// @notice Transfer token from a token to an address, using `ERC-223` semantics
/// @param _fromContract The address of the owning contract
/// @param _fromTokenId The owning token
/// @param _to The address the token is transferred to
/// @param _amount The amount of tokens to transfer
/// @param _data Additional data with no specified format, can be used to specify the sender tokenId
function transferFromParentERC223(
address _fromContract,
uint256 _fromTokenId,
address _to,
uint256 _amount,
bytes _data
)
external;
/// @notice Transfer a token from a token to another token
/// @param _fromContract The address of the owning contract
/// @param _fromTokenId The owning token
/// @param _toContract The ERC-721 contract of the receiving token
/// @param _toToken The receiving token
/// @param _amount The amount tokens to transfer
function transferAsChild(
address _fromContract,
uint256 _fromTokenId,
address _toContract,
uint256 _toTokenId,
uint256 _amount
)
external;
}balanceOfToken
function balanceOfToken(address _tokenContract, uint256 _tokenId) external view returns (uint256 amount);Overview
A function that returns the balance held by a non-fungible parent token.
Details
This function returns the balance associated with the specified parent token contract address and token ID. It is used to query the balance of tokens owned by an ERC721 parent token.
It resembles the standard ERC-20 balanceOf method, but accepts the parent token's contract address and ID.
The method works like balanceOf, except that it checks ownership by an ERC721 token instead of a user address.
Parameters
_tokenContract- Address of the contract that tracks the parent token.
_tokenId- ID of the parent token.
Return value
amount- Token balance held by the non-fungible parent token.
transferToParent
function transferToParent(address _from, address _toContract, uint256 _toTokenId, uint256 _amount) external;Overview
A function that transfers an amount of tokens from a user address to an ERC721 token.
Details
Transfers an amount of tokens from the specified user address to the specified ERC-721 token.
It must verify that the receiving contract implements ERC721 using the ERC165 supportsInterface function.
It must also verify that the receiving token actually exists.
This should be done by calling ownerOf on the receiving token's contract and verifying that the call neither throws an error nor returns the zero address.
If the token transfer succeeds, this function must emit the TransferToParent event in addition to the standard ERC20 Transfer event.
It must also throw an error if the _from account has an insufficient token balance for the transfer.
Parameters
_from- Address of the owner transferring the tokens.
_toContract- ERC721 contract of the receiving token.
_toTokenId- ID of the receiving token.
_amount- Amount of tokens to transfer.
transferFromParent
function transferFromParent(address _fromContract, uint256 _fromTokenId, address _to, uint256 _amount) external;Overview
A function that transfers an amount of tokens from an ERC721 token to an address.
Details
Transfers an amount of tokens from the specified ERC721 token to the specified address.
If the transfer succeeds, it must emit the TransferFromParent event in addition to the standard ERC20 Transfer event.
It must also throw an error if the sending ERC721 token's balance is less than _amount.
Furthermore, this function must verify that msg.sender owns the sending ERC721 token and throw an error otherwise.
Parameters
_fromContract- Address of the contract containing the owning token.
_fromTokenId- ID of the sending token.
_to- Address receiving the transferred tokens.
_amount- Amount of tokens to transfer.
transferFromParentERC223
function transferFromParentERC223(address _fromContract, uint256 _fromTokenId, address _to, uint256 _amount, bytes _data) external;Overview
A function that transfers an amount of tokens from an ERC721 token to an address using ERC-223 semantics.
Details
Transfers an amount of tokens from the specified ERC721 token to the specified address using ERC223 semantics.
It has the same requirements as transferFromParent, but if the receiving address is a contract, it must call tokenFallback at that address as specified by ERC223.
Parameters
_fromContract- Address of the contract containing the owning token.
_fromTokenId- ID of the sending token.
_to- Address receiving the transferred tokens.
_amount- Amount of tokens to transfer.
_data- Additional data with the specified format.
- Can be used to specify the sending token ID.
transferAsChild
function transferAsChild(address _fromContract, uint256 _fromTokenId, address _toContract, uint256 _toTokenId, uint256 _amount) external;Overview
This function transfers an amount of tokens from one ERC721 token to another ERC721 token.
Details
In addition to the standard ERC20 contract's Transfer event, both TransferFromParent and TransferToParent must be emitted.
If the sending ERC721 token's balance is less than _amount, the function throws an error.
It also checks that msg.sender owns the sending ERC721 token and throws an error otherwise.
Furthermore, the function must verify that the receiving contract implements ERC721 using the ERC165 supportsInterface function.
To verify that the receiving token actually exists, it must call ownerOf on the receiving token's contract and check that the call neither throws an error nor returns the zero address.
Parameters
_fromContract- Address of the contract containing the owning token.
_fromTokenId- ID of the sending token.
_toContract- ERC721 contract of the receiving token.
_toTokenId- ID of the receiving token.
_amount- Amount of tokens to transfer.
Notes
According to this note, implementations must emit the standard ERC20 contract's Transfer event whenever a token transfer occurs, regardless of whether the sender or recipient is an address or an ERC721 token.
In other words, transaction details must be exposed through events for transfers between tokens as well as transfers from tokens to addresses.
The implementation must also implement all ERC20 or ERC223 functions in addition to the functions specified in this interface. In other words, an implementation following this new token standard must provide other compatible functions in addition to the basic token functionality.
This ensures compatibility and seamless integration with existing tokens and enables users and developers to adopt the new tokens smoothly.
Additional considerations
There are two different types of composable tokens to accommodate different use cases. An ordinary ERC721 token cannot own a top-down composable token, but it can own a bottom-up composable token. Conversely, a bottom-up composable token cannot own an ordinary ERC721 token, whereas a top-down composable token can own one. Different composable types enable different token ownership structures.
Which type of composable token should you use?
Use top-down composability when you want to transfer an ordinary ERC721 token into a non-fungible token.
Use bottom-up composability when you want to transfer a non-fungible token into an ordinary ERC721 token.
Explicit transfer parameters
All ERC998 transfer functions include explicit parameters specifying the previous and new owners of a token.
The reason for explicitly specifying from and to is to prevent tokens from being transferred in unintended ways.
The following example shows what could happen if from were not explicitly supplied to a transfer function.
An exchange contract is an approved operator in a specific composable contract for user A, user B and user C. User A transfers token 1 to user B. At the same time the exchange contract transfers token 1 to user C (with the implicit intention to transfer from user A). User B gets token 1 for a minute before it gets incorrectly transferred to user C. The second transfer should have failed but it didn’t because no explicit from was provided to ensure that token 1 came from user A.
A trading contract is an approved operator for users A, B, and C in a particular composable contract. User A transfers token 1 to user B. At the same time, the trading contract transfers token 1 to user C, intending to transfer it from user A. User B owns token 1 for one minute before it is mistakenly transferred to user C. The second transfer should have failed, but it did not because
fromwas not explicitly supplied to ensure that token 1 was being transferred from user A.
Backward compatibility
Composable tokens are designed to work with ERC721, ERC223, and ERC20 tokens.
Some older ERC721 contracts may not provide the safeTransferFrom function for safely transferring tokens.
Even so, tokens can be transferred into an ERC721 top-down composable token using getChild.
This enables older contracts to use the new token functionality.
If an ERC20 contract does not implement the ERC223 function transfer(address _to, uint _value, bytes _data), the getERC20 function can be used to transfer ERC20 tokens into an ERC20TopDown composable token.
This provides flexibility when integrating ERC20 tokens with composable tokens.
Composable tokens are designed to work with older contracts and different token types, providing flexibility in how tokens can be combined.
Reference implementation
See the following reference.
Security considerations
Further discussion is needed.
References
Matt Lockyer mattdlockyer@gmail.com, Nick Mudge nick@perfectabstractions.com, Jordan Schalm jordan.schalm@gmail.com, sebastian echeverry sebastian.echeverry@robotouniverse.com, Zainan Victor Zhou (@xinbenlv), "ERC-998: Composable Non-Fungible Token [DRAFT]," Ethereum Improvement Proposals, no. 998, July 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-998.
Conclusion
This article covered ERC998, a proposal extending ERC721 so that tokens can own other ERC721 and ERC20 tokens. I hope this overview was helpful.
If you have any questions, feel free to contact me via Twitter DM or the channels below.
I also share information on other platforms, so feel free to explore them.


