Track 5 · Tokens · lesson 6

ERC-721 and NFTs

15 min


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.

NFT.solCompose
Loading editor…
Build a minimal NFT. Ownership of one specific thing, not a balance.
Run the code to see what happens.
Nudge
The shape is close to ERC-20, with one change: the mapping goes from id to owner, not address to amount.
Show me the approach
`mint` increments a counter, assigns `ownerOf[id]`, bumps `balanceOf[to]` by one, and emits. `transferFrom` checks twice, then moves ownership and adjusts both balances.
Show me the code
Increment `totalMinted` before using it as the id, so ids start at 1. In `transferFrom`, check `ownerOf[id] != from` and then `msg.sender != ownerOf[id]`.
Explain the solution
Note `balanceOf` counts *tokens held*, not a quantity — a real difference from ERC-20 that trips people writing their first NFT. The real ERC-721 adds approvals, operator approvals and a safe-transfer receiver check, but this is the core.

What changed, concretely

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

Your minimal NFT has no `safeTransferFrom`. What does the real standard's version add?

Choose one answer

What an NFT is not

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

In ERC-721, what does `balanceOf(alice)` return?

Choose one answer

Worth remembering

  • ERC-721 maps token id to owner, instead of address to amount.
  • `balanceOf` counts tokens held, not a quantity.
  • Transfers move a specific id — there are no partial transfers.
  • `setApprovalForAll` grants an operator control of your entire collection, including future tokens.
  • `safeTransferFrom` checks a contract recipient can handle NFTs, at the cost of an external call.