Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace solc dependency with a thin solidity_compile wrapper #6073

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading