# 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.

### Background <a href="#background" id="background"></a>

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 <a href="#set-up-your-environment" id="set-up-your-environment"></a>

**To begin, you will need the following tools:**

* ​[Node.js](https://nodejs.org/en/download) (v8.9.4 or later)
* ​[Truffle](https://trufflesuite.com/docs/truffle/how-to/install/) (v5.x.x)
* ​[Ganache](https://trufflesuite.com/ganache/) (for local blockchain deployment)
* A text editor or IDE (e.g., [Visual Studio Code](https://code.visualstudio.com/download))

**To install Truffle, use the following command:**

npm install -g truffle

### Create a new Truffle project <a href="#create-a-new-truffle-project" id="create-a-new-truffle-project"></a>

**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 <a href="#install-openzeppelin-contracts-library" id="install-openzeppelin-contracts-library"></a>

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 <a href="#create-an-erc-721-smart-contract" id="create-an-erc-721-smart-contract"></a>

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 <a href="#deploy-the-smart-contract" id="deploy-the-smart-contract"></a>

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 <a href="#compile-and-deploy-the-contract" id="compile-and-deploy-the-contract"></a>

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 <a href="#interact-with-the-smart-contract" id="interact-with-the-smart-contract"></a>

You can interact with the smart contract using Truffle console, web3.js, or other tools like the Exzo Wallet Chrome extension and [Remix IDE](https://remix.ethereum.org/).To use the Truffle console, run:truffle consoleInside the console, you can mint an NFT:let instance = await MyNFT.deployed()await instance.mintNFT("0xYourAddress", "<https://your-token-uri.com/")Replace> "0xYourAddress" with the recipient's Ethereum address and "<https://your-token-uri.com/>" with the actual token URI.You can utilize[ Pinata](https://www.pinata.cloud/) 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](https://docs.opensea.io/docs/metadata-standards).Below is an example of a metadata JSON file structure{"description": "Friendly OpenSea Creature that enjoys long swims in the ocean.","external\_url": "<https://openseacreatures.io/3","image>": "<https://storage.googleapis.com/opensea-prod.appspot.com/puffs/3.png","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](https://solidityscan.com/).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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ethereals-blockchain.gitbook.io/ethereal-chain-documentation/tutorials/solidity-tutorials/deploy-a-nft-erc-721-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
