Skip to content

Commit dac59db

Browse files
authored
docs(common): hardhat setup (#349)
* docs(common): hardhat setup * docs(common): fix INFURA_PRIVATE_KEY * docs(common): counter.sol hardhat tutorial for beginners * docs(common): fhecounter.sol step-by-step tutorial * docs(common): fix gitbook links
1 parent b455d1c commit dac59db

8 files changed

Lines changed: 1077 additions & 42 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```solidity
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.24;
4+
5+
/// @title A simple counter contract
6+
contract Counter {
7+
uint32 private _count;
8+
9+
/// @notice Returns the current count
10+
function getCount() external view returns (uint32) {
11+
return _count;
12+
}
13+
14+
/// @notice Increments the counter by 1
15+
function increment(uint32 value) external {
16+
_count += value;
17+
}
18+
19+
/// @notice Decrements the counter by 1
20+
function decrement(uint32 value) external {
21+
require(_count > value, "Counter: cannot decrement below zero");
22+
_count -= value;
23+
}
24+
}
25+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```ts
2+
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
3+
import { ethers } from "hardhat";
4+
import { Counter, Counter__factory } from "../types";
5+
import { expect } from "chai";
6+
7+
type Signers = {
8+
deployer: HardhatEthersSigner;
9+
alice: HardhatEthersSigner;
10+
bob: HardhatEthersSigner;
11+
};
12+
13+
async function deployFixture() {
14+
const factory = (await ethers.getContractFactory("Counter")) as Counter__factory;
15+
const counterContract = (await factory.deploy()) as Counter;
16+
const counterContractAddress = await counterContract.getAddress();
17+
18+
return { counterContract, counterContractAddress };
19+
}
20+
21+
describe("Counter", function () {
22+
let signers: Signers;
23+
let counterContract: Counter;
24+
25+
before(async function () {
26+
const ethSigners: HardhatEthersSigner[] = await ethers.getSigners();
27+
signers = { deployer: ethSigners[0], alice: ethSigners[1], bob: ethSigners[2] };
28+
});
29+
30+
beforeEach(async () => {
31+
({ counterContract } = await deployFixture());
32+
});
33+
34+
it("count should be zero after deployment", async function () {
35+
const count = await counterContract.getCount();
36+
console.log(`Counter.getCount() === ${count}`);
37+
// Expect initial count to be 0 after deployment
38+
expect(count).to.eq(0);
39+
});
40+
41+
it("increment the counter by 1", async function () {
42+
const countBeforeInc = await counterContract.getCount();
43+
const tx = await counterContract.connect(signers.alice).increment(1);
44+
await tx.wait();
45+
const countAfterInc = await counterContract.getCount();
46+
expect(countAfterInc).to.eq(countBeforeInc + 1n);
47+
});
48+
49+
it("decrement the counter by 1", async function () {
50+
// First increment, count becomes 1
51+
let tx = await counterContract.connect(signers.alice).increment();
52+
await tx.wait();
53+
// Then decrement, count goes back to 0
54+
tx = await counterContract.connect(signers.alice).decrement(1);
55+
await tx.wait();
56+
const count = await counterContract.getCount();
57+
expect(count).to.eq(0);
58+
});
59+
});
60+
```

0 commit comments

Comments
 (0)