The NFT market has been one of the fastest growing industries with the formidable expansion of NFT use cases. As a matter of fact, the market has grown by almost 20 times in one year, from 2020 to 2021. The benefits of access to unique ownership serve as promising reasons for entering the domain of NFTs. One of the most interesting tools used for deploying smart contracts and NFTs is Hardhat.
Therefore, a Hardhat NFT tutorial can help you learn how to create your own NFT smart contract. Hardhat is a reliable development and testing framework for smart contracts, and it has been tailored for upcoming advancements in web3. You can discover many viable functionalities with Hardhat for creating NFT smart contract and deploying them easily. The following post offers you a tutorial on using Hardhat for creating your own NFTs.
Aspiring to Become a Certified NFT Expert? Enroll in Certified NFT Professional (CNFTP) Course Now!
Basic Requirements to Create NFTs with Hardhat
Before you learn about methods to create NFTs with Hardhat, you should develop a detailed impression of the essential requirements. Which blockchain would you use for deploying NFT smart contract? What is the role of Hardhat in the process of creating your first NFT contract?
Hardhat is a comprehensive development environment that helps in compiling, deploying, testing, and debugging the Ethereum smart contracts. It is an essential tool before you deploy the NFT contract to the live chain, as it supports the local development of dApps and smart contracts.
However, you would need an application for compiling, deploying, and debugging on Hardhat. In this case, Alchemy comes to your rescue with a diverse collection of developer tools.
The funds for smart contract development also invite the necessity of a crypto wallet in the process of creating NFTs. Therefore, you would need a crypto wallet like Metamask to support the smart contract development process.
Excited to develop an in-depth understanding of solidity’s best practices and the tools needed for developing and testing an NFT marketplace, Enroll now in the NFT Development Course
Steps to Create NFTs with Hardhat
Once you have created your app, you need to set up the project in Hardhat. The tutorial to compile Solidity contract for NFTs would not focus on writing Solidity syntax or test scripts. On the contrary, prior knowledge of Solidity is a great asset for understanding the following steps easily. Awareness of concepts in other high-level languages such as JavaScript could also help you understand the methods for creating NFT smart contracts. Let us take a look at the individual steps in creating and deploying your NFT smart contract by using Hardhat with Solidity programming language.
Want to get an in-depth understanding of non-fungible tokens (NFTs)? Enroll Now in NFT Fundamentals Course.
Setting the Foundation for the Project
The first requirement in the steps to deploy contract for your NFT on Ethereum blockchain focuses on setting the project. You can begin with starting the npm project by using the following command.
npm init --yes
Now, you have to carry out a Hardhat package installation process with the following command,
npm install --save-dev hardhat
The basic steps can help you prepare for creating the new NFT Hardhat project by using the following command,
npx hardhat
You would receive a welcome message along with a prompt asking you, “What do you want to do?” along with three distinct options. The options include “Create a sample project,” “Create an empty hardhat.config.js,” and “Quit.” You must select the “Create an empty hardhat.config.js” option, which would create the “hardhat.config.js” within the root directory along with details of the version of Solidity compiler.
Learn the process of creating and deploying smart contracts on the Ethereum blockchain with the Solidity Fundamentals Course.
Writing and Compiling the Smart Contract
The biggest highlight in any Hardhat tutorial would focus on writing and compiling the smart contracts for your NFTs. You can begin by scripting a simple contract followed by compiling it. You have to start by developing another Solidity file in the separate “contracts” directory. Here is the command which can help you create the new Solidity file.
mkdir contracts && cd contracts && touch MyCryptoNFT.sol
It is important to note that you have to rely on OpenZeppelin package for writing your NFT contract. Therefore, you have to take care of open-zeppelin package installation before writing the smart contract for your NFT. Here is the command for installing open-zeppelin package to develop NFT smart contracts.
npm install --save-dev @openzeppelin/contracts
Now, you can write the smart contract code for your NFT like the following example.
pragma solidity ^0.7.3; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MyCryptoNFT is ERC721 { constructor(string memory name, string memory symbol) ERC721(name, symbol) {} }
One of the most basic things you must keep in mind while creating an NFT smart contract would refer to the understanding of the code example. The first highlight in a Solidity file would focus on declaring the version of the compiler. In the next step, you can work on importing the ERC-721 implementation or the NFT contract through the OpenZeppelin packages like in JavaScript.
If you want to deploy contracts associated with NFTs, you should have a fluent command of Solidity programming language. Solidity has been designed as a popular contract-centric language, and different contracts could include members like variables and functions. The example code highlighted here includes the ‘constructor’ function, which would be called upon deploying the contract.
It is also important to note the contract’s inheritance of the ERC-721 properties and it transfers the “name” and “symbol” arguments to the ERC-721 contract. The arguments help in defining the name & symbol for the NFT token you plan on creating. You can specify the values of “name” & “symbol” according to your preference once you are ready for deployment.
Now, you have to use the following command to compile Solidity contract for your NFT project on Hardhat.
npx hardhat compile
Users would receive some warnings for the compilation process. However, it is important to avoid them to stay away from any confusion in understanding how to create and deploy your NFT smart contracts. The result of the compilation process would show the “Compilation finished successfully” in the terminal. In addition, the compilation process also leads to creation of “/artifacts” alongside the “/cache” directories. On top of it, you must note that you could utilize “abi” for the artifacts when you need interactions with the smart contract during development of the front end.
Learn the concept, elements, future and use cases of NFTs from the E-book: Non Fungible Tokens (NFTS) E-Book
Testing the Contract
The steps to create NFTs with Hardhat would also include a profound emphasis on contract testing. NFTs command significant financial value and utility across different use cases. Therefore, testing is obviously a critical highlight in the process of creating NFTs. You would need a few packages for the testing procedure, and you can install them with the following command.
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
The command can showcase many comprehensive highlights in the testing process. For example, you can identify the “ethereum-waffle” element as a testing framework which fits perfectly for use cases with smart contracts. On the other hand, “chai” serves as the assertion library in this case. The process of creating and deploy NFT contract in this tutorial would use Mocha as well as Chai for writing tests in Waffle. Another important highlight in the test command would refer to “ethers.js,” which is the JavaScript SDK that helps in facilitating interactions with Ethereum blockchain. You will also note that remaining two packages in the test command are actually Hardhat plugins.
The next step in creating NFT smart contract for the testing phase focuses on developing another directory named ‘test’ within the root directory. In addition, you should also create another file named, “test.js,” by using the following command.
mkdir test && cd test && touch test.js
Most important of all, you should ensure that “@nomiclabs/hardhat-ethers” package is available in the “hardhat.config.js.” You can use the following command to require “@nomiclabs/hardhat-ethers” to ensure its seamless availability.
require (“@nomiclabs/hardhat-ethers”);
Here is an example of a simple test code for your NFT smart contract project.
const { expect } = require("chai"); describe("MyCryptoNFT", function () { it("Should return the right name and symbol", async function () { const MyCryptoNFT = await hre.ethers.getContractFactory("MyCryptoNFT"); const myCryptoNFT = await MyCryptoNFT.deploy("MyCryptoNFT", "MCN"); await myCryptoNFT.deployed(); expect(await myCryptoNFT.name()).to.equal("MyCryptoNFT"); expect(await myCryptoNFT.symbol()).to.equal("MCN"); }); });
The test code is an important highlight in the Hardhat NFT tutorial as it helps in deploying the contract to the local Hardhat environment. In addition, the code also verifies whether the values of “name” & “symbol” arguments are exactly same as you expected.
Upon running the test, you can easily find whether your contract passes the test. It shows you a clear glimpse of your preparedness for the next step.
Curious to understand the complete smart contract development lifecycle? Enroll in Smart Contracts Development Course Now!
Using console.log() for Hardhat
The console.log() feature is available with JavaScript, and interestingly, it is available for Hardhat now. You must focus on the use of console.log() feature as it is one of the significant reasons to choose Hardhat. Let us take a look at the example of using console.log() in the Solidity file with the following example.
pragma solidity ^0.7.3; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "hardhat/console.sol"; contract MyCryptoNFT is ERC721 { constructor(string memory name, string memory symbol) ERC721(name, symbol) { console.log("name", name); console.log("symbol", symbol); console.log("msg.sender", msg.sender); //msg.sender is the address that initially deploys a contract } }
Once you have added the console.log() option in Solidity file, you can run the testing process again by using “npx hardhat test” command. The command would compile Solidity contract once again, followed by running the test. The output would be a little different as you can notice the values documented from the contract, such as “name” and “symbol” details. You can notice that the console.log() feature plays a vital role in simplifying the debugging procedure. However, you must also account for the limitations associated with the console.log() option for debugging. It is useful for supporting data types such as string, address, uint, and bool. Apart from the trivial limitation, you can utilize Solidity, just like JavaScript.
Are you aspiring to learn the fundamentals of the Ethereum Virtual Machine and smart contracts’ upgradability? Enroll now in the Advanced Solidity Development Course.
Deploying the Contract
The curiosity regarding the process to deploy contract for NFTs is inevitable at this stage. Interestingly, you have many options for deploying your contract, including on a local mirrored implementation of the main network or the mainnet itself. You can also opt for deploying the contract to a testing network.
Let us assume the deployment process on a local in-memory instance for the Hardhat network to ensure simplicity. The local in-memory instance would run at startup by the default settings. You can start the deploy NFT contract process by creating another directory named “scripts” within the root directory. In addition, you must create the “deploy.js” directory in the new directory by using the following command.
mkdir scripts && cd scripts && touch deploy.js
However, it is important to note that you would need a deploy script for your NFT smart contract. Here is an example of the script for deploying the contract, where you can use constructor values.
async function main() { const MyCryptoNFT = await hre.ethers.getContractFactory("MyCryptoNFT"); const myCryptoNFT = await MyCryptoNFT.deploy("MyCryptoNFT", "MCN"); await myCryptoNFT.deployed(); console.log("MyCryptoNFT deployed to:", myCryptoNFT.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
The process to create NFTs with Hardhat becomes easier with access to Hardhat tutorials. The tutorials can deliver a detailed impression of the functions of each line of code. In the case of the deploy script example, you can notice the “ContractFactory” element in the ethers.js. It is basically an abstraction that helps in deploying new smart contracts. The MyCryptoNFT included with ContractFactory actually works as a factor for different instances of the NFT contract. Another important thing you must keep in mind before you deploy NFT contract is the necessity of removing console.log() before deployment.
Once you call deploy() on a ContractFactory, it would begin the deployment process along with a “Promise” resolving to the contract. It practically works as the object with a method for each smart contract function.
After verifying all the checklists for the deployment process, you can deploy the smart contract. You can deploy the NFT smart contract by returning back to the root of the project directory. Now, you can enter the following command in the command line terminal.
npx hardhat run scripts/deploy.js
Now, you can find output like the following message,
MyCryptoNFT deployed to: 0x6FbDB4217658afecb763f039d23F641f64980aa2
That’s it; you have deployed your NFT contract successfully on the local network.
Excited to build your skill in Ethereum development by leveraging the ethers.js library? Enroll Now in Ethers.Js Blockchain Developer Course!
Other Requirements
The process of creating NFT smart contract would also emphasize the necessity of Ethers.js and “hardhat.config.js.” You must keep in mind that Ethers.js serves as a functional library for easier interactions and requests to Ethereum. It works through wrapping standard JSON-RPC methods by leveraging methods with a favorable user experience. You can capitalize on Ethers.js for support during the contract deployment methods.
On the other hand, you must also focus on configuring “hardhat.config.js” according to your needs for targeting specific networks. Therefore, it is important to update “hardhat.config.js” so that the smart contract project is aware of all dependencies and plugins. Since an NFT smart contract project can involve multiple plugins and dependencies, “hardhat.config.js” can help in keeping the complete NFT contract updated.
Learn more about the basics of NFT (Non-Fungible Token) and the practical implications of non-fungible tokens through NFTs Skill Path
Bottom Line
The Hardhat NFT tutorial for writing, compiling, testing, and deploying an NFT contract delivers a practical guide for using Hardhat to create your own NFTs. You must note that Hardhat is the latest entry in smart contract development environments and features powerful functionalities. Some of the popular features of Hardhat refer to helpful stack traces and support for multiple versions of the Solidity compiler.
In addition, it also allows support for verifying smart contracts in Etherscan. Therefore, Hardhat is a credible option for creating your first NFT, which would rely on a smart contract. However, you need to develop fluency in Hardhat and smart contract development basics before trying your hand at NFTs. Learn more about smart contracts and NFTs and the best practices to develop one.
*Disclaimer: The article should not be taken as, and is not intended to provide any investment advice. Claims made in this article do not constitute investment advice and should not be taken as such. 101 Blockchains shall not be responsible for any loss sustained by any person who relies on this article. Do your own research!