Hardhat Essentials (ref)
1. Creating a new Hardhat project
mkdir hardhat-tutorial
cd hardhat-tutorial
npm init
npm install --save-dev hardhat
npx hardhat init
2. Hardhat's architecture
Hardhat is designed around the concepts of tasks and plugins.
2.1 Tasks
To see the currently available tasks in your project, run:
# List available tasks
npx hardhat
# Compile the smart contracts
npx hardhat compile
# Run the tests
npx hardhat test
2.2 Plugins
Hardhat is unopinionated in terms of what tools you end up using, but it does come with some built-in defaults. All of which can be overridden. Most of the time the way to use a given tool is by consuming a plugin that integrates it into Hardhat.
# Install the recommended plugin
npm install --save-dev @nomicfoundation/hardhat-toolbox
Add this line to the top of your hardhat.config.js:
require("@nomicfoundation/hardhat-toolbox");
3. Writing tests
- Contracts live inside a folder called
contracts. - Tests live inside a folder called
test.
const [owner] = await ethers.getSigners();
=> Here we're getting a list of the accounts in the node we're connected to, which in this case is Hardhat Network, and we're only keeping the first one.
NOTE: Test setups can be re-used with fixtures.
4. Debugging with Hardhat Network
When running your contracts and tests on Hardhat Network you can print logging messages and contract variables calling console.log() from your Solidity code. To use it you have to import hardhat/console.sol in your contract code.
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract Token {
// ...
// Using console.log() inside a Solidity contract
console.log(
"Transferring from %s to %s %s tokens",
msg.sender,
to,
amount
);
// ...
}
5. Deploying to a live network
Deployments are done through HardHat Ignition.
npx hardhat ignition deploy ./ignition/modules/Token.js --network sepolia
6. Verify on Etherscan
npx hardhat ignition deploy ignition/modules/Lock.ts --network sepolia --verify
7. Contract Deployed at:
0x69A5C0E26FA12FcDfaD9E88e78e03054fAb107a8
EtherScan
https://holesky.etherscan.io/address/0x69A5C0E26FA12FcDfaD9E88e78e03054fAb107a8#code
8. Using OpenZeppelin contracts
npm install @openzeppelin/contracts
import "@openzeppelin/contracts/access/Ownable.sol";
contract Box is Ownable {}
9. To deploy to HardHat local network
On one terminal
npx hardhat node
On a separate terminal:
npx hardhat ignition deploy ignition/modules/Escrow.ts --parameters ignition/parameters.json
10. Using HardHat vars
npx hardhat vars set INFURA_API_KEY
npx hardhat vars get INFURA_API_KEY
npx hardhat vars list
Then in hardhat.config.ts:
const INFURA_API_KEY = vars.get("INFURA_API_KEY");