Skip to content

Instantly share code, notes, and snippets.

@rsrini7
Created December 14, 2025 16:23
Show Gist options
  • Select an option

  • Save rsrini7/0bbba5d1e235fdc8d3f887f0e74ba325 to your computer and use it in GitHub Desktop.

Select an option

Save rsrini7/0bbba5d1e235fdc8d3f887f0e74ba325 to your computer and use it in GitHub Desktop.
Blockchain Hardhat Steps

Mastering Hardhat for Solidity Development

A Technical Guide to Building, Testing, and Deploying Smart Contracts

Executive Summary

Hardhat is a comprehensive development environment for Ethereum software. It facilitates performing frequent tasks, such as running tests, automatically checking code for mistakes, interacting with smart contracts, and debugging. Unlike web-based editors like Remix, Hardhat offers a robust local environment essential for professional-grade development, enabling faster iteration and integration with local blockchains [00:22].


1. Prerequisites & Environment Setup

Before initiating a Hardhat project, ensure the necessary tools are installed.

  • Visual Studio Code (VS Code): The recommended code editor for this workflow [01:00].
  • Node.js: Hardhat is built on top of Node.js. You must verify its installation.

Verification Step: Open your terminal and run the following command to check your Node.js version [01:29]:

node -v

If an error occurs or a version is not returned, download and install Node.js from the official website.


2. Project Initialization

Setting up a structured directory is the first step in creating a manageable development environment.

  1. Create a Project Directory: create a new folder for your project and navigate into it [02:00].

    mkdir my-first-hardhat-project
    cd my-first-hardhat-project
  2. Initialize Node.js Project: Generate a package.json file to manage dependencies [02:32].

    npm init -y

    This creates a default Node.js project structure.

  3. Install Hardhat: Install Hardhat as a development dependency [02:57].

    npm install --save-dev hardhat

3. Configuring the Hardhat Environment

Once the library is installed, you must initialize the specific Hardhat environment.

Command:

npx hardhat init

Configuration Prompts [04:18]:

  • Project Type: Select "Create a TypeScript project" (recommended for type safety).
  • Root: Accept the default project root.
  • Git Ignore: Select "Yes" to add .gitignore.
  • Dependencies: Select "Yes" to install the Hardhat Toolbox. This is a crucial bundle containing essential plugins for testing and deployment [04:53].

4. Understanding the Architecture

Upon initialization, Hardhat generates a specific folder structure [05:31]:

  • contracts/: Contains Solidity source files. The sample project includes Lock.sol, a simple time-lock contract where funds can be withdrawn only after a specific timestamp [06:06].
  • ignition/modules/: Contains deployment modules (TypeScript files). These declarative files describe how to deploy contracts (e.g., Lock.ts) [07:16].
  • test/: Contains unit tests. The sample includes Lock.ts to verify contract logic (e.g., checking unlock times and owner permissions) [09:25].
  • hardhat.config.ts: The configuration file for the entire Hardhat setup (networks, compilers, etc.).

5. Running a Local Blockchain

Hardhat includes a built-in local Ethereum network designed for development. This allows you to simulate a blockchain on your machine without spending real funds.

Start the Node: Open a new terminal window inside your project folder and run [11:31]:

npx hardhat node

Output: This starts a local JSON-RPC server (typically at http://127.0.0.1:8545) and generates 20 test accounts with fake ETH. Keep this terminal running.


6. Contract Deployment

With the local node running, you can deploy your smart contract to it.

Command: Open a second terminal window (ensure you are in the project root) and execute the deployment module [12:11]:

npx hardhat ignition deploy ./ignition/modules/Lock.ts --network localhost

Result: Hardhat will compile the Solidity code and execute the deployment script. You will receive a Contract Address in the output indicating where the contract now lives on your local blockchain [13:57].


7. Interacting with the Contract (Console)

Hardhat provides an interactive console to read from and write to your deployed contracts directly.

  1. Launch Console:

    npx hardhat console --network localhost
  2. Attach to the Contract [15:37]: Inside the console, run the following JavaScript commands:

    // 1. Get the Contract Factory
    const Lock = await ethers.getContractFactory("Lock");
    
    // 2. Attach to your specific deployed address (replace with your actual address)
    const lock = await Lock.attach("YOUR_DEPLOYED_CONTRACT_ADDRESS");
  3. Read Data [16:59]: Call public variables or functions:

    // Check the unlock time
    await lock.unlockTime();
    
    // Check the owner address
    await lock.owner();

8. Testing

Automated testing is critical for smart contract security. Hardhat uses the Mocha test framework and Chai assertions.

Run Tests: In your terminal, execute [18:33]:

npx hardhat test

This command compiles the contracts and runs all test files located in the test/ directory, providing a pass/fail report for each test case.


Conclusion

By following these steps, you have successfully established a professional solidity development environment. You can now compile, test, deploy, and interact with smart contracts on a local network, providing a solid foundation for building complex decentralized applications (dApps).

Source Material: Master Hardhat in MINUTES | The Best Solidity Dev Tool Explained YouTube URL: https://www.youtube.com/watch?v=rxK3UXld8xY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment