-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbiTypeGen.ts
77 lines (65 loc) · 2.25 KB
/
AbiTypeGen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Abi } from './abi/Abi';
import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
import type { IFile } from './types/interfaces/IFile';
import { assembleContracts } from './utils/assembleContracts';
import { assemblePredicates } from './utils/assemblePredicates';
import { assembleScripts } from './utils/assembleScripts';
import { validateBinFile } from './utils/validateBinFile';
/*
Manages many instances of Abi
*/
export class AbiTypeGen {
public readonly abis: Abi[];
public readonly abiFiles: IFile[];
public readonly binFiles: IFile[];
public readonly outputDir: string;
public readonly files: IFile[];
constructor(params: {
abiFiles: IFile[];
binFiles: IFile[];
outputDir: string;
programType: ProgramTypeEnum;
}) {
const { abiFiles, binFiles, outputDir, programType } = params;
this.outputDir = outputDir;
this.abiFiles = abiFiles;
this.binFiles = binFiles;
// Creates a `Abi` for each abi file
this.abis = this.abiFiles.map((abiFile) => {
const binFilepath = abiFile.path.replace('-abi.json', '.bin');
const relatedBinFile = this.binFiles.find(({ path }) => path === binFilepath);
if (!relatedBinFile) {
validateBinFile({
abiFilepath: abiFile.path,
binExists: !!relatedBinFile,
binFilepath,
programType,
});
}
const abi = new Abi({
filepath: abiFile.path,
rawContents: JSON.parse(abiFile.contents as string),
hexlifiedBinContents: relatedBinFile?.contents,
outputDir,
programType,
});
return abi;
});
// Assemble list of files to be written to disk
this.files = this.getAssembledFiles({ programType });
}
private getAssembledFiles(params: { programType: ProgramTypeEnum }): IFile[] {
const { abis, outputDir } = this;
const { programType } = params;
switch (programType) {
case ProgramTypeEnum.CONTRACT:
return assembleContracts({ abis, outputDir });
case ProgramTypeEnum.SCRIPT:
return assembleScripts({ abis, outputDir });
case ProgramTypeEnum.PREDICATE:
return assemblePredicates({ abis, outputDir });
default:
throw new Error(`Invalid Typegen programType: ${programType}`);
}
}
}