1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const linker_1 = __importDefault(require("./linker"));
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function versionToSemver(version) {
|
16 |
|
17 | const parsed = version.match(/^([0-9]+\.[0-9]+\.[0-9]+)-([0-9a-f]{8})[/*].*$/);
|
18 | if (parsed) {
|
19 | return parsed[1] + '+commit.' + parsed[2];
|
20 | }
|
21 | if (version.indexOf('0.1.3-0') !== -1) {
|
22 | return '0.1.3';
|
23 | }
|
24 | if (version.indexOf('0.3.5-0') !== -1) {
|
25 | return '0.3.5';
|
26 | }
|
27 |
|
28 | return version;
|
29 | }
|
30 | function translateErrors(ret, errors) {
|
31 | for (const error in errors) {
|
32 | let type = 'error';
|
33 | let extractType = /^(.*):(\d+):(\d+):(.*):/;
|
34 | extractType = extractType.exec(errors[error]);
|
35 | if (extractType) {
|
36 | type = extractType[4].trim();
|
37 | }
|
38 | else if (errors[error].indexOf(': Warning:')) {
|
39 | type = 'Warning';
|
40 | }
|
41 | else if (errors[error].indexOf(': Error:')) {
|
42 | type = 'Error';
|
43 | }
|
44 | ret.push({
|
45 | type: type,
|
46 | component: 'general',
|
47 | severity: (type === 'Warning') ? 'warning' : 'error',
|
48 | message: errors[error],
|
49 | formattedMessage: errors[error]
|
50 | });
|
51 | }
|
52 | }
|
53 | function translateGasEstimates(gasEstimates) {
|
54 | if (gasEstimates === null) {
|
55 | return 'infinite';
|
56 | }
|
57 | if (typeof gasEstimates === 'number') {
|
58 | return gasEstimates.toString();
|
59 | }
|
60 | const gasEstimatesTranslated = {};
|
61 | for (const func in gasEstimates) {
|
62 | gasEstimatesTranslated[func] = translateGasEstimates(gasEstimates[func]);
|
63 | }
|
64 | return gasEstimatesTranslated;
|
65 | }
|
66 | function translateJsonCompilerOutput(output, libraries) {
|
67 | const ret = {};
|
68 | ret.errors = [];
|
69 | let errors;
|
70 | if (output.error) {
|
71 | errors = [output.error];
|
72 | }
|
73 | else {
|
74 | errors = output.errors;
|
75 | }
|
76 | translateErrors(ret.errors, errors);
|
77 | ret.contracts = {};
|
78 | for (const contract in output.contracts) {
|
79 |
|
80 | const tmp = contract.match(/^((.*):)?([^:]+)$/);
|
81 | if (tmp.length !== 4) {
|
82 |
|
83 | return null;
|
84 | }
|
85 | let fileName = tmp[2];
|
86 | if (fileName === undefined) {
|
87 |
|
88 | fileName = '';
|
89 | }
|
90 | const contractName = tmp[3];
|
91 | const contractInput = output.contracts[contract];
|
92 | const gasEstimates = contractInput.gasEstimates;
|
93 | const translatedGasEstimates = {};
|
94 | if (gasEstimates.creation) {
|
95 | translatedGasEstimates.creation = {
|
96 | codeDepositCost: translateGasEstimates(gasEstimates.creation[1]),
|
97 | executionCost: translateGasEstimates(gasEstimates.creation[0])
|
98 | };
|
99 | }
|
100 | if (gasEstimates.internal) {
|
101 | translatedGasEstimates.internal = translateGasEstimates(gasEstimates.internal);
|
102 | }
|
103 | if (gasEstimates.external) {
|
104 | translatedGasEstimates.external = translateGasEstimates(gasEstimates.external);
|
105 | }
|
106 | const contractOutput = {
|
107 | abi: JSON.parse(contractInput.interface),
|
108 | metadata: contractInput.metadata,
|
109 | evm: {
|
110 | legacyAssembly: contractInput.assembly,
|
111 | bytecode: {
|
112 | object: contractInput.bytecode && linker_1.default.linkBytecode(contractInput.bytecode, libraries || {}),
|
113 | opcodes: contractInput.opcodes,
|
114 | sourceMap: contractInput.srcmap,
|
115 | linkReferences: contractInput.bytecode && linker_1.default.findLinkReferences(contractInput.bytecode)
|
116 | },
|
117 | deployedBytecode: {
|
118 | object: contractInput.runtimeBytecode && linker_1.default.linkBytecode(contractInput.runtimeBytecode, libraries || {}),
|
119 | sourceMap: contractInput.srcmapRuntime,
|
120 | linkReferences: contractInput.runtimeBytecode && linker_1.default.findLinkReferences(contractInput.runtimeBytecode)
|
121 | },
|
122 | methodIdentifiers: contractInput.functionHashes,
|
123 | gasEstimates: translatedGasEstimates
|
124 | }
|
125 | };
|
126 | if (!ret.contracts[fileName]) {
|
127 | ret.contracts[fileName] = {};
|
128 | }
|
129 | ret.contracts[fileName][contractName] = contractOutput;
|
130 | }
|
131 | const sourceMap = {};
|
132 | for (const sourceId in output.sourceList) {
|
133 | sourceMap[output.sourceList[sourceId]] = sourceId;
|
134 | }
|
135 | ret.sources = {};
|
136 | for (const source in output.sources) {
|
137 | ret.sources[source] = {
|
138 | id: sourceMap[source],
|
139 | legacyAST: output.sources[source].AST
|
140 | };
|
141 | }
|
142 | return ret;
|
143 | }
|
144 | function escapeString(text) {
|
145 | return text
|
146 | .replace(/\n/g, '\\n')
|
147 | .replace(/\r/g, '\\r')
|
148 | .replace(/\t/g, '\\t');
|
149 | }
|
150 |
|
151 | function formatAssemblyText(asm, prefix, source) {
|
152 | if (typeof asm === 'string' || asm === null || asm === undefined) {
|
153 | return prefix + (asm || '') + '\n';
|
154 | }
|
155 | let text = prefix + '.code\n';
|
156 | asm['.code'].forEach(function (item, i) {
|
157 | const v = item.value === undefined ? '' : item.value;
|
158 | let src = '';
|
159 | if (source !== undefined && item.begin !== undefined && item.end !== undefined) {
|
160 | src = escapeString(source.slice(item.begin, item.end));
|
161 | }
|
162 | if (src.length > 30) {
|
163 | src = src.slice(0, 30) + '...';
|
164 | }
|
165 | if (item.name !== 'tag') {
|
166 | text += ' ';
|
167 | }
|
168 | text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n';
|
169 | });
|
170 | text += prefix + '.data\n';
|
171 | const asmData = asm['.data'] || [];
|
172 | for (const i in asmData) {
|
173 | const item = asmData[i];
|
174 | text += ' ' + prefix + '' + i + ':\n';
|
175 | text += formatAssemblyText(item, prefix + ' ', source);
|
176 | }
|
177 | return text;
|
178 | }
|
179 | function prettyPrintLegacyAssemblyJSON(assembly, source) {
|
180 | return formatAssemblyText(assembly, '', source);
|
181 | }
|
182 | module.exports = {
|
183 | versionToSemver,
|
184 | translateJsonCompilerOutput,
|
185 | prettyPrintLegacyAssemblyJSON
|
186 | };
|