Welcome to the Hardhat QuickStart! This guide will walk you through setting up a local development environment for Ethereum smart contracts using Hardhat. Whether you're a beginner or experienced developer, you'll find everything you need to get started.
Table of Contents
Installation 🔧
To begin, install Hardhat via npm:
npm install --save-dev hardhat
Then, initialize a new project:
npx hardhat init
This creates a basic project structure with essential files like hardhat.config.js
and contracts/
.
Project Setup 🌐
Navigate to your project directory and run:
npx hardhat
You'll be prompted to select a sample project. For this guide, choose "Create a basic sample project". This sets up a contracts/
folder, test/
, and a hardhat.config.js
.
Writing Smart Contracts 📝
Create a new Solidity file in contracts/
, e.g., MyContract.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
uint public number = 10;
function increment() public {
number += 1;
}
}
For more details on Solidity syntax, check out our Smart Contract Development Guide.
Compiling & Testing 🧪
Compile your contracts with:
npx hardhat compile
Run tests using:
npx hardhat test
This executes the test files in test/
and verifies your contract logic.
Deploying to Ethereum 🌍
Deploy your contract to a testnet or mainnet using:
npx hardhat run scripts/deploy.js
Replace scripts/deploy.js
with your deployment script. For advanced deployment options, explore our Truffle QuickStart for comparison.