Skip to content

Commit 6f119ee

Browse files
committed
move vyper contract compile to static util function
1 parent dd1c10f commit 6f119ee

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

lib/modules/vyper/index.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ 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;
1733
if (!contractFiles || !contractFiles.length) {
@@ -25,37 +41,24 @@ class Vyper {
2541
compiled_object[className] = {};
2642
async.parallel([
2743
function getByteCode(paraCb) {
28-
shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
29-
if (stderr) {
30-
return paraCb(stderr);
31-
}
32-
if (code !== 0) {
33-
return paraCb(`Vyper exited with error code ${code}`);
34-
}
35-
if (!stdout) {
36-
return paraCb('Execution returned no bytecode');
44+
Vyper.compileVyperContract(file.filename, false, (err, byteCode) => {
45+
if (err) {
46+
return paraCb(err);
3747
}
38-
const byteCode = stdout.replace(/\n/g, '');
3948
compiled_object[className].runtimeBytecode = byteCode;
4049
compiled_object[className].realRuntimeBytecode = byteCode;
4150
compiled_object[className].code = byteCode;
4251
paraCb();
4352
});
4453
},
4554
function getABI(paraCb) {
46-
shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
47-
if (stderr) {
48-
return paraCb(stderr);
49-
}
50-
if (code !== 0) {
51-
return paraCb(`Vyper exited with error code ${code}`);
52-
}
53-
if (!stdout) {
54-
return paraCb('Execution returned no ABI');
55+
Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
56+
if (err) {
57+
return paraCb(err);
5558
}
5659
let ABI = [];
5760
try {
58-
ABI = JSON.parse(stdout.replace(/\n/g, ''));
61+
ABI = JSON.parse(ABIString);
5962
} catch (e) {
6063
return paraCb('ABI is not valid JSON');
6164
}
@@ -69,7 +72,6 @@ class Vyper {
6972
cb(err, compiled_object);
7073
});
7174
}
72-
7375
}
7476

7577
module.exports = Vyper;

0 commit comments

Comments
 (0)