Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build info watcher to the hardhat node task #6040

Open
galargh opened this issue Dec 10, 2024 · 0 comments
Open

Add build info watcher to the hardhat node task #6040

galargh opened this issue Dec 10, 2024 · 0 comments
Assignees
Labels
status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task
Milestone

Comments

@galargh
Copy link
Member

galargh commented Dec 10, 2024

This is a follow-up task to #5615

It is about adding a build info (from artifact) watcher functionality. The idea is to watch for build info changes and adding compilation info to EDR when they happen.

We'll be able to work on this once EDR is ready to receive build info from us.

Below, you'll find a draft of the implementation.

Implementation:

import fsPromises from "node:fs/promises";

export interface WatcherEvent {
  eventType: "change" | "rename";
  filename: string | null;
}

export type WatcherEventHandler = (event: WatcherEvent) => Promise<void>;

export class Watcher {
  readonly #abortController: AbortController;
  readonly #eventLoop: Promise<void>;

  constructor(path: string, eventHandler: WatcherEventHandler) {
    this.#abortController = new AbortController();

    const events = fsPromises.watch(path, {
      recursive: true,
      signal: this.#abortController.signal,
    });

    this.#eventLoop = new Promise(async () => {
      for await (const event of events) {
        await eventHandler(event);
      }
    });
  }

  public close(): Promise<void> {
    this.#abortController.abort();
    return this.#eventLoop;
  }
}

Usage:

import type { WatcherEvent } from "./watcher.js";
import type { BuildInfo } from "../../../types/artifacts.js";

import path from "node:path";

import { exists, readJsonFile } from "@ignored/hardhat-vnext-utils/fs";

import { BUILD_INFO_DIR_NAME } from "../artifacts/artifacts-manager.js";

import { Watcher } from "./watcher.js";

...

  const watcher = new Watcher(
    path.join(hre.config.paths.artifacts, BUILD_INFO_DIR_NAME),
    async ({ eventType, filename }: WatcherEvent) => {
      log(`Detected ${eventType} in ${filename}`);

      if (
        filename === null ||
        filename.endsWith(".output.json") ||
        !(await exists(filename))
      ) {
        return;
      }

      const filenameOutput = filename.replace(".json", ".output.json");
      if (await exists(filenameOutput)) {
        return;
      }

      const buildInfo: BuildInfo = await readJsonFile(filename);
      const buildInfoOutput = await readJsonFile(filenameOutput);

      try {
        await provider.request({
          method: "hardhat_addCompilationResult",
          params: [buildInfo.solcVersion, buildInfo.input, buildInfoOutput],
        });
      } catch (error) {
        log(error);
      }
    },
  );
  
  ...
  
  await watcher.close();
  
...
@galargh galargh added status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task labels Dec 10, 2024
@github-project-automation github-project-automation bot moved this to Backlog in Hardhat Dec 10, 2024
@galargh galargh self-assigned this Dec 10, 2024
@kanej kanej moved this from Backlog to To-do in Hardhat Dec 13, 2024
@kanej kanej added this to the Public Alpha milestone Dec 13, 2024
@kanej kanej mentioned this issue Dec 13, 2024
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task
Projects
Status: To-do
Development

No branches or pull requests

2 participants