Posted By : Yogesh
Non-Fungible Tokens (NFTs) have revolutionized digital ownership by enabling individuals and businesses to tokenize unique assets such as art, music, real estate, and collectibles. At the core of NFT development lies the ERC-721 token standard, a blueprint for creating unique and immutable digital assets on the Ethereum blockchain. This guide offers a step-by-step, technical, and business-oriented approach to creating your own ERC-721 NFT token.
The ERC-721 standard is an Ethereum-based framework that defines a non-fungible token. Unlike ERC-20 tokens, which are fungible and interchangeable, ERC-721 tokens are unique, making them ideal for representing one-of-a-kind assets.
Uniqueness: Each token has a unique identifier, ensuring its individuality.
Immutability: Once minted, the metadata and ownership details cannot be altered.
Interoperability: ERC-721 tokens can be used across various dApps and marketplaces that support the standard.
Ownership Rights: The token owner has full control over the asset, including the ability to transfer or sell it.
Digital Art: Tokenizing art pieces to authenticate originality and ownership.
Gaming Assets: Representing in-game items like weapons or avatars as NFTs.
Collectibles: Tokenizing rare items such as trading cards or memorabilia.
Real Estate: Representing property deeds or fractional ownership digitally.
Domain Names: Minting domain names as NFTs for resale or ownership proof.
Also, Discover | How to Create an NFT Rental Marketplace using ERC 4907
The ERC-721 standard includes a set of functions that define its functionality:
balanceOf(address owner)
: Returns the number of tokens owned by a specific address.ownerOf(uint256 tokenId)
:Returns the owner of a specific token ID.safeTransferFrom(address from, address to, uint256 tokenId)
: Safely transfers ownership of a token from one address to another.approve(address to, uint256 tokenId)
: Grants permission to transfer a specific token ID to another address.setApprovalForAll(address operator, bool approved)
: Approves or revokes approval for an operator to manage all of the caller's tokens.transferFrom(address from, address to, uint256 tokenId)
: Transfers ownership of a token.tokenURI(uint256 tokenId)
: Returns the metadata URI for a specific token ID.
To start, you need the following tools:
Install Node.js from nodejs.org.
Install Truffle or Hardhat:
npm install -g truffle
or
npm install --save-dev hardhat
Install Ganache:
npm install -g ganache-cli
Also, Read | How to Implement an On-Chain NFT Allowlist
Use Solidity to write your ERC-721 contract. Below is a basic example:
// 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;
mapping(uint256 => string) private _tokenURIs;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to, string memory tokenURI) public onlyOwner {
uint256 tokenId = nextTokenId;
_safeMint(to, tokenId);
_setTokenURI(tokenId, tokenURI);
nextTokenId++;
}
function _setTokenURI(uint256 tokenId, string memory tokenURI) internal {
_tokenURIs[tokenId] = tokenURI;
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
require(_exists(tokenId), "Token does not exist");
return _tokenURIs[tokenId];
}
}
ERC721
: Inherits the standard ERC-721 functionality.Ownable
: Restricts certain functions to the contract owner.mint
: Allows the owner to create a new token._setTokenURI
: Sets metadata URI for the token.
You can deploy the contract to a local testnet or Ethereum's mainnet.
Create a deployment script in scripts/deploy.js
:
const hre = require("hardhat");
async function main() {
const MyNFT = await hre.ethers.getContractFactory("MyNFT");
const myNFT = await MyNFT.deploy();
await myNFT.deployed();
console.log("MyNFT deployed to:", myNFT.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy the contract:
npx hardhat run scripts/deploy.js --network rinkeby
Also, Read | A Guide to Implementing NFT Royalties on ERC-721 & ERC-1155
After deployment, you can interact with your contract to mint an NFT.
const { ethers } = require("ethers");
const contractABI = [/* ABI JSON from compiled contract */];
const contractAddress = "YOUR_CONTRACT_ADDRESS";
const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL");
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, contractABI, signer);
async function mintNFT(to, tokenURI) {
const tx = await contract.mint(to, tokenURI);
await tx.wait();
console.log("NFT Minted:", tx.hash);
}
mintNFT("RECIPIENT_ADDRESS", "TOKEN_METADATA_URI");
NFT metadata and assets need to be accessible online. Use IPFS or services like Pinata for decentralized storage.
{
"name": "My First NFT",
"description": "This is my first ERC-721 token!",
"image": "https://gateway.pinata.cloud/ipfs/YOUR_IMAGE_HASH",
"attributes": [
{
"trait_type": "Rarity",
"value": "Legendary"
}
]
}
You may also like | DN-404 Token Standard : Revolutionizing Fractional NFT Ownership
An ERC-721 token is a non-fungible token (NFT) standard on Ethereum used to represent unique assets.
ERC-721 tokens are unique and indivisible, while ERC-20 tokens are fungible and interchangeable.
Yes, you can deploy on Ethereum-compatible networks like Binance Smart Chain, Polygon, or Avalanche.
Creating an ERC-721 NFT token requires a combination of technical expertise, strategic planning, and the right tools. By understanding the ERC-721 standard and leveraging blockchain development frameworks, businesses can unlock new revenue streams and opportunities in the digital asset space. With the NFT market booming, now is the perfect time to explore this innovative technology and capitalize on its transformative potential. If you are planning to create and launch your NFT using ERC-721, connect with our skilled NFT developers to get started.
January 22, 2025 at 10:03 pm
Your comment is awaiting moderation.