import type { ArtifactManager, GetArtifactByName, StringWithArtifactContractNamesAutocompletion, } from "../../../../types/artifacts.js"; import type { HardhatRuntimeEnvironmentHooks } from "../../../../types/hooks.js"; import type { ArtifactManagerImplementation as ArtifactManagerImplementationT } from "../artifact-manager.js"; let ArtifactManagerImplementation: typeof ArtifactManagerImplementationT | undefined; class LazyArtifactManager implements ArtifactManager { readonly #artifactsPath: string; #artifactManager: ArtifactManager | undefined; constructor(artifactsPath: string) { this.#artifactManager = undefined; this.#artifactsPath = artifactsPath; } public async readArtifact< ContractNameT extends StringWithArtifactContractNamesAutocompletion, >( contractNameOrFullyQualifiedName: ContractNameT, ): Promise> { const artifactManager = await this.#getArtifactManager(); return await artifactManager.readArtifact(contractNameOrFullyQualifiedName); } public async getArtifactPath( contractNameOrFullyQualifiedName: string, ): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getArtifactPath( contractNameOrFullyQualifiedName, ); } public async artifactExists( contractNameOrFullyQualifiedName: string, ): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.artifactExists( contractNameOrFullyQualifiedName, ); } public async getAllFullyQualifiedNames(): Promise> { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getAllFullyQualifiedNames(); } public async getAllArtifactPaths(): Promise> { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getAllArtifactPaths(); } public async getBuildInfoId( contractNameOrFullyQualifiedName: string, ): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getBuildInfoId( contractNameOrFullyQualifiedName, ); } public async getAllBuildInfoIds(): Promise> { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getAllBuildInfoIds(); } public async getBuildInfoPath( buildInfoId: string, ): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getBuildInfoPath(buildInfoId); } public async getBuildInfoOutputPath( buildInfoId: string, ): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.getBuildInfoOutputPath(buildInfoId); } public async clearCache(): Promise { const artifactManager = await this.#getArtifactManager(); return await artifactManager.clearCache(); } async #getArtifactManager(): Promise { if (ArtifactManagerImplementation === undefined) { ({ ArtifactManagerImplementation } = await import("../artifact-manager.js")); } if (this.#artifactManager === undefined) { this.#artifactManager = new ArtifactManagerImplementation( this.#artifactsPath, ); } return this.#artifactManager; } } export default async (): Promise> => { const handlers: Partial = { created: async (_context, hre): Promise => { hre.artifacts = new LazyArtifactManager(hre.config.paths.artifacts); }, }; return handlers; };