1 | import fsExtra from "fs-extra";
|
2 | import * as path from "path";
|
3 |
|
4 | import { Artifact } from "../types";
|
5 |
|
6 | import { BuidlerError } from "./core/errors";
|
7 | import { ERRORS } from "./core/errors-list";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export function getArtifactFromContractOutput(
|
16 | contractName: string,
|
17 | contractOutput: any
|
18 | ): Artifact {
|
19 | const evmBytecode = contractOutput.evm && contractOutput.evm.bytecode;
|
20 | let bytecode: string =
|
21 | evmBytecode && evmBytecode.object ? evmBytecode.object : "";
|
22 |
|
23 | if (bytecode.slice(0, 2).toLowerCase() !== "0x") {
|
24 | bytecode = `0x${bytecode}`;
|
25 | }
|
26 |
|
27 | const evmDeployedBytecode =
|
28 | contractOutput.evm && contractOutput.evm.deployedBytecode;
|
29 | let deployedBytecode: string =
|
30 | evmDeployedBytecode && evmDeployedBytecode.object
|
31 | ? evmDeployedBytecode.object
|
32 | : "";
|
33 |
|
34 | if (deployedBytecode.slice(0, 2).toLowerCase() !== "0x") {
|
35 | deployedBytecode = `0x${deployedBytecode}`;
|
36 | }
|
37 |
|
38 | const linkReferences =
|
39 | evmBytecode && evmBytecode.linkReferences ? evmBytecode.linkReferences : {};
|
40 | const deployedLinkReferences =
|
41 | evmDeployedBytecode && evmDeployedBytecode.linkReferences
|
42 | ? evmDeployedBytecode.linkReferences
|
43 | : {};
|
44 |
|
45 | return {
|
46 | contractName,
|
47 | abi: contractOutput.abi,
|
48 | bytecode,
|
49 | deployedBytecode,
|
50 | linkReferences,
|
51 | deployedLinkReferences,
|
52 | };
|
53 | }
|
54 |
|
55 | function getArtifactPath(artifactsPath: string, contractName: string): string {
|
56 | return path.join(artifactsPath, `${contractName}.json`);
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export async function saveArtifact(artifactsPath: string, artifact: Artifact) {
|
66 | await fsExtra.ensureDir(artifactsPath);
|
67 | await fsExtra.writeJSON(
|
68 | path.join(artifactsPath, `${artifact.contractName}.json`),
|
69 | artifact,
|
70 | {
|
71 | spaces: 2,
|
72 | }
|
73 | );
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export async function readArtifact(
|
83 | artifactsPath: string,
|
84 | contractName: string
|
85 | ): Promise<Artifact> {
|
86 | const artifactPath = getArtifactPath(artifactsPath, contractName);
|
87 |
|
88 | if (!fsExtra.pathExistsSync(artifactPath)) {
|
89 | throw new BuidlerError(ERRORS.ARTIFACTS.NOT_FOUND, { contractName });
|
90 | }
|
91 |
|
92 | return fsExtra.readJson(artifactPath);
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | export function readArtifactSync(
|
102 | artifactsPath: string,
|
103 | contractName: string
|
104 | ): Artifact {
|
105 | const artifactPath = getArtifactPath(artifactsPath, contractName);
|
106 |
|
107 | if (!fsExtra.pathExistsSync(artifactPath)) {
|
108 | throw new BuidlerError(ERRORS.ARTIFACTS.NOT_FOUND, { contractName });
|
109 | }
|
110 |
|
111 | return fsExtra.readJsonSync(artifactPath);
|
112 | }
|