Deploy a NFT (ERC-721) Tutorial

An ERC-721 token is a type of non-fungible token (NFT) on the Ethereum blockchain, defined by the Ethereum Request for Comments 721 (ERC-721) standard.


An ERC-721 token is a type of non-fungible token (NFT) on the Ethereum blockchain, defined by the Ethereum Request for Comments 721 (ERC-721) standard. It represents unique digital assets, such as digital art, virtual real estate, collectibles, and digital goods. The standard defines a set of rules and interfaces for managing and transferring NFTs on the Ethereum network.Unlike ERC-20 tokens, which are fungible and interchangeable, ERC-721 tokens are non-fungible, meaning each token has a unique value and is not interchangeable with other tokens. Each ERC-721 token has a unique identifier (Token ID) and can be associated with specific metadata, such as descriptions, images, or other information related to the digital asset.The ERC-721 standard includes functions for:

  1. 1.Token ownership management: These functions allow users to check the ownership of a token, transfer ownership, and approve others to transfer the token on their behalf.

  2. 2.Token metadata management: These functions help manage metadata associated with each token, such as token URI, which can be used to store information about the digital asset.

  3. 3.Enumeration: These functions enable enumeration of owned tokens, providing a way to count and list tokens owned by a specific address.

By implementing the ERC-721 standard, developers can create NFTs with unique properties and ensure compatibility with various wallets, marketplaces, and platforms that support this standard.Creating an ERC-721 NFT Smart Contract involves several steps. Here's a detailed guide to help you create one using Solidity and the Ethereum network:

Set up your environment

To begin, you will need the following tools:

To install Truffle, use the following command:

npm install -g truffle

Create a new Truffle project

In your terminal, navigate to the directory where you want to create the project and run:

truffle initThis command will generate a new Truffle project with basic files and folders.

Install OpenZeppelin Contracts library

ERC-721 is based on the OpenZeppelin Contracts library, which offers a secure and well-tested implementation of the standard.

Install the library by running:

npm install @openzeppelin/contracts

Create an ERC-721 smart contract

In your Truffle project, navigate to the contracts folder and create a new file called MyNFT.sol.

Add the following code:

pragma solidity ^0.8.0;​import "@openzeppelin/contracts/token/ERC721/ERC721.sol";import "@openzeppelin/contracts/utils/Counters.sol";​contract MyNFT is ERC721 {using Counters for Counters.Counter;Counters.Counter private _tokenIds;​constructor() ERC721("MyNFT", "MNFT") {}​function mintNFT(address recipient, string memory tokenURI) public returns (uint256) {_tokenIds.increment();​uint256 newItemId = _tokenIds.current();_mint(recipient, newItemId);_setTokenURI(newItemId, tokenURI);​return newItemId;}}​This code imports the necessary libraries and creates a basic ERC-721 NFT smart contract called MyNFT. The mintNFT function allows you to mint new NFTs and assign a tokenURI for metadata.

Deploy the smart contract

Create a new file in the migrations folder named 2_deploy_contract.js and add the following code:const MyNFT = artifacts.require("MyNFT");​module.exports = function (deployer) {deployer.deploy(MyNFT);};​This code tells Truffle how to deploy the smart contract to the Ethereum network.

Compile and deploy the contract

Start Ganache and make sure you have a local blockchain running. In your terminal, compile the contract using:truffle compileOnce compiled, deploy the contract to the local blockchain with:truffle migrate

Interact with the smart contract

You can interact with the smart contract using Truffle console, web3.js, or other tools like the Exzo Wallet Chrome extension and Remix IDE.To use the Truffle console, run:truffle consoleInside the console, you can mint an NFT:let instance = await MyNFT.deployed()await instance.mintNFT("0xYourAddress", "")Replace "0xYourAddress" with the recipient's Ethereum address and "" with the actual token URI.You can utilize Pinata to upload the images of your NFT and their metadata, which is in JSON format.To learn more about the recommended metadaa structure you can vieew the OpenSea metadata guidelines here.Below is an example of a metadata JSON file structure{"description": "Friendly OpenSea Creature that enjoys long swims in the ocean.","external_url": "","image": "","name": "Dave Starbelly","attributes": [{"trait_type": "Background","value": "Green"},{"trait_type": "Head","value": "Hat"},{"trait_type": "Level","display_type": "number","value": 10}...]...}Now you have a basic understanding of creating an ERC-721 NFT smart contract using Solidity, Truffle, and the OpenZeppelin Contracts library. You can expand the functionality by adding more features such as royalty payments, access.Make sure to always get your code audited before deploying it live. A good resource for quick auditing is SolidityScan.This tutorial is for informational purposes only. We did not have the code audited and do not take ownership of any of the code associated with this tutorial.

Last updated