Understanding Non-Fungible Tokens and Token Standards
Non-Fungible Tokens (NFTs) are unique digital assets that represent ownership of a specific item or piece of content. Unlike cryptocurrencies like Bitcoin or Ethereum, which are fungible (interchangeable), each NFT is unique and cannot be replaced by another token.
Each NFT has a unique identifier and cannot be duplicated or replaced by another token.
NFTs cannot be divided into smaller units like cryptocurrencies. You own the entire token or none of it.
Ownership is recorded on the blockchain and can be verified by anyone, providing proof of authenticity.
NFTs can be bought, sold, and traded on various marketplaces, with ownership transfers recorded on-chain.
ERC-721 is the most widely used standard for NFTs on Ethereum. It defines the basic functionality that all NFTs must implement:
// 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 private _tokenIdCounter;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to) public onlyOwner {
uint256 tokenId = _tokenIdCounter;
_tokenIdCounter++;
_safeMint(to, tokenId);
}
function _baseURI() internal pure override returns (string memory) {
return "https://api.mynft.com/metadata/";
}
}ERC-1155 is a more advanced standard that allows for both fungible and non-fungible tokens in a single contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MultiToken is ERC1155, Ownable {
constructor() ERC1155("https://api.multitoken.com/metadata/{id}.json") {}
function mint(
address to,
uint256 id,
uint256 amount,
bytes memory data
) public onlyOwner {
_mint(to, id, amount, data);
}
function mintBatch(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public onlyOwner {
_mintBatch(to, ids, amounts, data);
}
}NFTs typically store metadata off-chain and reference it via a URI. The most common standard is the ERC-721 metadata extension:
{
"name": "My Awesome NFT",
"description": "This is a description of my NFT",
"image": "https://example.com/image.png",
"attributes": [
{
"trait_type": "Color",
"value": "Blue"
},
{
"trait_type": "Rarity",
"value": "Legendary"
}
]
}Artists can create, sell, and trade digital artwork as NFTs, with royalties built into smart contracts for ongoing revenue.
In-game items, characters, and assets can be tokenized as NFTs, allowing players to own and trade their virtual possessions.
Property ownership can be represented as NFTs, enabling fractional ownership and easier transfer of real estate assets.
Event tickets can be issued as NFTs, preventing counterfeiting and enabling programmable features like resale restrictions.
Several marketplaces facilitate NFT trading:
The largest NFT marketplace supporting multiple blockchains and token standards.
Curated marketplace focused on high-quality digital art and creative works.
Here's how to interact with NFTs using Web3 libraries:
// Interacting with NFTs using Ethers.js
import { ethers } from 'ethers';
const nftABI = [
"function ownerOf(uint256 tokenId) view returns (address)",
"function tokenURI(uint256 tokenId) view returns (string)",
"function transferFrom(address from, address to, uint256 tokenId) external",
"function approve(address to, uint256 tokenId) external"
];
const nftAddress = '0x...';
const contract = new ethers.Contract(nftAddress, nftABI, provider);
// Get NFT owner
const owner = await contract.ownerOf(1);
console.log('Owner of token 1:', owner);
// Get NFT metadata URI
const tokenURI = await contract.tokenURI(1);
console.log('Token URI:', tokenURI);
// Transfer NFT
const tx = await contract.transferFrom(
fromAddress,
toAddress,
tokenId
);
await tx.wait();
// Approve NFT for marketplace
const approveTx = await contract.approve(marketplaceAddress, tokenId);
await approveTx.wait();The NFT ecosystem continues to evolve with new standards and use cases:
In this final chapter, we've explored NFTs and token standards:
Congratulations! You've completed the Ethereum development curriculum. You now have a solid foundation in Ethereum development, from smart contracts to DeFi protocols to NFTs. Continue building and exploring the vast possibilities of blockchain technology!