Chapter 6 · Tokens · lesson 6
ERC-721 and NFTs
15 min
Builds on ERC-20 from scratch
Chapter 6 · Tokens · lesson 6
15 min
Builds on ERC-20 from scratch
ERC-20 tracks how much you have. ERC-721 tracks which ones.
That single change — from a balance to an identity — is the whole difference between fungible and non-fungible.
mapping(address => uint256) public balanceOf; // ERC-20: an amount
mapping(uint256 => address) public ownerOf; // ERC-721: an owner per id
Build one.
ownerOf(id) replaces amounts. Each token id has exactly one owner.balanceOf still exists, but now counts how many tokens you hold, not a
quantity of anything.setApprovalForAll for an operator over
your whole collection.setApprovalForAll is the NFT version of the unlimited approval, and it is
worse: it grants control of every token in that collection, including ones
you have not bought yet.
Marketplaces require it, because they cannot know in advance which token you will list. Nearly every NFT theft that was not a private key leak went through an operator approval the victim granted and forgot.
Predict
It is a row in a mapping saying an address owns id 42, plus usually a URL.
It is not the image. It confers no copyright, no legal ownership, and no enforcement outside the chain. Where the picture lives is the next lesson, and the answer disappoints most people.
Check