10. Web2 to Web3: ERC-20, ERC-721 & IPFS
Overview
Section titled “Overview”A “standard” on Ethereum is an agreed-upon set of functions that a contract exposes so that wallets, exchanges, and other contracts can interact with it without custom code. This lesson covers the two you will meet everywhere — ERC-20 (fungible tokens) and ERC-721 (non-fungible tokens / NFTs) — and IPFS, the storage layer NFT metadata typically lives on. The practical message is: you rarely write these from scratch, you extend audited implementations.
Key concepts
Section titled “Key concepts”- What a token actually is — not a coin in a wallet, but a contract that keeps a ledger.
An ERC-20 is a
mapping(address => uint256)of balances plus standard functions to move them. “Owning a token” means the contract’s mapping credits your address. - ERC-20 (fungible) — every unit is interchangeable, like currency. The standard defines
transfer,approve,transferFrom,balanceOf,totalSupply, and theTransfer/Approvalevents. Because every ERC-20 shares this interface, any wallet or DEX can handle a token it has never seen before. - approve / transferFrom — the two-step pattern that lets a contract move your tokens on
your behalf (you
approvea spender, it later callstransferFrom). This is what makes DEXs and vending contracts possible — and, done carelessly, a common source of risk. - ERC-721 (non-fungible) — each token has a unique
tokenIdand an individual owner. The standard tracksownerOf(tokenId)and per-token transfers. Used for anything where items are distinct: art, memberships, deeds. - tokenURI and metadata — an ERC-721 doesn’t store the image or attributes onchain
(that would be enormous and expensive). It stores a
tokenURIthat points to a JSON metadata file, which in turn points to the image. - IPFS — a content-addressed storage network: a file’s address (its CID) is a hash of its contents, so the link can’t silently change what it serves. This is why NFT metadata is pinned to IPFS rather than a normal URL that an owner could swap out.
- OpenZeppelin — the community’s audited, reusable implementations of these standards.
You import and extend
ERC20orERC721and add only your specifics, rather than re-implementing (and re-bugging) the standard.
Common pitfalls
Section titled “Common pitfalls”- Believing the NFT image is “on the blockchain.” Usually only a pointer is onchain; the metadata and image live on IPFS (or, riskily, a plain server that can go away).
- Using a mutable
http://URL for metadata, which lets the content behind an NFT change or vanish. Content-addressed IPFS avoids this. - Writing your own ERC-20/721 from scratch for production. Reach for OpenZeppelin; the audited version has already handled the edge cases.
- Misunderstanding
approve— granting an unlimited allowance to an untrusted contract is a real way funds get drained.
Check yourself
Section titled “Check yourself”- What makes an ERC-20 “fungible” and an ERC-721 “non-fungible,” in terms of what the contract stores?
- Where does an NFT’s image actually live, and what part is onchain?
- Why is content-addressed IPFS storage safer for metadata than a normal web URL?
Go deeper
Section titled “Go deeper”There are two ways to understand tokens: read a minimal implementation to see what a standard actually requires, then use a battle-tested library to ship one safely. Do both.
See what’s under the hood — Solidity by Example
- ERC20 — a complete fungible-token contract in ~60 lines:
balanceOf,transfer,approve,transferFrom, and theTransfer/Approvalevents. - ERC721 — the non-fungible equivalent, with per-token ownership and approvals.
- ERC1155 — one contract holding many token types, fungible and non-fungible together.
Ship it safely — OpenZeppelin Contracts
Real projects almost never hand-write these. They inherit from OpenZeppelin, whose implementations are widely audited and reused. Rolling your own token invites the exact bugs these libraries have already fixed.
- ERC20 — inherit
ERC20, set name/symbol in the constructor, and call_mint. Add only the extensions you need:ERC20Burnable,ERC20Pausable,ERC20Capped,ERC20Permit(gasless approvals, EIP-2612), orERC20Votes. - ERC721 — base
ERC721plus extensions likeERC721URIStorage(per-token metadata URI — this is where your IPFStokenURIgoes),ERC721Enumerable, andERC721Pausable. - Access control — gate
mint/pausewithOwnable(single owner,onlyOwner) orAccessControl(named roles likeMINTER_ROLE, used viaonlyRole). - Utilities —
SafeERC20for calling tokens that don’t return a bool, plusReentrancyGuardandPausable. - Contracts Wizard — an interactive tool that generates a correct OpenZeppelin token from checkboxes. Great for seeing how the pieces fit; still read what it produces.
Sources: Solidity by Example (Cyfrin) and the OpenZeppelin Contracts docs. Storing tokenURI metadata on IPFS is covered in the notes above.
Watch the Video
Section titled “Watch the Video”Video credit: Austin Griffith · Duration: 1 hour 1 minute
