Introduction to ERC721 Tokens
ERC721 is a free, open standard that describes how to build non-fungible tokens (NFTs) on the Ethereum blockchain. Unlike ERC20 tokens, which are identical and interchangeable, each ERC721 token is unique and cannot be replicated. This makes ERC721 ideal for representing ownership of distinct assets like digital art, collectibles, or real estate.
Core Components of ERC721
1. Interfaces
The ERC721 standard consists of three primary interfaces:
IERC721: Mandatory for basic functionality (e.g., transferring tokens).IERC721Metadata: Adds details like token names, symbols, and URIs.IERC721Enumerable: Enables on-chain enumeration of tokens.
| Interface | Purpose |
|---|---|
IERC721 | Base functions for token transfers and ownership. |
IERC721Metadata | Provides token-specific metadata (e.g., name, symbol, tokenURI). |
IERC721Enumerable | Allows querying tokens by index or owner. |
2. Key Functions
balanceOf(owner): Returns the number of tokens owned by an address.ownerOf(tokenId): Identifies the owner of a specific token.safeTransferFrom(from, to, tokenId): Transfers tokens safely, verifying recipient contracts.
Implementing ERC721 Contracts
Step-by-Step Guide
Inherit Interfaces: Combine
IERC721with optional interfaces likeIERC721Metadata.contract MyNFT is ERC721, ERC721Metadata {}- Mint Tokens: Use
_mint(to, tokenId)to create new NFTs. - Set Metadata: Attach details via
_setTokenURI(tokenId, uri).
Example: Basic ERC721 Contract
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract SimpleNFT is ERC721 {
constructor() ERC721("SimpleNFT", "SNFT") {}
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
}Advanced Features
1. ERC721 Extensions
- Minting: Controlled via
ERC721Mintable. - Pausing: Freeze transfers with
ERC721Pausable. - Burning: Permanently remove tokens using
ERC721Burnable.
2. Security Considerations
- Receiver Contracts: Implement
IERC721Receiverto prevent tokens from being locked in non-compliant contracts. - Approvals: Use
approveorsetApprovalForAllto delegate transfers.
FAQs
Q1: What’s the difference between ERC20 and ERC721?
- ERC20: Fungible tokens (e.g., cryptocurrencies).
- ERC721: Non-fungible tokens (e.g., unique digital assets).
Q2: How do I view an ERC721 token’s metadata?
Call tokenURI(tokenId) to retrieve the token’s JSON metadata link.
Q3: Can ERC721 tokens be subdivided?
No. Each token is indivisible and represents a whole asset.
👉 Learn more about NFT standards
Conclusion
ERC721 tokens revolutionize digital ownership by enabling unique, verifiable assets on the blockchain. Whether for art, gaming, or real-world assets, ERC721’s flexibility and security make it the go-to standard for NFTs.
Further Reading: Explore OpenZeppelin’s ERC721 documentation for implementation details.