ERC721 is a standard for creating non-fungible tokens (NFTs) on the Ethereum blockchain. Unlike ERC20 tokens, which are fungible (interchangeable), ERC721 tokens are unique and cannot be exchanged on a one-to-one basis.
Fungibility vs. Non-Fungibility:
- Fungible Tokens (ERC20):
- Fungible tokens are interchangeable and have identical value. For example, one ERC20 token (e.g., 1 unit of a token like USDT) is equivalent to another of the same type.
- The transcript uses the analogy of Indian Rupees: ₹1 from one person is the same as ₹1 from another person.
- In ERC20, tokens are minted to an address, and the contract’s address represents the token itself. The tokens are tracked by quantity (e.g., 100 tokens).
- Non-Fungible Tokens (ERC721):
- Non-fungible tokens (NFTs) are unique and cannot be exchanged with another token of the same contract on a one-to-one basis.
- Example: A world-famous painting (like the Mona Lisa) is unique—there’s only one, and it cannot be replaced or swapped with another painting. This is non-fungibility.
- In ERC721, each token has a unique token ID associated with it, making every token distinct, even within the same contract.
Key Characteristics of ERC721:
- Uniqueness: Each ERC721 token has a unique token ID, ensuring no two tokens in the same contract are identical.
- Contract Address + Token ID: An ERC721 token is identified by the combination of the contract’s address and a unique token ID.
- Use Cases: NFTs are used for digital collectibles, art, gaming assets, virtual real estate, and more, where uniqueness is critical.
- Standardized Interface: Like ERC20, ERC721 defines a standard set of functions and events to ensure compatibility with Ethereum-based applications (e.g., wallets, marketplaces).
Comparison with ERC20:
- ERC20:
- Tokens are fungible, tracked by quantity (e.g., 100 tokens).
- The contract’s address represents the token, and no unique IDs are needed.
- Example: Minting 100 ERC20 tokens gives you a balance of 100 units.
- ERC721:
- Tokens are non-fungible, tracked by unique token IDs.
- Minting an ERC721 token assigns a unique token ID to an address, not a quantity.
- Example: Minting an ERC721 token gives you ownership of a specific token ID (e.g., token ID #5).
Key Concepts from the Transcript
The transcript explains the ERC721 standard, its implementation using OpenZeppelin, and how it differs from ERC20. It also covers the inheritance structure, key functions, and practical considerations like safe transfers to prevent tokens from getting stuck. Below are the main points:
- Non-Fungibility:
- ERC721 tokens are non-fungible, meaning each token is unique and cannot be swapped with another token from the same contract.
- Example: If you own an NFT with token ID #1, it’s distinct from an NFT with token ID #2, even if they’re from the same contract.
- The uniqueness is defined by the token ID, which is unique within a single ERC721 contract.
- OpenZeppelin Library:
-
OpenZeppelin provides audited, secure implementations of ERC721 (and other standards) to reduce vulnerabilities.
-
The transcript uses OpenZeppelin’s ERC721 contract, imported and inherited in the custom contract:
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyContract is ERC721 {
constructor() ERC721("MyNFT", "MNFT") {}
}
-
Inheritance: The is ERC721 keyword inherits all ERC721 functionalities from OpenZeppelin’s implementation.
-
Customization: OpenZeppelin’s wizard allows developers to customize ERC721 contracts (e.g., mintable, burnable).
- Inheritance Hierarchy:
- The ERC721 standard involves a chain of inheritance to ensure compatibility and functionality:
- IERC721: The interface defining the core ERC721 functions (e.g.,
balanceOf, ownerOf, transferFrom).
- IERC165: An interface that ERC721 inherits to validate whether a contract supports specific interfaces.
- IERC721Receiver: An interface that a contract must implement to safely receive ERC721 tokens via
safeTransferFrom.
- IERC165:
- Checks if a contract supports a specific interface (e.g., IERC721).
- Contains the
supportsInterface function, which validates whether a contract implements the expected interface.
- Example: When an ERC721 contract is deployed, it checks via
supportsInterface if it correctly implements IERC721.
- Why This Matters:
- Ensures that the contract adheres to the ERC721 standard.
- Prevents conflicts by validating supported interfaces.
- The transcript emphasizes that understanding this hierarchy is crucial for interviews or deep technical knowledge.
Key ERC721 Functions: