Skip to content

Commit e5ae6bc

Browse files
authored
Merge pull request #367 from embark-framework/refactor/vyper-utils
Refactor Vyper compiler
2 parents 114bf7f + 6f119ee commit e5ae6bc

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

lib/modules/vyper/index.js

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,66 @@ class Vyper {
1212
embark.registerCompiler(".py", this.compile_vyper.bind(this));
1313
}
1414

15+
static compileVyperContract(filename, compileABI, callback) {
16+
const params = compileABI ? '-f json ' : '';
17+
shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => {
18+
if (stderr) {
19+
return callback(stderr);
20+
}
21+
if (code !== 0) {
22+
return callback(`Vyper exited with error code ${code}`);
23+
}
24+
if (!stdout) {
25+
return callback('Execution returned no result');
26+
}
27+
callback(null, stdout.replace(/\n/g, ''));
28+
});
29+
}
30+
1531
compile_vyper(contractFiles, cb) {
1632
let self = this;
17-
async.waterfall([
18-
function compileContracts(callback) {
19-
self.logger.info("compiling vyper contracts...");
20-
const compiled_object = {};
21-
async.each(contractFiles,
22-
function (file, fileCb) {
23-
const className = path.basename(file.filename).split('.')[0];
24-
compiled_object[className] = {};
25-
async.parallel([
26-
function getByteCode(paraCb) {
27-
shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
28-
if (stderr) {
29-
return paraCb(stderr);
30-
}
31-
if (code !== 0) {
32-
return paraCb(`Vyper exited with error code ${code}`);
33-
}
34-
if (!stdout) {
35-
return paraCb('Execution returned no bytecode');
36-
}
37-
const byteCode = stdout.replace(/\n/g, '');
38-
compiled_object[className].runtimeBytecode = byteCode;
39-
compiled_object[className].realRuntimeBytecode = byteCode;
40-
compiled_object[className].code = byteCode;
41-
paraCb();
42-
});
43-
},
44-
function getABI(paraCb) {
45-
shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
46-
if (stderr) {
47-
return paraCb(stderr);
48-
}
49-
if (code !== 0) {
50-
return paraCb(`Vyper exited with error code ${code}`);
51-
}
52-
if (!stdout) {
53-
return paraCb('Execution returned no ABI');
54-
}
55-
let ABI = [];
56-
try {
57-
ABI = JSON.parse(stdout.replace(/\n/g, ''));
58-
} catch (e) {
59-
return paraCb('ABI is not valid JSON');
60-
}
61-
compiled_object[className].abiDefinition = ABI;
62-
paraCb();
63-
});
33+
if (!contractFiles || !contractFiles.length) {
34+
return cb();
35+
}
36+
self.logger.info("compiling Vyper contracts...");
37+
const compiled_object = {};
38+
async.each(contractFiles,
39+
function (file, fileCb) {
40+
const className = path.basename(file.filename).split('.')[0];
41+
compiled_object[className] = {};
42+
async.parallel([
43+
function getByteCode(paraCb) {
44+
Vyper.compileVyperContract(file.filename, false, (err, byteCode) => {
45+
if (err) {
46+
return paraCb(err);
6447
}
65-
], fileCb);
48+
compiled_object[className].runtimeBytecode = byteCode;
49+
compiled_object[className].realRuntimeBytecode = byteCode;
50+
compiled_object[className].code = byteCode;
51+
paraCb();
52+
});
6653
},
67-
function (err) {
68-
callback(err, compiled_object);
69-
});
70-
}
71-
], function (err, result) {
72-
cb(err, result);
73-
});
54+
function getABI(paraCb) {
55+
Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
56+
if (err) {
57+
return paraCb(err);
58+
}
59+
let ABI = [];
60+
try {
61+
ABI = JSON.parse(ABIString);
62+
} catch (e) {
63+
return paraCb('ABI is not valid JSON');
64+
}
65+
compiled_object[className].abiDefinition = ABI;
66+
paraCb();
67+
});
68+
}
69+
], fileCb);
70+
},
71+
function (err) {
72+
cb(err, compiled_object);
73+
});
7474
}
75-
7675
}
7776

7877
module.exports = Vyper;

0 commit comments

Comments
 (0)