A sample project used to illustrate how to configure and use Hardhat.
nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
git
To verify git installation, run:
git --version
You should see output similar to git version x.x.x
.
Node.js
To verify Node.js installation, run:
node --version
You should see output similar to vx.x.x
.
Yarn (instead of npm)
To verify Yarn installation, run:
yarn --version
You should see output similar to x.x.x
. Yarn might need to be installed with npm or corepack.
-
Clone the project repository and navigate into it:
git clone <repository_url> cd <repository_directory>
-
Install the project dependencies:
npm ci
Or
yarn install --frozen-lockfile
-
Run Hardhat:
npx hardhat
Or
yarn hardhat
-
Create a new project directory and navigate into it:
mkdir <repository_directory> cd <repository_directory>
-
Install and use the correct Node.js version:
nvm ls nvm install 16.20.2 nvm use 16.20.2
-
Install Yarn globally:
npm install -g yarn
-
Initialize a new Node.js project:
npm init -y
Or
yarn init -y
-
Install Hardhat:
npm install --save-dev hardhat@2.9.3
Or
yarn add --dev hardhat@2.9.3
-
Run Hardhat:
npx hardhat
Or
yarn hardhat
-
Choose “Create a basic sample project”.
-
Show Hardhat info:
yarn hardhat
-
Create
./contracts/SimpleStorage.sol
. -
Set Solidity version in
./hardhat.config.js
. -
Create
./scripts/deploy.js
. -
Install Prettier and the Prettier Solidity plugin:
yarn add --dev prettier prettier-plugin-solidity
-
Create
.prettierrc
and.prettierignore
. -
To use environment variables in the
hardhat.config.js
file, run:yarn add dotenv@14.2.0
-
Update
hardhat.config.js
to addrequire("dotenv").config()
, allowing it to use variables in the.env
file. -
Add Hardhat tasks:
yarn add --dev @nomiclabs/hardhat-etherscan@3.0.0 @nomicfoundation/hardhat-verify@2.0.3
-
Run:
yarn hardhat verify help
This shows the
verify
task requires arguments when used withrun
. -
Create
./tasks/block-number.js
as a custom task, then inhardhat.config.js
, require the task. -
Run:
yarn hardhat block-number --network sepolia
Note: Using scripts is a good alternative to tasks.
-
To spin up a node on the local network, run:
yarn hardhat node
Add the network into
hardhat.config.js
aslocalhost
, then run:npx hardhat run scripts/deploy.js --network localhost
-
Use Hardhat console, run:
yarn hardhat console --network localhost
The console can interact with different chains as scripts do.
-
To clean the
artifacts
andcache
folders, run:yarn hardhat clean
-
Create
./test/test-deploy.js
, then run:yarn hardhat test yarn hardhat test --grep store
-
Install Hardhat gas reporter:
yarn add --dev hardhat-gas-reporter@1.0.7
Or
npm install hardhat-gas-reporter@1.0.7 --save-dev
Add
require("hardhat-gas-reporter")
andgasReporter
section intohardhat.config.js
. Use a CoinMarketCap API key.Now run:
yarn hardhat test
-
Install Solidity coverage:
npm install --save-dev solidity-coverage@0.7.18
Or
yarn add --dev solidity-coverage@0.7.18
Add
require("solidity-coverage")
intohardhat.config.js
, then run:yarn hardhat coverage
To deploy the contract, run:
npx hardhat run scripts/deploy.js --network hardhat
To run the tests, execute:
npx hardhat test
To check test coverage, run:
npx hardhat coverage
You can estimate how much gas things cost by running:
npx hardhat test
And you'll see an output file called gas-report.txt
.
To run your own local Hardhat network:
npx hardhat node
In a different terminal:
npx hardhat run scripts/deploy.js --network localhost
You should see transactions happening in the terminal running npx hardhat node
.
If you use MetaMask with a local network, every time you shut down your node, you'll need to reset your account: Settings -> Advanced -> Reset Account. Do not do this with a MetaMask wallet that has real funds in it.
-
Setup Environment Variables
Set your
SEPOLIA_RPC_URL
andPRIVATE_KEY
as environment variables. Add them to a.env
file, similar to.env.example
.PRIVATE_KEY
: The private key of your account (like from MetaMask). NOTE: Use a key without any real funds for development.SEPOLIA_RPC_URL
: The URL of the Sepolia testnet node you're working with. Get setup with one for free from Alchemy.
-
Get Testnet ETH
Visit faucets.chain.link to get some testnet ETH. You should see the ETH show up in your MetaMask.
-
Deploy
npx hardhat run scripts/deploy.js --network sepolia
Or
yarn hardhat run scripts/deploy.js --network sepolia
To verify deployment on a testnet or mainnet, get an API Key from Etherscan and set it as an environment variable named ETHERSCAN_API_KEY
. Add it to your .env
file as seen in the .env.example
.
If your API key is set, it will auto-verify Sepolia contracts. Manually verify with:
npx hardhat verify --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS
To check linting/code formatting:
yarn lint
To fix:
yarn lint:fix