Odewole Babatunde Samson
BigSam Blog


BigSam Blog

How to write your first Ethereum smart contract with Hardhat and Connect a wallet to Hardhat Network

How to write your first Ethereum smart contract with Hardhat and Connect a wallet to Hardhat Network

Write Ethereum smart contract with Hardhat

Odewole Babatunde Samson's photo
Odewole Babatunde Samson
·Jan 9, 2022·

5 min read

Table of contents

  • Overview

In this article, I will take you through the process of writing your first smart contract in solidity with Hardhat, test, compile your contract, and connect your wallet or Dapp to the Hardhat Network. Follow this tutorial and deploy your first contract.


  1. What is Hardhat?
  2. Installation Of Hardhat
  3. Compiling your contract
  4. Deploying your Contract
  5. Conclusion

What is Hardhat?

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate the recurring tasks that are inherent to the process of building smart contracts and dApps, as well as easily introducing more functionality around this workflow. This means compiling, running and testing smart contracts at the very core.

Hardhat comes built-in with Hardhat Network, a local Ethereum network designed for development. Its functionality focuses around Solidity debugging, featuring stack traces, console.log() and explicit error messages when transactions fail.

Hardhat Runner, the CLI command to interact with Hardhat, is an extensible task runner. It's designed around the concepts of tasks and plugins. Every time you're running Hardhat from the CLI you're running a task. For E.g. npx hardhat compile is running the built-in compile task. Tasks can call other tasks, allowing complex workflows to be defined. Users and plugins can override existing tasks, making those workflows customizable and extendable.

A lot of Hardhat's functionality comes from plugins, and, as a developer, you're free to choose which ones you want to use. Hardhat is unopinionated in terms of what tools you end up using, but it does come with some built-in defaults. All of which can be overridden.

Installation Of Hardhat

First, you'll need to get node/npm. If you don't have its head over here.

I'd recommend you install at least node v15 or you may run into some issues!

Next, let's head to the terminal (Git Bash will not work). Go ahead and cd to the directory you want to work in.

Once you're there run these commands:

mkdir my-first-contract
cd my-first-contract
npm init -y
npm installsave-dev hardhat

Cool, now we should have Hardhat. Let's get a sample project going.


npx hardhat


Note: if you have yarn installed along with npm, you may get errors such as npm ERR! could not determine executable to run. In this case, you can do yarn add hardhat.

Choose the option to create a sample project. Say yes to everything.

The sample project will ask you to install hardhat-waffle and hardhat-ethers. These are other goodies we'll use later :).

Go ahead and install these other dependencies just in case it didn't do it automatically.

 npm install --save-dev "hardhat@^2.8.2" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"

Finally, run

npx hardhat accounts

and this should print out a bunch of strings that look like this: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720


Compiling your contract

Next, if you take a look at contracts/, you should be able to find Greeter.sol:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        console.log("Deploying a Greeter with greeting:", _greeting);
        greeting = _greeting;

    function greet() public view returns (string memory) {
        return greeting;

    function setGreeting(string memory _greeting) public {
        console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
        greeting = _greeting;

To make sure everything is working, run:

npx hardhat compile


Testing your contract

The sample project comes with these tests that use Waffle and Ethers.js. You can use other libraries if you want. Check the integrations described in our guides.

If you take a look at test, you should be able to find sample-test.js:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Greeter", function () {
  it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");
    await greeter.deployed();

    expect(await greeter.greet()).to.equal("Hello, world!");

    const setGreetingTx = await greeter.setGreeting("Hola, mundo!");

    // wait until the transaction is mined
    await setGreetingTx.wait();

    expect(await greeter.greet()).to.equal("Hola, mundo!");

You can then run:

npx hardhat test

You should see something like this:


Connecting a Wallet or Dapp to Hardhat Network

Hardhat will always spin up an in-memory instance of Hardhat Network on startup by default. It's also possible to run Hardhat Network in a standalone fashion so that external clients can connect to it. This could be MetaMask, your Dapp front-end, or a script.

To run Hardhat Network in this way, run

npx hardhat node

Started HTTP and WebSocket JSON-RPC server at

This will expose a JSON-RPC interface to Hardhat Network. To use it connect your wallet or application to http://localhost:8545.

If you want to connect Hardhat to this node to, for example, run a deployment script against it, you simply need to run it using --network localhost.

To try this, start a node with npx hardhat node and re-run the sample script using the network option:

npx hardhat run scripts/sample-script.js –network localhost


Congrats! You have created a project, run a Hardhat task, compiled a smart contract, installed a Waffle integration plugin, written and run a test using the Waffle and ethers.js plugins, and deployed a contract.

I hope you learned something new about solidity(using hardhat), web3, smart contracts. Kindly drop your feedback:)

You can also follow me on Twitter , Github and LinkedIn

Did you find this article valuable?

Support Odewole Babatunde Samson by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this