ERC-6551 · Review · ERC · 2023-02-23
ERC-6551: Non-fungible Token Bound Accounts
Gives every NFT its own smart contract account, at a deterministic address from a singleton registry, so the NFT can hold assets and act on-chain while control follows whoever owns the token.
What problem does it solve?
An ERC-721 token can be owned, but it cannot own anything or do anything. That makes it hard to model things like:
- a game character that accumulates items and currency over time,
- a car made of many components,
- a portfolio of tokens that should be sold as a single unit,
- a membership card that records its history of interactions.
ERC-6551 gives each NFT an Ethereum account of its own, a token bound account (TBA). Whoever holds the NFT controls the account. Sell the NFT and everything inside the account goes with it. Crucially, this works for existing NFTs without any change to their contracts.
How it works
Singleton registry. A permissionless, ownerless registry is deployed at the same address on every chain, 0x000000006551c19487814612e58FE06813775758, via Nick's deterministic deployment factory. It has two functions:
account(implementation, salt, chainId, tokenContract, tokenId)– compute the TBA address.createAccount(...)– deploy it withCREATE2; returns the existing address if already deployed, and emitsERC6551AccountCreated.
Because addresses are deterministic, a TBA exists counterfactually: it can receive assets before it is deployed.
Account bytecode. Each TBA is an ERC-1167 minimal proxy to an implementation, with salt, chainId, tokenContract and tokenId appended as constant data. Different implementations or salts give the same NFT different accounts, so one NFT may have several TBAs.
Account interface (ERC-165 ID 0x6faff5f1):
receive()– accept ETH.token()– returns(chainId, tokenContract, tokenId); must never change.state()– a value that should change whenever the account changes state.isValidSigner(signer, context)– returns0x523e3260ifsignermay act; by default the NFT's holder.
Accounts must also support ERC-1271 signature checks and expose at least one execution interface detectable via ERC-165. The optional IERC6551Executable (0x51945447) defines execute(to, value, data, operation) with operations 0 = CALL, 1 = DELEGATECALL, 2 = CREATE, 3 = CREATE2.
Sample code
Finding, creating and using an NFT's account (ethers v6):
import { Contract, ZeroHash, parseEther } from "ethers";
const REGISTRY = "0x000000006551c19487814612e58FE06813775758";
const registry = new Contract(REGISTRY, [
"function account(address,bytes32,uint256,address,uint256) view returns (address)",
"function createAccount(address,bytes32,uint256,address,uint256) returns (address)",
], nftHolder);
const { chainId } = await provider.getNetwork();
const args = [accountImpl, ZeroHash, chainId, nftContract, tokenId] as const;
const tba = await registry.account(...args); // works before deployment
await (await registry.createAccount(...args)).wait();
// The NFT holder makes the TBA send 0.1 ETH (operation 0 = CALL).
const acct = new Contract(tba, [
"function execute(address,uint256,bytes,uint8) payable returns (bytes)",
], nftHolder);
await (await acct.execute(recipient, parseEther("0.1"), "0x", 0)).wait();
Things to know
- Status: Review. The document is not yet Final, so details may still change.
- Sale fraud: a seller can drain the TBA just before a buyer's offer fills. Marketplaces should bind orders to the account's
state()or to committed assets; the standard leaves this to them. - Ownership cycles: sending an NFT into its own TBA (or any loop of TBAs) locks the NFT and everything in the account permanently.
- Only the account implementation needs to be trusted; the registry is immutable. Check which implementation a TBA uses before depositing.
- TBAs can also act as ERC-4337 smart accounts if the implementation supports it. Compare ERC-6059, which nests NFTs directly inside other NFTs.