Skip to content

Commit

Permalink
feat: port the node task to hardhat v3
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Dec 7, 2024
1 parent 7f07720 commit a34b576
Show file tree
Hide file tree
Showing 11 changed files with 871 additions and 14 deletions.
54 changes: 41 additions & 13 deletions pnpm-lock.yaml

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

12 changes: 11 additions & 1 deletion v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const ERROR_CATEGORIES: {
},
SOLIDITY: { min: 1200, max: 1299, websiteTitle: "Solidity errors" },
VIEM: { min: 1300, max: 1399, websiteTitle: "Hardhat-viem errors" },
NODE: { min: 1400, max: 1499, websiteTitle: "Hardhat node errors" },
};

export const ERRORS = {
Expand Down Expand Up @@ -135,7 +136,7 @@ Please double check whether you have multiple versions of the same plugin instal
ENV_VAR_NOT_FOUND: {
number: 7,
messageTemplate: `Configuration Variable '{name}' not found.
You can define it using a plugin like hardhat-keystore, or set it as an environment variable.`,
websiteTitle: "Configuration variable not found",
websiteDescription: `A configuration variable was expected to be set as an environment variable, but it wasn't.`,
Expand Down Expand Up @@ -1224,4 +1225,13 @@ Please check Hardhat's output for more details.`,
"The deployment transaction was mined but its receipt doesn't contain a contract address.",
},
},
NODE: {
INVALID_NETWORK_TYPE: {
number: 1400,
messageTemplate:
"The provided node network type {networkType} for network {networkName} is not recognized, only `edr` is supported.",
websiteTitle: "Invalid node network type",
websiteDescription: `The node only supports the 'edr' network type.`,
},
},
} as const;
3 changes: 3 additions & 0 deletions v-next/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/debug": "^4.1.4",
"@types/node": "^20.14.9",
"@types/semver": "^7.5.8",
"@types/ws": "^8.5.13",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"eslint": "8.57.0",
Expand Down Expand Up @@ -96,9 +97,11 @@
"ethereum-cryptography": "^2.2.1",
"micro-eth-signer": "^0.12.0",
"p-map": "^7.0.2",
"raw-body": "^2.4.1",
"semver": "^7.6.3",
"solc": "^0.8.27",
"tsx": "^4.11.0",
"ws": "^8.18.0",
"zod": "^3.23.8"
}
}
3 changes: 3 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import artifacts from "./artifacts/index.js";
import clean from "./clean/index.js";
import console from "./console/index.js";
import networkManager from "./network-manager/index.js";
import node from "./node/index.js";
import run from "./run/index.js";
import solidity from "./solidity/index.js";
import solidityTest from "./solidity-test/index.js";
Expand All @@ -19,6 +20,7 @@ export type * from "./network-manager/index.js";
export type * from "./clean/index.js";
export type * from "./console/index.js";
export type * from "./run/index.js";
export type * from "./node/index.js";

// This array should be kept in order, respecting the dependencies between the
// plugins.
Expand All @@ -31,4 +33,5 @@ export const builtinPlugins: HardhatPlugin[] = [
clean,
console,
run,
node,
];
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
JsonRpcRequest,
JsonRpcResponse,
RequestArguments,
SuccessfulJsonRpcResponse,
} from "../../../types/providers.js";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
Expand Down Expand Up @@ -56,6 +57,30 @@ export function parseJsonRpcResponse(
}
}

export function isJsonRpcRequest(payload: unknown): payload is JsonRpcRequest {
if (!isObject(payload)) {
return false;
}

if (payload.jsonrpc !== "2.0") {
return false;
}

if (typeof payload.id !== "number" && typeof payload.id !== "string") {
return false;
}

if (typeof payload.method !== "string") {
return false;
}

if (payload.params !== undefined && !Array.isArray(payload.params)) {
return false;
}

return true;
}

export function isJsonRpcResponse(
payload: unknown,
): payload is JsonRpcResponse {
Expand Down Expand Up @@ -96,6 +121,12 @@ export function isJsonRpcResponse(
return true;
}

export function isSuccessfulJsonRpcResponse(
payload: JsonRpcResponse,
): payload is SuccessfulJsonRpcResponse {
return "result" in payload;
}

export function isFailedJsonRpcResponse(
payload: JsonRpcResponse,
): payload is FailedJsonRpcResponse {
Expand Down
85 changes: 85 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/node/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type {
EdrNetworkAccountConfig,
EdrNetworkAccountsConfig,
GenesisAccount,
} from "../../../types/config.js";

import {
bytesToHexString,
hexStringToBytes,
} from "@ignored/hardhat-vnext-utils/hex";
import chalk from "chalk";
import { addr } from "micro-eth-signer";

import { derivePrivateKeys } from "../network-manager/json-rpc-request-modifiers/accounts/derive-private-keys.js";

export function normalizeEdrNetworkConfigAccounts(
accounts: EdrNetworkAccountsConfig,
): EdrNetworkAccountConfig[] {
const normalizedAccounts: EdrNetworkAccountConfig[] = [];

if (accounts !== undefined) {
if (Array.isArray(accounts)) {
normalizedAccounts.push(...accounts);
} else if (accounts !== undefined) {
const privateKeys = derivePrivateKeys(
accounts.mnemonic,
accounts.path,
accounts.initialIndex,
accounts.count,
accounts.passphrase,
);
for (const privateKey of privateKeys) {
normalizedAccounts.push({
privateKey: bytesToHexString(privateKey),
balance: accounts.accountsBalance,
});
}
}
}

return normalizedAccounts;
}

export function printEdrNetworkConfigAccounts(
accounts: Array<EdrNetworkAccountConfig | GenesisAccount>,
): void {
if (accounts.length === 0) {
return;
}

console.log("Accounts");
console.log("========");

// NOTE: In v2, we were printing the warning only if the default config was used.
console.log();
printPublicPrivateKeysWarning();
console.log();

for (const [index, account] of accounts.entries()) {
const address = addr
.fromPrivateKey(hexStringToBytes(account.privateKey))
.toLowerCase();
const balance = (BigInt(account.balance) / 10n ** 18n).toString(10);

console.log(`Account #${index}: ${address} (${balance} ETH)`);
// TODO: Should we print the private key as well?
// console.log(`Private Key: ${account.privateKey}`);

console.log();
}

// NOTE: In v2, we were printing the warning only if the default config was used.
printPublicPrivateKeysWarning();
console.log();
}

// NOTE: In v2, we were printing the warning only if the default config was used.
// Because of that we were certain that the printed accounts were publicly known.
function printPublicPrivateKeysWarning(): void {
console.log(
chalk.bold(
"WARNING: Funds sent on live network to accounts with publicly known private keys WILL BE LOST.",
),
);
}
Loading

0 comments on commit a34b576

Please sign in to comment.