diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de85be8c9a..0d2679a695 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1517,7 +1517,7 @@ importers: version: 5.3.0 debug: specifier: ^4.1.1 - version: 4.3.7(supports-color@8.1.1) + version: 4.3.7 enquirer: specifier: ^2.3.0 version: 2.4.1 @@ -1533,9 +1533,6 @@ importers: semver: specifier: ^7.6.3 version: 7.6.3 - solc: - specifier: ^0.8.27 - version: 0.8.28(debug@4.3.7) tsx: specifier: ^4.11.0 version: 4.19.2 @@ -6372,11 +6369,6 @@ packages: engines: {node: '>=10.0.0'} hasBin: true - solc@0.8.28: - resolution: {integrity: sha512-AFCiJ+b4RosyyNhnfdVH4ZR1+TxiL91iluPjw0EJslIu4LXGM9NYqi2z5y8TqochC4tcH9QsHfwWhOIC9jPDKA==} - engines: {node: '>=10.0.0'} - hasBin: true - solidity-coverage@0.8.13: resolution: {integrity: sha512-RiBoI+kF94V3Rv0+iwOj3HQVSqNzA9qm/qDP1ZDXK5IX0Cvho1qiz8hAXTsAo6KOIUeP73jfscq0KlLqVxzGWA==} hasBin: true @@ -7433,7 +7425,7 @@ snapshots: '@eslint/eslintrc@2.1.4': dependencies: ajv: 6.12.6 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 espree: 9.6.1 globals: 13.24.0 ignore: 5.3.2 @@ -7734,7 +7726,7 @@ snapshots: '@humanwhocodes/config-array@0.11.14': dependencies: '@humanwhocodes/object-schema': 2.0.3 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 minimatch: 3.1.2 transitivePeerDependencies: - supports-color @@ -8487,7 +8479,7 @@ snapshots: '@typescript-eslint/type-utils': 7.7.1(eslint@8.57.0)(typescript@5.5.4) '@typescript-eslint/utils': 7.7.1(eslint@8.57.0)(typescript@5.5.4) '@typescript-eslint/visitor-keys': 7.7.1 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 eslint: 8.57.0 graphemer: 1.4.0 ignore: 5.3.2 @@ -8517,7 +8509,7 @@ snapshots: '@typescript-eslint/types': 7.18.0 '@typescript-eslint/typescript-estree': 7.18.0(typescript@5.5.4) '@typescript-eslint/visitor-keys': 7.18.0 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 eslint: 8.57.0 optionalDependencies: typescript: 5.5.4 @@ -8530,7 +8522,7 @@ snapshots: '@typescript-eslint/types': 7.7.1 '@typescript-eslint/typescript-estree': 7.7.1(typescript@5.5.4) '@typescript-eslint/visitor-keys': 7.7.1 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 eslint: 8.57.0 optionalDependencies: typescript: 5.5.4 @@ -8573,7 +8565,7 @@ snapshots: dependencies: '@typescript-eslint/typescript-estree': 7.18.0(typescript@5.5.4) '@typescript-eslint/utils': 7.18.0(eslint@8.57.0)(typescript@5.5.4) - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 eslint: 8.57.0 ts-api-utils: 1.3.0(typescript@5.5.4) optionalDependencies: @@ -8585,7 +8577,7 @@ snapshots: dependencies: '@typescript-eslint/typescript-estree': 7.7.1(typescript@5.5.4) '@typescript-eslint/utils': 7.7.1(eslint@8.57.0)(typescript@5.5.4) - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 eslint: 8.57.0 ts-api-utils: 1.3.0(typescript@5.5.4) optionalDependencies: @@ -8633,7 +8625,7 @@ snapshots: dependencies: '@typescript-eslint/types': 7.18.0 '@typescript-eslint/visitor-keys': 7.18.0 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 globby: 11.1.0 is-glob: 4.0.3 minimatch: 9.0.5 @@ -8648,7 +8640,7 @@ snapshots: dependencies: '@typescript-eslint/types': 7.7.1 '@typescript-eslint/visitor-keys': 7.7.1 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 globby: 11.1.0 is-glob: 4.0.3 minimatch: 9.0.5 @@ -8779,7 +8771,7 @@ snapshots: agent-base@6.0.2: dependencies: - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 transitivePeerDependencies: - supports-color @@ -9351,6 +9343,10 @@ snapshots: dependencies: ms: 2.1.3 + debug@4.3.7: + dependencies: + ms: 2.1.3 + debug@4.3.7(supports-color@8.1.1): dependencies: ms: 2.1.3 @@ -9652,7 +9648,7 @@ snapshots: eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@7.18.0(eslint@8.57.0)(typescript@5.5.4))(eslint-plugin-import@2.29.1)(eslint@8.57.0): dependencies: '@nolyfill/is-core-module': 1.0.39 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 enhanced-resolve: 5.17.1 eslint: 8.57.0 eslint-module-utils: 2.12.0(@typescript-eslint/parser@7.18.0(eslint@8.57.0)(typescript@5.5.4))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.0) @@ -9818,7 +9814,7 @@ snapshots: ajv: 6.12.6 chalk: 4.1.2 cross-spawn: 7.0.3 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 doctrine: 3.0.0 escape-string-regexp: 4.0.0 eslint-scope: 7.2.2 @@ -10419,7 +10415,7 @@ snapshots: https-proxy-agent@5.0.1: dependencies: agent-base: 6.0.2 - debug: 4.3.7(supports-color@8.1.1) + debug: 4.3.7 transitivePeerDependencies: - supports-color @@ -11594,18 +11590,6 @@ snapshots: transitivePeerDependencies: - debug - solc@0.8.28(debug@4.3.7): - dependencies: - command-exists: 1.2.9 - commander: 8.3.0 - follow-redirects: 1.15.9(debug@4.3.7) - js-sha3: 0.8.0 - memorystream: 0.3.1 - semver: 5.7.2 - tmp: 0.0.33 - transitivePeerDependencies: - - debug - solidity-coverage@0.8.13(hardhat@packages+hardhat-core): dependencies: '@ethersproject/abi': 5.7.0 diff --git a/v-next/hardhat/package.json b/v-next/hardhat/package.json index f1d35776d2..7de30ed8aa 100644 --- a/v-next/hardhat/package.json +++ b/v-next/hardhat/package.json @@ -98,7 +98,6 @@ "micro-eth-signer": "^0.13.0", "p-map": "^7.0.2", "semver": "^7.6.3", - "solc": "^0.8.27", "tsx": "^4.11.0", "ws": "^8.18.0", "zod": "^3.23.8" diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-runner.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-runner.ts index 51e215e106..0809495537 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-runner.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-runner.ts @@ -14,9 +14,7 @@ async function readStream( } async function getSolcJs(solcJsPath: string) { - /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- - We cast to string because it doesn't have types, and otherwise TS complains */ - const { default: solcWrapper } = await import("solc/wrapper.js" as string); + const { default: solcWrapper } = await import("./solcjs-wrapper.js"); const { default: solc } = await import(solcJsPath); return solcWrapper(solc); @@ -28,7 +26,8 @@ async function main() { const solcjsPath = process.argv[2]; const solc = await getSolcJs(solcjsPath); - const output = solc.compile(input); + // 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); console.log(output); } diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-wrapper.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-wrapper.ts new file mode 100755 index 0000000000..ae85557339 --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/compiler/solcjs-wrapper.ts @@ -0,0 +1,139 @@ +// 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 * as semver from "semver"; + +interface Solc { + cwrap(ident: string, returnType: string | null, argTypes: string[]): T; + + // eslint-disable-next-line @typescript-eslint/naming-convention -- this is a C function + _solidity_reset?: Reset | null; + // eslint-disable-next-line @typescript-eslint/naming-convention -- this is a C function + _solidity_version?: Version | null; + // eslint-disable-next-line @typescript-eslint/naming-convention -- this is a C function + _version?: Version | null; + // eslint-disable-next-line @typescript-eslint/naming-convention -- this is a C function + _compileStandard?: Compile | null; + // eslint-disable-next-line @typescript-eslint/naming-convention -- this is a C function + _solidity_compile?: Compile | null; +} + +type Reset = () => string; +type Version = () => string; +type Compile = ( + input: string, + callbackPtr: number | null, + callbackContextPtr?: null, +) => string; + +export interface SolcWrapper { + compile: CompileWrapper; +} + +export type CompileWrapper = (input: string) => string; + +export default function wrapper(solc: Solc): SolcWrapper { + const version = bindVersion(solc); + 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', + ); + } + + return { + compile: compileWrapper(isVersion6OrNewer, compile, reset), + }; +} + +function compileWrapper( + isVersion6OrNewer: boolean, + compile: Compile, + reset?: Reset, +): CompileWrapper { + return (input: string): string => { + const output = isVersion6OrNewer + ? compile(input, null, null) + : compile(input, null); + + if (reset !== undefined) { + // Explicitly free memory. + // + // NOTE: cwrap() of "compile" will copy the returned pointer into a + // Javascript string and it is not possible to call free() on it. + // reset() however will clear up all allocations. + reset(); + } + + return output; + }; +} + +function bindVersion(solc: Solc): Version { + if (solc._solidity_version === null || solc._solidity_version === undefined) { + return solc.cwrap("version", "string", []); + } + + return solc.cwrap("solidity_version", "string", []); +} + +function bindReset(solc: Solc): Reset | undefined { + if (solc._solidity_reset === null || solc._solidity_reset === undefined) { + return undefined; + } + + return solc.cwrap("solidity_reset", null, []); +} + +function bindCompile( + solc: Solc, + isVersion6OrNewer: boolean, +): CompileWrapper | undefined { + if (isVersion6OrNewer) { + if ( + solc._solidity_compile !== null && + solc._solidity_compile !== undefined + ) { + return solc.cwrap("solidity_compile", "string", [ + "string", + "number", + "number", + ]); + } + } else { + if ( + solc._solidity_compile !== null && + solc._solidity_compile !== undefined + ) { + return solc.cwrap("solidity_compile", "string", ["string", "number"]); + } + if (solc._compileStandard !== null && solc._compileStandard !== undefined) { + return solc.cwrap("compileStandard", "string", ["string", "number"]); + } + } + + return undefined; +} + +function versionToSemver(version: string): string { + // FIXME: parse more detail, but this is a good start + const parsed = version.match( + /^([0-9]+\.[0-9]+\.[0-9]+)-([0-9a-f]{8})[/*].*$/, + ); + if (parsed !== null) { + return parsed[1] + "+commit." + parsed[2]; + } + if (version.indexOf("0.1.3-0") !== -1) { + return "0.1.3"; + } + if (version.indexOf("0.3.5-0") !== -1) { + return "0.3.5"; + } + // assume it is already semver compatible + return version; +}