0
# Translation Utilities
1
2
Legacy output translation and assembly formatting utilities for compatibility with older compiler versions and tooling.
3
4
## Capabilities
5
6
### Legacy Output Translation
7
8
Convert raw compiler output from older versions to Standard JSON format.
9
10
```typescript { .api }
11
/**
12
* Translate legacy compiler output to Standard JSON format
13
* @param output - Raw compiler output object from legacy versions
14
* @param libraries - Optional library addresses for bytecode linking
15
* @returns Translated output in Standard JSON format or null if translation fails
16
*/
17
function translateJsonCompilerOutput(output: any, libraries?: any): any | null;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import translate from "solc/translate";
24
25
// Translate legacy compiler output
26
const legacyOutput = {
27
contracts: {
28
'MyContract': {
29
bytecode: '608060405234801561001057...',
30
interface: '[{"type":"function","name":"test",...}]',
31
gasEstimates: { creation: [21000, 200] },
32
functionHashes: { "test()": "f8a8fd6d" }
33
}
34
},
35
errors: ['Warning: Unused variable'],
36
sourceList: ['Contract.sol']
37
};
38
39
const modernOutput = translate.translateJsonCompilerOutput(legacyOutput);
40
console.log(modernOutput.contracts['']['MyContract'].evm.bytecode.object);
41
```
42
43
### Legacy Assembly Formatting
44
45
Format old JSON assembly output into human-readable text format.
46
47
```typescript { .api }
48
/**
49
* Format legacy assembly JSON output for display
50
* @param assembly - Assembly JSON object or string
51
* @param source - Original source code string for context
52
* @returns Formatted assembly text
53
*/
54
function prettyPrintLegacyAssemblyJSON(assembly: any, source?: string): string;
55
```
56
57
**Usage Examples:**
58
59
```typescript
60
import translate from "solc/translate";
61
62
// Format assembly output
63
const assemblyJSON = {
64
'.code': [
65
{ name: 'PUSH1', value: '0x80' },
66
{ name: 'PUSH1', value: '0x40' },
67
{ name: 'MSTORE' }
68
],
69
'.data': {}
70
};
71
72
const sourceCode = 'contract Test { uint public value = 42; }';
73
const formattedAssembly = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode);
74
console.log(formattedAssembly);
75
```
76
77
### Version String Conversion
78
79
Convert old-style version strings to semver-compatible format.
80
81
```typescript { .api }
82
/**
83
* Convert old-style version strings to semver-compatible format
84
* @param version - Version string in any format
85
* @returns Semver-compatible version string
86
*/
87
function versionToSemver(version: string): string;
88
```
89
90
**Usage Examples:**
91
92
```typescript
93
import translate from "solc/translate";
94
95
// Convert various version formats
96
console.log(translate.versionToSemver("0.3.6-3fc68da5/Release-Emscripten/clang"));
97
// Output: "0.3.6+commit.3fc68da5"
98
99
console.log(translate.versionToSemver("0.1.3-0"));
100
// Output: "0.1.3"
101
102
console.log(translate.versionToSemver("0.4.5+commit.b318366e.Emscripten.clang"));
103
// Output: "0.4.5+commit.b318366e.Emscripten.clang" (already compatible)
104
```
105
106
## Legacy Compiler Support
107
108
These utilities are primarily used for supporting older compiler versions and legacy tooling:
109
110
### Output Format Evolution
111
112
Legacy compilers used different output formats:
113
114
**Legacy Format:**
115
```json
116
{
117
"contracts": {
118
"ContractName": {
119
"bytecode": "0x608060405234801561001057...",
120
"interface": "[{\"type\":\"function\",\"name\":\"test\"}]",
121
"gasEstimates": {
122
"creation": [21000, 200],
123
"external": { "test()": 500 }
124
}
125
}
126
}
127
}
128
```
129
130
**Modern Standard JSON:**
131
```json
132
{
133
"contracts": {
134
"filename.sol": {
135
"ContractName": {
136
"evm": {
137
"bytecode": {
138
"object": "0x608060405234801561001057...",
139
"linkReferences": {}
140
},
141
"gasEstimates": {
142
"creation": {
143
"executionCost": "21000",
144
"codeDepositCost": "200"
145
}
146
}
147
},
148
"abi": [{"type":"function","name":"test"}]
149
}
150
}
151
}
152
}
153
```
154
155
### Assembly Format Changes
156
157
Legacy assembly format used nested objects with `.code` and `.data` sections:
158
159
```typescript
160
const legacyAssembly = {
161
'.code': [
162
{ name: 'PUSH1', value: '0x80', begin: 0, end: 5 },
163
{ name: 'PUSH1', value: '0x40', begin: 5, end: 10 },
164
{ name: 'MSTORE', begin: 10, end: 15 }
165
],
166
'.data': {
167
'0': {
168
'.code': [...],
169
'.data': {}
170
}
171
}
172
};
173
```
174
175
The `prettyPrintLegacyAssemblyJSON` function converts this to readable text:
176
177
```
178
.code
179
PUSH1 0x80 contract Test { uint p...
180
PUSH1 0x40
181
MSTORE
182
.data
183
0:
184
.code
185
...
186
```
187
188
### Integration with Modern Workflows
189
190
```typescript
191
import solc from "solc";
192
import translate from "solc/translate";
193
194
// Working with legacy compiler output
195
function processLegacyContract(legacyOutput: any) {
196
// Translate to modern format
197
const modernOutput = translate.translateJsonCompilerOutput(legacyOutput);
198
199
if (!modernOutput) {
200
throw new Error('Failed to translate legacy output');
201
}
202
203
// Now compatible with modern tooling
204
return modernOutput;
205
}
206
207
// Version compatibility checking
208
function checkVersionCompatibility(versionString: string) {
209
const semverVersion = translate.versionToSemver(versionString);
210
const [major, minor] = semverVersion.split('.').map(Number);
211
212
if (major === 0 && minor < 4) {
213
console.log('Very old compiler version - may need special handling');
214
}
215
216
return semverVersion;
217
}
218
```