Initial commit

This commit is contained in:
2026-02-16 23:37:30 +01:00
commit 9ab5e3b873
10 changed files with 3175 additions and 0 deletions

26
.gitignore vendored Normal file
View File

@@ -0,0 +1,26 @@
# Node modules
/node_modules
# Compilation output
/dist
# pnpm deploy output
/bundle
# Hardhat Build Artifacts
/artifacts
# Deployments by students
/ignition/deployments
# Hardhat compilation (v2) support directory
/cache
# Typechain output
/types
# Hardhat coverage reports
/coverage
# Environment variables
.env

51
README.md Normal file
View File

@@ -0,0 +1,51 @@
# Hardhat 3 Project
This is a Hardhat 3 project, which uses the native Node.js test runner (`node:test`) and the `viem` library for Ethereum interactions.
## Project Overview
This example project includes:
- A simple Hardhat configuration file.
- An example contract called Counter.
- TypeScript integration tests.
## Building
To build, run:
```shell
npx hardhat build
```
### Running Tests
To run all the tests in the project, execute the following command:
```shell
npx hardhat test
```
You can also selectively run the `node:test` tests:
```shell
npx hardhat test nodejs
```
### Make a deployment to Next Testnet
This project includes an example Ignition module to deploy a contract to our testnet.
To run the deployment to Next Testnet, set your mnemonics in an .env file (MNEMONICS=...) and run:
```shell
npx hardhat ignition deploy --network next ignition/modules/Counter.ts
```

15
contracts/Counter.sol Normal file
View File

@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
contract Counter {
uint256 public x;
function inc() public {
x++;
}
function incBy(uint by) public {
require(by > 0, "incBy: increment should be positive");
x += by;
}
}

49
hardhat.config.ts Normal file
View File

@@ -0,0 +1,49 @@
import "dotenv/config";
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { configVariable, defineConfig } from "hardhat/config";
export default defineConfig({
plugins: [hardhatToolboxViemPlugin],
solidity: {
profiles: {
default: {
version: "0.8.28",
},
production: {
version: "0.8.28",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
},
},
networks: {
hardhatMainnet: {
type: "edr-simulated",
chainType: "l1",
},
hardhatOp: {
type: "edr-simulated",
chainType: "op",
},
sepolia: {
type: "http",
chainType: "l1",
url: configVariable("SEPOLIA_RPC_URL"),
accounts: [configVariable("SEPOLIA_PRIVATE_KEY")],
},
next: {
type: "http",
chainId: 1337,
chainType: "l1",
url: "https://eth.code-camp.org",
accounts: {
mnemonic: configVariable("MNEMONICS"),
},
},
},
});

View File

@@ -0,0 +1,6 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
export default buildModule("CounterModule", (m) => {
const counter = m.contract("Counter");
return { counter };
});

2948
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@@ -0,0 +1,17 @@
{
"name": "hardhat-lesson",
"version": "1.0.0",
"type": "module",
"devDependencies": {
"@nomicfoundation/hardhat-ignition": "^3.0.7",
"@nomicfoundation/hardhat-toolbox-viem": "^5.0.2",
"@types/node": "^22.19.11",
"forge-std": "github:foundry-rs/forge-std#v1.9.4",
"hardhat": "^3.1.8",
"typescript": "~5.8.0",
"viem": "^2.46.1"
},
"dependencies": {
"dotenv": "^17.3.1"
}
}

45
test/Counter.ts Normal file
View File

@@ -0,0 +1,45 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { network } from "hardhat";
describe("Counter", async function () {
const { viem } = await network.connect();
const publicClient = await viem.getPublicClient();
it("The value should match the sum of the increments", async function () {
const counter = await viem.deployContract("Counter");
await counter.write.inc();
// run a series of increments
// 3+...+10 = 52
for (let i = 3n; i <= 10n; i++) {
await counter.write.incBy([i]);
}
// read the value
const value = await counter.read.x();
assert.equal(value, 1n + 52n);
});
it("Demo reading from a public client with Viem", async function () {
const counter = await viem.deployContract("Counter");
await counter.write.inc();
await counter.write.inc();
await counter.write.inc();
// read the value
const value = await publicClient.readContract({
address: counter.address,
abi: counter.abi,
functionName: "x",
});
assert.equal(value, 3n);
});
});

5
test/accounts/index.ts Normal file
View File

@@ -0,0 +1,5 @@
export const account1 = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
export const account2 = '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
export const account3 = '0xcccccccccccccccccccccccccccccccccccccccc';
export const account4 = '0xdddddddddddddddddddddddddddddddddddddddd';
export const account5 = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';

13
tsconfig.json Normal file
View File

@@ -0,0 +1,13 @@
/* Based on https://github.com/tsconfig/bases/blob/501da2bcd640cf95c95805783e1012b992338f28/bases/node22.json */
{
"compilerOptions": {
"lib": ["es2023"],
"module": "node16",
"target": "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node16",
"outDir": "dist"
}
}