Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article introduces ERC-721.
Understanding ERC-20 first will make ERC-721 easier to follow. The following article introduces the fungible token standard.
[ERC20] ERC-20 Explained from the Ground Up
What is ERC-721?
This section introduces the characteristics of ERC-721. ERC-721 is a standard for non-fungible tokens. The following definition is adapted from Coinbase's explanation.
Unlike fungible assets such as bitcoin or dollar bills, non-fungible tokens are individually distinct. They can be used to represent ownership of digital assets, such as artwork, recordings, virtual real estate, and virtual pets.
Each token is distinct and is not interchangeable with another token on a like-for-like basis. This is the meaning of NFT (non-fungible token). NFTs can still be traded or exchanged; non-fungibility means that their identities and potentially their values differ. In contrast, fungible tokens of the same denomination and type can be exchanged for an equivalent amount. ERC-20 is a standard for such fungible tokens.
Popular NFTs using ERC-721
Not every NFT uses ERC-721. Another widely used standard is ERC-1155. Its main features include the following.
Features of ERC-1155
- Multiple items can be transferred in a single batch.
- A transaction can distribute NFTs or fungible assets to multiple recipients using additional batching logic; the standard batch transfer function itself has one recipient.
- Combining operations can reduce gas costs compared with separate transactions.
Some prominent NFT collections use ERC-721, while others use ERC-1155 or different standards. Examples of collections and projects associated with ERC-721 include the following.
ERC-721 NFT collections
- CryptoKitties
- Sorare
- ENS
- Bored Ape Yacht Club
- Art Blocks
- CloneX
You may recognize some of these names. Many other well-known collections also use ERC-721. Individual projects may have multiple contracts or use other standards alongside ERC-721. Etherscan provides a list of NFT token contracts.
https://etherscan.io/tokens-nft
EIP-721
What is an EIP?
So far, we have used the term ERC-721. What does EIP-721 mean?
EIP stands for Ethereum Improvement Proposal, a document that proposes features, standards, or processes for Ethereum. Further information is available on the EIP website.
In simple terms, EIPs are proposals discussed as part of Ethereum's development. Ethereum is not controlled by a single individual, and people around the world can contribute by submitting EIPs. The number 721 is the identifier assigned to this proposal; it is not a guarantee of its chronological position among all proposals.
EIP-721 specification
An ERC-721-compliant contract must implement both the ERC-721 and ERC-165 interfaces. ERC-165 defines how contracts declare support for interfaces and how callers can detect that support. See the ERC-165 specification for details.
Interfaces
A Solidity interface resembles a contract definition, but it does not contain executable function implementations. It specifies function signatures, including names, parameters, and return types. A concrete contract inheriting an interface must supply the required implementations, either directly or through inherited code.
Why use an interface at all? It provides a clear description of which functions an implementation must expose. Developers and callers can inspect it to understand the contract's available functionality without reading every implementation detail. Interfaces are widely used in established projects, so understanding them is useful when working with Solidity. See the official documentation.
Identifiers
Each NFT has a unique numeric identifier within its contract. The contract can choose any non-duplicated identifier, and the identifier must remain unchanged throughout the life of the contract. One implementation might assign sequential IDs, but callers must treat IDs as opaque values rather than assume a particular numbering pattern. A token is identified by the combination of its contract address and token ID on a given chain.
ERC721
With interfaces covered, we can examine the code defined in EIP-721. The specification is available here.
The following excerpt omits comments for brevity. We will examine each part in turn.
pragma solidity ^0.4.20;
interface ERC721 /* is ERC165 */ {
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
function balanceOf(address _owner) external view returns (uint256);
function ownerOf(uint256 _tokenId) external view returns (address);
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
function approve(address _approved, uint256 _tokenId) external payable;
function setApprovalForAll(address _operator, bool _approved) external;
function getApproved(uint256 _tokenId) external view returns (address);
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}
interface ERC165 {
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}Events
In Solidity, events record information in transaction logs. Frontend applications and indexers can listen for these logs to learn about contract activity. See the Solidity documentation for details.
Transfer
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);Emitted when ownership of an NFT changes, including minting and burning. The standard permits an exception for NFTs created and assigned during contract creation.
Approval
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);Emitted when the approved address (_approved) for an NFT owned by _owner is changed or reaffirmed.
ApprovalForAll
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);Emitted when _operator is granted or denied authority to manage all NFTs of _owner in the contract.
Functions
The following functions define the core ERC-721 operations.
balanceOf
function balanceOf(address _owner) external view returns (uint256);Returns the number of NFTs owned by the address supplied as _owner. A query for the zero address must revert.
ownerOf
function ownerOf(uint256 _tokenId) external view returns (address);Returns the owner of the NFT identified by _tokenId. An NFT with the zero address as owner is invalid, and a query for an invalid token must revert.
safeTransferFrom
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;Transfers the NFT identified by _tokenId from _from to _to. When the destination has contract code, the token contract calls its onERC721Received function and checks the returned magic value. This is the reason the transfer is called "safe." Transferring an NFT to a contract that cannot handle it can otherwise lock the token permanently. The receiver check mitigates that risk, though it cannot guarantee that all accepting contracts provide a withdrawal mechanism. The data parameter contains additional data passed to the receiver callback.
Conditions
_frommust be the current owner. The caller must be that owner, an approved address for the token, or an authorized operator._tokenIdmust identify an existing NFT._tomust not be the zero address; if it is a contract, it must accept the safe transfer through the required receiver callback.
safeTransferFrom
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;This function has the same name as the previous one, but does not have a data parameter. Solidity supports overloading: functions with the same name but different parameter lists are distinct functions. Use this overload when no additional data is needed. Its behavior otherwise matches the first overload, passing empty data to the receiver.
transferFrom
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;Transfers the NFT identified by _tokenId from _from to _to. Unlike safeTransferFrom, it does not require a receiver callback. A token sent to a contract that cannot manage it may therefore become permanently locked.
Conditions
_frommust be the current owner, and the caller must be the owner, an approved address for the token, or an authorized operator._tokenIdmust identify an existing NFT, and_tomust not be the zero address.
approve
function approve(address _approved, uint256 _tokenId) external payable;Grants the address specified by _approved permission to transfer the NFT identified by _tokenId. Approval applies to a single token.
Conditions
- The zero address is permitted as
_approvedand clears the token's existing approval. - The caller must be the owner of
_tokenIdor an operator authorized by that owner. A single-token approved address cannot grant further approval solely by virtue of that approval.
setApprovalForAll
function setApprovalForAll(address _operator, bool _approved) external;Grants or revokes authority for _operator to manage all NFTs owned by the caller in the contract. If _approved is true, the operator is authorized; if false, that authority is revoked. This approval applies only to NFTs of the authorizing owner in this contract.
getApproved
function getApproved(uint256 _tokenId) external view returns (address);Returns the address approved to transfer the NFT identified by _tokenId. If there is no approved address, returns the zero address.
Conditions
- The NFT identified by
_tokenIdmust exist.
isApprovedForAll
function isApprovedForAll(address _owner, address _operator) external view returns (bool);Checks whether _owner has authorized _operator to manage all of the owner's NFTs in the contract. Returns true if authorized and false otherwise.
supportsInterface
interface ERC165 {
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}This function implements ERC-165 interface detection. Supply the ERC-165 interface identifier as interfaceID. The function returns true only if the contract supports that interface and the identifier is not 0xffffffff; otherwise it returns false. ERC-165 requires the invalid identifier 0xffffffff to return false.
ERC721TokenReceiver
Having examined the ERC-721 functions, we can now look at ERC721TokenReceiver, the interface that recipient contracts implement to accept safe transfers.
interface ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}As described under safeTransferFrom, when the destination is a contract, the token contract checks whether the destination accepts the transfer. This reduces the risk of permanently locking tokens in contracts that cannot receive them.
onERC721Received
The token contract calls this function on the recipient. The _operator parameter identifies the account that initiated the safe transfer; it is not the contract being tested for receiver support. The recipient must return the required selector to accept the transfer, or the transaction reverts.
ERC721Metadata
The ERC721Metadata interface is an optional ERC-721 extension. Metadata can be described as follows (adapted from the referenced glossary).
Metadata is information describing data, such as its properties and related details. Correct metadata helps organize, maintain, and search the underlying data.
For ERC-721, metadata describes the assets represented by NFTs. The interface is shown below.
interface ERC721Metadata /* is ERC721 */ {
function name() external view returns (string _name);
function symbol() external view returns (string _symbol);
function tokenURI(uint256 _tokenId) external view returns (string);
}name
function name() external view returns (string _name);Returns the descriptive name of the NFT collection.
symbol
function symbol() external view returns (string _symbol);Returns the collection's abbreviated symbol.
tokenURI
function tokenURI(uint256 _tokenId) external view returns (string);Returns the URI (Uniform Resource Identifier) associated with the NFT identified by _tokenId. The URI identifies a resource and may point to JSON metadata conforming to the ERC-721 metadata schema, illustrated below.
{
"title": "Asset Metadata",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Identifies the asset to which this NFT represents"
},
"description": {
"type": "string",
"description": "Describes the asset to which this NFT represents"
},
"image": {
"type": "string",
"description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
}
}
}The name field describes the asset's name, description describes the asset, and image contains a URI pointing to its image resource. A suitable data URI can also encode the image directly.
ERC721Enumerable
This final interface is an optional extension. ERC721Enumerable lets callers enumerate tokens owned by a particular address. A common implementation maintains additional ownership and enumeration data in storage, increasing gas costs. Other implementations can derive these values differently. The interface is shown below.
interface ERC721Enumerable /* is ERC721 */ {
function totalSupply() external view returns (uint256);
function tokenByIndex(uint256 _index) external view returns (uint256);
function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);
}totalSupply
function totalSupply() external view returns (uint256);Returns the number of valid NFTs currently tracked by the contract.
tokenByIndex
function tokenByIndex(uint256 _index) external view returns (uint256);Returns the token ID at position _index in the contract-wide enumeration.
Conditions
_indexmust be strictly less thantotalSupply().
tokenOfOwnerByIndex
function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);Returns the token ID at position _index in the enumeration of NFTs owned by _owner.
Conditions
_indexmust be strictly less thanbalanceOf(_owner), not the contract-wide total supply. The owner must not be the zero address.
ERC-721 implementation code
The preceding sections covered the ERC-721 standard and its interfaces. We will now examine an actual implementation rather than only the specification. The examples in this section use an older OpenZeppelin Contracts implementation; the linked master branch may have changed since these excerpts were written.
IERC721
This interface defines the required functions and events of ERC-721.
Functions and events
balanceOf(owner)ownerOf(tokenId)safeTransferFrom(from, to, tokenId)transferFrom(from, to, tokenId)approve(to, tokenId)getApproved(tokenId)setApprovalForAll(operator, _approved)isApprovedForAll(owner, operator)safeTransferFrom(from, to, tokenId, data)Transfer(from, to, tokenId)Approval(owner, approved, tokenId)ApprovalForAll(owner, operator, approved)
These are the same functions and events defined in EIP-721. When reviewing another standard, its specification is a useful starting point for identifying required functionality. The interface implementation is available here.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
interface IERC721 is IERC165 {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
function safeTransferFrom(address from, address to, uint256 tokenId) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function approve(address to, uint256 tokenId) external;
function setApprovalForAll(address operator, bool approved) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function isApprovedForAll(address owner, address operator) external view returns (bool);
}IERC721Metadata
This interface defines the optional metadata extension to ERC-721.
IERC721Metadata
name()symbol()tokenURI(tokenId)
These match ERC721Metadata as defined in EIP-721. See the code here.
IERC721Enumerable
This interface defines the optional enumeration extension. Implementers can decide whether they need it.
IERC721Enumerable
totalSupply()tokenOfOwnerByIndex(owner, index)tokenByIndex(index)
These are the functions defined by ERC721Enumerable in EIP-721. OpenZeppelin expresses the standard's requirements as Solidity interfaces. The code is available here.
ERC721
We can now examine the contract that implements ERC-721. The full implementation is linked below rather than reproduced in the article.
State variables
The following variables store core token information in the illustrated implementation.
// Token name
string private _name;
// Token symbol
string private _symbol;
// Mapping from token ID to owner address
mapping(uint256 => address) private _owners;
// Mapping owner address to token count
mapping(address => uint256) private _balances;
// Mapping from token ID to approved address
mapping(uint256 => address) private _tokenApprovals;
// Mapping from owner to operator approvals
mapping(address => mapping(address => bool)) private _operatorApprovals;_name
Stores the name of the ERC-721 token collection.
_symbol
Stores the collection's symbol.
_owners
A mapping from each ERC-721 token ID to its owner's address.
_balances
A mapping recording how many ERC-721 tokens each address owns.
_tokenApprovals
A mapping from each token ID to the address approved to transfer that token.
_operatorApprovals
A nested mapping recording whether a token owner has authorized an operator to manage their NFTs.
Functions
Many functions correspond to the interfaces already covered, so this section focuses on the main implementation functions. Consult the full OpenZeppelin code for other details.
constructor
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}Sets the ERC-721 collection name and symbol. The constructor runs once when the contract is deployed.
_transfer
function _transfer(address from, address to, uint256 tokenId) internal virtual {
require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
require(to != address(0), "ERC721: transfer to the zero address");
_beforeTokenTransfer(from, to, tokenId, 1);
require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
delete _tokenApprovals[tokenId];
unchecked {
_balances[from] -= 1;
_balances[to] += 1;
}
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
_afterTokenTransfer(from, to, tokenId, 1);
}In the illustrated version, this function verifies that from owns the ERC-721 token identified by tokenId and that the recipient is not the zero address. It invokes _beforeTokenTransfer, updates the relevant mappings and approvals, and invokes _afterTokenTransfer. Newer OpenZeppelin versions use different internal mechanisms.
_mint
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_beforeTokenTransfer(address(0), to, tokenId, 1);
require(!_exists(tokenId), "ERC721: token already minted");
unchecked {
_balances[to] += 1;
}
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
_afterTokenTransfer(address(0), to, tokenId, 1);
}Performs checks and state updates before minting an ERC-721 token. The destination must not be the zero address, and the token identified by tokenId must not already exist.
ERC721Enumerable
The enumeration extension is implemented in the contract linked below.
Rather than cover the entire implementation, we will focus on the main data structures and functions.
State variables
mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
mapping(uint256 => uint256) private _ownedTokensIndex;
uint256[] private _allTokens;
mapping(uint256 => uint256) private _allTokensIndex;These mappings and arrays allow the implementation to efficiently enumerate tokens owned by a specific address and tokens across the collection.
Implementing ERC-721
Now that we have examined the implementation, we can create a minimal ERC-721 contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract CardeneToken is ERC721 {
constructor() ERC721("CardeneToken", "CARD") {}
}This is sufficient for a minimal collection contract implementing the inherited ERC-721 interface. The earlier code is not missing: the contract inherits the functions from OpenZeppelin's ERC721 implementation. This particular example does not expose a public minting function, so a usable collection also needs an appropriately authorized minting mechanism.
Running ERC-721
The following link opens Remix in a browser with the example code already loaded. The example pins OpenZeppelin Contracts 4.8.0, so use a compatible Solidity compiler.
Compile and deploy the contract.

The inherited ERC-721 functions are available in the deployed contract. Demonstrating every function would make this article considerably longer, so try them in your own local environment or Remix VM. Token-specific operations require tokens to have been minted first.
Conclusion
This article introduced ERC-721. I also publish articles on other platforms.
https://amzn.asia/d/gxvJ0Pw https://cardene.dev/proposals https://zenn.dev/heku https://mirror.xyz/0xcE77b9fCd390847627c84359fC1Bc02fC78f0e58 https://twitter.com/cardene777 https://cardene.substack.com/
References
- https://gaiax-blockchain.com/erc721
- https://www.metaverse-style.com/nft-standard-erc721-erc1155
- https://ethereum.org/ja/developers/docs/standards/tokens/erc-721/
- https://eips.ethereum.org/EIPS/eip-721
- https://ethereumnavi.com/2021/11/09/contract-study-2-solidity-erc721/
- https://note.com/standenglish/n/n09fd7dc58427
- https://docs.openzeppelin.com/contracts/3.x/
- https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC721
- https://wizard.openzeppelin.com/#erc721




