Skip to main content
Create your own

Basic Hardhat CLI Interactions

Hello! Welcome back to your course on blockchain development.

In the last few lessons, we've covered a lot of ground. You've set up a full local development environment with Hardhat, secured some test ETH, and learned how to send and inspect a transaction on a public testnet using Etherscan. This gave you a feel for the end-to-end lifecycle of a live transaction.

Now, we're bringing our focus back to the local environment. Before you deploy a smart contract to any network (test or mainnet), you must first be sure it's correct and works as intended. This lesson is about the fundamental developer workflow you'll use every day: compiling your code and running automated tests.

By the end of this lesson, you will be able to interact with your Hardhat project using the two most important command-line interface (CLI) tasks: compile and test.

1. The Core Developer Workflow

In our second lesson, you initialized a sample Hardhat project. Let's revisit its structure. Your project folder contains several key directories:

  • contracts/: This is where you write your Solidity smart contract code (e.g., Lock.sol).
  • scripts/: This holds scripts to automate tasks, like deploying your contract.
  • test/: This contains the automated tests for your smart contract.
Hardhat Project File Structure
This image shows a typical folder structure for a Hardhat project. The `contracts`, `test`, and `hardhat.config.js` files are the ones we'll be most concerned with in the coming lessons.

The standard development loop for a smart contract developer looks like this:

  1. Write Code: Create or modify your Solidity contract in the contracts folder.
  2. Compile: Convert the Solidity code into a format the Ethereum Virtual Machine (EVM) can understand.
  3. Test: Run automated tests to verify the contract's logic and security.
  4. Deploy: Deploy the compiled contract to a network (like our local Hardhat network or a public testnet).

Today, we will master steps 2 and 3 using the Hardhat CLI.

2. Hardhat as a Task Runner

Hardhat is built around the concept of "tasks." Every command you run, like npx hardhat <task>, is executing a predefined piece of logic. To begin, let's explore what tasks are available in our project.

Open a terminal in your project's root directory and run the following command:

npx hardhat

You'll see a list of built-in tasks like compile, test, node, and run. This command is your go-to reference for discovering what you can do with Hardhat.

To understand this concept more formally, please read the brief introduction from the official Hardhat documentation.

Getting started with Hardhat

This section from the official Hardhat documentation introduces the core concepts of the Hardhat Runner, tasks, and plugins.

Read the sections 'Overview' and 'Running tasks'. Focus on understanding that every command you run with Hardhat is a task.

Now that you understand the task-based nature of Hardhat, let's execute our first and most fundamental task: compile.

3. Compiling a Smart Contract

A smart contract written in Solidity is human-readable, but the EVM cannot execute it directly. The compilation step translates your .sol files into two crucial pieces of data:

  • Bytecode: The low-level machine code that is actually deployed to the blockchain and executed by the EVM.
  • Application Binary Interface (ABI): A JSON file that acts like an API definition for your contract. It tells applications (and other contracts) what functions are available and how to call them.

Given your background in business informatics, you can think of this process as being analogous to compiling a Java program into bytecode for the JVM. The ABI serves a similar purpose to an interface definition file or API documentation, standardizing how external clients interact with the compiled object.

The following video will demonstrate how to run the compile task and show you where the output files are stored.

Build and Deploy Smart Contracts using Hardhat | [See Description]

Watch this segment from Patrick Collins's tutorial to see the compile command in action.

Watch from 02:42 to 03:56. Pay close attention to the npx hardhat compile command and the creation of the artifacts directory. The video explains the purpose of this directory and its contents.

As you saw in the video, running the compile command is straightforward. In your terminal, execute:

npx hardhat compile

After the command finishes, you will see a new directory in your project: artifacts/. This folder contains the bytecode and ABI for your compiled contracts. You rarely need to edit these files manually, but it's essential to know that this is where your deployable contract and its interface definition live.

4. Testing a Smart Contract

With immutable code and real assets at stake, testing is arguably the most critical part of the smart contract development lifecycle. Hardhat makes this process efficient with the Hardhat Network, an integrated local Ethereum node designed for development.

When you run your tests, Hardhat automatically:

  1. Starts a fresh, in-memory blockchain instance.
  2. Deploys your contracts to this local network.
  3. Runs your test scripts against them.
  4. Tears down the network once the tests are complete.

This provides a clean, fast, and isolated environment for every test run, which is a massive improvement over deploying to a public testnet for every small change.

Let's watch how this works in practice.

Build and Deploy Smart Contracts using Hardhat | [See Description]

This next segment from the same video demonstrates the test command and explains the role of the Hardhat Network.

Watch from 04:49 to 05:32. Note the npx hardhat test command and the explanation of how Hardhat uses its own built-in network to run tests without needing an external tool like Ganache or a public testnet.

Now, run the test command yourself:

npx hardhat test

You should see output indicating that the tests in the sample test/Lock.js file have passed. These tests check if the Lock.sol contract deploys correctly and if its withdraw function behaves as expected.

We will dedicate Module 3 entirely to writing tests, so for now, the key takeaway is simply how to run them and to understand that they execute on a temporary, local blockchain.

For convenience, you can also use the hh shorthand, which is an alias for npx hardhat.

hh test

This is a small but welcome time-saver you'll appreciate in your daily workflow.

Conclusion

You have just mastered the two pillar commands of the Hardhat workflow. This local compile-and-test loop is where you will spend the vast majority of your time as a developer, ensuring your contracts are robust and secure before they ever touch a public network.

In this lesson, you learned to:

  • Interact with a Hardhat project through its command-line task runner.
  • Compile a Solidity smart contract using npx hardhat compile and understand that the results (bytecode and ABI) are stored in the artifacts directory.
  • Run automated tests using npx hardhat test and appreciate that they execute on a temporary, local Hardhat Network for speed and isolation.

This lesson concludes our "Ethereum Development Kickstarter" module. You've gone from zero to having a complete, professional setup for building, testing, and interacting with the Ethereum blockchain.

In our next lesson, we will begin Module 2: Solidity Fundamentals. We'll move beyond the sample project and start writing our own smart contract from scratch, beginning with the foundational elements: state variables, constructors, and functions.

Can't find a good explanation? Sign up and we'll make it for you

Sign up