hardhat-external-artifacts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ import type {NetworkHooks, HookContext} from 'hardhat/types/hooks';
2
+ import type {NetworkConnection, ChainType} from 'hardhat/types/network';
3
+ import {ArtifactLoader} from '../artifacts/loader.js';
4
+ import {
5
+ artifactsToCompilations,
6
+ type SyntheticCompilation,
7
+ } from '../artifacts/converter.js';
8
+ import {isRichArtifact} from '../artifacts/types.js';
9
+
10
+ export default async (): Promise<Partial<NetworkHooks>> => {
11
+ const handlers: Partial<NetworkHooks> = {
12
+ newConnection: async <ChainTypeT extends ChainType | string>(
13
+ context: HookContext,
14
+ next: (
15
+ nextContext: HookContext,
16
+ ) => Promise<NetworkConnection<ChainTypeT>>,
17
+ ): Promise<NetworkConnection<ChainTypeT>> => {
18
+ // Call the default behavior first to create the connection
19
+ const connection = await next(context);
20
+
21
+ // Only inject artifacts for EDR networks (hardhat network)
22
+ if (connection.networkConfig.type !== 'edr-simulated') {
23
+ return connection;
24
+ }
25
+
26
+ const config = context.config.externalArtifacts;
27
+
28
+ // Check if config exists and there's anything to load
29
+ if (!config || (!config.paths?.length && !config.resolver)) {
30
+ return connection;
31
+ }
32
+
33
+ const debug = config.debug ?? false;
34
+ const log = debug ? console.log.bind(console) : () => {};
35
+
36
+ try {
37
+ // Load external artifacts
38
+ const loader = new ArtifactLoader(config, context.config.paths.root);
39
+ const artifacts = await loader.loadAll();
40
+
41
+ if (artifacts.length === 0) {
42
+ log('[hardhat-external-artifacts] No artifacts found');
43
+ return connection;
44
+ }
45
+
46
+ log(
47
+ `[hardhat-external-artifacts] Loaded ${artifacts.length} artifact(s):`,
48
+ );
49
+ for (const artifact of artifacts) {
50
+ const isRich = isRichArtifact(artifact);
51
+ const deployedBytecodeLength = artifact.deployedBytecode?.length || 0;
52
+ log(
53
+ ` - ${artifact.sourceName}:${artifact.contractName} (${isRich ? 'rich' : 'simple'}, deployedBytecode: ${deployedBytecodeLength} chars)`,
54
+ );
55
+ }
56
+
57
+ // Convert to compilation format(s)
58
+ // Rich artifacts with solcInput get their own compilations
59
+ // Simple artifacts get grouped into a synthetic compilation
60
+ const compilations = artifactsToCompilations(
61
+ artifacts,
62
+ config.solcVersion, // Default already set in config.ts
63
+ );
64
+
65
+ log(
66
+ `[hardhat-external-artifacts] Created ${compilations.length} compilation(s)`,
67
+ );
68
+
69
+ // Add to the EDR provider - use type assertion since addCompilationResult is an internal method
70
+ const provider = connection.provider as {
71
+ addCompilationResult?: (
72
+ solcVersion: string,
73
+ compilerInput: any,
74
+ compilerOutput: any,
75
+ ) => Promise<void>;
76
+ };
77
+
78
+ if (typeof provider.addCompilationResult === 'function') {
79
+ for (const compilation of compilations) {
80
+ logCompilation(compilation, log);
81
+
82
+ // Ensure clean JSON serialization for the native EDR binding
83
+ // by round-tripping through JSON stringify/parse
84
+ const cleanInput = JSON.parse(
85
+ JSON.stringify(compilation.compilerInput),
86
+ );
87
+ const cleanOutput = JSON.parse(
88
+ JSON.stringify(compilation.compilerOutput),
89
+ );
90
+
91
+ log(
92
+ `[hardhat-external-artifacts] Calling addCompilationResult with solcVersion: ${compilation.solcVersion}`,
93
+ );
94
+
95
+ await provider.addCompilationResult(
96
+ compilation.solcVersion,
97
+ cleanInput,
98
+ cleanOutput,
99
+ );
100
+ log(
101
+ `[hardhat-external-artifacts] Successfully added compilation to EDR provider`,
102
+ );
103
+ }
104
+
105
+ console.log(
106
+ `[hardhat-external-artifacts] Loaded ${artifacts.length} external artifact(s) in ${compilations.length} compilation(s)`,
107
+ );
108
+ } else {
109
+ console.warn(
110
+ `[hardhat-external-artifacts] Warning: provider.addCompilationResult is not available. ` +
111
+ `This might indicate an incompatible Hardhat version.`,
112
+ );
113
+ }
114
+ } catch (error) {
115
+ if (config.warnOnInvalidArtifacts !== false) {
116
+ console.warn(
117
+ `[hardhat-external-artifacts] Warning: Failed to load external artifacts:`,
118
+ error,
119
+ );
120
+ }
121
+ }
122
+
123
+ return connection;
124
+ },
125
+ };
126
+
127
+ return handlers;
128
+ };
129
+
130
+ function logCompilation(
131
+ compilation: SyntheticCompilation,
132
+ log: (...args: any[]) => void,
133
+ ): void {
134
+ log(`[hardhat-external-artifacts] Compilation details:`);
135
+ log(` solcVersion: ${compilation.solcVersion}`);
136
+ log(
137
+ ` sources in compilerInput: ${Object.keys(compilation.compilerInput.sources).join(', ')}`,
138
+ );
139
+ log(
140
+ ` sources in compilerOutput: ${Object.keys(compilation.compilerOutput.sources).join(', ')}`,
141
+ );
142
+
143
+ for (const [sourceName, contracts] of Object.entries(
144
+ compilation.compilerOutput.contracts || {},
145
+ )) {
146
+ for (const [contractName, contract] of Object.entries(
147
+ contracts as Record<string, any>,
148
+ )) {
149
+ const deployedBytecodeLength =
150
+ contract.evm?.deployedBytecode?.object?.length || 0;
151
+ const methodCount = Object.keys(
152
+ contract.evm?.methodIdentifiers || {},
153
+ ).length;
154
+ log(
155
+ ` ${sourceName}:${contractName} - deployedBytecode: ${deployedBytecodeLength} chars (${Math.floor(deployedBytecodeLength / 2)} bytes), methods: ${methodCount}`,
156
+ );
157
+ // Log first 100 chars of bytecode for verification
158
+ if (deployedBytecodeLength > 0) {
159
+ const preview = contract.evm.deployedBytecode.object.substring(0, 100);
160
+ log(` bytecode preview: ${preview}...`);
161
+ }
162
+ }
163
+ }
164
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type {HardhatPlugin} from 'hardhat/types/plugins';
2
+
3
+ // Import type extensions
4
+ import './type-extensions.js';
5
+
6
+ const hardhatExternalArtifactsPlugin: HardhatPlugin = {
7
+ id: 'hardhat-external-artifacts',
8
+ hookHandlers: {
9
+ config: () => import('./hooks/config.js'),
10
+ network: () => import('./hooks/network.js'),
11
+ },
12
+ };
13
+
14
+ export default hardhatExternalArtifactsPlugin;
15
+ export * from './artifacts/types.js';
@@ -0,0 +1,19 @@
1
+ import "hardhat/types/config";
2
+ import type { ExternalArtifactsConfig } from "./artifacts/types.js";
3
+
4
+ /**
5
+ * Resolved external artifacts configuration with defaults applied.
6
+ */
7
+ export type ResolvedExternalArtifactsConfig = Required<Omit<ExternalArtifactsConfig, "resolver">> & {
8
+ resolver?: ExternalArtifactsConfig["resolver"];
9
+ };
10
+
11
+ declare module "hardhat/types/config" {
12
+ interface HardhatUserConfig {
13
+ externalArtifacts?: ExternalArtifactsConfig;
14
+ }
15
+
16
+ interface HardhatConfig {
17
+ externalArtifacts?: ResolvedExternalArtifactsConfig;
18
+ }
19
+ }