Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v-next/config/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
CHANGELOG.md
/test/fixture-projects/**/artifacts
/test/fixture-projects/**/cache
/.snapshots
40 changes: 40 additions & 0 deletions v-next/example-project/.snapshots/gas-stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
| Gas Usage Statistics | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| contracts/Calculator.sol:Calculator | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| Deployment Cost | Deployment Size | | | | |
| 324722 | 1291 | | | | |
| Function name | Min | Average | Median | Max | #calls |
| divide | 27217 | 29661.57 | 27217 | 44317 | 7 |
| multiply(uint256,uint256) | 22355 | 31355 | 27155 | 44255 | 7 |
| multiply(uint256,uint256,uint256) | 27776 | 32051 | 27776 | 44876 | 4 |
| reset | 21486 | 21486 | 21486 | 21486 | 3 |
| result | 23510 | 23510 | 23510 | 23510 | 24 |
| subtract | 22340 | 29602.4 | 27140 | 44252 | 5 |
| | | | | | |
| contracts/Counter.sol:Counter | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| Deployment Cost | Deployment Size | | | | |
| 234940 | 870 | | | | |
| Function name | Min | Average | Median | Max | #calls |
| add(uint256) | 24004 | 26353.33 | 26816 | 26840 | 6 |
| add(uint256,bool) | 27186 | 27321 | 27321 | 27456 | 6 |
| inc | 26383 | 32083 | 26383 | 43483 | 3 |
| x | 23466 | 23466 | 23466 | 23466 | 5 |
| | | | | | |
| contracts/FailingContract.sol:FailingContract | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| Deployment Cost | Deployment Size | | | | |
| 286537 | 1114 | | | | |
| | | | | | |
| contracts/Revert.sol:Revert | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| Deployment Cost | Deployment Size | | | | |
| 113967 | 311 | | | | |
| | | | | | |
| contracts/Rocket.sol:Rocket | | | | | |
| --------------------------------------------- | --------------- | -------- | ------ | ----- | ------ |
| Deployment Cost | Deployment Size | | | | |
| 493627 | 3120 | | | | |
| Function name | Min | Average | Median | Max | #calls |
| launch | 32946 | 32946 | 32946 | 32946 | 2 |
28 changes: 28 additions & 0 deletions v-next/example-project/contracts/Calculator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Calculator {
uint256 public result;

function multiply(uint256 a, uint256 b) public {
result = a * b;
}

function multiply(uint256 a, uint256 b, uint256 c) public {
result = a * b * c;
}

function divide(uint256 a, uint256 b) public {
require(b > 0, "Cannot divide by zero");
result = a / b;
}

function subtract(uint256 a, uint256 b) public {
require(a >= b, "Result would be negative");
result = a - b;
}

function reset() public {
result = 0;
}
}
14 changes: 13 additions & 1 deletion v-next/example-project/contracts/Counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
pragma solidity ^0.8.0;

contract Counter {
uint public x;
uint256 public x;

function inc() public {
x++;
}

function add(uint256 amount) public {
x += amount;
}

function add(uint256 amount, bool double) public {
if (double) {
x += amount * 2;
} else {
x += amount;
}
}
}
4 changes: 3 additions & 1 deletion v-next/example-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"build": "tsc --build .",
"clean": "rimraf dist artifacts cache ignition/deployments types",
"pretest": "pnpm build && pnpm install",
"test": "hardhat test nodejs && hardhat test mocha"
"test": "hardhat test nodejs && hardhat test mocha && pnpm run snapshot",
"snapshot": "node test/snapshot-test.js",
"snapshot:update": "node test/snapshot-test.js --update"
},
"devDependencies": {
"hardhat": "workspace:^3.0.9",
Expand Down
137 changes: 137 additions & 0 deletions v-next/example-project/test/node/calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { describe, it, before } from "node:test";
import assert from "node:assert/strict";
import hre from "hardhat";
import { ContractReturnType } from "@nomicfoundation/hardhat-viem/types";

const { viem } = await hre.network.connect();

// This test suite is used to generate gas usage data for the Calculator contract
describe("Calculator", () => {
let calculator: ContractReturnType<"Calculator">;

before(async () => {
calculator = await viem.deployContract("Calculator");
});

it("should start with result = 0", async () => {
const result = await calculator.read.result();
assert.equal(result, 0n);
});

describe("multiply", () => {
it("should multiply with varying two-parameter combinations", async () => {
await calculator.write.multiply([2n, 3n]);
assert.equal(await calculator.read.result(), 6n);

await calculator.write.multiply([10n, 5n]);
assert.equal(await calculator.read.result(), 50n);

await calculator.write.multiply([100n, 2n]);
assert.equal(await calculator.read.result(), 200n);

await calculator.write.multiply([7n, 11n]);
assert.equal(await calculator.read.result(), 77n);

await calculator.write.multiply([0n, 999n]);
assert.equal(await calculator.read.result(), 0n);
});

it("should multiply with varying three-parameter combinations", async () => {
await calculator.write.multiply([2n, 3n, 4n]);
assert.equal(await calculator.read.result(), 24n);

await calculator.write.multiply([1n, 1n, 100n]);
assert.equal(await calculator.read.result(), 100n);

await calculator.write.multiply([5n, 6n, 7n]);
assert.equal(await calculator.read.result(), 210n);

await calculator.write.multiply([10n, 10n, 10n]);
assert.equal(await calculator.read.result(), 1000n);
});
});

describe("divide", () => {
it("should divide with various combinations", async () => {
await calculator.write.divide([20n, 4n]);
assert.equal(await calculator.read.result(), 5n);

await calculator.write.divide([100n, 10n]);
assert.equal(await calculator.read.result(), 10n);

await calculator.write.divide([49n, 7n]);
assert.equal(await calculator.read.result(), 7n);

await calculator.write.divide([1000n, 25n]);
assert.equal(await calculator.read.result(), 40n);

await calculator.write.divide([1n, 1n]);
assert.equal(await calculator.read.result(), 1n);

await calculator.write.divide([99n, 3n]);
assert.equal(await calculator.read.result(), 33n);
});

it("should revert when dividing by zero", async () => {
await viem.assertions.revertWith(
calculator.write.divide([10n, 0n]),
"Cannot divide by zero",
);

await viem.assertions.revertWith(
calculator.write.divide([999n, 0n]),
"Cannot divide by zero",
);
});
});

describe("subtract", () => {
it("should subtract with various combinations", async () => {
await calculator.write.subtract([10n, 3n]);
assert.equal(await calculator.read.result(), 7n);

await calculator.write.subtract([100n, 25n]);
assert.equal(await calculator.read.result(), 75n);

await calculator.write.subtract([50n, 50n]);
assert.equal(await calculator.read.result(), 0n);

await calculator.write.subtract([1000n, 1n]);
assert.equal(await calculator.read.result(), 999n);

await calculator.write.subtract([77n, 7n]);
assert.equal(await calculator.read.result(), 70n);
});

it("should revert when result would be negative", async () => {
await viem.assertions.revertWith(
calculator.write.subtract([3n, 10n]),
"Result would be negative",
);

await viem.assertions.revertWith(
calculator.write.subtract([1n, 100n]),
"Result would be negative",
);

await viem.assertions.revertWith(
calculator.write.subtract([0n, 1n]),
"Result would be negative",
);
});
});

it("should reset result to zero", async () => {
await calculator.write.multiply([5n, 5n]);
await calculator.write.reset();
assert.equal(await calculator.read.result(), 0n);

await calculator.write.multiply([3n, 7n]);
await calculator.write.reset();
assert.equal(await calculator.read.result(), 0n);

await calculator.write.divide([100n, 4n]);
await calculator.write.reset();
assert.equal(await calculator.read.result(), 0n);
});
});
55 changes: 55 additions & 0 deletions v-next/example-project/test/node/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, before } from "node:test";
import assert from "node:assert/strict";
import hre from "hardhat";
import { ContractReturnType } from "@nomicfoundation/hardhat-viem/types";

const { viem } = await hre.network.connect();

// This test suite is used to generate gas usage data for the Counter contract
describe("Counter", () => {
let counter: ContractReturnType<"Counter">;

before(async () => {
counter = await viem.deployContract("Counter");
});

it("should start with x = 0", async () => {
const x = await counter.read.x();
assert.equal(x, 0n);
});

it("should increment multiple times", async () => {
await counter.write.inc();
await counter.write.inc();
await counter.write.inc();
const x = await counter.read.x();
assert.equal(x, 3n);
});

it("should add varying amounts", async () => {
await counter.write.add([1n]);
await counter.write.add([10n]);
await counter.write.add([100n]);
await counter.write.add([1000n]);
const x = await counter.read.x();
assert.equal(x, 1114n); // 3 + 1 + 10 + 100 + 1000
});

it("should add with different double flag combinations", async () => {
await counter.write.add([5n, false]);
await counter.write.add([10n, true]); // 10 * 2 = 20
await counter.write.add([2n, false]);
await counter.write.add([3n, true]); // 3 * 2 = 6
await counter.write.add([7n, false]);
const x = await counter.read.x();
assert.equal(x, 1154n); // 1114 + 5 + 20 + 2 + 6 + 7
});

it("should handle edge cases and large numbers", async () => {
await counter.write.add([0n]);
await counter.write.add([999999n]);
await counter.write.add([1n, true]); // 1 * 2 = 2
const x = await counter.read.x();
assert.equal(x, 1001155n); // 1154 + 0 + 999999 + 2
});
});
Loading
Loading