Mint an NFT
This tutorial describes how to mint an NFT using the viem and the smart contract from Part I: Write & deploy an Nft.
Last updated
This tutorial describes how to mint an NFT using the viem and the smart contract from Part I: Write & deploy an Nft.
Last updated
Download the starter kits below that contains the contract code from the previous project: Write and Deploy an Nft.
follow the guidlines on the README.md file to get your starter kits ready.
Doing so should open http://localhost:3000/ in your browser, where you'll see the frontend for our project. It should consist of 3 fields: a place to input a link to your NFT's asset, enter the name of your NFT, and provide a description.
So remember the NFT metadata we just talked about in Step 1 of this tutorial—it brings an NFT to life, allowing it to have properties, such as a digital asset, name, description, and other attributes.
We're going to need to configure this metadata as a JSON object and store it, so we can pass it in as the tokenURI
parameter when calling our smart contract's mintNFT
function.
The text in the "Link to Asset", "Name", "Description" fields will comprise the different properties of our NFT's metadata. We'll format this metadata as a JSON object, but there are a couple options for where we can store this JSON object:
We could store it on the Asset Chain blockchain; however, doing so would be SUPER expensive (we're talking upwards of hundreds of dollars).
We could store it on a centralized server, like AWS or Firebase. But that would defeat our decentralization ethos. ❌
We could use IPFS, a decentralized protocol and peer-to-peer network for storing and sharing data in a distributed file system. As this protocol is decentralized and free, it is our best option! ✅
To store our metadata on IPFS, we will use Pinata, a convenient IPFS API and toolkit. In the next step, we'll explain exactly how to do this!
If you don't have a Pinata account, sign up for a free account here and complete the steps to verify your email and account.
Navigate to the https://app.pinata.cloud/developers/api-keys page, then select the "New Key" button at the top, set the Admin widget as enabled, and name your key.
You'll then be shown a popup with your API info. Make sure to put this somewhere safe.
Create a .env file
create a .env
file in the root directory of your frontend and navigate to your .env
file and add your Pinata API key and API secret to it, like so:
Save the file, and then you're ready to start writing the function to upload your JSON metadata to IPFS!
pinJSONToIPFS
Fortunately for us, Pinata has an API specifically for uploading JSON data to IPFS and a convenient JavaScript with axios example that we can use, with some slight modifications.
In your utils
folder, let's create another file called pinPinata.ts
and then import our Pinata secret and key from the .env file like so:
Next, paste the additional code from below into your pinPinata.ts
file. Don't worry, we'll break down what everything means!
So what does this code do exactly?
First, it imports axios, a promise based HTTP client for the browser and node.js, which we will use to make a request to Pinata.
Then we have our asynchronous function pinJSONToIPFS
, which takes a JSONBody
as its input and the Pinata api key and secret in its header, all to make a POST request to theirpinJSONToIPFS
API.
If this POST request is successful, then our function returns ahashstring
where our metadata was pinned. We will use this hashstring
returned as the tokenURI
input to our smart contract's mint function.
If this post request fails, then our function returns a JSON object with the a message
string that relays our error.
MintNFt
functionInside your Debug.tsx
file, let's define our function, MintNFt
, which will mint our NFT as the name implies.
The three inputs to our function will be the url
of our digital asset, name
, and description
.
Naturally, it makes sense to have some sort of input error handling at the start of the function, so we exit this function if our input parameters aren't correct. Inside our function, let's add the following code:
Essentially, if any of the input parameters are an empty string, then we return a JSON object where the toast.error
string relays that all fields in our UI must be complete.
Once we know our metadata is formatted properly, the next step is to wrap it into a JSON object and upload it to IPFS via the pinJSONToIPFS
we wrote!
To do so, we firstly need to import the pinJSONToIPFS
function into our Debug.tsx
file. At the very top of the Debug.tsx
, let's add:
Recall that pinJSONToIPFS
takes in a JSON body. So before we make a call to it, we're going to need to format our url
, name
, and description
parameters into a JSON object.
The last thing to add in our mintNFT
function is our Ethereum transaction:
That's a giant function! Below is the full code example of the MintNFT function
Hurray, YourNFtMinter Project
is Complete.
Below are link to preview project and the Completed Code for the Nft Minter
If you have questions, join our Telegram and say hello 👋. We're Active!