Advance Blockchain Module 1
Advance Blockchain Module 1
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
Hardhat
```
● 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint public storedData;
// 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.
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.)