Cover photo

How to mint a NFT on mirror

STEP 1: INSTALL WEB3

If you followed the first tutorial on creating your NFT smart contract, you already have experience using Ethers.js. Web3 is similar to Ethers, as it is a library used to make creating requests to the Ethereum blockchain easier. In this tutorial we’ll be using Alchemy Web3, which is an enhanced Web3 library that offers automatic retries and robust WebSocket support.

In your project home directory run:

1 npm install @alch/alchemy-web3 2 STEP 2: CREATE A MINT-NFT.JS FILE

Inside your scripts directory, create a mint-nft.js file and add the following lines of code:

1 require("dotenv").config() 2 const API_URL = process.env.API_URL 3 const { createAlchemyWeb3 } = require("@alch/alchemy-web3") 4 const web3 = createAlchemyWeb3(API_URL) 5 📋 複製 STEP 3: GRAB YOUR CONTRACT ABI

Our contract ABI (Application Binary Interface) is the interface to interact with our smart contract. You can learn more about Contract ABIs here. Hardhat automatically generates an ABI for us and saves it in the MyNFT.json file. In order to use this we’ll need to parse out the contents by adding the following lines of code to our mint-nft.js file:

1 const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json") 2 📋 複製 If you want to see the ABI you can print it to your console:

1 console.log(JSON.stringify(contract.abi)) 2 📋 複製 To run mint-nft.js and see your ABI printed to the console navigate to your terminal and run:

1 node scripts/mint-nft.js 2 📋 複製 STEP 4: CONFIGURE THE METADATA FOR YOUR NFT USING IPFS

If you remember from our tutorial in Part 1, our mintNFT smart contract function takes in a tokenURI parameter that should resolve to a JSON document describing the NFT's metadata— which is really what brings the NFT to life, allowing it to have configurable properties, such as a name, description, image, and other attributes.

Interplanetary File System (IPFS) is a decentralized protocol and peer-to-peer network for storing and sharing data in a distributed file system. We will use Pinata, a convenient IPFS API and toolkit, to store our NFT asset and metadata to ensure our NFT is truly decentralized. If you don’t have a Pinata account, sign up for a free account here and complete the steps to verify your email.

Once you’ve created an account:

Navigate to the “Files” page and click the blue "Upload" button at the top-left of the page. Upload an image to Pinata — this will be the image asset for your NFT. Feel free to name the asset whatever you wish After you upload, you'll see the file info in the table on the "Files" page. You'll also see a CID column. You can copy the CID by clicking the copy button next to it. You can view your upload at: https://gateway.pinata.cloud/ipfs/. You can find the image we used on IPFS here, for example. For the more visual learners, the steps above are summarized here: