UNPKG

3.17 kBPlain TextView Raw
1import fsExtra from "fs-extra";
2import * as path from "path";
3
4import { Artifact } from "../types";
5
6import { BuidlerError } from "./core/errors";
7import { ERRORS } from "./core/errors-list";
8
9/**
10 * Retrieves an artifact for the given `contractName` from the compilation output.
11 *
12 * @param contractName the contract's name.
13 * @param contractOutput the contract's compilation output as emitted by `solc`.
14 */
15export 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
55function getArtifactPath(artifactsPath: string, contractName: string): string {
56 return path.join(artifactsPath, `${contractName}.json`);
57}
58
59/**
60 * Stores an artifact in the given path.
61 *
62 * @param artifactsPath the artifacts' directory.
63 * @param artifact the artifact to be stored.
64 */
65export 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 * Asynchronically reads an artifact with the given `contractName` from the given `artifactPath`.
78 *
79 * @param artifactsPath the artifacts' directory.
80 * @param contractName the contract's name.
81 */
82export 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 * Synchronically reads an artifact with the given `contractName` from the given `artifactPath`.
97 *
98 * @param artifactsPath the artifacts directory.
99 * @param contractName the contract's name.
100 */
101export 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}