ERC-721
ERC721¶
ERC721 is an Ethereum standard for creating Non-Fungible Tokens (NFTs). Unlike the ERC20 standard, ERC721 tokens are unique, with each token having its own distinct attributes and value.
Features of ERC721¶
- Non-Interchangeable / Uniqueness:
-
Each ERC721 token is unique with a distinct ID. This means one ERC721 token cannot be directly exchanged at equal value with another ERC721 token. They represent unique digital assets: artworks, collectibles, virtual real estate, domain names, etc.
-
Metadata:
-
Each ERC721 token can carry metadata for storing additional information about the token, such as name, description, image, etc. This metadata can make each token more unique and valuable.
-
Ownership:
- The ERC721 standard defines the concept of token ownership and allows token owners to transfer and authorize tokens. Ownership information is publicly recorded on the blockchain, ensuring transparency and security.
ERC721 Main Interfaces and Events¶
pragma solidity ^0.8.0;
// IERC721 The ERC721 standard requires implementation of ERC165
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) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function approve(address to, uint256 tokenId) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function setApprovalForAll(address operator, bool _approved) external;
function isApprovedForAll(address owner, address operator) external view returns (bool);
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
}
interface IERC721Metadata {
function name() external view returns (string _name);
function symbol() external view returns (string _symbol);
// Returns the unique Uniform Resource Identifier (URI) for a given asset, typically pointing to a JSON file conforming to the "ERC721 Metadata JSON Schema".
function tokenURI(uint256 _tokenId) external view returns (string);
}
ERC721 Metadata JSON Reference Schema:
{
"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."
}
}
}
ERC721 Implementation¶
Below is a simple ERC721 token contract example based on OpenZeppelin code:
// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract GameItem is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("GameItem", "ITM") {}
function awardItem(address player, string memory tokenURI) public returns (uint256) {
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, tokenURI);
_tokenIds.increment();
return newItemId;
}
}
Applications¶
ERC721 tokens have been applied in many fields, particularly in digital art, gaming, and DeFi. For example, CryptoKitties was one of the first well-known projects to use the ERC721 standard, allowing users to buy, breed, and trade unique digital cats. Uniswap V3 uses NFTs to represent the unique liquidity positions provided by users.
The ERC721 standard provides a powerful tool for creating and managing unique digital assets.