import solc from 'solc'; import { readFileSync, writeFileSync } from 'fs'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); /** * @typedef {Object} ContractConfig * @property {string} name - The name of the contract * @property {string} main - The main contract file * @property {'Solidity' | 'Yul'} language - The language of the contract * @property {string[]} [dependencies] - Additional files to include (Solidity only) */ /** @type {ContractConfig[]} */ const contracts = [ { name: 'SimpleContract', main: 'SimpleContract.sol', language: 'Solidity', dependencies: [], }, { name: 'ComprehensiveContract', main: 'ComprehensiveContract.sol', language: 'Solidity', dependencies: ['lib/utils/MathUtils.sol'], }, { name: 'InheritedContract', main: 'InheritedContract.sol', language: 'Solidity', dependencies: ['ComprehensiveContract.sol', 'lib/utils/MathUtils.sol'], }, { name: 'SimpleYul', main: 'SimpleYul.yul', language: 'Yul', }, ]; const remappings = [ 'utils/=lib/utils/', '@openzeppelin/=../../../../node_modules/@openzeppelin/', ]; // Compile each contract for (const config of contracts) { /** @type {{[key: string]: {content: string}}} */ const sources = {}; // Add main contract sources[config.main] = { content: readFileSync(join(__dirname, config.main), 'utf8'), }; // Add dependencies (Solidity only) if (config.dependencies) { for (const dep of config.dependencies) { sources[dep] = { content: readFileSync(join(__dirname, dep), 'utf8'), }; } } const output = JSON.parse( solc.compile( JSON.stringify({ language: config.language, sources, settings: { outputSelection: { '*': { '*': ['*'], '': ['*'], }, }, ...(config.language === 'Solidity' && { remappings }), }, }), config.language === 'Solidity' ? { import: (/** @type {string} */ path) => ({ contents: readFileSync(join(__dirname, path), 'utf8'), }), } : undefined, ), ); const tsContent = `/// Auto-generated by compile.js\n// @ts-nocheck\nimport type { SolcOutput } from '@tevm/solc'\n\nconst solcOutput: SolcOutput = ${JSON.stringify( output, null, 2, )}\n\nexport default solcOutput\n`; writeFileSync( join(__dirname, `${config.name}.solcOutput.ts`), tsContent, 'utf8', ); console.log(`✓ Generated ${config.name}.solcOutput.ts`); // Extract AST for SimpleContract if (config.name === 'SimpleContract' && output.sources && output.sources[config.main]) { const ast = output.sources[config.main].ast; if (ast) { writeFileSync( join(__dirname, 'SimpleContractAst.json'), JSON.stringify(ast, null, 2), 'utf8', ); console.log(`✓ Generated SimpleContractAst.json`); } } } // Compile SimpleContractAst.json using SolidityAST language console.log('\n--- Compiling SimpleContractAst.json ---'); const astJson = JSON.parse(readFileSync(join(__dirname, 'SimpleContractAst.json'), 'utf8')); const astOutput = JSON.parse( solc.compile( JSON.stringify({ language: 'SolidityAST', sources: { 'SimpleContract.sol': { ast: astJson, }, }, settings: { outputSelection: { '*': { '*': ['*'], '': ['*'], }, }, }, }), ), ); const astTsContent = `/// Auto-generated by compile.js - compiled from AST\n// @ts-nocheck\nimport type { SolcOutput } from '@tevm/solc'\n\nconst solcOutput: SolcOutput = ${JSON.stringify( astOutput, null, 2, )}\n\nexport default solcOutput\n`; writeFileSync( join(__dirname, 'SimpleContractAst.solcOutput.ts'), astTsContent, 'utf8', ); console.log(`✓ Generated SimpleContractAst.solcOutput.ts`);