How To Tutorials

Mint NFTs with Hardhat

26min

Overview

This guide walks you through using Hardhat to deploy an NFT smart contract to the Palm network.



🏁 Prerequisites

  1. Get PALM Tokens to pay the deployment transaction fees.
  2. Set up MetaMask wallet.
  3. Install Node.js.


Tutorial | Mint NFTs using Hardhat

1. Create a mint.js file in your scripts folder

Inside your scripts directory, create a mint.js file.

We will use the Hardhat-flavored version of the ethers.js library. This provides more abstractions and leaner code than web3.js.

Require the correct libraries:

JS



2. Create a variable representing your contract's ABI

Our contract's ABI (Application Binary Interface) is the interface enabling our JavaScript to interact with our smart contract. Hardhat generates and stores the ABI version of our contract in the artifacts folder as a JSON file.

Add the following to mint.js:

JS



3. Add a provider

In Ethers.js parlance, a provider represents a connection to an Ethereum blockchain. Adding one will enable your app to interact with the Palm network.

In this scenario, because we're using the Hardhat plugin version of Ethers.js, our ethers.provider object retrieves all the connection details from hardhat.config.js.

Add this line to mint.js:

JS



4. Upload a media file to IPFS

This step consists of adding a media file of your choice to IPFS which is a distributed system for storing and accessing files, websites, applications, and data.

In order to help ensure this file's availability, you can "pin" the file in IPFS. There are several pinning services available that will maintain your file on IPFS:

Example Using NFT Storage

a) Log into NFT Storage and create an account

b) Upload your file

Document image


c) Save its CID (the content identifier for a file or piece of data)

Document image



5. Create your NFT's Metadata file

In this example we will implement the ERC721 Metadata JSON Schema.





An NFT Metadata file is a JSON document that serves as a descriptor for the media file your NFT represents.An NFT Metadata file is a JSON document that serves as a descriptor for the media file your NFT represents.

It enables you to specify the

  • NFT name
  • Image URI (e.g. as an IPFS CID URI)
  • Description
  • Other attributes like a trait type



a) In the root directory, create a new file called nft-metadata.json

b) Edit the following JSON code to match your NFT's specifications

c) Add the CID of the image you saved in Step 4.

d) Upload nft-metadata.json to NFT Storage

Document image


e) Copy its CID

Document image


f) Add this line to mint.js file

JS


The variable tokenCID will be passed as an argument to the contract's mintNFT function.



6. Update your .env file:

a) Add your public key to a PUBLIC_KEY variable in your .env file:

PUBLIC_KEY = "your-public-wallet-account-address"

b) Get the address of the contract you deployed earlier from Using Hardhat, the address returned by Hardhat upon deployment, and create a .env variable pointing to that address:

Text


Your .env file should now contain the following variables:

Text



7. Set up the Ethers.js signer and wallet

The wallet is a convenient class in Ethers.js, it represents an Ethereum account and enables one to sign transactions and messages like an EOA (Externally-owned account) would.

Add the following to mint.js:

JS



8. Set up the Ethers.js contract

An Ethers.js contract is a representation of the actual contract that has been deployed. This class enables you to interact with your contract using JavaScript. You can send it a transaction; in this case, we will use it to trigger our MintNFT function.

Add these lines of code to mint.js:

JS



9. Build the main function

Create the main function, which will asynchronously call the MintNFT function passing as arguments:

  • Your address (or any other recipient address you chose)
  • The CID of your NFT's metadata

We required the confirmation to be 5 blocks:

main function: tx.wait(5)),

Therefore, it will take a few seconds before seeing confirmation in your terminal.

Your mint.js file should now look like this:

JS



10. Call mint.js

Lastly, run mint.js to deploy your NFT:

Mint on Palm Testnet
Mint on Palm Mainnet
Example Response



11. Look up your deployment on the Palm network block explorer

Paste the address in Contract deployed to address: from the previous step into the search bar. It may take 10-20 minutes to appear.



Testnet

Mainnet

Explorer URL

✅ Your NFT is minted!



Use Your Deployed Contract as an NFT Factory

Note that your deployed contract can act as an NFT factory, enabling you to mint any number of tokens.



View Token in MetaMask

If you want to see your token appear in your MetaMask wallet

  1. Go to MetaMask
  2. Choose Import tokens
    Choose Import tokens
    
  3. Paste the address of the contract (located in your .env file) in the Token contract address i.e. the value of CONTRACT_ADDRESS = 0x3DB7Da7f66a416B1865b699E426fE9e413285BB9
  4. Add a Token Symbol
  5. Add 0 as a decimal
  6. Click Add Tokens
  7. Click Import Tokens



Updated 25 Mar 2024
Doc contributor
Doc contributor
Did this page help you?