One of Dinda's PTS and one of Yoga's are no different. Coffee Edition Shirt certificates are another story. Raka makes 100 numbered certificates for a limited-edition shirt, symbol COFFEE, and each number exists only once. In the shop they live in a certificate register: "No. 1 belongs to Dinda, No. 2 belongs to Yoga", plus one line with the address where each number's description can be read. The register doesn't hold photos of the shirt; it only records the number, the owner, and a pointer to the description. The on-chain version of that register uses ERC-721, the standard for NFTs (Non-Fungible Tokens): tokens whose every unit is unique, with its own number (tokenId) and its own owner, so they can't be swapped one for one like PTS.
ERC-20 and ERC-721
| ERC-20 (PTS) | ERC-721 (COFFEE) | |
|---|---|---|
| Each unit | identical, interchangeable | unique, has a tokenId |
balanceOf(address) |
the number of tokens (with decimals) | how many NFTs it owns |
| Owner | no "owner of token number so-and-so" | ownerOf(tokenId) |
| Moving | transfer(to, value) |
safeTransferFrom(from, to, tokenId) |
| Fractions | yes, decimals |
none; 1 NFT is 1 |
Transfer event |
value in data |
tokenId is indexed too: 4 topics, empty data |
The event signature is the same, Transfer(address,address,uint256), so its topic0 is the same too (0xddf252ad...). The difference is the topic count: ERC-721 puts tokenId in the third topic.
Coffee Edition Shirt with OpenZeppelin
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
contract CoffeeEditionShirt is ERC721, Ownable {
uint256 public constant MAX_SUPPLY = 100;
uint256 public totalMinted;
error SoldOut();
constructor() ERC721("Coffee Edition Shirt", "COFFEE") Ownable(msg.sender) {}
function mint(address to) external onlyOwner returns (uint256) {
if (totalMinted == MAX_SUPPLY) revert SoldOut();
uint256 tokenId = ++totalMinted;
_safeMint(to, tokenId);
return tokenId;
}
function _baseURI() internal pure override returns (string memory) {
return "https://raka-shirt-shop.test/coffee/";
}
}
ERC721("Coffee Edition Shirt", "COFFEE"): the name and symbol, from OpenZeppelin 5.6.1. Inside it is amapping(uint256 => address)fromtokenIdto owner.++totalMintedincrements first and then uses the value, so the first number is 1 and the last is 100._safeMint(to, tokenId)mints a new token and emitsTransferfrom the zero address._mintdoes the same without checking the receiver.onlyOwner: only Raka may mint._baseURIis the prefix of the metadata address;tokenURI(id)= prefix + number.
Compiled with solc 0.8.37 and deployed from account #0 after RakaPoints. That's account #0's second transaction, so the address is 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 (1,991,736 gas). supportsInterface(0x80ac58cd) answers true: that's how wallets and marketplaces check "this is ERC-721".
Mint: #1 for Dinda, #2 for Yoga
Raka calls mint(Dinda) and then mint(Yoga). The first mint's log, exactly as in its receipt:
topics: 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef Transfer(address,address,uint256)
0x0000000000000000000000000000000000000000000000000000000000000000 from: the zero address (mint)
0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8 to: Dinda
0x0000000000000000000000000000000000000000000000000000000000000001 tokenId: 1
data: 0x
| Read | Result |
|---|---|
ownerOf(1) |
Dinda 0x7099...79C8 |
ownerOf(2) |
Yoga 0x3C44...93BC |
balanceOf(Dinda) |
1 |
totalMinted() |
2 |
ownerOf(3) |
revert ERC721NonexistentToken(3) |
So a new NFT is made with _mint(to, tokenId) or _safeMint(to, tokenId), not with a transferFrom from the zero address or by filling in a URI.
safeTransferFrom and onERC721Received
Dinda moves certificate #1 to her second account (test account #3, 0x90F79bf6EB2c4f870365E785982E1f101E93b906):
await dinda.writeContract({ address: COFFEE, abi, functionName: 'safeTransferFrom', args: [DINDA, DINDA_2, 1n] })
// Transfer(Dinda, Dinda#2, 1); ownerOf(1) is now Dinda#2
Only the owner, or someone the owner approved, may move it. Yoga tries to move Dinda's #1: ERC721InsufficientApproval(0x3C44...93BC, 1).
Why safe? When the receiver is a contract, safeTransferFrom asks it first: the contract must have an onERC721Received function that returns its selector, 0x150b7a02. If it doesn't, the transfer is undone. Yoga mistakenly enters RakaPoints's address (a contract that knows nothing about NFTs):
Error: ERC721InvalidReceiver(address receiver)
(0x5FbDB2315678afecb367f032d93F642f64180aa3)
With a plain transferFrom, the same transfer succeeds: ownerOf(2) becomes RakaPoints's address, and since RakaPoints has no function to move NFTs, certificate #2 is locked there forever. That's why you use safeTransferFrom.
tokenURI and off-chain metadata
await publicClient.readContract({ address: COFFEE, abi, functionName: 'tokenURI', args: [1n] })
// 'https://raka-shirt-shop.test/coffee/1'
tokenURI(1) only returns address text: not the image, not a JSON file. The contract doesn't even store that text per token: it joins the prefix from _baseURI with the number. The image and description live outside the blockchain (off-chain), in a JSON file the shop's server serves. Its format follows the ERC-721 metadata schema (name, description, image), and attributes is a marketplace convention. A sample of its contents (the .test domain is only an example and can't be opened):
{
"name": "Coffee Edition Shirt #1",
"description": "Numbered certificate, limited Coffee Print Shirt, Raka's Shirt Shop. 1 of 100.",
"image": "https://raka-shirt-shop.test/coffee/1.png",
"attributes": [
{ "trait_type": "Size", "value": "L" },
{ "trait_type": "Number", "value": 1, "display_type": "number" }
]
}
To be honest: the NFT doesn't contain the image. The blockchain only guarantees "#1 belongs to this address" and the address text. If the shop's server goes down or its contents change, the image goes or changes with it. IPFS (the next lesson) ties the address to the file's contents, so they can't be swapped quietly.
The limit of 100: MAX_SUPPLY
The limit is checked in the contract, not on the website, because anyone can call the contract without going through the website. On the local chain, Raka minted up to #100, then tried once more:
The contract function "mint" reverted.
Error: SoldOut()
totalMinted stops at 100, and there's no way to mint #101 without deploying a new contract.
⚠️ What people often get wrong when building an NFT
- Thinking the NFT stores its image. On-chain there's only the owner and the metadata address. Keep metadata somewhere that lasts, such as IPFS.
transferFromto a contract. If the receiving contract can't move NFTs, the token is locked forever. UsesafeTransferFrom.- A supply limit only in the frontend. The contract can be called directly; check
MAX_SUPPLYin the contract. - tokenIds starting at 0 by accident.
totalMinted++hands out 0 first;++totalMintedhands out 1. Decide, and write it in the docs.
TL;DR: ERC-721 is the NFT standard: each token has its own tokenId and owner (ownerOf), balanceOf counts NFTs, and there are no decimals. Coffee Edition Shirt (COFFEE, OpenZeppelin 5.6.1) mints #1 to #100 through an onlyOwner mint, uses _safeMint, and refuses #101 with SoldOut(). The Transfer event has the same signature as ERC-20's, but tokenId becomes the third topic. safeTransferFrom checks onERC721Received (0x150b7a02) on a receiving contract and reverts with ERC721InvalidReceiver when it's missing; a plain transferFrom can lock an NFT in a contract. tokenURI(1) = 'https://raka-shirt-shop.test/coffee/1': only a pointer to an off-chain JSON file; the NFT doesn't contain the image.
⚡ Fungible versus non-fungible
Meaning: One of Dinda's PTS and one of Yoga's are no different: ERC-20 is fungible. COFFEE certificates use ERC-721: every token is unique, with its own tokenId and owner (ownerOf), and no decimals.
⚡ Mint: the first number is 1
Write:
uint256 tokenId = ++totalMinted;
_safeMint(to, tokenId);
Meaning: ++totalMinted increments first and then uses the value, so the first number is 1 and the last is 100. Mint #101 is refused with SoldOut().
⚡ Why safeTransferFrom
Guess first: Yoga mistakenly enters RakaPoints's address, a contract that knows nothing about NFTs. With a plain transferFrom, what happens?
The transfer succeeds, and certificate #2 is locked in RakaPoints forever. safeTransferFrom refuses it with ERC721InvalidReceiver.
Meaning: A receiving contract must have an onERC721Received that returns 0x150b7a02.
⚡ The NFT doesn't contain the image
Meaning: tokenURI(1) only returns address text. The image and description live in an off-chain JSON file, so if the shop's server goes down, the image goes with it.