Hardhat for Blockchain developer
Project Creation , Project Compilation , Project Deployment in Hardhat
Hardhat is an Ethereum development environment created to simplify the creation, testing, and deployment of smart contracts for programmers. It offers a set of features and tools that let developers create and implement contracts more quickly, effectively, and securely.
1) Project Creation in Hardhat
npm init -y
npm install --save-dev hardhat
npx hardhat
Note : @nomicfoundation/hardhat-toolbox plugin bundles all the commonly used packages and Hardhat plugins we recommend to start developing with Hardhat.
with this plugin all the below will be included
@nomiclabs/hardhat-ethers
@nomiclabs/hardhat-etherscan
hardhat-gas-reporter
solidity-coverage
@typechain/hardhat
npm install --save-dev @nomicfoundation/hardhat-toolbox
NOTE : importing the Toolbox in your Hardhat config. This will make many other imports redundant
Now once project setup is done , sample project structure will contains folders as below
contracts/ - contains source files of the contracts
scripts/ - contains simple automation scripts.
test/ - contains tests files
hardhat.config.js
Hardhat project structure in below
hardhat.config.js is the important file, where we can set up a compiler version to avoid unexpected behaviour or compiling errors as new releases of Solidity are published, To deploy to a specific network /testnet, you need to add a network
entry to your hardhat.config.js
2) Project Compilation in HardHat
To Compile contracts in your Hardhat project
npx hardhat compile
To clear the cache and delete the artifacts.
npx hardhat clean
For code coverage measurement - Hardhat Toolbox includes the solidity-coverage
plugin by default to measure the test coverage in your project
npx hardhat coverage
3) Project Deployment in Hardhat:
Deploying your smart contracts using scripts
npx hardhat node --> gives accounts in hardhat test network
npx hardhat run scripts/deploy.js
npx hardhat run scripts/deploy.js --network <network-name>
eg : To deploy to goerli network
npx hardhat run scripts/deploy.js --network goerli
4) VERIFYING THE CONTRACT :
Once deployed the contract using
npx hardhat run scripts/deploy.js --network <network-name>
Take a note of the address and the unlock time and run the verify
task as mention in below to verfiy deployed contract
npx hardhat verify --network <network> <contract address>