Skip to content

Commit 614f1ec

Browse files
Merge pull request #6574 from NomicFoundation/viem-matchers-first-iteration
Viem matchers first iteration
2 parents df260c3 + 8c7bfce commit 614f1ec

40 files changed

+1800
-6
lines changed

.changeset/config.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@
3232
"@nomicfoundation/hardhat-utils",
3333
"@nomicfoundation/hardhat-toolbox-mocha-ethers",
3434
"@nomicfoundation/hardhat-viem",
35+
"@nomicfoundation/hardhat-viem-matchers",
3536
"@nomicfoundation/hardhat-zod-utils"
3637
],
37-
[
38-
"@nomicfoundation/hardhat-toolbox-viem"
39-
],
40-
[
41-
"@nomicfoundation/hardhat-ethers"
42-
]
38+
["@nomicfoundation/hardhat-toolbox-viem"],
39+
["@nomicfoundation/hardhat-ethers"]
4340
],
4441
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
4542
"onlyUpdatePeerDependentsWhenOutOfRange": true

.changeset/pre.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@nomicfoundation/hardhat-utils": "2.0.0",
2323
"@nomicfoundation/hardhat-toolbox-mocha-ethers": "2.0.0",
2424
"@nomicfoundation/hardhat-viem": "2.0.6",
25+
"@nomicfoundation/hardhat-viem-matchers": "2.0.0",
2526
"@nomicfoundation/hardhat-zod-utils": "2.0.0",
2627
"@nomicfoundation/template-package": "3.0.0",
2728
"template-empty-javascript": "0.0.1",

pnpm-lock.yaml

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Node modules
2+
/node_modules
3+
4+
# Compilation output
5+
/dist
6+
7+
# pnpm deploy output
8+
/bundle
9+
10+
# test coverage output
11+
coverage
12+
13+
# all the tmp folders in the fixture projects
14+
/test/fixture-projects/tmp/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/dist
3+
/coverage
4+
CHANGELOG.md
5+
/test/fixture-projects/*/artifacts
6+
/test/fixture-projects/*/cache

v-next/hardhat-viem-matchers/LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Nomic Foundation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createConfig } from "../config/eslint.config.js";
2+
3+
export default createConfig(import.meta.filename);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@nomicfoundation/hardhat-viem-matchers",
3+
"version": "3.0.0-next.11",
4+
"description": "A Hardhat plugin that adds matchers for Viem",
5+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-viem-matchers",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/NomicFoundation/hardhat",
9+
"directory": "v-next/hardhat-viem-matchers"
10+
},
11+
"author": "Nomic Foundation",
12+
"license": "MIT",
13+
"type": "module",
14+
"types": "dist/src/index.d.ts",
15+
"exports": {
16+
".": "./dist/src/index.js",
17+
"./types": "./dist/src/types.js"
18+
},
19+
"keywords": [
20+
"ethereum",
21+
"smart-contracts",
22+
"hardhat",
23+
"hardhat-plugin",
24+
"viem",
25+
"matchers"
26+
],
27+
"scripts": {
28+
"lint": "pnpm prettier --check && pnpm eslint",
29+
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
30+
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
31+
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
32+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
33+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
34+
"test:coverage": "c8 --reporter html --reporter text --all --exclude test --exclude \"src/**/{types,type-extensions}.ts\" --src src node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
35+
"pretest": "pnpm build",
36+
"pretest:only": "pnpm build",
37+
"build": "tsc --build .",
38+
"prepublishOnly": "pnpm build",
39+
"clean": "rimraf dist"
40+
},
41+
"files": [
42+
"dist/src/",
43+
"src/",
44+
"CHANGELOG.md",
45+
"LICENSE",
46+
"README.md"
47+
],
48+
"devDependencies": {
49+
"@nomicfoundation/hardhat-node-test-reporter": "workspace:^3.0.0-next.11",
50+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
51+
"@nomicfoundation/hardhat-viem": "workspace:^3.0.0-next.11",
52+
"@types/node": "^20.14.9",
53+
"c8": "^9.1.0",
54+
"eslint": "9.25.1",
55+
"expect-type": "^0.19.0",
56+
"prettier": "3.2.5",
57+
"rimraf": "^5.0.5",
58+
"tsx": "^4.19.3",
59+
"typescript": "~5.5.0",
60+
"viem": "^2.30.0"
61+
},
62+
"dependencies": {
63+
"@nomicfoundation/hardhat-errors": "workspace:^3.0.0-next.11",
64+
"@nomicfoundation/hardhat-utils": "workspace:^3.0.0-next.11"
65+
},
66+
"peerDependencies": {
67+
"@nomicfoundation/hardhat-viem": "workspace:^3.0.0-next.11",
68+
"hardhat": "workspace:^3.0.0-next.11",
69+
"viem": "^2.30.0"
70+
}
71+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { HardhatPlugin } from "hardhat/types/plugins";
2+
3+
import "./type-extensions.js";
4+
5+
const hardhatPlugin: HardhatPlugin = {
6+
id: "hardhat-viem-matchers",
7+
dependencies: [
8+
async () => {
9+
const { default: viemPlugin } = await import(
10+
"@nomicfoundation/hardhat-viem"
11+
);
12+
13+
return viemPlugin;
14+
},
15+
],
16+
hookHandlers: {
17+
network: import.meta.resolve("./internal/hook-handlers/network.js"),
18+
},
19+
npmPackage: "@nomicfoundation/hardhat-viem-matchers",
20+
};
21+
22+
export default hardhatPlugin;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { HookContext, NetworkHooks } from "hardhat/types/hooks";
2+
import type { ChainType, NetworkConnection } from "hardhat/types/network";
3+
4+
import { initializeViemMatchers } from "../viem-matchers-initialization.js";
5+
6+
export default async (): Promise<Partial<NetworkHooks>> => {
7+
const handlers: Partial<NetworkHooks> = {
8+
async newConnection<ChainTypeT extends ChainType | string>(
9+
context: HookContext,
10+
next: (
11+
nextContext: HookContext,
12+
) => Promise<NetworkConnection<ChainTypeT>>,
13+
) {
14+
const connection = await next(context);
15+
16+
connection.viem.assertions = await initializeViemMatchers(
17+
connection.viem,
18+
);
19+
20+
return connection;
21+
},
22+
};
23+
24+
return handlers;
25+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { HardhatViemHelpers } from "@nomicfoundation/hardhat-viem/types";
2+
import type { ChainType } from "hardhat/types/network";
3+
import type { Address, Hash } from "viem";
4+
5+
import assert from "node:assert/strict";
6+
7+
import { isHash } from "@nomicfoundation/hardhat-utils/eth";
8+
9+
export async function balancesHaveChanged<
10+
ChainTypeT extends ChainType | string = "generic",
11+
>(
12+
viem: HardhatViemHelpers<ChainTypeT>,
13+
txHash: Promise<Hash>,
14+
changes: Array<{
15+
address: Address;
16+
amount: bigint;
17+
}>,
18+
): Promise<void> {
19+
const resolvedTxHash = await txHash;
20+
21+
assert.ok(
22+
isHash(resolvedTxHash),
23+
`The promise should return a transaction hash, but it returned: ${String(resolvedTxHash)}`,
24+
);
25+
26+
const publicClient = await viem.getPublicClient();
27+
28+
const receipt = await publicClient.waitForTransactionReceipt({
29+
hash: resolvedTxHash,
30+
});
31+
32+
const senderAddress = receipt.from;
33+
const blockNrAfterTx = receipt.blockNumber;
34+
35+
const beforeBalances = await Promise.all(
36+
changes.map(({ address }) =>
37+
publicClient.getBalance({
38+
address,
39+
blockNumber: blockNrAfterTx - 1n,
40+
}),
41+
),
42+
);
43+
44+
const afterBalances = await Promise.all(
45+
changes.map(async ({ address }) => {
46+
let balance = await publicClient.getBalance({
47+
address,
48+
blockNumber: blockNrAfterTx,
49+
});
50+
51+
if (address === senderAddress) {
52+
const senderGasFee = receipt.effectiveGasPrice * receipt.gasUsed;
53+
balance = balance + senderGasFee;
54+
}
55+
56+
return balance;
57+
}),
58+
);
59+
60+
changes.forEach(({ address, amount }, index) => {
61+
const balanceBefore = beforeBalances[index];
62+
const balanceAfter = afterBalances[index];
63+
64+
const actualChange = balanceAfter - balanceBefore;
65+
66+
assert.equal(
67+
actualChange,
68+
amount,
69+
`For address "${address}", expected balance to change by ${amount} (from ${balanceBefore} to ${balanceBefore + amount}), but got a change of ${actualChange} instead.`,
70+
);
71+
});
72+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type {
2+
ContractAbis,
3+
ContractReturnType,
4+
HardhatViemHelpers,
5+
} from "@nomicfoundation/hardhat-viem/types";
6+
import type { ChainType } from "hardhat/types/network";
7+
import type {
8+
AbiEvent,
9+
ContractEventName,
10+
ReadContractReturnType,
11+
WriteContractReturnType,
12+
} from "viem";
13+
14+
import assert from "node:assert/strict";
15+
16+
import { parseEventLogs } from "viem";
17+
18+
export async function handleEmit<
19+
ContractName extends keyof ContractAbis,
20+
EventName extends ContractEventName<ContractAbis[ContractName]>,
21+
ChainTypeT extends ChainType | string = "generic",
22+
>(
23+
viem: HardhatViemHelpers<ChainTypeT>,
24+
contractFn: Promise<ReadContractReturnType | WriteContractReturnType>,
25+
contract: ContractReturnType<ContractName>,
26+
eventName: EventName,
27+
): Promise<Array<{ args?: Record<string, any> }>> {
28+
const abiEvents: AbiEvent[] = contract.abi.filter(
29+
(item): item is AbiEvent =>
30+
item.type === "event" && item.name === eventName,
31+
);
32+
33+
assert.ok(
34+
abiEvents.length !== 0,
35+
`Event "${eventName}" not found in the contract ABI`,
36+
);
37+
38+
await contractFn;
39+
40+
const publicClient = await viem.getPublicClient();
41+
42+
const logs = await publicClient.getLogs({
43+
address: contract.address,
44+
});
45+
46+
const parsedLogs = parseEventLogs({
47+
abi: contract.abi,
48+
eventName,
49+
logs,
50+
});
51+
52+
assert.ok(
53+
parsedLogs.length > 0,
54+
`No events were emitted for contract with address "${contract.address}" and event name "${eventName}"`,
55+
);
56+
57+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- logs generated from emitted events have the `args` field
58+
return parsedLogs as unknown as Array<{ args?: Record<string, any> }>;
59+
}

0 commit comments

Comments
 (0)