Mint NFTs with Hardhat
This guide walks you through using Hardhat to deploy an NFT smart contract to the Palm network.
Inside your scripts directory, create a mint.js file.
Require the correct libraries:
Add the following to mint.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:
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
c) Save its CID (the content identifier for a file or piece of data)
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)
- 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
e) Copy its CID
f) Add this line to mint.js file
The variable tokenCID will be passed as an argument to the contract's mintNFT function.
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 , the address returned by Hardhat upon deployment, and create a .env variable pointing to that address:
Your .env file should now contain the following variables:
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:
8. Set up the Ethers.js contract
Add these lines of code to mint.js:
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:
Lastly, run mint.js to deploy your NFT:
Paste the address in Contract deployed to address: from the previous step into the search bar. It may take 10-20 minutes to appear.
✅ Your NFT is minted!
Note that your deployed contract can act as an NFT factory, enabling you to mint any number of tokens.
If you want to see your token appear in your MetaMask wallet
- Go to MetaMask
- Choose Import tokens
- Paste the address of the contract (located in your .env file) in the Token contract address i.e. the value of CONTRACT_ADDRESS = 0x3DB7Da7f66a416B1865b699E426fE9e413285BB9
- Add a Token Symbol
- Add 0 as a decimal
- Click Add Tokens
- Click Import Tokens