Skip to content

Commit

Permalink
Merge pull request #6073 from NomicFoundation/solcjs-compile-wrapper
Browse files Browse the repository at this point in the history
Replace solc dependency with a thin solidity_compile wrapper
  • Loading branch information
galargh authored Jan 3, 2025
2 parents a8c02e1 + 7652f97 commit 217c35f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 6 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,12 @@ This happens when your files require incompatible versions of solc or you haven'
Please check Hardhat's output for more details.`,
},
INVALID_SOLCJS_COMPILER: {
number: 1231,
messageTemplate: `A wasm version of solc {version} is invalid. The compile function is not available.`,
websiteTitle: "Invalid solcjs compiler",
websiteDescription: `Hardhat successfully downloaded a WASM version of solc {version} but it is invalid. The compile function is missing.`,
},
},
VIEM: {
NETWORK_NOT_FOUND: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { SolcWrapper } from "./solcjs-wrapper.js";

async function readStream(
stream: NodeJS.ReadStream,
encoding: BufferEncoding = "utf8",
) {
): Promise<string> {
stream.setEncoding(encoding);

return new Promise((resolve, reject) => {
Expand All @@ -13,7 +15,7 @@ async function readStream(
});
}

async function getSolcJs(solcJsPath: string) {
async function getSolcJs(solcJsPath: string): Promise<SolcWrapper> {
const { default: solcWrapper } = await import("./solcjs-wrapper.js");
const { default: solc } = await import(solcJsPath);

Expand All @@ -26,8 +28,7 @@ async function main() {
const solcjsPath = process.argv[2];
const solc = await getSolcJs(solcjsPath);

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- the input read from the stdin should be a string
const output = solc.compile(input as string);
const output = solc.compile(input);

console.log(output);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This wrapper was created by extracting the parts of the solc-js package
// (https://github.com/ethereum/solc-js) that we need to perform compilation.

import { HardhatError } from "@ignored/hardhat-vnext-errors";
import * as semver from "semver";

interface Solc {
Expand Down Expand Up @@ -34,14 +35,17 @@ export type CompileWrapper = (input: string) => string;

export default function wrapper(solc: Solc): SolcWrapper {
const version = bindVersion(solc);
const isVersion6OrNewer = semver.gte(versionToSemver(version()), "0.6.0");
const semverVersion = versionToSemver(version());
const isVersion6OrNewer = semver.gte(semverVersion, "0.6.0");
const reset = bindReset(solc);
const compile = bindCompile(solc, isVersion6OrNewer);

if (compile === undefined) {
// eslint-disable-next-line no-restricted-syntax -- should we use a HardhatError here?
throw new Error(
'Could not find the "compile" function in the solc library',
throw new HardhatError(
HardhatError.ERRORS.SOLIDITY.INVALID_SOLCJS_COMPILER,
{
version: version(),
},
);
}

Expand Down

0 comments on commit 217c35f

Please sign in to comment.