Complete Hardhat Tutorial: How to build smart contracts with hardhat and blockchain
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum smart contract. It makes the life of a Blockchain developer very easy. Let us talk about Hardhat.
How To Install Hardhat?
Requirements Before Installing
- NodeJS v12 or later.
- Install the latest version of Visual Studio.
Create an empty folder and open the folder in the VS Code.
Run the following command in the VS code.
npm init --yes
Code language: Bash (bash)
npm install --save-dev hardhat
Code language: Bash (bash)
Once your installation is done.Run the following command in the VS code
npx hardhat
Code language: Bash (bash)
Select the first one i.e. “Create a basic sample project” and press enter.
Once you will click enter. A hardhat project will get created.
Run the following command to install the dependencies for using Hardhat.
npm install --save-dev "hardhat@^2.9.9" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"
Code language: Bash (bash)
How To Use Hardhat For Smart Contract?
Let us have a look at all the project folders created during the installation.
- contracts – In this, you will be creating your smart contracts.
- scripts – This folder contains the deployment script to deploy your contract on the Blockchain.
- test – This will contain the test file for your smart contract.
- hardhat.config.js – This is the configuration file for setting the various things like the solidity compiler version, the blockchain at which you want to deploy your smart contract, etc.
How To Compile A Smart Contract?
In your contracts
folder, you should be seeing Greeter.sol
smart contract. Let us compile this smart contract.
Run the following command in the terminal.
npx hardhat compile
This command will compile all the smart contracts present in the contracts
folder.
artifacts
folder will be created once your smart contract will get compiled. This folder will contain the ABI and Bytecode of the smart contracts that got compiled.
How To Deploy A Smart Contract?
Locally
Let us deploy the Greeter.sol
smart contract that we have just compiled to hardhat local network.
Before deploying the smart contract let us have look at our scripts/sample-script.js
as this will be used to deploy the smart contract.
The below code is used to create an instance of the Greeter.sol
smart contract.
const Greeter = await hre.ethers.getContractFactory("Greeter");
Code language: JavaScript (javascript)
The below code is used to deploy the Greeter.sol
smart contract on the Blockchain.
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
Code language: JavaScript (javascript)
“Hello, Hardhat” is passed as an argument to the constructor of the Greeter.sol
smart contract.
If you want to deploy any smart contract just pass the name of your contract to the getContractFactory("NameOfYourContract")
.
The below code is to console.log the contract address of the smart contract that gets deployed to the blockchain.
console.log("Greeter deployed to:", greeter.address);
Code language: JavaScript (javascript)
Step 1: First, we need to start a hardhat local network so that we can deploy the smart contract on it.
To start, the hardhat local network. Run the following command.
npx hardhat node
This will start the hardhat local network.
Step 2: Split the VS code terminal. Run the following command in the second terminal of the VS code to deploy the smart contract.
npx hardhat run --network localhost scripts/sample-script.js
Code language: Bash (bash)
Once the contract is deployed you will see the deployed address of your smart contract in the terminal.
Remote Test network
Let us deploy the Greeter.sol
smart contract over a test network.The same steps will be applied when deploying it to the mainnet.
1. Login to Infura.
2. Click on “CREATE NEW PROJECT”.
3. Choose “Ethereum” as Prduct.
4. Choose “Rospten” as Endpoints.
5. Choose Ropsten as your network in your Metamask Wallet.
6. Copy and paste the below code to your hardhat.config.js
file.
require("@nomiclabs/hardhat-waffle");
const ROPSTEN_PRIVATE_KEY = "YOUR ROPSTEN PRIVATE KEY";
module.exports = {
solidity: "0.8.4",
networks: {
ROPSTEN: {
url: "YOUR INFURA ROPSTEN ENDPOINT",
accounts: [`${ROPSTEN_PRIVATE_KEY}`],
},
},
};
Code language: JavaScript (javascript)
7. Copy-paste your “Infura Ropsten Endpoint” URL into the URL part of the code. Use the HTTPS one.
8. Select an account from the Metamask wallet and copy-paste its private key and assign it to the ROPSTEN_PRIVATE_KEY variable given in the hardhat-config.js
file.
Let us now deploy the Greeter.sol
smart contract on Ropsten testnet. Run the following command in the terminal.
npx hardhat run scripts/sample-script.js
--network ROPSTEN
Code language: Bash (bash)
If everything is done correctly, you will see your deployed smart contract address in your terminal.
Let us check this on ropsten etherscan . Paste your contract address in the “All Filters” search bar.
How To Test A Smart Contract?
Let us test the Greeter.sol
smart contract.
If you will go to the test folder provided by hardhat. You will see a sample-test.js
file.
The test written here is using Mocha,Ether.js and ethereum-waffle.
To test the Greeter.sol
smart contract run the following command in the VS Code terminal.
npx hardhat test
Code language: Bash (bash)
If everything is correct, you will see the following output in the terminal.
Conclusion
I hope by now you have become a master of the Hardhat tool. We learned so many things such as how to compile, deploy, and test a smart contract.
If you like hardhat, you’d love our web3 learning path that helps you become an employable web3 developer in a few months. Try out the first solidity programming course today.
Sharing is caring
Did you like what Kshitij wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
2.43k students learning
Kshitij
Blockchain Developer Guide
217 students learning
Rahul Agarwal
Learn NFT Programming: Create and sell your own NFTs using Solidity