JavaScript bindings for the Solidity compiler with Standard JSON I/O interface
—
Primary compilation interface supporting Standard JSON I/O format with import callbacks and SMT solver integration. Provides both high-level unified API and low-level version-specific access.
Standard JSON compilation interface that works uniformly across all compiler versions.
/**
* Compile Solidity source code using Standard JSON I/O format
* @param input - Standard JSON input as string
* @param callbacks - Optional callbacks for import resolution and SMT solving
* @returns Standard JSON output as string
*/
function compile(input: string, callbacks?: CompilationCallbacks): string;
interface CompilationCallbacks {
/** Callback to resolve import statements */
import?: (path: string) => { contents: string } | { error: string };
/** Callback to solve SMT queries for formal verification */
smtSolver?: (query: string) => { contents: string } | { error: string };
}Usage Examples:
import solc from "solc";
// Basic compilation without callbacks
const input = {
language: 'Solidity',
sources: {
'Contract.sol': {
content: 'contract Test { uint public value = 42; }'
}
},
settings: {
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode']
}
}
}
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output.contracts['Contract.sol']['Test'].evm.bytecode.object);Handle contract dependencies and imports through callback resolution.
import solc from "solc";
import fs from "fs";
const input = {
language: 'Solidity',
sources: {
'Main.sol': {
content: 'import "./Library.sol"; contract Main { Library lib; }'
}
},
settings: {
outputSelection: {
'*': { '*': ['*'] }
}
}
};
function findImports(path) {
try {
return {
contents: fs.readFileSync(path, 'utf8')
};
} catch (e) {
return { error: 'File not found' };
}
}
const output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);Direct access to compiler-specific interfaces for advanced use cases and older compiler versions.
interface LowLevelAPI {
/** Legacy single file compilation (may be null for newer versions) */
compileSingle: ((input: string, optimize: boolean) => string) | null;
/** Legacy multi-file compilation (may be null for newer versions) */
compileMulti: ((input: string, optimize: boolean) => string) | null;
/** Legacy callback-based compilation (may be null for newer versions) */
compileCallback: ((input: string, optimize: boolean, callback: any) => string) | null;
/** Standard JSON compilation for newer versions */
compileStandard: ((input: string, callback?: any) => string) | null;
}Usage Examples:
import solc from "solc";
// Check feature availability
if (solc.lowlevel.compileStandard) {
// Use standard JSON interface directly
const result = solc.lowlevel.compileStandard(JSON.stringify(input));
} else if (solc.lowlevel.compileMulti) {
// Fallback to legacy multi-file interface
const result = solc.lowlevel.compileMulti(JSON.stringify({sources: {...}}), false);
}Detect available features and capabilities of the current compiler instance.
interface CompilerFeatures {
/** Support for legacy single input compilation */
legacySingleInput: boolean;
/** Support for multiple input files */
multipleInputs: boolean;
/** Support for import callback functions */
importCallback: boolean;
/** Native Standard JSON I/O support */
nativeStandardJSON: boolean;
}Access version and license information from the compiler instance.
/**
* Get the full version string of the compiler
* @returns Version string (e.g., "0.8.30+commit.12345678.Emscripten.clang")
*/
function version(): string;
/**
* Get semver-compatible version string
* @returns Semantic version string (e.g., "0.8.30+commit.12345678")
*/
function semver(): string;
/**
* Get compiler license information
* @returns License string
*/
function license(): string;interface StandardJSONInput {
language: "Solidity";
sources: {
[filename: string]: {
content: string;
};
};
settings?: {
optimizer?: {
enabled: boolean;
runs?: number;
};
outputSelection: {
[file: string]: {
[contract: string]: string[];
};
};
libraries?: {
[file: string]: {
[library: string]: string;
};
};
modelChecker?: {
engine: "chc" | "bmc";
solvers?: string[];
timeout?: number;
};
};
}interface StandardJSONOutput {
contracts?: {
[filename: string]: {
[contractName: string]: {
abi: any[];
metadata: string;
evm: {
bytecode: {
object: string;
opcodes: string;
sourceMap: string;
linkReferences: LinkReferences;
};
deployedBytecode: {
object: string;
sourceMap: string;
linkReferences: LinkReferences;
};
methodIdentifiers: { [signature: string]: string };
gasEstimates: {
creation: {
codeDepositCost: string;
executionCost: string;
};
external: { [method: string]: string };
internal: { [method: string]: string };
};
};
};
};
};
sources?: {
[filename: string]: {
id: number;
legacyAST: any;
};
};
errors?: Array<{
type: string;
component: string;
severity: "error" | "warning";
message: string;
formattedMessage: string;
sourceLocation?: {
file: string;
start: number;
end: number;
};
}>;
}Install with Tessl CLI
npx tessl i tessl/npm-solc