Understanding ERC721 Tokens
What is ERC721?
ERC721 is the standard for non-fungible tokens (NFTs) on the Ethereum blockchain. Unlike ERC20 tokens, each ERC721 token is unique and can represent ownership of a specific asset, whether digital or physical. This standard is widely used for digital art, collectibles, virtual real estate, and other unique digital assets.
OpenZeppelin provides several ERC721-related contracts. You can find more information on their implementation and usage here.
Key Features
- Unique token identification
- Individual token ownership tracking
- Transfer of unique tokens
- Approval mechanism for third-party transfers
- Optional metadata and enumeration
Core Functions
// Required functions function balanceOf(address owner) public view returns (uint256) function ownerOf(uint256 tokenId) public view returns (address) function transferFrom(address from, address to, uint256 tokenId) public function approve(address to, uint256 tokenId) public function getApproved(uint256 tokenId) public view returns (address) function setApprovalForAll(address operator, bool approved) public function isApprovedForAll(address owner, address operator) public view returns (bool) // Required events 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)
Common Use Cases
- Digital art and collectibles
- Virtual real estate and land
- Gaming assets and items
- Digital identity and certificates
- Event tickets and memberships
Best Practices
- Implement proper token URI management
- Use safe transfer functions
- Include comprehensive event logging
- Implement proper access controls
- Consider gas optimization for batch operations
Security Considerations
- Protect against reentrancy attacks
- Implement proper access controls
- Validate token URIs and metadata
- Consider upgrade mechanisms if needed
- Implement proper error handling
Example Implementation
This simple ERC721 token implementation creates a collection of unique tokens with metadata support. It inherits from OpenZeppelin's ERC721 and Ownable contracts, allowing only the owner to mint new tokens.
In this example, "MyNFT" is the name of the token collection (like "CryptoPunks" or "Bored Ape Yacht Club"), and "MNFT" is the token symbol (like "PUNK" or "BAYC"). The token symbol is typically a short, uppercase abbreviation used in wallets and exchanges.
The contract uses OpenZeppelin's Counters library to automatically generate unique token IDs. Each time a new token is minted, the counter increments, ensuring that each token has a unique identifier.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to) public onlyOwner returns (uint256) {
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current();
_mint(to, newTokenId);
return newTokenId;
}
}