ERC-721 · Final · ERC · 2018-01-24
ERC-721: Non-Fungible Token Standard
The standard interface for non-fungible tokens (NFTs): each token has a unique ID and a single owner, with a common way to transfer it and discover its metadata.
What problem does it solve?
ERC-20 treats every unit of a token as identical, so it cannot represent a specific collectible, a plot of virtual land or a ticket for seat 12B. Before ERC-721, projects such as CryptoKitties had to invent their own contracts, and marketplaces had to integrate each one separately.
ERC-721 defines a common interface for tokens where each unit is distinct and has exactly one owner. It is the basis of NFTs on Ethereum.
How it works
Every token has a uint256 tokenId. The contract tracks ownerOf(tokenId) and exposes:
| Function | Purpose |
|---|---|
balanceOf(owner) |
How many tokens an address owns |
ownerOf(tokenId) |
Current owner of one token |
safeTransferFrom(from, to, tokenId) |
Transfer, refusing to send to a contract that cannot handle NFTs |
transferFrom(from, to, tokenId) |
Transfer without that check |
approve(to, tokenId) |
Allow one address to move one token |
setApprovalForAll(operator, approved) |
Allow an operator (e.g. a marketplace) to move all your tokens |
getApproved / isApprovedForAll |
Read approvals |
safeTransferFrom calls onERC721Received on the recipient when it is a contract, so tokens are not accidentally locked in contracts that cannot move them. Events Transfer, Approval and ApprovalForAll allow indexers to reconstruct ownership.
Two optional extensions are near-universal: Metadata (name, symbol, tokenURI(tokenId) returning a URL or data URI to a JSON file with image and attributes) and Enumerable (totalSupply, tokenByIndex, tokenOfOwnerByIndex). Contracts advertise what they support through ERC-165 supportsInterface.
Sample code
A minimal mintable NFT using OpenZeppelin:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC721URIStorage, ERC721} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
contract Tickets is ERC721URIStorage, Ownable {
uint256 public nextId = 1;
constructor() ERC721("Tickets", "TIX") Ownable(msg.sender) {}
function mint(address to, string calldata uri) external onlyOwner returns (uint256 id) {
id = nextId++;
_safeMint(to, id);
_setTokenURI(id, uri); // e.g. ipfs://.../12.json
}
}
Looking up an NFT's owner and metadata (viem):
import { createPublicClient, http, parseAbi } from "viem";
import { mainnet } from "viem/chains";
const client = createPublicClient({ chain: mainnet, transport: http() });
const abi = parseAbi([
"function ownerOf(uint256) view returns (address)",
"function tokenURI(uint256) view returns (string)",
]);
const nft = { address: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", abi } as const; // BAYC
const owner = await client.readContract({ ...nft, functionName: "ownerOf", args: [1n] });
const uri = await client.readContract({ ...nft, functionName: "tokenURI", args: [1n] });
A contract that wants to receive NFTs via safeTransferFrom must implement:
function onERC721Received(address, address, uint256, bytes calldata) external returns (bytes4) {
return this.onERC721Received.selector; // 0x150b7a02
}
Things to know
tokenURImetadata is only as permanent as where it points. IPFS or on-chain data URIs are more durable than a web server.setApprovalForAllis powerful: a malicious operator can drain every token in the collection. Marketplace approvals are the most common phishing target.- Royalties are not part of ERC-721; ERC-2981 adds a standard way to signal them, but enforcement is up to marketplaces.
- Need many copies of the same item, or fungible and non-fungible tokens in one contract? Look at ERC-1155.