Minting an NFT is the act of publishing a unique instance of an ERC721 token on the blockchain. Now that we have successfully deployed a smart contract to the Sepolia network in Part I of this NFT tutorial series, let’s flex our web3 skills and mint an NFT!
At the end of this tutorial, you’ll be able to mint as many NFTs as you’d like with this code —let’s get started!
- Creating a Provide using ethers
Open the repository from Part 1 in your favorite code editor (e.g. VSCode), and create a new file in the scripts folder called ‘mint-nft.js’. We will be using the ethers library from Part 1 to connect to the Provider. Add the following code to the file:
require("dotenv").config();
const ethers = require("ethers");
// Get App URL
const API_KEY = process.env.API_KEY;
const API_URL = process.env.API_URL;
// Define a Provider
const provider = new ethers.providers.JsonRpcProvider(API_URL);
// Get contract ABI file
const contract = require("../artifacts/contracts/MyNFT.sol/MyNFT.json");
// Create a signer
const privateKey = process.env.PRIVATE_KEY;
const signer = new ethers.Wallet(privateKey, provider);
// Get contract ABI and address
const abi = contract.abi;
const contractAddress = process.env.CONTRACT_ADDRESS;
// Create a contract instance
const myNftContract = new ethers.Contract(contractAddress, abi, signer);
// Get the NFT Metadata IPFS URL
const tokenUri =
"https://gateway.pinata.cloud/ipfs/<your-metadata-code>";
// Call mintNFT function
const mintNFT = async () => {
let nftTxn = await myNftContract.mintNFT(signer.address, tokenUri);
await nftTxn.wait();
console.log(
`NFT Minted! Check it out at: https://sepolia.etherscan.io/tx/${nftTxn.hash}`
);
};
mintNFT()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
- Configure the metadata of your NFT using IPFS
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.
We will use Pinata, a convenient IPFS API and toolkit, to store our NFT asset and metadata and ensure that our NFT is truly decentralized. If you don’t have a Pinata account, sign up for a free account.
Once you’ve created an account:
- Navigate to the Pinata Upload button on the top right
- 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, at the top of the page, there should be a green popup that allows you to view the hash of your upload —> Copy that hashcode. You can view your upload at:
In your root directory, make a new file called ‘nft-metadata.json’ and add the following json code:
{ "description" : "The bitcoin logo.", "image" : "https://gateway.pinata.cloud/ipfs/QmVheSF1WCq3qY9B6idGZChrhJEf5nkGcNqhrFKMr4VWmV", "name" : "BTC" }
Feel free to change the data in the json. You can add or remove attributes. Most importantly, make sure the image field points to the location of your IPFS image— otherwise, your NFT will not include a photo.
Once you’re done editing the json file, save it and upload it to Pinata, following the same steps we did for uploading the image.
- Updating the Code
Make sure you add this to your .env file so that it looks something like this:
API_URL = "https://eth-sepolia.g.alchemy.com/v2/your-api-key" PRIVATE_KEY = "your-metamask-private-key" API_KEY = "your-api-key" CONTRACT_ADDRESS = '0xC76A1042E5cB0C365dEB0A81Eb2b4F98c9451898'
The API_KEY is provided by services such as Infura or Alchemy, as outlined in Part 1.
The CONTRACT_ADDRESS refers to the address where the contract was deployed, also detailed at the end of Part 1.
In the ‘mint-nft.js’ file, replace the tokenUri variable with your Metadata IPFS URL that you can get from Pinata.
// Get the NFT Metadata IPFS URL const tokenUri = "https://gateway.pinata.cloud/ipfs/<your-metadata-code>";
- Call mintNFT function of the contract
We’re all set. Let’s mint our NFT by running the following command:
node scripts/mint-nft.js
You should get an output that looks something like this:
NFT Minted! Check it out at: https://sepolia.etherscan.io/tx/0xd6faa473bde8f4c2ffa9932d72ca2bc8bc07c4902c923cff8e41c1db52bd9977
You can check out your NFT mint on Etherscan by following the URL above.
You can view your NFT on OpenSea by searching for your contract address. Check out our NFT here.
Using the mint-nft.js you can mint as many NFT’s as your heart (and wallet) desires! Just be sure to pass in a new tokenURI describing the NFT’s metadata –otherwise, you’ll just end up making a bunch of identical ones with different IDs.