Build your NFT on POSI Chain

Doris Position
Position Exchange
Published in
7 min readDec 20, 2022

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 contracts and deploying them easily. The following post offers you a tutorial on using Hardhat for creating your own NFTs.

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 contracts 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.
Setting the Foundation for the Project
The first requirement in the steps to deploy a contract for your NFT on the 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.

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.solCopy

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 the 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 the 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 the 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 the development of the front end.

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 that 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 deploying NFT contracts 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 the Ethereum blockchain. You will also note that the remaining two packages in the test command are actually Hardhat plugins.

The next step in creating an 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 local Hardhat environment. In addition, the code also verifies whether the values of “name” & “symbol” arguments are exactly the 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.

Deploying the Contract

The curiosity regarding the process to deploy contracts 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.

Once you call deploy() on a ContractFactory, it will begin the deployment process along with a “Promise” resolution 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

--

--