Make NFT using ERC721
Tags: web3, solidity, nft, blockchain
Published: 2024-05-22 03:55:56

## What is ERC721 ? 

[ERC721](https://eips.ethereum.org/EIPS/eip-721) is a standard that describes how to build NFTs on EVM compatible blockchains. We use the [OppenZeppelin](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol) implementaion of ERC721

## Let's make the NFT

First we need to setup the images and the metadata describing each image. We are creating 6 NFTs. So we have 6 images and metadata files in a directory. 

![Images and Metadata](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrul5n9hu3cuqedig7ob.png)

The Metadata JSON Schema is given [Metadata Schema](https://eips.ethereum.org/EIPS/eip-721)

```json
{
    "name": "MyNFT Collection #1",
    "description": "My First NFT.",
    "image": "ipfs://imageBaseURI/1.png"
}
```

Metadata needs the image path. So upload images folder to IPFS first. I used [pinata](https://www.pinata.cloud/) here.

![IPFS Upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sj7jm0dk9p968yykk1nf.PNG)

Now use the image directory CID to correct metadata files.

```json
{
    "name": "MyNFT Collection #1",
    "description": "My First NFT.",
    "image": "https://gateway.pinata.cloud/ipfs/<PASTE_CID_HERE>/1.png"
}
```

Upload the metadata files to the IPFS. Now we have both NFT images and metadata in IPFS. 

![Pinata File Upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miwwycfcu6do6cyy293t.png)

Now we can deploy the contract and mint the NFT.

### Smart Contract

Lets import packages needed.

```js
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

```

Now the contract extends ERC721:

```js
contract MyNFT is ERC721, ERC721URIStorage, Ownable {
    uint256 private _nextTokenId;

    constructor(address initialOwner)
        ERC721("MyNFT", "MNT")
        Ownable(initialOwner)
    {}

    // functions goes here ...
}
```

Additional functions needed:
```js
    function safeMint(address to, string memory uri) public onlyOwner {
        uint256 tokenId = _nextTokenId++;
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
```
[_Complete code_](https://github.com/abdxzi/dev.to/blob/main/code/MyNFT.sol)

### Deploy contract using hardhat

Install hardhat

```js
npx hardhat init
```

Get a JSON RPC url from Infura/Alchemy. Setup the `hardhat.config.json`. 

Use the script to deploy and mint the NFT:

```js
const hre = require("hardhat");

async function main() {
     // Deploy contract with initial owner address
     const contract = await hre.ethers.deployContract("MyNFT", ["0xcF469d3BEB3Fc24cEe979eFf83BE33ed50988502"], {});

    await contract.waitForDeployment();

    console.log("Contract deployed to:", contract.target);

    const uris = [
      "https://gateway.pinata.cloud/ipfs/<PASTE_CID_HERE>/1.json",
      "https://gateway.pinata.cloud/ipfs/<PASTE_CID_HERE>/2.json",
      "https://gateway.pinata.cloud/ipfs/<PASTE_CID_HERE>/3.json",
        // Add more URIs as needed
    ];
    
    for (let i = 0; i < uris.length; i++) {
        const recipientAddress = "0xYourRecipientAddressHere"; // Replace with the actual recipient address
        await myNFT.safeMint(recipientAddress, uris[i]);

        console.log(`Minted NFT with URI ${uris[i]} to address ${recipientAddress}`);
  }
}

main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});
```

Now we created and deployed an NFT using ERC721


[Github](https://github.com/abdxzi) | [LinkedIn](https://www.linkedin.com/in/abdul-azeez-v/)