Skip to content

Commit c41488d

Browse files
Merge pull request #5753 from NomicFoundation/feature/move-hardhat-network-helpers
V3 hardhat-network-helpers
2 parents 198612d + 6e92275 commit c41488d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3855
-2
lines changed

.changeset/pre.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@ignored/hardhat-vnext-build-system": "2.0.0",
2525
"@ignored/hardhat-vnext-errors": "2.0.0",
2626
"@ignored/hardhat-vnext-keystore": "2.0.0",
27+
"@ignored/hardhat-vnext-network-helpers": "2.0.0",
2728
"@ignored/hardhat-vnext-node-test-reporter": "2.0.0",
2829
"@ignored/hardhat-vnext-utils": "2.0.0",
2930
"@ignored/hardhat-vnext-zod-utils": "2.0.0",

pnpm-lock.yaml

+73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/example-project/hardhat.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
88
import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
99
import HardhatKeystore from "@ignored/hardhat-vnext-keystore";
1010
import { viemScketchPlugin } from "./viem-scketch-plugin.js";
11+
import hardhatNetworkHelpersPlugin from "@ignored/hardhat-vnext-network-helpers";
1112

1213
const exampleEmptyTask = emptyTask("empty", "An example empty task").build();
1314

@@ -122,6 +123,7 @@ const config: HardhatUserConfig = {
122123
HardhatKeystore,
123124
// HardhatMochaTestRunner,
124125
// if testing node plugin, use the following line instead
126+
hardhatNetworkHelpersPlugin,
125127
HardhatNodeTestRunner,
126128
viemScketchPlugin,
127129
],

v-next/example-project/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.3",
2424
"@ignored/hardhat-vnext-keystore": "workspace:^3.0.0-next.2",
2525
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
26+
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.2",
2627
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
2728
"@types/mocha": ">=9.1.0",
2829
"@types/node": "^20.14.9",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* How to run this script:
3+
* 1) Start a Hardhat V2 node (until the V3 node is ready)
4+
* 3) Run this script with `npx hardhat run scripts/network-helpers.ts`.
5+
*/
6+
7+
import hre from "@ignored/hardhat-vnext";
8+
// Example on how to import load fixture
9+
// import { loadFixture } from "@ignored/hardhat-vnext-network-helpers/load-fixture";
10+
11+
const { networkHelpers } = await hre.network.connect();
12+
13+
await networkHelpers.mine();

v-next/example-project/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
{
55
"path": "../hardhat"
66
},
7+
{
8+
"path": "../hardhat-network-helpers"
9+
},
710
{
811
"path": "../hardhat-node-test-runner"
912
},

v-next/hardhat-errors/src/descriptors.ts

+104
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export const ERROR_CATEGORIES: {
6868
BUILTIN_TASKS: { min: 600, max: 699, websiteTitle: "Built-in tasks errors" },
6969
NETWORK: { min: 700, max: 799, websiteTitle: "Network errors" },
7070
KEYSTORE: { min: 800, max: 899, websiteTitle: "Keystore errors" },
71+
NETWORK_HELPERS: {
72+
min: 900,
73+
max: 999,
74+
websiteTitle: "Network-helpers errors",
75+
},
7176
};
7277

7378
export const ERRORS = {
@@ -581,4 +586,103 @@ If you want to use a different chain type, please update your networks config.`,
581586
"Within the Keystore plugin, some direct user interruptions are not implemented. They are not expected to be invoked.",
582587
},
583588
},
589+
NETWORK_HELPERS: {
590+
ONLY_ALLOW_0X_PREFIXED_STRINGS: {
591+
number: 900,
592+
messageTemplate: `Only hex-encoded strings prefixed with "0x" are accepted`,
593+
websiteTitle: `Only hex-encoded strings prefixed with "0x" are accepted`,
594+
websiteDescription: `Only hex-encoded strings prefixed with "0x" are accepted`,
595+
},
596+
CANNOT_CONVERT_TO_RPC_QUANTITY: {
597+
number: 901,
598+
messageTemplate: `The value "{value}" cannot be converted into an RPC quantity`,
599+
websiteTitle: "Cannot converted into an RPC quantity",
600+
websiteDescription: "The value cannot be converted into an RPC quantity",
601+
},
602+
INVALID_HEX_STRING: {
603+
number: 902,
604+
messageTemplate: `"{value}" is not a valid hex string`,
605+
websiteTitle: "Invalid hex string",
606+
websiteDescription: "The value is not a valid hex string",
607+
},
608+
INVALID_TX_HASH: {
609+
number: 903,
610+
messageTemplate: `"{value}" is not a valid transaction hash`,
611+
websiteTitle: "Invalid transaction hash",
612+
websiteDescription: "The value is not a valid transaction hash",
613+
},
614+
INVALID_ADDRESS: {
615+
number: 904,
616+
messageTemplate: `"{value}" is not a valid address`,
617+
websiteTitle: "Invalid address",
618+
websiteDescription: "The value is not a valid address",
619+
},
620+
INVALID_CHECKSUM_ADDRESS: {
621+
number: 905,
622+
messageTemplate: `Address "{value}" has an invalid checksum`,
623+
websiteTitle: "Invalid checksum address",
624+
websiteDescription: "The address has an invalid checksum",
625+
},
626+
BLOCK_NUMBER_SMALLER_THAN_CURRENT: {
627+
number: 906,
628+
messageTemplate: `The block number "{newValue}" is smaller than the current block number "{currentValue}"`,
629+
websiteTitle: "Block number smaller than the current block number",
630+
websiteDescription:
631+
"The block number is smaller than the current block number",
632+
},
633+
EVM_SNAPSHOT_VALUE_NOT_A_STRING: {
634+
number: 907,
635+
messageTemplate: `The value returned by evm_snapshot should be a string`,
636+
websiteTitle: "The evm_snapshot value should be a string",
637+
websiteDescription:
638+
"The value returned by evm_snapshot should be a string",
639+
},
640+
EVM_REVERT_VALUE_NOT_A_BOOLEAN: {
641+
number: 908,
642+
messageTemplate: `The value returned by evm_revert should be a boolean`,
643+
websiteTitle: "The evm_revert value should be a boolean",
644+
websiteDescription:
645+
"The value returned by evm_revert should be a boolean",
646+
},
647+
INVALID_SNAPSHOT: {
648+
number: 909,
649+
messageTemplate: `Trying to restore an invalid snapshot.`,
650+
websiteTitle: "Trying to restore an invalid snapshot.",
651+
websiteDescription: "Trying to restore an invalid snapshot.",
652+
},
653+
EXPECTED_NON_NEGATIVE_NUMBER: {
654+
number: 910,
655+
messageTemplate: `Invalid input: expected a non-negative number but "{value}" was given.`,
656+
websiteTitle: "Invalid input, expected a non-negative number",
657+
websiteDescription: "Invalid input, expected a non-negative number",
658+
},
659+
CANNOT_CONVERT_NEGATIVE_NUMBER_TO_RPC_QUANTITY: {
660+
number: 911,
661+
messageTemplate: `Cannot convert negative number "{value}" to RPC quantity`,
662+
websiteTitle: "Cannot convert negative number to RPC quantity",
663+
websiteDescription: "Cannot convert negative number to RPC quantity",
664+
},
665+
FIXTURE_ANONYMOUS_FUNCTION_ERROR: {
666+
number: 912,
667+
messageTemplate: `Anonymous functions cannot be used as fixtures.
668+
669+
You probably did something like this:
670+
671+
loadFixture(async () => ... );
672+
673+
Instead, define a fixture function and refer to that same function in each call to loadFixture.
674+
675+
Learn more at (https://hardhat.org/hardhat-network-helpers/docs/reference#fixtures)`,
676+
websiteTitle: "Anonymous functions cannot be used as fixtures",
677+
websiteDescription: "Anonymous functions cannot be used as fixtures",
678+
},
679+
FIXTURE_SNAPSHOT_ERROR: {
680+
number: 913,
681+
messageTemplate: `There was an error reverting the snapshot of the fixture.
682+
683+
This might be caused by using hardhat_reset and loadFixture calls in a testcase.`,
684+
websiteTitle: "Error while reverting snapshot",
685+
websiteDescription: "Error while reverting snapshot",
686+
},
687+
},
584688
} as const;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { createConfig } = require("../../config-v-next/eslint.cjs");
2+
3+
module.exports = createConfig(__filename);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Node modules
2+
/node_modules
3+
4+
# Compilation output
5+
/dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules
2+
/dist
3+
/coverage
4+
CHANGELOG.md
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024 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.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Hardhat Network Helpers
2+
3+
Hardhat Network Helpers is a library that provides a set of utility functions to interact with the [Hardhat Network](https://hardhat.org/hardhat-network/docs).
4+
5+
### Usage
6+
7+
```javascript
8+
const { networkHelpers } = await hre.network.connect();
9+
10+
// Network helpers methods exposed via `networkHelpers`
11+
await networkHelpers.mine();
12+
13+
// Time methods exposed via `time`
14+
await networkHelpers.time.increase(1);
15+
16+
// Duration methods exposed via `duration`
17+
networkHelpers.time.duration.days(1);
18+
```
19+
20+
### Tests
21+
22+
Temporary solution to run manual tests until the V3 node is ready.
23+
24+
1. Start a node in Hardhat V2: `npx hardhat node`
25+
2. Run the tests: `pnpm test:tmp`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "@ignored/hardhat-vnext-network-helpers",
3+
"version": "3.0.0-next.2",
4+
"description": "Hardhat utils for testing",
5+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/network-helpers",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/NomicFoundation/hardhat",
9+
"directory": "v-next/network-helpers"
10+
},
11+
"author": "Nomic Foundation",
12+
"license": "MIT",
13+
"type": "module",
14+
"exports": {
15+
".": "./dist/src/index.js",
16+
"./load-fixture": "./dist/src/load-fixture.js"
17+
},
18+
"keywords": [
19+
"ethereum",
20+
"smart-contracts",
21+
"hardhat",
22+
"testing"
23+
],
24+
"scripts": {
25+
"lint": "pnpm prettier --check && pnpm eslint",
26+
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
27+
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
28+
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
29+
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
30+
"test:tmp": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test-tmp/*.ts\" \"test-tmp/duration/*.ts\" && node --import tsx/esm ./run-tests.ts",
31+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
32+
"test:coverage": "c8 --reporter html --reporter text --all --exclude test --exclude src/internal/types.ts --exclude src/internal/ui/direct-user-interruption-manager.ts --src src node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
33+
"pretest": "pnpm build",
34+
"pretest:only": "pnpm build",
35+
"build": "tsc --build .",
36+
"prepublishOnly": "pnpm build",
37+
"clean": "rimraf dist"
38+
},
39+
"files": [
40+
"dist/src/",
41+
"src/",
42+
"CHANGELOG.md",
43+
"LICENSE",
44+
"README.md"
45+
],
46+
"devDependencies": {
47+
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
48+
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
49+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
50+
"@types/debug": "^4.1.4",
51+
"@types/node": "^20.14.9",
52+
"@typescript-eslint/eslint-plugin": "^7.7.1",
53+
"@typescript-eslint/parser": "^7.7.1",
54+
"c8": "^9.1.0",
55+
"eslint": "8.57.0",
56+
"eslint-config-prettier": "9.1.0",
57+
"eslint-import-resolver-typescript": "^3.6.1",
58+
"eslint-plugin-import": "2.29.1",
59+
"eslint-plugin-no-only-tests": "3.1.0",
60+
"expect-type": "^0.19.0",
61+
"prettier": "3.2.5",
62+
"rimraf": "^5.0.5",
63+
"tsx": "^4.11.0",
64+
"typescript": "~5.5.0",
65+
"typescript-eslint": "7.7.1"
66+
},
67+
"dependencies": {
68+
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
69+
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2"
70+
},
71+
"peerDependencies": {
72+
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.2"
73+
}
74+
}

0 commit comments

Comments
 (0)