Skip to content

Commit

Permalink
chore: move common helpers from solidity-test to solidity plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Jan 7, 2025
1 parent 18dca31 commit c882e69
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,102 +1,10 @@
import type {
Artifact as HardhatArtifact,
BuildInfo,
} from "../../../types/artifacts.js";
import type {
CompilationJobCreationError,
FailedFileBuildResult,
FileBuildResult,
} from "../../../types/solidity/build-system.js";
import type {
ArtifactId as EdrArtifactId,
Artifact as EdrArtifact,
} from "@ignored/edr";

import path from "node:path";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs";

import { FileBuildResultType } from "../../../types/solidity/build-system.js";

type SolidityBuildResults =
| Map<string, FileBuildResult>
| CompilationJobCreationError;
type SuccessfulSolidityBuildResults = Map<
string,
Exclude<FileBuildResult, FailedFileBuildResult>
>;

/**
* This function asserts that the given Solidity build results are successful.
* It throws a HardhatError if the build results indicate that the compilation
* job failed.
*/
export function throwIfSolidityBuildFailed(
results: SolidityBuildResults,
): asserts results is SuccessfulSolidityBuildResults {
if ("reason" in results) {
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR,
{
reason: results.formattedReason,
rootFilePath: results.rootFilePath,
buildProfile: results.buildProfile,
},
);
}

const sucessful = [...results.values()].every(
({ type }) =>
type === FileBuildResultType.CACHE_HIT ||
type === FileBuildResultType.BUILD_SUCCESS,
);

if (!sucessful) {
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED);
}
}

/**
* This function returns the artifacts generated during the compilation associated
* with the given Solidity build results. It relies on the fact that each successful
* build result has a corresponding artifact generated property.
*/
export async function getArtifacts(
results: SuccessfulSolidityBuildResults,
artifactsRootPath: string,
): Promise<EdrArtifact[]> {
const artifacts: EdrArtifact[] = [];

for (const [source, result] of results.entries()) {
for (const artifactPath of result.contractArtifactsGenerated) {
const artifact: HardhatArtifact = await readJsonFile(artifactPath);
const buildInfo: BuildInfo = await readJsonFile(
path.join(artifactsRootPath, "build-info", `${result.buildId}.json`),
);

const id = {
name: artifact.contractName,
solcVersion: buildInfo.solcVersion,
source,
};

const contract = {
abi: JSON.stringify(artifact.abi),
bytecode: artifact.bytecode,
deployedBytecode: artifact.deployedBytecode,
};

artifacts.push({
id,
contract,
});
}
}

return artifacts;
}

/**
* This function returns the test suite ids associated with the given artifacts.
* The test suite ID is the relative path of the test file, relative to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { getAllFilesMatching } from "@ignored/hardhat-vnext-utils/fs";
import { createNonClosingWriter } from "@ignored/hardhat-vnext-utils/stream";

import { shouldMergeCompilationJobs } from "../solidity/build-profiles.js";

import {
getArtifacts,
getTestSuiteIds,
throwIfSolidityBuildFailed,
} from "./helpers.js";
} from "../solidity/build-results.js";

import { getTestSuiteIds } from "./helpers.js";
import { testReporter } from "./reporter.js";
import { run } from "./runner.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type {
Artifact as HardhatArtifact,
BuildInfo,
} from "../../../types/artifacts.js";
import type { Artifact as EdrArtifact } from "@ignored/edr";

import path from "node:path";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs";

import {
FileBuildResultType,
type CompilationJobCreationError,
type FailedFileBuildResult,
type FileBuildResult,
} from "../../../types/solidity.js";

type SolidityBuildResults =
| Map<string, FileBuildResult>
| CompilationJobCreationError;
type SuccessfulSolidityBuildResults = Map<
string,
Exclude<FileBuildResult, FailedFileBuildResult>
>;

/**
* This function asserts that the given Solidity build results are successful.
* It throws a HardhatError if the build results indicate that the compilation
* job failed.
*/
export function throwIfSolidityBuildFailed(
results: SolidityBuildResults,
): asserts results is SuccessfulSolidityBuildResults {
if ("reason" in results) {
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR,
{
reason: results.formattedReason,
rootFilePath: results.rootFilePath,
buildProfile: results.buildProfile,
},
);
}

const sucessful = [...results.values()].every(
({ type }) =>
type === FileBuildResultType.CACHE_HIT ||
type === FileBuildResultType.BUILD_SUCCESS,
);

if (!sucessful) {
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED);
}
}

/**
* This function returns the artifacts generated during the compilation associated
* with the given Solidity build results. It relies on the fact that each successful
* build result has a corresponding artifact generated property.
*/
export async function getArtifacts(
results: SuccessfulSolidityBuildResults,
artifactsRootPath: string,
): Promise<EdrArtifact[]> {
const artifacts: EdrArtifact[] = [];

for (const [source, result] of results.entries()) {
for (const artifactPath of result.contractArtifactsGenerated) {
const artifact: HardhatArtifact = await readJsonFile(artifactPath);
const buildInfo: BuildInfo = await readJsonFile(
path.join(artifactsRootPath, "build-info", `${result.buildId}.json`),
);

const id = {
name: artifact.contractName,
solcVersion: buildInfo.solcVersion,
source,
};

const contract = {
abi: JSON.stringify(artifact.abi),
bytecode: artifact.bytecode,
deployedBytecode: artifact.deployedBytecode,
};

artifacts.push({
id,
contract,
});
}
}

return artifacts;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { NewTaskActionFunction } from "../../../../types/tasks.js";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path";

import { FileBuildResultType } from "../../../../types/solidity.js";
import { shouldMergeCompilationJobs } from "../build-profiles.js";
import { throwIfSolidityBuildFailed } from "../build-results.js";
import { isNpmRootPath } from "../build-system/root-paths-utils.js";

interface CompileActionArguments {
Expand Down Expand Up @@ -37,26 +36,7 @@ const compileAction: NewTaskActionFunction<CompileActionArguments> = async (
quiet,
});

if ("reason" in results) {
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR,
{
reason: results.formattedReason,
rootFilePath: results.rootFilePath,
buildProfile: results.buildProfile,
},
);
}

const sucessful = [...results.values()].every(
({ type }) =>
type === FileBuildResultType.CACHE_HIT ||
type === FileBuildResultType.BUILD_SUCCESS,
);

if (!sucessful) {
throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED);
}
throwIfSolidityBuildFailed(results);

// If we recompiled the entire project we cleanup the artifacts
if (files.length === 0) {
Expand Down

0 comments on commit c882e69

Please sign in to comment.