0
# Solc
1
2
Solc provides JavaScript/TypeScript bindings for the Solidity compiler, enabling developers to compile Solidity smart contracts directly from Node.js applications and web browsers. It wraps the Emscripten-compiled Solidity compiler and offers both high-level and low-level APIs for compilation using the Standard JSON input/output format.
3
4
## Package Information
5
6
- **Package Name**: solc
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install solc`
10
11
## Core Imports
12
13
```typescript
14
import solc from "solc";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const solc = require("solc");
21
```
22
23
For specific utilities:
24
25
```typescript
26
import linker from "solc/linker";
27
import abi from "solc/abi";
28
import translate from "solc/translate";
29
import smtchecker from "solc/smtchecker";
30
import smtsolver from "solc/smtsolver";
31
```
32
33
## Basic Usage
34
35
```typescript
36
import solc from "solc";
37
38
// Standard JSON compilation
39
const input = {
40
language: 'Solidity',
41
sources: {
42
'Contract.sol': {
43
content: 'contract MyContract { function get() public pure returns (uint) { return 42; } }'
44
}
45
},
46
settings: {
47
outputSelection: {
48
'*': {
49
'*': ['*']
50
}
51
}
52
}
53
};
54
55
const output = JSON.parse(solc.compile(JSON.stringify(input)));
56
57
// Access compiled contract
58
console.log(output.contracts['Contract.sol']['MyContract'].evm.bytecode.object);
59
```
60
61
## Architecture
62
63
Solc is built around several key components:
64
65
- **High-Level API**: Standard JSON compilation interface with uniform interface across compiler versions
66
- **Low-Level API**: Direct access to compiler-specific interfaces for advanced use cases
67
- **Version Management**: Remote loading of different compiler versions from GitHub
68
- **Import Resolution**: Callback system for resolving contract dependencies and imports
69
- **SMT Integration**: Integration with SMT solvers for formal verification via SMTChecker
70
- **Bytecode Utilities**: Library linking and placeholder resolution for deployed contracts
71
- **CLI Tools**: Command-line interface for standalone compilation workflows
72
73
## Capabilities
74
75
### Core Compilation
76
77
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.
78
79
```typescript { .api }
80
function compile(input: string, callbacks?: CompilationCallbacks): string;
81
82
interface CompilationCallbacks {
83
import?: (path: string) => { contents: string } | { error: string };
84
smtSolver?: (query: string) => { contents: string } | { error: string };
85
}
86
```
87
88
[Core Compilation](./compilation.md)
89
90
### Version Management
91
92
Load specific compiler versions remotely and manage compiler instances with feature detection capabilities.
93
94
```typescript { .api }
95
function loadRemoteVersion(
96
versionString: string,
97
callback: (error: Error | null, solc?: any) => void
98
): void;
99
100
interface CompilerFeatures {
101
legacySingleInput: boolean;
102
multipleInputs: boolean;
103
importCallback: boolean;
104
nativeStandardJSON: boolean;
105
}
106
```
107
108
[Version Management](./version-management.md)
109
110
### Bytecode Linking
111
112
Link library addresses into compiled bytecode and analyze library dependencies with support for both legacy and modern placeholder formats.
113
114
```typescript { .api }
115
function linkBytecode(bytecode: string, libraries: LibraryAddresses): string;
116
function findLinkReferences(bytecode: string): LinkReferences;
117
118
interface LibraryAddresses {
119
[qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string };
120
}
121
```
122
123
[Bytecode Linking](./linking.md)
124
125
### ABI Utilities
126
127
Translate and update ABI definitions across different Solidity compiler versions to ensure compatibility.
128
129
```typescript { .api }
130
function update(compilerVersion: string, abi: any[]): any[];
131
```
132
133
[ABI Utilities](./abi-utilities.md)
134
135
### SMT Integration
136
137
Integration with external SMT solvers for formal verification through Solidity's SMTChecker with support for Z3, Eldarica, and cvc5.
138
139
```typescript { .api }
140
function smtCallback(
141
solverFunction: (query: string, solver?: any) => string,
142
solver?: any
143
): (query: string) => { contents: string } | { error: string };
144
145
const availableSolvers: Array<{
146
name: string;
147
command: string;
148
params: string;
149
}>;
150
```
151
152
[SMT Integration](./smt-integration.md)
153
154
### Translation Utilities
155
156
Legacy output translation and assembly formatting utilities for compatibility with older compiler versions and tooling.
157
158
```typescript { .api }
159
function translateJsonCompilerOutput(output: any, libraries?: any): any | null;
160
function prettyPrintLegacyAssemblyJSON(assembly: any, source?: string): string;
161
function versionToSemver(version: string): string;
162
```
163
164
[Translation Utilities](./utilities.md)
165
166
### Command Line Interface
167
168
Command-line tool (`solcjs`) for standalone Solidity compilation with project structure support and Standard JSON I/O.
169
170
```bash { .api }
171
solcjs [options] <files...>
172
173
Options:
174
--bin Output binary bytecode
175
--abi Output ABI specification
176
--standard-json Use Standard JSON I/O mode
177
--optimize Enable bytecode optimizer
178
--base-path <path> Root of project source tree
179
--include-path <paths...> Extra source directories
180
-o, --output-dir <dir> Output directory
181
```
182
183
[Command Line Interface](./cli.md)
184
185
## Types
186
187
```typescript { .api }
188
interface LibraryAddresses {
189
[qualifiedNameOrSourceUnit: string]: string | { [unqualifiedLibraryName: string]: string };
190
}
191
192
interface LinkReferences {
193
[libraryLabel: string]: Array<{ start: number, length: number }>;
194
}
195
196
interface CompilerInstance {
197
version(): string;
198
semver(): string;
199
license(): string;
200
compile(input: string, callbacks?: CompilationCallbacks): string;
201
loadRemoteVersion(version: string, callback: (err: Error | null, solc?: any) => void): void;
202
features: CompilerFeatures;
203
lowlevel: {
204
compileSingle: ((input: string, optimize: boolean) => string) | null;
205
compileMulti: ((input: string, optimize: boolean) => string) | null;
206
compileCallback: ((input: string, optimize: boolean, callback: any) => string) | null;
207
compileStandard: ((input: string, callback?: any) => string) | null;
208
};
209
}
210
```