0% found this document useful (0 votes)
11 views16 pages

Advance Blockchain Module 1

This document provides an overview of Hardhat, an Ethereum development framework, including its setup, core functionalities, and deployment of smart contracts. It outlines the steps for creating a Hardhat project, writing and deploying a simple smart contract, and interacting with it. Additionally, it discusses various network options available in Hardhat for testing and deploying contracts, such as the Hardhat Network, Localhost, Mainnet, and Testnets.

Uploaded by

shivenyadavs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Advance Blockchain Module 1

This document provides an overview of Hardhat, an Ethereum development framework, including its setup, core functionalities, and deployment of smart contracts. It outlines the steps for creating a Hardhat project, writing and deploying a simple smart contract, and interacting with it. Additionally, it discusses various network options available in Hardhat for testing and deploying contracts, such as the Hardhat Network, Localhost, Mainnet, and Testnets.

Uploaded by

shivenyadavs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CO1: Hardhat Setup and

Fundamentals
Objectives of Unit I
● Understand the basics of Hardhat and its role in
blockchain development.
● Learn how to set up and configure a Hardhat
project.
● Deploy a basic smart contract using Hardhat.
● Explore networks in Hardhat.
● Create and use custom Hardhat tasks for
automation.
● Debug and troubleshoot smart contracts
effectively.
What is HardHat
Hardhat is a powerful Ethereum development
framework that simplifies tasks like compiling,
deploying, testing, and debugging smart
contracts.
Why Use Hardhat?
● Local Ethereum network simulation.
● Robust Testing Framework
● Debugging tools for error resolution.
● Plugin ecosystem to enhance capabilities
(hardhat-ethers).
Why use
Feature
Hardhat
Hardhat Truffle Foundry

Local Network Yes Yes Yes

Plugin Support Excellent Limited Basic

Debugging Robust Moderate Minimal

“Hardhat Plugins be like ‘ I got you covered ‘ “


Core Functionality of Hardhat

Hardhat

Hardhat Task and


Runner Plugins
The main interface for interacting with Hardhat. It Tasks: Each command executed in Hardhat is considered a
acts as a task runner, allowing users to automate task. Tasks can be built-in or user-defined, enabling complex
repetitive tasks in the development workflow. workflows.
Plugins: These extend Hardhat's functionality. Users can add
plugins to customize their development environment according
to their needs.
Installing and Setting
●Hardhat
Install Node.js
● Create a new directory for your project
```
mkdir my-hardhat-project
cd my-hardhat-project

```
● Run npm init -y
● Install Hardhat : “ npm install –save-dev hardhat ”
● Create a project: npx hardhat
● Select options like "empty project" or "sample project."
● Verify folder creation and dependencies.
Continue
● Folder Structure:
○ contracts/: Store Solidity contracts.
○ scripts/: Deployment scripts.
○ test/: Unit testing scripts.
○ hardhat.config.js: Custom
configuration.
● Example Configuration:
Acts as the project’s backbone, defining
Solidity versions, network connections,
and paths.
Writing a Smart Contract
SimpleStorage contract Example

Create SimpleStorage.sol file inside contracts folder

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
uint public storedData;

function set(uint x) public {


storedData = x;
}

function get() public view returns (uint) {


return storedData;
}
}
Deploying a Smart Contract
In the scripts/ folder, create a file named deploy.js
// Import Hardhat runtime environment
const hre = require("hardhat"); hre: Hardhat Runtime
Environment, which provides
async function main() { tools for deployment and
// 1. Get the ContractFactory for the contract interaction.
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");

// 2. Deploy the contract (you can pass constructor arguments here if needed) getContractFactory:
const simpleStorage = await SimpleStorage.deploy(); Prepares the contract for
deployment.
// 3. Wait for the deployment to be mined
await simpleStorage.deployed();
deploy(): Deploys the
console.log("SimpleStorage deployed to:", simpleStorage.address); contract to the specified
} network.
// Execute the main function
main().catch((error) => {
console.error(error);
deployed(): Waits for the
process.exitCode = 1; transaction to be mined.
});
console.log: Logs the
address of the deployed
contract.
Interacting with a Smart Contract
In the scripts/ folder, create a file named interact.js
const hre = require("hardhat");
async function main() { hre: Hardhat Runtime
// Replace with your deployed contract's address Environment, which provides
const deployedAddress = "0xYourDeployedContractAddressHere"; tools for deployment and
interaction.
// Get the contract factory and attach it to the deployed address
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.attach(deployedAddress); getContractFactory:
Prepares the contract for
console.log("Interacting with deployed SimpleStorage contract at:", deployedAddress); deployment.
const setTx = await simpleStorage.set(42);
await setTx.wait(); // Wait for the transaction to be mined
console.log("Stored number set to 42."); deploy(): Deploys the
// Retrieve the stored number contract to the specified
const storedNumber = await simpleStorage.get(); network.
console.log("Retrieved stored number:", storedNumber.toString());
}
main()
deployed(): Waits for the
.catch((error) => { transaction to be mined.
console.error(error);
process.exit(1); console.log: Logs the
}); address of the deployed
contract.
What Happens When You Call
getContractFactory()?
● Loads Compiled Artifacts:
○ The getContractFactory method loads the compiled contract
artifacts from Hardhat's artifacts/ directory.
● Creates a Factory Object:
○ Based on the compiled artifacts, getContractFactory creates a
Contract Factory object.
○ The Contract Factory is a helper object that allows you to:
■ Deploy a new instance of the contract to the blockchain.
■ Specify constructor arguments if the contract requires them.

eg: const simpleStorage = await


SimpleStorage.deploy(arg1, arg2);
Run the Deployment Script
● Start a local blockchain with Hardhat

—> npx hardhat node

● Deploy the contract


—>npm install --save-dev @nomiclabs/hardhat-ethers
ethers

—> npx hardhat run scripts/deploy.js --network localhost

After deployment you should see output like

SimpleStorage deployed to: 0xYourContractAddress


Hardhat Networks
Hardhat provides multiple network options to deploy and interact with
Ethereum smart contracts.
1. Hardhat Network (Default)
● Why Use It?
○ Fast Testing: Transactions are mined instantly, making it ideal for
quick unit tests and debugging.
○ Advanced Debugging: Provides detailed stack traces and supports
Solidity console.log, making it easier to debug contracts.
○ Ephemeral State: Resets the blockchain state each time you restart,
ensuring clean test environments.
● When to Use It?
○ Writing and running automated unit tests.
○ Debugging smart contracts.
○ Initial development before deploying to persistent networks.
Continue
2. Localhost Network :
● local blockchain network for development and testing.
● Typically used to connect external tools (like Metamask,
frontend apps, or scripts) to your local development
environment.
● Persists the blockchain state until the process is terminated.
Continue
3. Ethereum Mainnet

● The live Ethereum blockchain used for deploying production smart


contracts.
● Requires real ETH for transactions.

module.exports = {
networks: {
mainnet: {
url: "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID",
accounts: [`0x${PRIVATE_KEY}`], // Replace with your private key
},
},
};
4. Testnets (Goerli, Sepolia, etc.)

● Public Ethereum test networks for testing smart contracts before


deploying to Mainnet.
● Transactions require test ETH, which can be obtained from faucets.

When to Use Them?


● Testing smart contracts in an environment that closely resembles
Mainnet.
● Sharing contracts with other developers or testers for feedback.
● Ensuring contracts function as intended under live network
conditions.
module.exports = {
networks: {
goerli: {
url: "https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID",
accounts: [`0x${PRIVATE_KEY}`], // Replace with your private key
},
},
};

You might also like