ERC721 tokens are a type of non-fungible token (NFT) standard on the Ethereum blockchain. Unlike ERC20 tokens, which are fungible and identical to one another, ERC721 tokens are unique and distinguishable from each other. This uniqueness makes them ideal for representing ownership of digital assets such as art, collectibles, and virtual real estate.
Key Characteristics of ERC721 Tokens
- Non-Fungibility: Each ERC721 token is unique, meaning it cannot be exchanged on a one-to-one basis with another token. This characteristic is essential for representing items like collectibles or artwork.
- Ownership Tracking: The ERC721 standard allows for clear ownership tracking of each token, ensuring that ownership can be verified on the blockchain.
- Interoperability: ERC721 tokens can be used across various platforms and applications, allowing for a wide range of use cases and integrations.
- Metadata: ERC721 tokens can include metadata, which can provide additional information about the asset, such as its name, description, and image URL.
Mandatory Functions of ERC721 Tokens
balanceOf(address owner)
: Returns the number of NFTs owned by a specific address.ownerOf(uint256 tokenId)
: Returns the owner of a specific token ID.transferFrom(address from, address to, uint256 tokenId)
: Transfers ownership of a specific token ID from one address to another.approve(address to, uint256 tokenId)
: Grants permission to another address to manage a specific token ID.getApproved(uint256 tokenId)
: Returns the address approved for a specific token ID.setApprovalForAll(address operator, bool approved)
: Grants or revokes permission to an operator to manage all tokens owned by the caller.
Sample ERC721 Token Code
Below is a simple implementation of an ERC721 token using Solidity:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721, Ownable {
uint256 public nextTokenId;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to) external onlyOwner {
_safeMint(to, nextTokenId);
nextTokenId++;
}
}
Explanation of the Sample Code
- The contract
MyNFT
inherits from theERC721
contract provided by the OpenZeppelin library, which implements the ERC721 standard. - The constructor initializes the token with a name ("MyNFT") and a symbol ("MNFT").
- The
nextTokenId
variable keeps track of the next available token ID. - The
ownerOf(uint256 tokenId)
0 function allows the owner of the contract to create new tokens and assign them to a specific address. Each time a token is minted, thenextTokenId
is incremented.
Conclusion
ERC721 tokens have revolution ized the concept of digital ownership by enabling the creation of unique assets on the blockchain. Their non-fungible nature allows for a wide range of applications, from digital art and collectibles to virtual real estate and gaming items. By adhering to the ERC721 standard, developers can ensure that their NFTs are compatible with various platforms and can be easily traded or showcased across different marketplaces.