-
Notifications
You must be signed in to change notification settings - Fork 30
/
handler.js
446 lines (384 loc) · 17 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict'
/**
* @author github.com/tintinweb
* @license MIT
* */
/** IMPORT */
const path = require('path');
const solc = require('solc');
const { getRemoteCompiler } = require('./compiler/remoteCompiler.js');
const { readFileCallback } = require('./compiler/utils.js');
const { ExternalProcessBlockchain, ExternalUrlBlockchain, BuiltinGanacheBlockchain } = require('./blockchain.js');
/** CONST */
const rexTypeErrorReturnArgumentX = /Return argument type (.*) is not implicitly convertible to expected type \(type of first return variable\)/;
const rexAssign = /[^=]=[^=];?/;
const rexTypeDecl = /^([\w\[\]]+\s(memory|storage)?\s*\w+);?$/;
const rexUnits = /^(\d+\s*(wei|gwei|szabo|finney|ether|seconds|minutes|hours|days|weeks|years))\s*;?$/;
const IGNORE_WARNINGS = [
"Statement has no effect.",
"Function state mutability can be restricted to ",
"Unused local variable."
];
const TYPE_ERROR_DETECT_RETURNS = 'Different number of arguments in return statement than in returns declaration.';
const SCOPE = {
CONTRACT: 1, /* statement in contract scope */
SOURCE_UNIT: 2, /* statement in source unit scope */
MAIN: 4, /* statement in main function scope */
VERSION_PRAGMA: 5 /* statement is a solidity version pragma */
}
/** STATIC FUNC */
function getBestSolidityVersion(source) {
var rx = /^pragma solidity (\^?[^;]+);$/gm;
let allVersions = source.match(rx).map(e => {
try {
return e.match(/(\d+)\.(\d+)\.(\d+)/).splice(1, 3).map(a => parseInt(a))
} catch { }
})
let lastVersion = allVersions[allVersions.length - 1];
if (!lastVersion) {
return undefined;
}
return `^${lastVersion.join('.')}`;
}
/** CLASS */
class SolidityStatement {
constructor(rawCommand, scope) {
this.rawCommand = rawCommand ? rawCommand.trim() : "";
this.hasNoReturnValue = (rexAssign.test(this.rawCommand))
|| (this.rawCommand.startsWith('delete '))
|| (this.rawCommand.startsWith('assembly'))
|| (this.rawCommand.startsWith('revert'))
|| (this.rawCommand.startsWith('require('))
|| (this.rawCommand.startsWith('unchecked '))
|| (this.rawCommand.startsWith('{'))
|| (rexTypeDecl.test(this.rawCommand) && !rexUnits.test(this.rawCommand)) /* looks like type decl but is not special builtin like "2 ether" */
if (scope) {
this.scope = scope;
} else {
if (['function ', 'modifier ', 'mapping ', 'event ', 'error ', 'type '].some(e => this.rawCommand.startsWith(e))) {
this.scope = SCOPE.CONTRACT;
this.hasNoReturnValue = true;
} else if (this.rawCommand.startsWith('pragma solidity ')) {
this.scope = SCOPE.VERSION_PRAGMA;
this.hasNoReturnValue = true;
this.rawCommand = this.fixStatement(this.rawCommand);
} else if (['pragma ', 'import '].some(e => this.rawCommand.startsWith(e))) {
this.scope = SCOPE.SOURCE_UNIT;
this.hasNoReturnValue = true;
this.rawCommand = this.fixStatement(this.rawCommand);
} else if (['contract ', 'interface ', 'abstract', 'library', 'struct ', 'enum '].some(e => this.rawCommand.startsWith(e))) {
this.scope = SCOPE.SOURCE_UNIT;
this.hasNoReturnValue = true;
} else {
this.scope = SCOPE.MAIN;
this.rawCommand = this.fixStatement(this.rawCommand);
if (this.rawCommand === ';') {
this.hasNoReturnValue = true;
}
}
}
if (this.hasNoReturnValue) {
// expression
this.returnExpression = ';';
this.returnType = '';
} else {
// not an expression
this.returnExpression = this.rawCommand;
this.returnType = 'bool'
}
}
fixStatement(stm) {
return (stm.endsWith(';') || stm.endsWith('}')) ? stm : `${stm};`
}
toString() {
return this.rawCommand;
}
toList() {
return [this.rawCommand, this.scope]
}
}
class InteractiveSolidityShell {
constructor(settings, log) {
this.log = log || console.log;
const defaults = {
templateContractName: 'MainContract',
templateFuncMain: 'main',
installedSolidityVersion: null, // overridden after merging settings; never use configured value
providerUrl: 'http://127.0.0.1:8545',
autostartGanache: true,
blockchainProvider: 'internal',
ganacheOptions: {},
ganacheCmd: 'ganache-cli',
ganacheArgs: [/*'--gasLimit=999000000'*/], //optionally increase default gas limit
debugShowContract: false,
resolveHttpImports: true,
enableAutoComplete: true,
callGas: 3e6,
deployGas: 3e6,
etherscanApiKey: 'YourApiKeyToken'
}
this.settings = {
...defaults, ... (settings || {})
};
this.settings.installedSolidityVersion = require('../package.json').dependencies.solc.split("-", 1)[0];
this.cache = {
compiler: {} /** compilerVersion:object */
};
this.cache.compiler[this.settings.installedSolidityVersion.startsWith("^") ? this.settings.installedSolidityVersion.substring(1) : this.settings.installedSolidityVersion] = solc;
this.reset();
this.initBlockchain();
}
initBlockchain() {
if (this.blockchain) {
this.blockchain.stopService();
}
if (!this.settings.blockchainProvider || this.settings.blockchainProvider === "internal") {
this.blockchain = new BuiltinGanacheBlockchain(this);
} else if (this.settings.blockchainProvider.startsWith("https://") || this.settings.blockchainProvider.startsWith("http://")) {
this.blockchain = new ExternalUrlBlockchain(this, this.settings.blockchainProvider);
} else if (this.settings.blockchainProvider.length > 0) {
this.settings.ganacheCmd = this.settings.blockchainProvider;
this.blockchain = new ExternalProcessBlockchain(this);
} else {
this.log(" 🧨 unknown blockchain provider. falling back to built-in ganache.")
this.blockchain = new BuiltinGanacheBlockchain(this);
}
this.blockchain.connect();
}
loadSession(stmts) {
if (!stmts) {
this.session.statements = []
} else {
this.session.statements = stmts.map(s => new SolidityStatement(s[0], s[1]));
}
}
dumpSession() {
return this.session.statements.map(s => s.toList());
}
setSetting(key, value) {
switch (key) {
case 'installedSolidityVersion': return;
case 'ganacheArgs':
if (!value) {
value = [];
}
else if (!Array.isArray(value)) {
value = value.split(' ');
}
break;
case 'ganacheCmd':
value = value.trim();
}
this.settings[key] = value;
}
reset() {
this.session = {
statements: [],
}
}
revert() {
this.session.statements.pop();
}
prepareNextStatement(stm /* SolidityStatement */) {
this.session.statements.push(stm);
}
template() {
const prologue = this.session.statements.filter(stm => stm.scope === SCOPE.SOURCE_UNIT);
const contractState = this.session.statements.filter(stm => stm.scope === SCOPE.CONTRACT);
const mainStatements = this.session.statements.filter(stm => stm.scope === SCOPE.MAIN);
/* figure out which compiler version to use */
const lastVersionPragma = this.session.statements.filter(stm => stm.scope === SCOPE.VERSION_PRAGMA).pop();
/* prepare body and return statement */
var lastStatement = this.session.statements[this.session.statements.length - 1] || {}
if (lastStatement.scope !== SCOPE.MAIN || lastStatement.hasNoReturnValue === true) {
/* not a main statement, put everything in the body and use a dummy as returnexpression */
var mainBody = mainStatements;
lastStatement = new SolidityStatement() // add dummy w/o return value
} else {
var mainBody = mainStatements.slice(0, mainStatements.length - 1)
}
const ret = `
// SPDX-License-Identifier: GPL-2.0-or-later
${lastVersionPragma ? lastVersionPragma.rawCommand : `pragma solidity ${this.settings.installedSolidityVersion};`}
${prologue.join('\n\n')}
contract ${this.settings.templateContractName} {
${contractState.join(' \n\n')}
function ${this.settings.templateFuncMain}() public ${lastStatement.returnType ? `returns (${lastStatement.returnType})` : ''} {
${mainBody.join('\n ')}
return ${lastStatement.returnExpression}
}
}`.trim();
if (this.settings.debugShowContract) this.log(ret)
return ret;
}
loadCachedCompiler(solidityVersion) {
solidityVersion = solidityVersion.startsWith("^") ? solidityVersion.substring(1) : solidityVersion; //strip leading ^
var that = this;
/** load remote version - (maybe cache?) */
return new Promise((resolve, reject) => {
if (that.cache.compiler[solidityVersion]) {
return resolve(that.cache.compiler[solidityVersion]);
}
getRemoteCompiler(solidityVersion)
.then(remoteSolidityVersion => {
solc.loadRemoteVersion(remoteSolidityVersion, function (err, solcSnapshot) {
that.cache.compiler[solidityVersion] = solcSnapshot;
return resolve(solcSnapshot)
})
})
.catch(err => {
return reject(err)
})
});
}
compile(source, cbWarning) {
let solidityVersion = getBestSolidityVersion(source);
return new Promise((resolve, reject) => {
if (!solidityVersion) {
return reject(new Error(`No valid solidity version found in source code (e.g. pragma solidity 0.8.10).`));
}
this.loadCachedCompiler(solidityVersion).then(solcSelected => {
let input = {
language: 'Solidity',
sources: {
'': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
//
},
},
},
}
input.settings.outputSelection['*']['*'] = ['abi', 'evm.bytecode', 'storageLayout']
const callbacks = {
'import': (sourcePath) => readFileCallback(
sourcePath, {
basePath: process.cwd(),
includePath: [
path.join(process.cwd(), "node_modules")
],
allowHttp: this.settings.resolveHttpImports
}
)
};
let ret = JSON.parse(solcSelected.compile(JSON.stringify(input), callbacks))
if (ret.errors) {
let realErrors = ret.errors.filter(err => err.type !== 'Warning');
if (realErrors.length) {
return reject(realErrors);
}
// print handle warnings
let warnings = ret.errors.filter(err => err.type === 'Warning' && !IGNORE_WARNINGS.some(target => err.message.includes(target)));
if (warnings.length) cbWarning(warnings);
}
return resolve(ret);
})
.catch(err => {
return reject(err);
});
});
}
run(statement) {
return new Promise((resolve, reject) => {
this.prepareNextStatement(statement)
const sourceCode = this.template();
// 1st. pass
this.compile(sourceCode, console.warn).then((res) => {
// happy path; types are correct
//console.log("first happy path")
let contractData = res.contracts[''];
contractData[this.settings.templateContractName]['main'] = this.settings.templateFuncMain;
this.blockchain.deploy(contractData, (err, retval) => {
if (err) {
this.revert();
return reject(err)
}
return resolve(retval) // return value
})
}).catch(errors => {
// frownie face
if (!Array.isArray(errors)) { //handle single error
this.revert();
return reject(errors);
}
//get last typeError to detect return type:
let lastTypeError = errors.slice().reverse().find(err => err.type === "TypeError");
if (!lastTypeError) {
this.revert();
return reject(errors);
}
let retType = ""
let matches = lastTypeError.message.match(rexTypeErrorReturnArgumentX);
if (matches) {
//console.log("2nd pass - detect return type")
retType = matches[1].trim();
if (retType.startsWith('int_const -')) {
retType = 'int';
} else if (retType.startsWith('int_const ')) {
retType = 'uint';
} else if (retType.startsWith('contract ')) {
retType = retType.split("contract ", 2)[1]
} else if (retType.endsWith(' pointer')) {
let fragments = retType.split(' '); //address[] storage pointer
fragments.pop() // pop 'pointer'
console.log(fragments)
if (fragments[1] == "storage") {
fragments[1] = "memory";
}
retType = fragments.join(' ');
}
} else if (lastTypeError.message.includes(TYPE_ERROR_DETECT_RETURNS)) {
console.error("WARNING: cannot auto-resolve type for complex function yet ://\n If this is a function call, try unpacking the function return values into local variables explicitly!\n e.g. `(uint a, address b, address c) = myContract.doSomething(1,2,3);`")
// lets give it a low-effort try to resolve return types. this will not always work.
let rexFunctionName = new RegExp(`([a-zA-Z0-9_\\.]+)\\s*\\(.*?\\)`);
let matchedFunctionNames = statement.rawCommand.match(rexFunctionName);
if (matchedFunctionNames.length >= 1) {
let funcNameParts = matchedFunctionNames[1].split(".");
let funcName = funcNameParts[funcNameParts.length - 1]; //get last
let rexReturns = new RegExp(`function ${funcName}\\s*\\(.* returns\\s*\\(([^\\)]+)\\)`)
let returnDecl = sourceCode.match(rexReturns);
if (returnDecl.length > 1) {
retType = returnDecl[1];
}
}
if (retType === "") {
this.revert();
return reject(errors);
}
} else {
console.error("BUG: cannot resolve type ://")
this.revert();
return reject(errors);
}
this.session.statements[this.session.statements.length - 1].returnType = retType;
//try again!
this.compile(this.template(), console.warn).then((res) => {
// happy path
//console.log(res)
let contractData = res.contracts[''];
contractData[this.settings.templateContractName]['main'] = this.settings.templateFuncMain;
this.blockchain.deploy(contractData, (err, retval) => {
if (err) {
this.revert();
return reject(err)
}
return resolve(retval) // return value
})
}).catch(errors => {
// error here
this.revert();
return reject(errors);
})
})
});
}
}
module.exports = {
InteractiveSolidityShell,
SolidityStatement,
SCOPE
}