igneum/tools/reference-apps/oracle/compile.mjs

35 lines
1.7 KiB
JavaScript

// Compiles the oracle contracts with solc-js and writes artifacts/<Name>.json ({abi, bytecode, deployedBytecode}).
import solc from 'solc';
import { readFileSync, writeFileSync, readdirSync, mkdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
const here = path.dirname(fileURLToPath(import.meta.url));
const dir = path.join(here, 'contracts');
const sources = {};
for (const f of readdirSync(dir)) if (f.endsWith('.sol')) sources[f] = { content: readFileSync(path.join(dir, f), 'utf8') };
const input = {
language: 'Solidity',
sources,
settings: {
optimizer: { enabled: true, runs: 200 },
evmVersion: 'cancun',
viaIR: true,
outputSelection: { '*': { '*': ['abi', 'evm.bytecode.object', 'evm.deployedBytecode.object'] } },
},
};
const out = JSON.parse(solc.compile(JSON.stringify(input)));
let failed = false;
for (const e of out.errors ?? []) { console.error(e.formattedMessage); if (e.severity === 'error') failed = true; }
if (failed) process.exit(1);
mkdirSync(path.join(here, 'artifacts'), { recursive: true });
const want = ['IgneumStateOracle', 'StubCertificateVerifier'];
for (const file of Object.keys(out.contracts)) {
for (const [name, c] of Object.entries(out.contracts[file])) {
if (!want.includes(name)) continue;
const art = { name, solc: solc.version(), abi: c.abi, bytecode: '0x' + c.evm.bytecode.object, deployedBytecode: '0x' + c.evm.deployedBytecode.object };
writeFileSync(path.join(here, 'artifacts', name + '.json'), JSON.stringify(art, null, 1) + '\n');
console.log(name, 'creation bytes', c.evm.bytecode.object.length / 2, 'runtime bytes', c.evm.deployedBytecode.object.length / 2);
}
}
console.log('solc', solc.version());