Hardhat is a popular development environment for EVM-compatible blockchains, consisting of multiple components for writing, compiling, debugging, and deploying smart contracts.
For more information about the topics covered in this tutorial, check out the HardHat Tutorial repository.
Installation
Navigate to your profile folder and follow the steps below to install HardHat (npm/node v8.9.4 LTS or later is required):
Note that we need to pass in private keys/mnemonic for Provider. You can create a secret.json to store them. Do not forget to add this file to the .gitignore of your project so that you don't accidentally check your private keys into a public repository. And make sure you keep this file in an absolutely safe place!
Writing Smart Contracts
For the sake of simplicity, let's use the 1_Storage.sol file we're already familiar with from the Remix tutorial. Copy the code below into a new file called Storage.sol and save it to the contracts folder.
// SPDX-License-Identifier: GPL-3.0pragmasolidity >=0.7.0 <0.9.0;/** * @title Storage * @dev Store & retrieve value in a variable */contract Storage {uint256 number;/** * @dev Store value in variable * @param num value to store */functionstore(uint256 num) public { number = num; }/** * @dev Return value * @return value of 'number' */functionretrieve() publicviewreturns (uint256){return number; }}
Contract Compilation
Run the following command to compile the contract:
npx hardhat compile
Contract Testing
Create a new file called storage-test.js containing the following code, and save it to the test folder:
To test our Storage.sol contract on the built-in HardHat network, run the following command:
npx hardhat test --network hardhat
$ npx hardhat test --network hardhat Storage Test store function ✔ shouldworkproperly ✔ shouldthrow 2 passing (1s)
Contract Deployment and Interaction
HardHat makes it easy to use the ethers.js library to deploy and interact with our smart contracts. Create a new file called deploy-and-call.js in the scripts folder that contains the following code:
// We require the Hardhat Runtime Environment explicitly here. This is optional// but useful for running the script in a standalone fashion through `node <script>`.//// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat// will compile your contracts, add the Hardhat Runtime Environment's members to the// global scope, and execute the script.consthre=require("hardhat");asyncfunctionmain() {constStorage=awaithre.ethers.getContractFactory("Storage");conststorage=awaitStorage.deploy();awaitstorage.deployed();console.log("Storage contract deployed to:",storage.address);console.log("call retrieve():",awaitstorage.retrieve())console.log("call store(), set value to 100")consttx=awaitstorage.store(100)awaittx.wait()console.log("call retrieve() again:",awaitstorage.retrieve())}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main().catch((error) => {console.error(error);process.exitCode =1;});
This script does a few things:
Deploys our smart contract;
Prints the deployed contract's address;
Calls the retrieve function to check the stored number;
Calls the store function to store a new number;
Calls the retrieve function to check the stored number again;
Let's run the script by executing the following command:
npx hardhat run scripts/deploy-and-call.js
$ npx hardhat run scripts/call.jsStorage contract deployed to:0x65e2F3E4287C0563fBB066134A380e90a48d2D99call retrieve(): BigNumber { value:"0" }call store(), set value to 100call retrieve() again: BigNumber { value:"100" }
We can see that the script correctly deployed the contract, stored a number, and confirmed that the number is now stored in the contract.
We can use Core Scan to search for the contract's address to verify that the contract was successfully deployed and called.
Further Reading
For detailed instructions on using HardHat and plugins such as ethers.js, please visit HardHat's official website.