The ERC-721 standard defines a way to create unique digital items called NFTs on the Ethereum blockchain. It helps people prove ownership of one-of-a-kind things like art or collectibles.
0
0
ERC-721 NFT standard in Blockchain / Solidity
Introduction
You want to create a digital collectible that is unique, like a rare trading card.
You want to sell digital art where each piece is one of a kind.
You want to track ownership of virtual items in a game.
You want to prove ownership of a digital asset that cannot be copied.
You want to build a marketplace where users can buy and sell unique tokens.
Syntax
Blockchain / Solidity
interface IERC721 {
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;
}This is an interface in Solidity, the language for Ethereum smart contracts.
It defines functions and events that any NFT contract must implement to follow the ERC-721 standard.
Examples
Returns how many NFTs the owner has.
Blockchain / Solidity
function balanceOf(address owner) external view returns (uint256 balance);
Returns the owner address of a specific NFT by its unique ID.
Blockchain / Solidity
function ownerOf(uint256 tokenId) external view returns (address owner);
Transfers ownership of an NFT from one address to another.
Blockchain / Solidity
function transferFrom(address from, address to, uint256 tokenId) external;Event emitted when an NFT is transferred. Helps track ownership changes.
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);Sample Program
This smart contract creates a simple NFT collection called 'MyUniqueArt'. Only the admin can create (mint) new unique tokens. Each token has a unique ID starting from 0.
Blockchain / Solidity
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MyUniqueArt is ERC721 { uint256 public nextTokenId; address public admin; constructor() ERC721('MyUniqueArt', 'MUA') { admin = msg.sender; } function mint(address to) external { require(msg.sender == admin, 'only admin can mint'); _safeMint(to, nextTokenId); nextTokenId++; } }
OutputSuccess
Important Notes
ERC-721 tokens are unique and not interchangeable like regular cryptocurrencies.
Each NFT has a unique ID called tokenId to identify it.
Use libraries like OpenZeppelin to avoid mistakes and save time.
Summary
ERC-721 is the standard for unique digital tokens called NFTs on Ethereum.
It defines how to track ownership and transfer NFTs safely.
It is used for digital art, collectibles, and unique virtual items.