Complete Hardhat Tutorial: How to build smart contracts with hardhat and blockchain

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

  1. NodeJS v12 or later.
  2. 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 --yesCode language: Bash (bash)
npm install --save-dev hardhatCode language: Bash (bash)

Once your installation is done.Run the following command in the VS code

npx hardhatCode language: Bash (bash)

Select the first one i.e. “Create a basic sample project” and press enter.

Select the first one

Once you will click enter. A hardhat project will get created.

Project 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.

  1. contracts – In this, you will be creating your smart contracts.
  2. scripts – This folder contains the deployment script to deploy your contract on the Blockchain.
  3. test – This will contain the test file for your smart contract.
  4. 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.

artifacts folder

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.

sample-script.js

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.jsCode language: Bash (bash)

Once the contract is deployed you will see the deployed address of your smart contract in the terminal.

Deployed Smart Contract

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”.

CREATE NEW PROJECT

3. Choose “Ethereum” as Prduct.

Select the above options

4. Choose “Rospten” as Endpoints.

Select Ropsten

5. Choose Ropsten as your network in your Metamask Wallet.

Choose Ropsten Testnet

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.

Infura Ropsten Endpoint

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 ROPSTENCode language: Bash (bash)

If everything is done correctly, you will see your deployed smart contract address in your terminal.

Deployed Smart Contract Address

Let us check this on ropsten etherscan . Paste your contract address in the “All Filters” search bar.

Ropsten Etherscan

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.

sample-test.js

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 testCode language: Bash (bash)

If everything is correct, you will see the following output in the terminal.

Terminal Output

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.

0/10000

No comments so far