-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move common helpers from solidity-test to solidity plugin
- Loading branch information
Showing
4 changed files
with
100 additions
and
117 deletions.
There are no files selected for viewing
92 changes: 0 additions & 92 deletions
92
v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
v-next/hardhat/src/internal/builtin-plugins/solidity/build-results.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters