Unit-2
Debugging smart contracts in Hardhat can be done using multiple techniques, including [Link],
Hardhat’s built-in debugger, and setting breakpoints in VS Code.
[Link]
1. Commands
npm init
npm install --save-dev hardhat
npx hardhat
What do you want to do? · Create an empty [Link]
Config file created
add this command in hardhat [Link] file
require("@nomiclabs/hardhat-ethers")
npm install --save-dev @nomiclabs/hardhat-ethers ethers
2. Install Hardhat Console Library
Since [Link] is part of Hardhat, you don’t need to install anything extra.
Write a Solidity Smart Contract with [Link]
Create a Solidity contract in contracts/[Link]:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/[Link]";
contract DebugExample {
uint256 public count;
function increment() public {
count++;
[Link]("Count is now:", count); }
// Function to set count to a specific value
function setValue(uint256 _value) public {
[Link]("Setting value from", count, "to", _value);
count = _value; }}
3. Write a Deployment Script
Create a script scripts/[Link] to deploy the contract:
const hre = require("hardhat");
async function main() {
const DebugExample = await [Link]("DebugExample");
const debugExample = await [Link]();
await [Link]();
[Link]("Contract deployed to:", [Link]);
await [Link](); }
main().catch((error) => { [Link](error); [Link](1);});
4. Commands
npx hardhat node
npx hardhat compile
npx hardhat run scripts/[Link] --network localhost
//output
Contract deployed to: 0x123456...
Testing [Link] in Hardhat
Step 1: Create a Test File
This test file is written in JavaScript using Mocha (test framework) and Chai (assertion library) for
testing a Solidity smart contract ([Link]) using Hardhat.
Create test/[Link] in the test/ folder:
const { expect } = require("chai");
describe("DebugExample", function () {
let debugExample;
beforeEach(async function () {
const DebugExample = await [Link]("DebugExample");
debugExample = await [Link]();
await [Link]();
});
it("should log count increment", async function () {
await [Link]();
await [Link]();
});
it("should log value setting", async function () {
await [Link](10);
});
});
Step 2: Run the Test
npm install --save-dev chai
npx hardhat test
Expected Console Output:
DebugExample
Count is now: 1
Count is now: 2
✔ should log count increment
Setting value from 0 to 10
✔ should log value setting
2 passing
● "2 passing" → Indicates that 2 test cases passed successfully.
● "(1s)" → The tests took 1 second to execute.
This output is generated by Mocha, the test framework used in Hardhat.
Explanation:
● [Link]("Count is now:", count); logs the incremented value.
● [Link]("Setting value from", count, "to", _value); logs the old and new values when
setting a new count.
Note: [Link] only works in Hardhat's local environment, not on live networks.
Use of [Link] in Solidity (Hardhat)
The [Link] utility in Solidity (provided by Hardhat) is used for debugging smart contracts during
local development. It helps developers print values and track execution flow, similar to [Link] in
JavaScript.
Why Use [Link] in Solidity?
1. Debugging Transactions – Helps identify issues in contract logic without requiring event logs.
2. Inspecting Variable Values – Print variable values during contract execution.
3. Tracking Function Calls – See which functions are executing and in what order.
4. Understanding Gas Costs – Analyze function execution and gas consumption.
Limitations of [Link]
1. Only Works in Hardhat – [Link] is for local development; it won’t work on live networks
(like Ethereum mainnet or testnets).
2. Not Supported in Production – Remove [Link] before deploying to real networks.
Assignments
1. Write and test a Solidity smart contract that performs multiplication and division operations
while using [Link]() for debugging.
2. Write and test a Solidity smart contract to swap two values while using [Link]() for
debugging.