Repository navigation
Migrate Hardhat V2 Project to V3: Understanding Hook System and HRE Usage in New Extensibility Model #7465
Replies: 2 comments 3 replies
|
Hey @AlirezaEthDev, improving the documentation is our top priority - including documenting the extensibility via the hook system. But we are not there yet (we are putting together an extensibility guide for plugins at the moment). Is it a plugin you are migrating or where you using If your project is up on github can you link it so I can take a look? |
|
The docs for this are out now. The plugin development section covers it, and the most relevant pages are Hooks and Hook Handlers and Extending the NetworkConnection. Here's the short version for replacing In Hardhat 3 a config can't run code against the HRE directly. You put the logic in a small local plugin and list it in 1. The plugin and its hook handler. The // plugins/my-env/index.ts
import type { HardhatPlugin } from "hardhat/types/plugins";
import "./type-extensions.js";
const plugin: HardhatPlugin = {
id: "my-env",
hookHandlers: {
hre: () => import("./hook-handlers/hre.js"),
},
};
export default plugin;// plugins/my-env/hook-handlers/hre.ts
import type { HardhatRuntimeEnvironmentHooks } from "hardhat/types/hooks";
export default async (): Promise<Partial<HardhatRuntimeEnvironmentHooks>> => ({
created: async (_context, hre) => {
hre.greeting = "hi";
hre.hello = (name) => `${hre.greeting} ${name}`;
},
});2. Types. This replaces the // plugins/my-env/type-extensions.ts
import "hardhat/types/hre";
declare module "hardhat/types/hre" {
interface HardhatRuntimeEnvironment {
greeting: string;
hello: (name: string) => string;
}
}3. Register it in the config: // hardhat.config.ts
import { defineConfig } from "hardhat/config";
import myEnv from "./plugins/my-env/index.js";
export default defineConfig({
plugins: [myEnv],
solidity: "0.8.28",
});Now If the v2 extension used the network (a provider, signers, // plugins/my-env/hook-handlers/network.ts
import type { NetworkHooks } from "hardhat/types/hooks";
export default async (): Promise<Partial<NetworkHooks>> => ({
newConnection: async (context, next) => {
const connection = await next(context);
connection.blockNumber = async () =>
BigInt(await connection.provider.request({ method: "eth_blockNumber" }));
return connection;
},
});Add |
Uh oh!
There was an error while loading. Please reload this page.
I am currently working on a project using Hardhat V2, which incorporates HRE and extendEnvironment. I need to migrate this project to Hardhat V3. While reviewing the V3 documentation, I noticed the statement:
These extensibility points were replaced by the hook system. We'll add details on how to migrate them soon.How can I learn more about the hook system and how to utilize HRE in Hardhat V3?All reactions