ERC-721 Composition Extension

An ERC-721 extension for tokens that hold assets in a container of their own: nothing leaves the container while the token exists, and any contract can read what the token was minted with.

It grew out of the two ERC-721 extensions @jay suggested in the ERC-6551 thread: one to read who burned a token, now ERC-8423, and one for the canonical assets of an NFT. This proposal is the second, narrowed to tokens whose assets stay locked until the burn, and it builds on the first.

The problem. ERC-6551’s Security Considerations walk through a case every buyer of a token with an account should know: Alice’s token owns an account holding 10 ETH, Bob offers to buy the token, Alice empties the account and accepts the offer, and Bob receives an empty token. The mitigations it suggests include a list of the assets expected to stay in the account and a lock that keeps the owner from taking them out, and it leaves them to marketplaces and implementations, which keeps ERC-6551 general. This proposal takes up those two suggestions for one kind of token. Without a common interface, a marketplace, an indexer or a wallet cannot tell a lower bound it can verify from a figure in the metadata. It falls back on the balance, which says what is there now, not what is bound to stay.

The idea. A gold ring carries two things at once: the goldsmith’s work, and the gold it is made of. You cannot take the gold without destroying the ring. Picture a collector who brings their own gold to a goldsmith: the piece is made of that gold, and getting the gold back means destroying the piece. On chain, the gold is the native asset or ERC-20 tokens, the piece is an ERC-721 token, and destroying it is the burn. Each token declares at mint what it is made of and keeps it in a container that nobody can operate while the token exists. After the burn, only the address that owned the token at that moment can take the assets out, as recorded by ERC-8423.

The interface.

/// @dev The ERC-165 identifier for this interface is 0xd60a6c7e
interface IERC721Composition /* is IERC721, IERC721BurnRecord, IERC165 */ {
    /// @param asset  The ERC-20 contract, or 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE (ERC-7528) for the native asset
    /// @param amount The declared amount, in the asset's smallest unit, never zero
    struct Constituent {
        address asset;
        uint256 amount;
    }

    /// @notice Emitted once per token, in the transaction that mints it, batch mints included
    event CompositionDeclared(
        uint256 indexed tokenId,
        address indexed account,
        bytes4 accountType,
        bytes accountData,
        Constituent[] constituents
    );

    /// @notice The declared composition, fixed at mint, sorted by asset address
    function compositionOf(uint256 tokenId) external view returns (Constituent[] memory constituents);

    /// @notice The container that holds the constituents, its type, and the data to verify it
    function compositionAccountOf(uint256 tokenId)
        external
        view
        returns (address account, bytes4 accountType, bytes memory accountData);
}

accountType is the ERC-165 identifier of the container’s interface, which the container confirms through supportsInterface. For an ERC-6551 account it is 0x6faff5f1, accountData is abi.encode(implementation, salt), and account must be the address the canonical registry computes for this chain, this contract and this token id. Other container types define their own data. The container takes its orders from ERC-8423: burnedBy is how it knows who may open it.

Proposed rules, in short.

  1. Locked until the burn. While the token exists, the container MUST NOT execute, approve or validate anything that moves its assets out; after the burn, only the address returned by burnedBy can move them. The rule is about the result, not the mechanism, so any container type can meet it. It cannot be changed later, and on any other chain the container validates nothing.
  2. ERC-6551 accounts spell the rule out on the two calls that are easiest to get wrong: before the burn, isValidSigner and isValidSignature never return their magic values; after the burn, they do only for the burner.
  3. One container per token, used by no other token. A token cannot be transferred to its own container.
  4. Fixed at mint. Composition and container never change, not even after the burn.
  5. A lower bound. When the minting transaction ends, the container holds at least each declared amount, and no declared amount exceeds what the container actually received. Anything sent later is surplus: it stays inside and leaves with the rest after the burn.
  6. One event per token, at mint, batch mints included, with the same data the two functions return.
  7. No re-minting of a burned id, so the recorded burner of a token never changes.
  8. No reverts. Both functions answer for any id. An empty composition means “not declared”, never “declared as zero”; after the burn they return the same values as before.
  9. Constituents are the native asset, through ERC-7528, and ERC-20 tokens, each at most once, sorted by address, with amounts above zero. Tokens whose balance can drop without a transfer by the holder, such as downward rebasing, freezable or seizable tokens, SHOULD NOT be declared: no container can protect their lower bound.

Where each fact is read.

Fact Read with Announced by Changed by
Declared composition, the lower bound compositionOf CompositionDeclared, at mint Nothing, fixed at mint
Container, its type and verification data compositionAccountOf CompositionDeclared, at mint Nothing, fixed at mint
Owner when the token was burned burnedBy (ERC-8423) Transfer to the zero address Only the burn, once
What the container holds now The constituents: balanceOf, or the native balance Nothing in this proposal Later deposits, actions by a constituent’s issuer, withdrawals after the burn

The table comes from the Contracts can’t read events thread: thanks to @Zealynx for raising the question and to @priors-agents for proposing the list.

Out of scope: minting and burning, as in ERC-8423; prices, valuations and oracles; rebalancing and yield; splitting a token into shares; other asset types, such as NFTs inside the container, which could come in a separate proposal.

Related work. Two proposals come closest, and I would rather say so first:

Proposal What it does How this proposal differs
ERC-3589 (2021, Stagnant) Assembles ETH and tokens into an NFT and gives them back on burn; the composition is a hash in the token id One container per token instead of a shared balance; a composition contracts can read; the lock as a rule rather than a property of the implementation
ERC-8060 (open PR) Embeds native ETH in an NFT, redeemable only by burning it Several assets; a composition fixed at mint and verifiable token by token; the burn stays outside the interface

Others it builds on or differs from: ERC-6551 provides the container and suggests the mitigations above; ERC-4353 already suggested staking tokens at mint and withdrawing them only at burn; ERC-7527 pays back a premium on unwrap, priced by a function over a shared reserve; ERC-998, ERC-7401, ERC-5700 and ERC-7590 bind contents the owner can take out; ERC-5058, ERC-5753, ERC-6982 and ERC-7066 lock a token’s transfers rather than its contents; ERC-8414 (open PR) reaches the same container approach for a different purpose; ERC-7656 could serve as a custom container; and a shared container such as ERC-4626 cannot fit, because the lower bound must be checkable token by token. Pointers to prior art I missed are welcome.

What verification still needs. The data above tell a reader which container holds the assets and which code governs it; they cannot tell whether that code really keeps the assets in. A reader has to recognise two pieces of code: the container’s, and the token contract’s, because the container opens for whoever burnedBy names. It is the same trust ERC-6551 places in ownerOf. A minimal prototype includes a reader contract that does this for an ERC-6551 account and for a custom container, and that turns away an ordinary account the owner can empty. So the declared composition is a lower bound a reader can check under these conditions, not a statement about what the token is worth.

My question: for a marketplace, an indexer or a wallet, are the address, the type and the verification data returned by compositionAccountOf enough to verify the container on chain, or would you need something else?

The prototype and its tests will go into a public repository, and I will open a PR once ERC-8423 is merged.

Thanks for carrying the table over. On your question, for an ERC-6551 container I think the three values are enough once the spec adds two things.

The account needs code by the end of the minting transaction. A counterfactual account can already hold the assets, but supportsInterface on an address with no code fails, so a reader can’t run its check until someone calls createAccount. The registry returns the existing address when the account is already deployed, so making it a MUST costs nothing in that case.

The implementation in accountData also has to be the code that actually runs. With Tokenbound’s V3 contracts that address can be AccountProxy, an ERC-1967 proxy that delegates to an implementation kept in the account’s own storage, and AccountV3Upgradable lets the owner upgrade it to anything the guardian trusts. No contract can read another contract’s ERC-1967 slot, so a reader that recognises implementation addresses wouldn’t see that upgrade. I’d say the implementation MUST NOT delegate onward or be upgradeable, and then recognising that one address is enough.

On cycles, the ban on sending a token to its own container covers length one. If A sits in B’s container and B in A’s, neither container can act, so both stay locked with their constituents for good, assuming the burn needs the owner or an approved operator. ERC-6551 leaves longer cycles out of scope, but here the token contract knows every container it created, so it can reject transfers to any container of the collection and rule out every cycle inside one collection with a single lookup.

Thanks @Zealynx, this answers the question, and I agree with all three points.

The container already has to be a contract by the end of the minting transaction: the prototype creates the account through the registry inside the mint, so a reader can run its checks straight away. I left that rule out of the summary above; it will be a MUST in the specification.

Rule 1 already says the lock cannot be changed later, but your wording names the case it has to exclude and gives the reader something concrete to check. The specification will say that the implementation in accountData is the code that runs behind the registry’s proxy, and that it MUST NOT be upgradeable or delegate onward. Recognising that one address is then enough.

On cycles, I will replace the own-container check with your rule: the contract rejects a transfer, or a mint, to any container it has declared. Cycles that pass through other accounts or other collections stay in the Security Considerations, as in ERC-6551.

The reference implementation and its tests are now public: GitHub - antferr/erc721-composition: Reference implementation and tests for a proposed ERC-721 extension: tokens that hold assets in a container locked until the burn · GitHub

It follows the rules in the opening post plus the three from my reply to @Zealynx, and every specification test runs on two container types: an ERC-6551 account from the canonical registry, and a minimal container of its own. A few results that bear on the discussion so far:

  • Any declared container is refused as a recipient, by transfer or by mint. Random sequences of mints, transfers, burns and deposits check after every step that a token inside another token’s container is always older than that token, so no cycle can close within the collection (invariant_InsideImpliesOlder).
  • Why the implementation must not be upgradeable: an account behind an upgradeable proxy keeps its address, its code and its declared data, and after an upgrade the holder empties it while the token exists (test_Reader_RejectsUpgradeableProxyAccount). The example reader turns it away because it recognises the implementation itself.
  • Thirteen defects are kept in the repository, each caught by the same check that passes on the reference implementation. One of them is an account that locks isValidSigner but still accepts the holder’s ERC-1271 signatures.

@SergeevDmitry, the invariants follow your point in the “Contracts can’t read events” thread: functions and events are compared with a model built from the operations, not only with each other. One of the defects writes the same wrong container to the function and to the event; the two agree, and only the model catches it (test_Defect_X5_SeenByInvariant).

The code has not been audited. Review of the code is as welcome as review of the rules.

Does the reader also reject an upgradeable NFT contract even when its container implementation is immutable? An NFT upgrade could change what burnedBy returns and let the container release assets without a genuine burn. You already identify trust in both contracts in the opening post. A regression test where only the NFT implementation changes would help show how the reader enforces that boundary.

From reading CompositionReader, only while the proxy isn’t on the list. _verify checks recognizedTokenCode[token.codehash], and for a token behind a proxy that’s the proxy’s own hash, which an upgrade doesn’t change. A list that includes it keeps returning Verified after burnedBy starts answering differently, and the locked account obeys that answer, as test_Reader_RejectsUnreviewedTokenContract shows for a token off the list.

Accounts got out of this with rule 11 and by recognising the implementation address. The token contract has neither, so I’d give it the same wording: not upgradeable, not delegating. The regression test could copy test_Reader_RejectsUpgradeableProxyAccount with the proxy on the token side and a naive reader that lists it. That test ends in Verified, which records that the check depends on whoever builds the list.

Thanks @SergeevDmitry and @Zealynx. The answer is yes, as long as the proxy is not on the reader’s list: the reader checks the code hash at the token’s address, and an upgrade does not change the proxy’s hash.

The regression test is now in the repository, test_Reader_RejectsUpgradeableTokenContract, and it has the shape @Zealynx describes: the proxy on the token side, an immutable container implementation, and only the NFT implementation changing. Before the upgrade the container is locked, and a reader that lists the token’s logic turns the proxy away. After an upgrade that rewrites burnedBy, the address, the code hash and the declared container are all unchanged, a naive reader that lists the proxy still returns Verified, and the container releases its assets with no burn.

So the token contract gets the same wording as the account: it MUST NOT be upgradeable or delegate its logic to code that can change. Narrowing the rule to burnedBy would not be enough, since an upgrade could also move the token to someone else before an honest burn, mint a burned id again or rewrite the composition. The naive reader stays in the test as a record that the check depends on whoever builds the list, as it does for accounts. The README already lists the new rule as rule 13.

1 Like

Rule 5’s end-of-transaction funding requirement leaves the timing of the lower bound unclear for contracts called during minting. The reference mint funds the container before _safeMint, so the assets are present when onERC721Received runs. Making that ordering a requirement would let consumers rely on the same guarantee across implementations.

There’s also a case where the transaction should legitimately finish below the declared amounts: the recipient burns the NFT and withdraws its assets inside onERC721Received. That appears consistent with release after burn, but conflicts with the literal end-of-transaction requirement. I’d specify that funding must be complete before the NFT becomes observable as live, with the lower bound applying until burn. Could the specification make that timing explicit and add a receiver callback test that checks the backing, then burns and withdraws during mint?

Thanks @Revinand, both points hold, and the test you suggest is now in the repository.

Rules 5 and 10 take the timing you propose, pinned to the mint itself: a token is minted only once its composition and container are declared, its container is already a contract, and it holds at least each declared amount. A contract that receives the token, through onERC721Received or otherwise, finds all of that in place. Burning the token and withdrawing in the same transaction is then release after the burn, not a broken lower bound. The README already has the new wording.

test_MintCallback_SeesBackedToken checks the backing from inside onERC721Received, and test_MintCallback_BurnAndWithdrawDuringMint then burns the token and empties its container during the mint, on both container types. A fourteenth defect mints first and funds afterwards: a check at the end of the transaction passes, the one in the callback fails (test_Defect_X14_MintBeforeFund). The batch example now funds every container before minting, too.

One nuance on the bound “applying until burn”: what holds until the burn is the lock, so no call to the container can take a constituent below what it held at mint. The balance can still fall if a constituent’s issuer rebases, freezes or seizes it, which is why rule 9 advises against declaring such tokens and a reader compares balances with the declared amounts.