Skip to content
Closed
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
7 changes: 5 additions & 2 deletions packages/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import myPlugin from "hardhat-my-plugin";
export default {
plugins: [myPlugin],
solidity: "0.8.29",
myConfig: {
greeting: "Hola",
networks: {
default: {
type: "edr-simulated",
myAccountIndex: 1,
},
},
} satisfies HardhatUserConfig;
5 changes: 5 additions & 0 deletions packages/example-project/scripts/my-account-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { network } from "hardhat";

const connection = await network.connect();

console.log("connection.myAccount", connection.myAccount);
65 changes: 40 additions & 25 deletions packages/plugin/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { HardhatUserConfig } from "hardhat/config";
import { HardhatConfig } from "hardhat/types/config";
import { HardhatUserConfigValidationError } from "hardhat/types/hooks";

const DEFAULT_MY_ACCOUNT_INDEX = 0;

/**
* This function validates the parts of the HardhatUserConfig that are relevant
* to the plugin.
Expand All @@ -14,34 +16,35 @@ import { HardhatUserConfigValidationError } from "hardhat/types/hooks";
export async function validatePluginConfig(
userConfig: HardhatUserConfig,
): Promise<HardhatUserConfigValidationError[]> {
if (userConfig.myConfig === undefined) {
if (
userConfig.networks === undefined ||
typeof userConfig.networks !== "object"
) {
// If there's no networks field or it's invalid, we don't validate anything
// in this plugin
return [];
}

if (typeof userConfig.myConfig !== "object") {
return [
{
path: ["myConfig"],
message: "Expected an object with an optional greeting.",
},
];
}

const greeting = userConfig.myConfig?.greeting;
if (greeting === undefined) {
return [];
}
const errors = [];
for (const [networkName, networkConfig] of Object.entries(
userConfig.networks,
)) {
if (networkConfig.myAccountIndex === undefined) {
continue;
}

if (typeof greeting !== "string" || greeting.length === 0) {
return [
{
path: ["myConfig", "greeting"],
message: "Expected a non-empty string.",
},
];
if (
typeof networkConfig.myAccountIndex !== "number" ||
networkConfig.myAccountIndex < 0
) {
errors.push({
path: ["networks", networkName, "myAccountIndex"],
message: "Expected a non-negative number.",
});
}
}

return [];
return errors;
}

/**
Expand All @@ -59,11 +62,23 @@ export async function resolvePluginConfig(
userConfig: HardhatUserConfig,
partiallyResolvedConfig: HardhatConfig,
): Promise<HardhatConfig> {
const greeting = userConfig.myConfig?.greeting ?? "Hello";
const myConfig = { greeting };
const networks: HardhatConfig["networks"] = {};

for (const [networkName, networkConfig] of Object.entries(
partiallyResolvedConfig.networks,
)) {
const myAccountIndex =
userConfig.networks?.[networkName]?.myAccountIndex ??
DEFAULT_MY_ACCOUNT_INDEX;

networks[networkName] = {
...networkConfig,
myAccountIndex,
};
}

return {
...partiallyResolvedConfig,
myConfig,
networks,
};
}
24 changes: 16 additions & 8 deletions packages/plugin/src/hooks/network.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HardhatPluginError } from "hardhat/plugins";
import type { HookContext, NetworkHooks } from "hardhat/types/hooks";
import { ChainType, NetworkConnection } from "hardhat/types/network";

Expand All @@ -11,16 +12,23 @@ export default async (): Promise<Partial<NetworkHooks>> => {
): Promise<NetworkConnection<ChainTypeT>> {
const connection = await next(context);

console.log("Connection created with ID", connection.id);
// Get the accounts from the connection
const accounts: string[] = await connection.provider.request({
method: "eth_accounts",
});

return connection;
},
async onRequest(context, networkConnection, jsonRpcRequest, next) {
console.log(
`Request from connection ${networkConnection.id} is being processed — Method: ${jsonRpcRequest.method}`,
);
const myAccountIndex = connection.networkConfig.myAccountIndex;

if (accounts.length <= myAccountIndex) {
throw new HardhatPluginError(
`hardhat-plugin-template`,
`Invalid index ${myAccountIndex} for myAccount when connecting to network ${connection.networkName}`,
);
}

return next(context, networkConnection, jsonRpcRequest);
connection.myAccount = accounts[myAccountIndex];

return connection;
},
};

Expand Down
10 changes: 5 additions & 5 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const plugin: HardhatPlugin = {
network: () => import("./hooks/network.js"),
},
tasks: [
task("my-task", "Prints a greeting.")
task("my-account", "Prints your account.")
.addOption({
name: "who",
description: "Who is receiving the greeting.",
name: "title",
description: "The title to use before printing the account.",
type: ArgumentType.STRING,
defaultValue: "Hardhat",
defaultValue: "My account:",
})
.setAction(() => import("./tasks/my-task.js"))
.setAction(() => import("./tasks/my-account.js"))
.build(),
],
};
Expand Down
14 changes: 14 additions & 0 deletions packages/plugin/src/tasks/my-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HardhatRuntimeEnvironment } from "hardhat/types/hre";

interface MyAccountTaskArguments {
title: string;
}

export default async function (
taskArguments: MyAccountTaskArguments,
hre: HardhatRuntimeEnvironment,
) {
const conn = await hre.network.connect();
console.log(taskArguments.title);
console.log(conn.myAccount);
}
12 changes: 0 additions & 12 deletions packages/plugin/src/tasks/my-task.ts

This file was deleted.

25 changes: 19 additions & 6 deletions packages/plugin/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { MyPluginConfig, MyPluginUserConfig } from "./types.js";

import "hardhat/types/config";
declare module "hardhat/types/config" {
interface HardhatUserConfig {
myConfig?: MyPluginUserConfig;
export interface EdrNetworkUserConfig {
myAccountIndex?: number;
}

export interface EdrNetworkConfig {
myAccountIndex: number;
}

export interface HttpNetworkUserConfig {
myAccountIndex?: number;
}

interface HardhatConfig {
myConfig: MyPluginConfig;
export interface HttpNetworkConfig {
myAccountIndex: number;
}
}

import "hardhat/types/network";
declare module "hardhat/types/network" {
export interface NetworkConnection {
myAccount: string;
}
}

Expand Down
7 changes: 0 additions & 7 deletions packages/plugin/src/types.ts

This file was deleted.

Loading