DEV Community 👩‍💻👨‍💻

nicholas ma
nicholas ma

Posted on

Building An NFT Project: Part 1 - Getting Started With Hardhat

NOTE: we are not coding a smart contract just yet. We are getting familiar with the capabilities of Hardhat so I don’t overwhelm you when we began our full-stack dapp.

If you’re looking to get started with building out Solidity Smart Contracts, I’m sure you’ve dealt with the countless environments that you can go through. From Hardhat to Truffle, to Brownie (although this is a python environment) it’s hard to find the “right” one - and rightfully so, there’s no clear cut winner - but I firmly believe that Hardhat is the most beginner-friendly one out there so we’ll be starting with that.

Setting Up

Prerequisites: make sure you have NodeJS installed.

Let’s get our environment set-up. Make a folder - this is going to be the sandbox that we’re working in - and cd into this folder (I’m assuming you’re using terminal).

mkdir hardhat-sandbox
cd hardhat-sandbox

Initialize a package.json file and we’re going to install hardhat as a dev dependency. (for our testing purposes, it doesn’t matter if we save it as a dev dependency or global, but it will matter later on when we’re actually publishing our work).

npm install --save-dev hardhat

Now we have hardhat downloaded! Our basic setup is almost complete.

npx hardhat

888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888

Welcome to Hardhat v2.9.1

? What do you want to do? …
▸ Create a basic sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
Create an empty hardhat.config.js

  1. We’ll be prompted with a few questions now:
  2. Create a basic sample project
  3. Leave the file path as is
  4. Do you want to add a .gitignore? Yes.
  5. Yes

Do you want to install this sample project's dependencies with npm (hardhat @nomiclabs/hardhat-waffle Ethereum-waffle chai @nomiclabs/hardhat-ethers ethers)? Yes - these dependencies all play a role and we’ll go over their uses as we get familiar with our environment.

Your file structure should look like this:

├── contracts
├── hardhat.config.js
├── node_modules
├── package-lock.json
├── package.json
├── scripts
└── test

Congratulations! Our sandbox is officially set up and now we can really start messing around with what we can really do.

Understanding the file structure

Depending on your developer level, the files in here can be a bit overwhelming (If you’ve been a dev on a team then this might seem relatively standard). The four main components that we need to understand are contracts, scripts, test and hardhat.config.js.

Understanding the Configuration File

If you’re anything like me, config files somehow always give you issues. Not to fear! I’m going to do my best to introduce it in it’s most digestible way. We’re not going to mess and edit our config just yet - we just want to see what we get from Hardhat.

`task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
What are tasks you might be asking. Because I was asking the same question. In our terminal - if we type npx hardhat we’ll see a section representing global sections and tasks available. So in simplest terms. tasks are going to be these global functions that we can call from our terminal to check various things. The one provided to us provides a list of accounts that our local network will spin up (don’t worry if this doesn’t make sense yet).

And remember when we spun up our initialization of hardhat? We installed a few dependencies that we didn’t really know. Well here we see our first example of it being use require("@nomiclabs/hardhat-waffle");. Without getting into the nitty gritty, this is giving us access to the getSigners() function. (hre.ethers is already injected to this file).


This is where all our smart contract files will stay. We’re defaulted with a basic Greeter.sol contract but this folder can hold as many smart contracts as your dapp would need.


Okay no need to be overwhelmed when we’re looking at the sample-script.js file inside the scripts folder! So the main thing to understand is that this is going to be our script that deploys our smart contract. That may seem confusing for now, but once we look at our sample-script, it’ll began to make more sense.

`async function main() {
const Greeter = await hre.ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");

await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
I removed their comments so that we can observe a little more clearly:

Calling getContractFactory(“Greeter”), let’s our script understand where to look for the compiled smart contract code.

We call a built in function deploy to…deploy our contract(passing in “Hello, Hardhat” as the argument).

await greeter.deployed() gives use the time needed to deploy the contract - this is a concept I’m sure you may be familiar with, but working on the Ethereum mainnet, transactions take some time to process. So we need to wait until they’re done.

greeter.address will be some sort of 32 digit address indicating where we can find the smart contract.

Testing, testing, testing

The best part, testing! Before we dive in, let’s run: npx hardhat test and see what happens.

It’s like any other testing framework! So the magic happens through hardhats ability to understand where to find our test files and run them. The concept of what happens is nearly identical with our scripts file so I don’t think I need to go into much more detail on what the sample-test.js file is doing, but I do want to specify what happens when we call npx hardhat test.

Hardhat will spin up a private local blockchain on your machine - deploy the contract - run whatever tests we give it - and then shut off the local blockchain. Neat huh?!


I hope I was able to give you an introduction to Hardhat in an easy and digestible way. I am going to go more into depth on how we are able to use Hardhat for our own development later, but don’t be shy and start messing around with it. If you have any questions, you can always find my on Twitter @yeeckin.

Top comments (0)

🌚 Life is too short to browse without dark mode