UNPKG

3.87 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const fs_extra_1 = __importDefault(require("fs-extra"));
14const path = __importStar(require("path"));
15const errors_1 = require("./core/errors");
16const errors_list_1 = require("./core/errors-list");
17/**
18 * Retrieves an artifact for the given `contractName` from the compilation output.
19 *
20 * @param contractName the contract's name.
21 * @param contractOutput the contract's compilation output as emitted by `solc`.
22 */
23function getArtifactFromContractOutput(contractName, contractOutput) {
24 const evmBytecode = contractOutput.evm && contractOutput.evm.bytecode;
25 let bytecode = evmBytecode && evmBytecode.object ? evmBytecode.object : "";
26 if (bytecode.slice(0, 2).toLowerCase() !== "0x") {
27 bytecode = `0x${bytecode}`;
28 }
29 const evmDeployedBytecode = contractOutput.evm && contractOutput.evm.deployedBytecode;
30 let deployedBytecode = evmDeployedBytecode && evmDeployedBytecode.object
31 ? evmDeployedBytecode.object
32 : "";
33 if (deployedBytecode.slice(0, 2).toLowerCase() !== "0x") {
34 deployedBytecode = `0x${deployedBytecode}`;
35 }
36 const linkReferences = evmBytecode && evmBytecode.linkReferences ? evmBytecode.linkReferences : {};
37 const deployedLinkReferences = evmDeployedBytecode && evmDeployedBytecode.linkReferences
38 ? evmDeployedBytecode.linkReferences
39 : {};
40 return {
41 contractName,
42 abi: contractOutput.abi,
43 bytecode,
44 deployedBytecode,
45 linkReferences,
46 deployedLinkReferences,
47 };
48}
49exports.getArtifactFromContractOutput = getArtifactFromContractOutput;
50function getArtifactPath(artifactsPath, contractName) {
51 return path.join(artifactsPath, `${contractName}.json`);
52}
53/**
54 * Stores an artifact in the given path.
55 *
56 * @param artifactsPath the artifacts' directory.
57 * @param artifact the artifact to be stored.
58 */
59async function saveArtifact(artifactsPath, artifact) {
60 await fs_extra_1.default.ensureDir(artifactsPath);
61 await fs_extra_1.default.writeJSON(path.join(artifactsPath, `${artifact.contractName}.json`), artifact, {
62 spaces: 2,
63 });
64}
65exports.saveArtifact = saveArtifact;
66/**
67 * Asynchronically reads an artifact with the given `contractName` from the given `artifactPath`.
68 *
69 * @param artifactsPath the artifacts' directory.
70 * @param contractName the contract's name.
71 */
72async function readArtifact(artifactsPath, contractName) {
73 const artifactPath = getArtifactPath(artifactsPath, contractName);
74 if (!fs_extra_1.default.pathExistsSync(artifactPath)) {
75 throw new errors_1.BuidlerError(errors_list_1.ERRORS.ARTIFACTS.NOT_FOUND, { contractName });
76 }
77 return fs_extra_1.default.readJson(artifactPath);
78}
79exports.readArtifact = readArtifact;
80/**
81 * Synchronically reads an artifact with the given `contractName` from the given `artifactPath`.
82 *
83 * @param artifactsPath the artifacts directory.
84 * @param contractName the contract's name.
85 */
86function readArtifactSync(artifactsPath, contractName) {
87 const artifactPath = getArtifactPath(artifactsPath, contractName);
88 if (!fs_extra_1.default.pathExistsSync(artifactPath)) {
89 throw new errors_1.BuidlerError(errors_list_1.ERRORS.ARTIFACTS.NOT_FOUND, { contractName });
90 }
91 return fs_extra_1.default.readJsonSync(artifactPath);
92}
93exports.readArtifactSync = readArtifactSync;
94//# sourceMappingURL=artifacts.js.map
\No newline at end of file