import fs from 'node:fs/promises'; import path from 'node:path'; import glob from 'fast-glob'; import { NativeCompiler } from 'hardhat/internal/solidity/compiler'; import { CompilerDownloader } from 'hardhat/internal/solidity/compiler/downloader'; import { getCompilersDir } from 'hardhat/internal/util/global-dir'; import { CompilerInput, CompilerOutput as HardhatCompilerOutput } from 'hardhat/types'; import { version as CurrentSolcVersion } from 'solc/package.json'; import { SourceUnit } from 'solidity-ast'; export interface CompileParams { rootDir?: string; sources?: string | string[]; version?: string; } export interface CompilerOutput extends Omit { sources: { [sourceName: string]: { ast: SourceUnit; }; }; } export async function compileSolidityContents( contents: { [sourceName: string]: string }, version = CurrentSolcVersion ) { const compiler = await _getCompiler(version); const sources: { [sourceName: string]: { content: string } } = {}; for (const [sourceName, content] of Object.entries(contents)) { sources[sourceName] = { content }; } const input: CompilerInput = { language: 'Solidity', sources, settings: { optimizer: {}, outputSelection: { '*': { '': ['ast'] }, }, }, }; const output = await compiler.compile(input); if (output.errors && output.errors.some((x: { severity: string }) => x.severity === 'error')) { console.error(output.errors); throw new Error('There was an error during compilation'); } const sourceUnits = Object.values((output as CompilerOutput).sources).map(({ ast }) => ast); return sourceUnits.sort((a, b) => a.absolutePath > b.absolutePath ? 1 : a.absolutePath < b.absolutePath ? -1 : 0 ); } export async function compileSolidityFolder({ rootDir = process.cwd(), sources = '**/*.sol', version = CurrentSolcVersion, }: CompileParams) { const sourceNames = await glob(sources, { cwd: rootDir }); const contents: { [sourceName: string]: string } = {}; await Promise.all( sourceNames.map(async (sourceName) => { const file = path.resolve(rootDir, sourceName); const content = (await fs.readFile(file)).toString(); contents[sourceName] = content; }) ); return compileSolidityContents(contents, version); } async function _getCompiler(version: string) { const downloader = new CompilerDownloader( CompilerDownloader.getCompilerPlatform(), await getCompilersDir() ); const isDownloaded = await downloader.isCompilerDownloaded(version); if (!isDownloaded) { // NOTE: any is used here because on some people's computers, the `downloadCompiler` function comes out with different types (who knows why but its better to just not) // eslint-disable-next-line await (downloader as any).downloadCompiler( version, async () => {}, async () => {} ); } const buildInfo = await downloader.getCompiler(version); return new NativeCompiler(buildInfo!.compilerPath); }