Back to Blog
EthereumSmart ContractsHardhatBlockchain Development

Developing Smart Contracts Locally with Hardhat: A Comprehensive Guide

Buidl On Base

Buidl On Base

Nov 22, 2024

Developing Smart Contracts Locally with Hardhat: A Comprehensive Guide

Hardhat is a powerful Ethereum development environment that simplifies the process of building, testing, and deploying smart contracts. Whether you're a beginner or an experienced developer, Hardhat provides a suite of tools designed to make local development smooth and efficient. It stands out for its flexibility, allowing you to tailor your development environment to your specific needs.

Why Use Hardhat?

Hardhat is preferred by many developers due to its extensive features. Unlike other tools like Truffle, Hardhat offers advanced debugging capabilities, flexible scripting, and seamless integration with other Ethereum development tools. It’s particularly valuable for its ability to run a local Ethereum network, which mimics the mainnet, enabling thorough testing and debugging before deploying your contracts to live environments.

Prerequisites

Installing Hardhat

Installing Hardhat is straightforward. Start by installing it as a development dependency in your project:

npm install --save-dev hardhat

Next, set up a new Hardhat project by running:

npx hardhat

This command will guide you through creating a basic Hardhat project, where you can choose to create a sample project or an empty one based on your needs.

Project Structure

Once your project is set up, you’ll notice several directories and files generated by Hardhat:

Understanding the purpose of these directories will help you organize your code and streamline your development process.

Creating a Simple Contract

Let’s create a basic Solidity contract. Open the `contracts/` directory and create a file named `Storage.sol`:

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

contract Storage {
    uint256 private number;

    function set(uint256 _number) public {
        number = _number;
    }

    function get() public view returns (uint256) {
        return number;
    }
}

This contract allows users to store and retrieve a number, a simple yet effective way to understand Solidity’s syntax and contract structure.

Compiling the Contract

To compile your smart contract, use the following command:

npx hardhat compile

Hardhat will compile your contract and create the necessary artifacts in the `artifacts/` directory, which you can use later for deployment and testing.

Writing Tests

Testing is a critical part of smart contract development. Hardhat uses Mocha and Chai for writing and running tests. Create a test file in the `test/` directory:

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

describe("Storage", function () {
  it("Should store and return the correct value", async function () {
    const Storage = await ethers.getContractFactory("Storage");
    const storage = await Storage.deploy();
    await storage.deployed();

    await storage.set(42);
    expect(await storage.get()).to.equal(42);
  });
});

This test ensures that the `Storage` contract correctly stores and returns the value passed to it.

Running Tests

Run your tests with:

npx hardhat test

Hardhat will execute the tests and provide feedback on the results, helping you catch any issues early in the development process.

Deploying Your Contract

To deploy your contract to the Base testnet, run:

npx hardhat run scripts/deploy.js --network base-sepolia