Skip to content

Commit

Permalink
Merge pull request #5753 from NomicFoundation/feature/move-hardhat-ne…
Browse files Browse the repository at this point in the history
…twork-helpers

V3 hardhat-network-helpers
  • Loading branch information
ChristopherDedominici authored Sep 18, 2024
2 parents 198612d + 6e92275 commit c41488d
Show file tree
Hide file tree
Showing 79 changed files with 3,855 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@ignored/hardhat-vnext-build-system": "2.0.0",
"@ignored/hardhat-vnext-errors": "2.0.0",
"@ignored/hardhat-vnext-keystore": "2.0.0",
"@ignored/hardhat-vnext-network-helpers": "2.0.0",
"@ignored/hardhat-vnext-node-test-reporter": "2.0.0",
"@ignored/hardhat-vnext-utils": "2.0.0",
"@ignored/hardhat-vnext-zod-utils": "2.0.0",
Expand Down
73 changes: 73 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions v-next/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
import HardhatKeystore from "@ignored/hardhat-vnext-keystore";
import { viemScketchPlugin } from "./viem-scketch-plugin.js";
import hardhatNetworkHelpersPlugin from "@ignored/hardhat-vnext-network-helpers";

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

Expand Down Expand Up @@ -122,6 +123,7 @@ const config: HardhatUserConfig = {
HardhatKeystore,
// HardhatMochaTestRunner,
// if testing node plugin, use the following line instead
hardhatNetworkHelpersPlugin,
HardhatNodeTestRunner,
viemScketchPlugin,
],
Expand Down
1 change: 1 addition & 0 deletions v-next/example-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.3",
"@ignored/hardhat-vnext-keystore": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
"@types/mocha": ">=9.1.0",
"@types/node": "^20.14.9",
Expand Down
13 changes: 13 additions & 0 deletions v-next/example-project/scripts/network-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* How to run this script:
* 1) Start a Hardhat V2 node (until the V3 node is ready)
* 3) Run this script with `npx hardhat run scripts/network-helpers.ts`.
*/

import hre from "@ignored/hardhat-vnext";
// Example on how to import load fixture
// import { loadFixture } from "@ignored/hardhat-vnext-network-helpers/load-fixture";

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

await networkHelpers.mine();
3 changes: 3 additions & 0 deletions v-next/example-project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
{
"path": "../hardhat"
},
{
"path": "../hardhat-network-helpers"
},
{
"path": "../hardhat-node-test-runner"
},
Expand Down
104 changes: 104 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const ERROR_CATEGORIES: {
BUILTIN_TASKS: { min: 600, max: 699, websiteTitle: "Built-in tasks errors" },
NETWORK: { min: 700, max: 799, websiteTitle: "Network errors" },
KEYSTORE: { min: 800, max: 899, websiteTitle: "Keystore errors" },
NETWORK_HELPERS: {
min: 900,
max: 999,
websiteTitle: "Network-helpers errors",
},
};

export const ERRORS = {
Expand Down Expand Up @@ -581,4 +586,103 @@ If you want to use a different chain type, please update your networks config.`,
"Within the Keystore plugin, some direct user interruptions are not implemented. They are not expected to be invoked.",
},
},
NETWORK_HELPERS: {
ONLY_ALLOW_0X_PREFIXED_STRINGS: {
number: 900,
messageTemplate: `Only hex-encoded strings prefixed with "0x" are accepted`,
websiteTitle: `Only hex-encoded strings prefixed with "0x" are accepted`,
websiteDescription: `Only hex-encoded strings prefixed with "0x" are accepted`,
},
CANNOT_CONVERT_TO_RPC_QUANTITY: {
number: 901,
messageTemplate: `The value "{value}" cannot be converted into an RPC quantity`,
websiteTitle: "Cannot converted into an RPC quantity",
websiteDescription: "The value cannot be converted into an RPC quantity",
},
INVALID_HEX_STRING: {
number: 902,
messageTemplate: `"{value}" is not a valid hex string`,
websiteTitle: "Invalid hex string",
websiteDescription: "The value is not a valid hex string",
},
INVALID_TX_HASH: {
number: 903,
messageTemplate: `"{value}" is not a valid transaction hash`,
websiteTitle: "Invalid transaction hash",
websiteDescription: "The value is not a valid transaction hash",
},
INVALID_ADDRESS: {
number: 904,
messageTemplate: `"{value}" is not a valid address`,
websiteTitle: "Invalid address",
websiteDescription: "The value is not a valid address",
},
INVALID_CHECKSUM_ADDRESS: {
number: 905,
messageTemplate: `Address "{value}" has an invalid checksum`,
websiteTitle: "Invalid checksum address",
websiteDescription: "The address has an invalid checksum",
},
BLOCK_NUMBER_SMALLER_THAN_CURRENT: {
number: 906,
messageTemplate: `The block number "{newValue}" is smaller than the current block number "{currentValue}"`,
websiteTitle: "Block number smaller than the current block number",
websiteDescription:
"The block number is smaller than the current block number",
},
EVM_SNAPSHOT_VALUE_NOT_A_STRING: {
number: 907,
messageTemplate: `The value returned by evm_snapshot should be a string`,
websiteTitle: "The evm_snapshot value should be a string",
websiteDescription:
"The value returned by evm_snapshot should be a string",
},
EVM_REVERT_VALUE_NOT_A_BOOLEAN: {
number: 908,
messageTemplate: `The value returned by evm_revert should be a boolean`,
websiteTitle: "The evm_revert value should be a boolean",
websiteDescription:
"The value returned by evm_revert should be a boolean",
},
INVALID_SNAPSHOT: {
number: 909,
messageTemplate: `Trying to restore an invalid snapshot.`,
websiteTitle: "Trying to restore an invalid snapshot.",
websiteDescription: "Trying to restore an invalid snapshot.",
},
EXPECTED_NON_NEGATIVE_NUMBER: {
number: 910,
messageTemplate: `Invalid input: expected a non-negative number but "{value}" was given.`,
websiteTitle: "Invalid input, expected a non-negative number",
websiteDescription: "Invalid input, expected a non-negative number",
},
CANNOT_CONVERT_NEGATIVE_NUMBER_TO_RPC_QUANTITY: {
number: 911,
messageTemplate: `Cannot convert negative number "{value}" to RPC quantity`,
websiteTitle: "Cannot convert negative number to RPC quantity",
websiteDescription: "Cannot convert negative number to RPC quantity",
},
FIXTURE_ANONYMOUS_FUNCTION_ERROR: {
number: 912,
messageTemplate: `Anonymous functions cannot be used as fixtures.
You probably did something like this:
loadFixture(async () => ... );
Instead, define a fixture function and refer to that same function in each call to loadFixture.
Learn more at (https://hardhat.org/hardhat-network-helpers/docs/reference#fixtures)`,
websiteTitle: "Anonymous functions cannot be used as fixtures",
websiteDescription: "Anonymous functions cannot be used as fixtures",
},
FIXTURE_SNAPSHOT_ERROR: {
number: 913,
messageTemplate: `There was an error reverting the snapshot of the fixture.
This might be caused by using hardhat_reset and loadFixture calls in a testcase.`,
websiteTitle: "Error while reverting snapshot",
websiteDescription: "Error while reverting snapshot",
},
},
} as const;
3 changes: 3 additions & 0 deletions v-next/hardhat-network-helpers/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { createConfig } = require("../../config-v-next/eslint.cjs");

module.exports = createConfig(__filename);
5 changes: 5 additions & 0 deletions v-next/hardhat-network-helpers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Node modules
/node_modules

# Compilation output
/dist
4 changes: 4 additions & 0 deletions v-next/hardhat-network-helpers/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/dist
/coverage
CHANGELOG.md
9 changes: 9 additions & 0 deletions v-next/hardhat-network-helpers/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024 Nomic Foundation

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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 changes: 25 additions & 0 deletions v-next/hardhat-network-helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hardhat Network Helpers

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).

### Usage

```javascript
const { networkHelpers } = await hre.network.connect();

// Network helpers methods exposed via `networkHelpers`
await networkHelpers.mine();

// Time methods exposed via `time`
await networkHelpers.time.increase(1);

// Duration methods exposed via `duration`
networkHelpers.time.duration.days(1);
```

### Tests

Temporary solution to run manual tests until the V3 node is ready.

1. Start a node in Hardhat V2: `npx hardhat node`
2. Run the tests: `pnpm test:tmp`
74 changes: 74 additions & 0 deletions v-next/hardhat-network-helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@ignored/hardhat-vnext-network-helpers",
"version": "3.0.0-next.2",
"description": "Hardhat utils for testing",
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/network-helpers",
"repository": {
"type": "git",
"url": "https://github.com/NomicFoundation/hardhat",
"directory": "v-next/network-helpers"
},
"author": "Nomic Foundation",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/src/index.js",
"./load-fixture": "./dist/src/load-fixture.js"
},
"keywords": [
"ethereum",
"smart-contracts",
"hardhat",
"testing"
],
"scripts": {
"lint": "pnpm prettier --check && pnpm eslint",
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"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",
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"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\"",
"pretest": "pnpm build",
"pretest:only": "pnpm build",
"build": "tsc --build .",
"prepublishOnly": "pnpm build",
"clean": "rimraf dist"
},
"files": [
"dist/src/",
"src/",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
"@types/debug": "^4.1.4",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"c8": "^9.1.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-no-only-tests": "3.1.0",
"expect-type": "^0.19.0",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"tsx": "^4.11.0",
"typescript": "~5.5.0",
"typescript-eslint": "7.7.1"
},
"dependencies": {
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2"
},
"peerDependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.2"
}
}
Loading

0 comments on commit c41488d

Please sign in to comment.