hardhat-deploy 2.0.0-next.6 → 2.0.0-next.60
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.
- package/LICENSE +1 -1
- package/README.md +22 -1
- package/dist/esm/config/default.d.ts.map +1 -1
- package/dist/esm/config/default.js +2 -5
- package/dist/esm/config/default.js.map +1 -1
- package/dist/esm/config/get-config.d.ts.map +1 -1
- package/dist/esm/config/get-config.js +3 -2
- package/dist/esm/config/get-config.js.map +1 -1
- package/dist/esm/config/validation.d.ts.map +1 -1
- package/dist/esm/config/validation.js +8 -7
- package/dist/esm/config/validation.js.map +1 -1
- package/dist/esm/generate-types.d.ts +2 -3
- package/dist/esm/generate-types.d.ts.map +1 -1
- package/dist/esm/generate-types.js +202 -156
- package/dist/esm/generate-types.js.map +1 -1
- package/dist/esm/helpers.d.ts +28 -12
- package/dist/esm/helpers.d.ts.map +1 -1
- package/dist/esm/helpers.js +106 -72
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/hook-handlers/config.d.ts.map +1 -1
- package/dist/esm/hook-handlers/config.js +53 -1
- package/dist/esm/hook-handlers/config.js.map +1 -1
- package/dist/esm/hook-handlers/solidity.d.ts.map +1 -1
- package/dist/esm/hook-handlers/solidity.js +13 -7
- package/dist/esm/hook-handlers/solidity.js.map +1 -1
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +22 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/tasks/deploy.d.ts +1 -0
- package/dist/esm/tasks/deploy.d.ts.map +1 -1
- package/dist/esm/tasks/deploy.js +20 -9
- package/dist/esm/tasks/deploy.js.map +1 -1
- package/dist/esm/type-extensions.d.ts +2 -2
- package/dist/esm/type-extensions.d.ts.map +1 -1
- package/dist/esm/types.d.ts +8 -10
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/utils/files.d.ts +9 -0
- package/dist/esm/utils/files.d.ts.map +1 -0
- package/dist/esm/utils/files.js +25 -0
- package/dist/esm/utils/files.js.map +1 -0
- package/package.json +18 -13
- package/src/config/default.ts +6 -0
- package/src/config/get-config.ts +12 -0
- package/src/config/validation.ts +25 -0
- package/src/generate-types.ts +281 -0
- package/src/helpers.ts +311 -0
- package/src/hook-handlers/config.ts +80 -0
- package/src/hook-handlers/solidity.ts +33 -0
- package/src/index.ts +53 -0
- package/src/tasks/deploy.ts +43 -0
- package/src/type-extensions.ts +12 -0
- package/src/types.ts +9 -0
- package/src/utils/files.ts +37 -0
- package/dist/esm/tasks/compile.d.ts +0 -6
- package/dist/esm/tasks/compile.d.ts.map +0 -1
- package/dist/esm/tasks/compile.js +0 -254
- package/dist/esm/tasks/compile.js.map +0 -1
|
@@ -2,10 +2,10 @@ import 'hardhat/types/config';
|
|
|
2
2
|
import { ArtifactGenerationConfig, ArtifactGenerationUserConfig } from './types.js';
|
|
3
3
|
declare module 'hardhat/types/config' {
|
|
4
4
|
interface HardhatUserConfig {
|
|
5
|
-
|
|
5
|
+
generateTypedArtifacts?: ArtifactGenerationUserConfig;
|
|
6
6
|
}
|
|
7
7
|
interface HardhatConfig {
|
|
8
|
-
readonly
|
|
8
|
+
readonly generateTypedArtifacts: ArtifactGenerationConfig;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=type-extensions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-extensions.d.ts","sourceRoot":"","sources":["../../src/type-extensions.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAC9B,OAAO,EAAC,wBAAwB,EAAE,4BAA4B,EAAC,MAAM,YAAY,CAAC;AAElF,OAAO,QAAQ,sBAAsB,CAAC;IACrC,UAAiB,iBAAiB;QACjC,
|
|
1
|
+
{"version":3,"file":"type-extensions.d.ts","sourceRoot":"","sources":["../../src/type-extensions.ts"],"names":[],"mappings":"AAAA,OAAO,sBAAsB,CAAC;AAC9B,OAAO,EAAC,wBAAwB,EAAE,4BAA4B,EAAC,MAAM,YAAY,CAAC;AAElF,OAAO,QAAQ,sBAAsB,CAAC;IACrC,UAAiB,iBAAiB;QACjC,sBAAsB,CAAC,EAAE,4BAA4B,CAAC;KACtD;IAED,UAAiB,aAAa;QAC7B,QAAQ,CAAC,sBAAsB,EAAE,wBAAwB,CAAC;KAC1D;CACD"}
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
export type ArtifactGenerationUserConfig = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
tsm?: string[];
|
|
2
|
+
destinations?: {
|
|
3
|
+
mode?: 'javascript' | 'typescript';
|
|
4
|
+
folder?: string;
|
|
5
|
+
}[];
|
|
7
6
|
};
|
|
8
7
|
export type ArtifactGenerationConfig = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
tsm: string[];
|
|
8
|
+
destinations: {
|
|
9
|
+
mode: 'javascript' | 'typescript';
|
|
10
|
+
folder: string;
|
|
11
|
+
}[];
|
|
14
12
|
};
|
|
15
13
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/esm/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,4BAA4B,GAAG;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,4BAA4B,GAAG;IAE1C,YAAY,CAAC,EAAE;QAAC,IAAI,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAC,EAAE,CAAC;CACvE,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG;IAEtC,YAAY,EAAE;QAAC,IAAI,EAAE,YAAY,GAAG,YAAY,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAC,EAAE,CAAC;CACpE,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type FileTraversed = {
|
|
2
|
+
name: string;
|
|
3
|
+
path: string;
|
|
4
|
+
relativePath: string;
|
|
5
|
+
mtimeMs: number;
|
|
6
|
+
directory: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function traverse(dir: string, result?: any[], topDir?: string, filter?: (name: string, stats: any) => boolean): Array<FileTraversed>;
|
|
9
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../src/utils/files.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,wBAAgB,QAAQ,CACvB,GAAG,EAAE,MAAM,EACX,MAAM,GAAE,GAAG,EAAO,EAClB,MAAM,CAAC,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,OAAO,GAC5C,KAAK,CAAC,aAAa,CAAC,CAoBtB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export function traverse(dir, result = [], topDir, filter // TODO any is Stats
|
|
4
|
+
) {
|
|
5
|
+
fs.readdirSync(dir).forEach((name) => {
|
|
6
|
+
const fPath = path.resolve(dir, name);
|
|
7
|
+
const stats = fs.statSync(fPath);
|
|
8
|
+
if ((!filter && !name.startsWith('.')) || (filter && filter(name, stats))) {
|
|
9
|
+
const fileStats = {
|
|
10
|
+
name,
|
|
11
|
+
path: fPath,
|
|
12
|
+
relativePath: path.relative(topDir || dir, fPath),
|
|
13
|
+
mtimeMs: stats.mtimeMs,
|
|
14
|
+
directory: stats.isDirectory(),
|
|
15
|
+
};
|
|
16
|
+
if (fileStats.directory) {
|
|
17
|
+
result.push(fileStats);
|
|
18
|
+
return traverse(fPath, result, topDir || dir, filter);
|
|
19
|
+
}
|
|
20
|
+
result.push(fileStats);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../../src/utils/files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAU7B,MAAM,UAAU,QAAQ,CACvB,GAAW,EACX,SAAgB,EAAE,EAClB,MAAe,EACf,MAA8C,CAAC,oBAAoB;;IAEnE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACpC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACtC,MAAM,KAAK,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACjC,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;YAC3E,MAAM,SAAS,GAAG;gBACjB,IAAI;gBACJ,IAAI,EAAE,KAAK;gBACX,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE,KAAK,CAAC;gBACjD,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,SAAS,EAAE,KAAK,CAAC,WAAW,EAAE;aAC9B,CAAC;YACF,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;gBACzB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACvB,OAAO,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,GAAG,EAAE,MAAM,CAAC,CAAC;YACvD,CAAC;YACD,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACxB,CAAC;IACF,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hardhat-deploy",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.60",
|
|
4
4
|
"description": "deployment plugin for hardhat",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -24,26 +24,31 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"dist"
|
|
27
|
+
"dist",
|
|
28
|
+
"src"
|
|
28
29
|
],
|
|
29
30
|
"devDependencies": {
|
|
30
|
-
"@
|
|
31
|
+
"@changesets/cli": "^2.29.8",
|
|
32
|
+
"@rocketh/node": "0.17.15",
|
|
33
|
+
"@types/node": "^25.0.6",
|
|
31
34
|
"as-soon": "^0.0.11",
|
|
32
|
-
"hardhat": "
|
|
33
|
-
"rimraf": "^
|
|
34
|
-
"
|
|
35
|
-
"
|
|
35
|
+
"hardhat": "3.1.3",
|
|
36
|
+
"rimraf": "^6.1.2",
|
|
37
|
+
"rocketh": "0.17.12",
|
|
38
|
+
"set-defaults": "^0.0.5",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
36
40
|
},
|
|
37
41
|
"peerDependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
42
|
+
"@rocketh/node": "^0.17.15",
|
|
43
|
+
"hardhat": "^3.1.3"
|
|
40
44
|
},
|
|
41
45
|
"dependencies": {
|
|
42
|
-
"@nomicfoundation/hardhat-
|
|
43
|
-
"@nomicfoundation/hardhat-zod-utils": "3.0.0-next.0",
|
|
46
|
+
"@nomicfoundation/hardhat-zod-utils": "3.0.1",
|
|
44
47
|
"@types/debug": "^4.1.12",
|
|
45
|
-
"debug": "^4.4.
|
|
46
|
-
"
|
|
48
|
+
"debug": "^4.4.3",
|
|
49
|
+
"named-logs-console": "^0.5.1",
|
|
50
|
+
"slash": "^5.1.0",
|
|
51
|
+
"zod": "^3.25.76"
|
|
47
52
|
},
|
|
48
53
|
"scripts": {
|
|
49
54
|
"build": "tsc --project tsconfig.json",
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {ArtifactGenerationConfig, ArtifactGenerationUserConfig} from '../types.js';
|
|
2
|
+
|
|
3
|
+
import {DEFAULT_CONFIG} from './default.js';
|
|
4
|
+
|
|
5
|
+
export function getConfig(userConfig: ArtifactGenerationUserConfig | undefined): ArtifactGenerationConfig {
|
|
6
|
+
return {
|
|
7
|
+
destinations:
|
|
8
|
+
userConfig?.destinations?.map((v) => ({mode: v.mode || 'javascript', folder: v.folder || './generated'})) ||
|
|
9
|
+
DEFAULT_CONFIG.destinations,
|
|
10
|
+
// externalArtifacts: userConfig?.externalArtifacts || DEFAULT_CONFIG.externalArtifacts,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type {HardhatUserConfigValidationError} from '@nomicfoundation/hardhat-zod-utils';
|
|
2
|
+
import type {HardhatUserConfig} from 'hardhat/config';
|
|
3
|
+
|
|
4
|
+
import {validateUserConfigZodType} from '@nomicfoundation/hardhat-zod-utils';
|
|
5
|
+
import {z} from 'zod';
|
|
6
|
+
|
|
7
|
+
const artifactGenerationUserConfigSchema = z
|
|
8
|
+
.object({
|
|
9
|
+
// externalArtifacts: z.array(z.string()).optional(),
|
|
10
|
+
destinations: z
|
|
11
|
+
.array(
|
|
12
|
+
z.object({
|
|
13
|
+
mode: z.union([z.literal('javascript'), z.literal('typescript')]).optional(),
|
|
14
|
+
folder: z.string().optional(),
|
|
15
|
+
})
|
|
16
|
+
)
|
|
17
|
+
.optional(),
|
|
18
|
+
})
|
|
19
|
+
.optional();
|
|
20
|
+
|
|
21
|
+
export async function validateTypechainUserConfig(
|
|
22
|
+
userConfig: HardhatUserConfig
|
|
23
|
+
): Promise<HardhatUserConfigValidationError[]> {
|
|
24
|
+
return validateUserConfigZodType(userConfig.generateTypedArtifacts, artifactGenerationUserConfigSchema);
|
|
25
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type {ArtifactGenerationConfig} from './types.js';
|
|
2
|
+
import debug from 'debug';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path, {basename, dirname} from 'node:path';
|
|
5
|
+
import slash from 'slash';
|
|
6
|
+
import {FileTraversed, traverse} from './utils/files.js';
|
|
7
|
+
|
|
8
|
+
const log = debug('hardhat-deploy:generate-types');
|
|
9
|
+
|
|
10
|
+
function writeIfDifferent(filePath: string, newTextContent: string) {
|
|
11
|
+
// Ensure we're working with a string
|
|
12
|
+
const contentToWrite = String(newTextContent);
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
let existingContent;
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
existingContent = fs.readFileSync(filePath, 'utf8');
|
|
19
|
+
} catch (error) {
|
|
20
|
+
// console.log(`do not exist? => writing ${filePath}`);
|
|
21
|
+
// File doesn't exist, write and return
|
|
22
|
+
fs.writeFileSync(filePath, contentToWrite);
|
|
23
|
+
return {written: true, reason: 'File did not exist'};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Simple string comparison
|
|
27
|
+
if (contentToWrite !== existingContent) {
|
|
28
|
+
// console.log(`content different => writing ${filePath}`);
|
|
29
|
+
fs.writeFileSync(filePath, contentToWrite);
|
|
30
|
+
return {written: true, reason: 'Content was different'};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {written: false, reason: 'Content was identical'};
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error('Error in writeIfDifferent:', error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ensureDirExistsSync(folderPath: string) {
|
|
41
|
+
// Check if directory already exists
|
|
42
|
+
if (fs.existsSync(folderPath)) {
|
|
43
|
+
return {created: false, reason: 'Directory already exists'};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// console.log(`do not exist? => mkdir ${folderPath}`);
|
|
47
|
+
// Directory doesn't exist, create it
|
|
48
|
+
fs.mkdirSync(folderPath, {recursive: true});
|
|
49
|
+
return {created: true, reason: 'Directory was created'};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Artifact = {
|
|
53
|
+
contractName: string;
|
|
54
|
+
abi: any[];
|
|
55
|
+
// ...
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type Artifacts = {[key: string]: Artifact};
|
|
59
|
+
|
|
60
|
+
function writeArtifactToFile(folder: string, canonicalName: string, data: Artifact, mode: 'typescript' | 'javascript') {
|
|
61
|
+
const name = canonicalName.split('/').pop();
|
|
62
|
+
const artifactName = `Artifact_${name}`;
|
|
63
|
+
const tsFilepath = path.join(folder, 'artifacts', canonicalName) + '.ts';
|
|
64
|
+
const folderPath = path.dirname(tsFilepath);
|
|
65
|
+
ensureDirExistsSync(folderPath);
|
|
66
|
+
if (mode === 'typescript') {
|
|
67
|
+
const newContent = `export const ${artifactName} = ${JSON.stringify(data, null, 2)} as const;`;
|
|
68
|
+
writeIfDifferent(tsFilepath, newContent);
|
|
69
|
+
} else if (mode === 'javascript') {
|
|
70
|
+
const newContent = `export const ${artifactName} = /** @type {const} **/ (${JSON.stringify(data, null, 2)});`;
|
|
71
|
+
const dtsContent = `export declare const ${artifactName}: ${JSON.stringify(data, null, 2)};`;
|
|
72
|
+
const jsFilepath = path.join(folder, 'artifacts', canonicalName) + '.js';
|
|
73
|
+
writeIfDifferent(jsFilepath, newContent);
|
|
74
|
+
writeIfDifferent(jsFilepath.replace(/\.js$/, '.d.ts'), dtsContent);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function writeArtifactIndexToFile(folder: string, data: Artifacts, mode: 'typescript' | 'javascript') {
|
|
79
|
+
const tsFilepath = path.join(folder, 'artifacts', 'index') + '.ts';
|
|
80
|
+
const folderPath = path.dirname(tsFilepath);
|
|
81
|
+
ensureDirExistsSync(folderPath);
|
|
82
|
+
if (mode === 'typescript') {
|
|
83
|
+
let newContent = '';
|
|
84
|
+
for (const canonicalName of Object.keys(data)) {
|
|
85
|
+
const transformedName = canonicalName.replaceAll('/', '_').replaceAll('.', '_');
|
|
86
|
+
const name = canonicalName.split('/').pop();
|
|
87
|
+
const artifactName = `Artifact_${name}`;
|
|
88
|
+
const importNaming =
|
|
89
|
+
canonicalName != name ? `${artifactName} as ${transformedName}` : `${artifactName} as ${name}`;
|
|
90
|
+
newContent += `export {${importNaming}} from './${canonicalName}.js';\n`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
writeIfDifferent(tsFilepath, newContent);
|
|
94
|
+
} else if (mode === 'javascript') {
|
|
95
|
+
let newContent = '';
|
|
96
|
+
for (const canonicalName of Object.keys(data)) {
|
|
97
|
+
const transformedName = canonicalName.replaceAll('/', '_').replaceAll('.', '_');
|
|
98
|
+
const name = canonicalName.split('/').pop();
|
|
99
|
+
const artifactName = `Artifact_${name}`;
|
|
100
|
+
const importNaming =
|
|
101
|
+
canonicalName != name ? `${artifactName} as ${transformedName}` : `${artifactName} as ${name}`;
|
|
102
|
+
newContent += `export {${importNaming}} from './${canonicalName}.js';\n`;
|
|
103
|
+
}
|
|
104
|
+
const jsFilepath = path.join(folder, 'artifacts', 'index') + '.js';
|
|
105
|
+
writeIfDifferent(jsFilepath, newContent);
|
|
106
|
+
writeIfDifferent(jsFilepath.replace(/\.js$/, '.d.ts'), newContent);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function writeABIDefinitionToFile(
|
|
111
|
+
folder: string,
|
|
112
|
+
canonicalName: string,
|
|
113
|
+
data: Artifact,
|
|
114
|
+
mode: 'typescript' | 'javascript',
|
|
115
|
+
) {
|
|
116
|
+
const nameAsPath = canonicalName.split('/');
|
|
117
|
+
const name = nameAsPath[nameAsPath.length - 1];
|
|
118
|
+
const abiName = `Abi_${name}`;
|
|
119
|
+
const artifactName = `Artifact_${name}`;
|
|
120
|
+
const relativePath = `../`.repeat(nameAsPath.length);
|
|
121
|
+
const tsFilepath = path.join(folder, 'abis', canonicalName) + '.ts';
|
|
122
|
+
const folderPath = path.dirname(tsFilepath);
|
|
123
|
+
ensureDirExistsSync(folderPath);
|
|
124
|
+
if (mode === 'typescript') {
|
|
125
|
+
const newContent = `import {${artifactName}} from '${relativePath}artifacts/${canonicalName}.js';
|
|
126
|
+
export type ${abiName} = (typeof ${artifactName})['abi'];\n`;
|
|
127
|
+
writeIfDifferent(tsFilepath, newContent);
|
|
128
|
+
} else if (mode === 'javascript') {
|
|
129
|
+
const jsFilepath = path.join(folder, 'abis', canonicalName) + '.js';
|
|
130
|
+
const newContent = `export {};\n`;
|
|
131
|
+
const dtsContent = `import {${artifactName}} from '${relativePath}artifacts/${canonicalName}.js';
|
|
132
|
+
export type ${abiName} = (typeof ${artifactName})['abi'];\n`;
|
|
133
|
+
writeIfDifferent(jsFilepath, newContent);
|
|
134
|
+
writeIfDifferent(jsFilepath.replace(/\.js$/, '.d.ts'), dtsContent);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function writeABIDefinitionIndexToFile(folder: string, data: Artifacts, mode: 'typescript' | 'javascript') {
|
|
138
|
+
const tsFilepath = path.join(folder, 'abis', 'index') + '.ts';
|
|
139
|
+
const folderPath = path.dirname(tsFilepath);
|
|
140
|
+
ensureDirExistsSync(folderPath);
|
|
141
|
+
if (mode === 'typescript') {
|
|
142
|
+
let newContent = '';
|
|
143
|
+
for (const canonicalName of Object.keys(data)) {
|
|
144
|
+
const transformedName = canonicalName.replaceAll('/', '_').replaceAll('.', '_');
|
|
145
|
+
const name = canonicalName.split('/').pop();
|
|
146
|
+
const abiName = `Abi_${name}`;
|
|
147
|
+
const importNaming = canonicalName != name ? `${abiName} as ${transformedName}` : `${abiName} as ${name}`;
|
|
148
|
+
newContent += `export {${importNaming}} from "./${canonicalName}.js"\n`;
|
|
149
|
+
}
|
|
150
|
+
writeIfDifferent(tsFilepath, newContent);
|
|
151
|
+
} else if (mode === 'javascript') {
|
|
152
|
+
const jsFilepath = path.join(folder, 'abis', 'index') + '.js';
|
|
153
|
+
let newContent = '';
|
|
154
|
+
for (const canonicalName of Object.keys(data)) {
|
|
155
|
+
const transformedName = canonicalName.replaceAll('/', '_').replaceAll('.', '_');
|
|
156
|
+
const name = canonicalName.split('/').pop();
|
|
157
|
+
const abiName = `Abi_${name}`;
|
|
158
|
+
const importNaming = canonicalName != name ? `${abiName} as ${transformedName}` : `${abiName} as ${name}`;
|
|
159
|
+
newContent += `export {${importNaming}} from "./${canonicalName}.js"\n`;
|
|
160
|
+
}
|
|
161
|
+
writeIfDifferent(jsFilepath, newContent);
|
|
162
|
+
writeIfDifferent(jsFilepath.replace(/\.js$/, '.d.ts'), newContent);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function generateTypes(paths: {artifacts: string[]}, config: ArtifactGenerationConfig): Promise<void> {
|
|
167
|
+
const buildInfoCache = new Map<string, any>();
|
|
168
|
+
const allArtifacts: {[name: string]: any} = {};
|
|
169
|
+
const shortNameDict: {[shortName: string]: boolean} = {};
|
|
170
|
+
|
|
171
|
+
for (const artifactsPath of paths.artifacts) {
|
|
172
|
+
const files: FileTraversed[] = traverse(
|
|
173
|
+
artifactsPath,
|
|
174
|
+
[],
|
|
175
|
+
artifactsPath,
|
|
176
|
+
(name) => name != 'build-info' && !name.endsWith('.t.sol') && !name.endsWith('.dbg.json'),
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
// console.log('--------------------------');
|
|
180
|
+
// console.log(files);
|
|
181
|
+
// console.log('--------------------------');
|
|
182
|
+
|
|
183
|
+
for (const file of files) {
|
|
184
|
+
const filepath = file.path;
|
|
185
|
+
if (file.directory || !filepath.endsWith('.json')) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const filename = slash(path.basename(filepath));
|
|
189
|
+
const dirname = slash(path.dirname(file.relativePath));
|
|
190
|
+
|
|
191
|
+
// const namePath = dirname.replace('.sol', '');
|
|
192
|
+
const contractName = filename.replace('.json', '');
|
|
193
|
+
// const shortName = artifact.artifactsEmitted[i];
|
|
194
|
+
// console.log({path: filepath});
|
|
195
|
+
const content = fs.readFileSync(filepath, 'utf-8');
|
|
196
|
+
const parsed = JSON.parse(content);
|
|
197
|
+
|
|
198
|
+
if (!parsed.buildInfoId) continue;
|
|
199
|
+
|
|
200
|
+
// TODO read config for artifacts folder
|
|
201
|
+
let buildInfoFilepath = path.join(artifactsPath, 'build-info', `${parsed.buildInfoId}.output.json`);
|
|
202
|
+
|
|
203
|
+
if (!parsed.buildInfoId) {
|
|
204
|
+
// support hardhat v2 artifacts files
|
|
205
|
+
if (fs.existsSync(filepath.replace('.json', '.dbg.json'))) {
|
|
206
|
+
// console.warn(`Artifact ${filepath} does not have a buildInfoId, but found a .dbg.json file. Using that instead.`);
|
|
207
|
+
const dbgContent = fs.readFileSync(filepath.replace('.json', '.dbg.json'), 'utf-8');
|
|
208
|
+
const dbgParsed = JSON.parse(dbgContent);
|
|
209
|
+
const buildInfoRelativePath = dbgParsed.buildInfo;
|
|
210
|
+
parsed.buildInfoId = path.basename(buildInfoRelativePath, '.json');
|
|
211
|
+
// console.log({buildInfoRelativePath, buildInfoId: parsed.buildInfoId});
|
|
212
|
+
buildInfoFilepath = path.join(artifactsPath, 'build-info', `${parsed.buildInfoId}.json`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// const backupBuildInfoFilepath = path.join(
|
|
217
|
+
// './generated',
|
|
218
|
+
// buildInfoFilepath.slice(buildInfoFilepath.indexOf('/', 1))
|
|
219
|
+
// );
|
|
220
|
+
let buildInfoFilepathToUse = buildInfoFilepath;
|
|
221
|
+
// if (!fs.existsSync(buildInfoFilepathToUse)) {
|
|
222
|
+
// buildInfoFilepathToUse = backupBuildInfoFilepath;
|
|
223
|
+
// }
|
|
224
|
+
let parsedBuildInfo;
|
|
225
|
+
if (!buildInfoCache.has(buildInfoFilepathToUse)) {
|
|
226
|
+
if (!fs.existsSync(buildInfoFilepathToUse)) continue;
|
|
227
|
+
const buildInfoContent = fs.readFileSync(buildInfoFilepathToUse, 'utf-8');
|
|
228
|
+
parsedBuildInfo = JSON.parse(buildInfoContent);
|
|
229
|
+
buildInfoCache.set(buildInfoFilepathToUse, parsedBuildInfo);
|
|
230
|
+
} else {
|
|
231
|
+
parsedBuildInfo = buildInfoCache.get(buildInfoFilepathToUse);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const solidityOutput = parsedBuildInfo.output.contracts[parsed.inputSourceName][contractName];
|
|
235
|
+
const hardhatArtifactObject = {...parsed, ...solidityOutput};
|
|
236
|
+
const {buildInfoId, _format, ...artifactObject} = hardhatArtifactObject;
|
|
237
|
+
const fullName = `${dirname}/${contractName}`;
|
|
238
|
+
allArtifacts[fullName] = artifactObject;
|
|
239
|
+
if (shortNameDict[contractName]) {
|
|
240
|
+
delete allArtifacts[contractName];
|
|
241
|
+
} else {
|
|
242
|
+
allArtifacts[contractName] = artifactObject;
|
|
243
|
+
shortNameDict[contractName] = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
for (const key of Object.keys(allArtifacts)) {
|
|
249
|
+
if (key.indexOf('/') >= 0) {
|
|
250
|
+
const split = key.split('/');
|
|
251
|
+
if (split.length > 1) {
|
|
252
|
+
const shortName = split[split.length - 1];
|
|
253
|
+
if (allArtifacts[shortName]) {
|
|
254
|
+
delete allArtifacts[key];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// for (const key of Object.keys(allArtifacts)) {
|
|
261
|
+
// const artifact = allArtifacts[key];
|
|
262
|
+
// writeFiles(key, artifact, config);
|
|
263
|
+
// }
|
|
264
|
+
// // const json = hre.config.generateTypedArtifacts.json || [];
|
|
265
|
+
// // json.push('./generated/_artifacts.json');
|
|
266
|
+
// // writeFiles(undefined, allArtifacts, {...hre.config.generateTypedArtifacts, json: json});
|
|
267
|
+
|
|
268
|
+
// writeFiles(undefined, allArtifacts, config);
|
|
269
|
+
|
|
270
|
+
for (const destination of config.destinations) {
|
|
271
|
+
const generatedFolder = destination.folder;
|
|
272
|
+
const mode = destination.mode;
|
|
273
|
+
for (const key of Object.keys(allArtifacts)) {
|
|
274
|
+
writeABIDefinitionToFile(generatedFolder, key, allArtifacts[key], mode);
|
|
275
|
+
writeArtifactToFile(generatedFolder, key, allArtifacts[key], mode);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
writeArtifactIndexToFile(generatedFolder, allArtifacts, mode);
|
|
279
|
+
writeABIDefinitionIndexToFile(generatedFolder, allArtifacts, mode);
|
|
280
|
+
}
|
|
281
|
+
}
|