From dd1c10fe8537bea19c08e375b40624a08c26ff51 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 16 Apr 2018 13:10:55 -0400 Subject: [PATCH 1/2] remove useless waterfall --- lib/modules/vyper/index.js | 107 ++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/lib/modules/vyper/index.js b/lib/modules/vyper/index.js index df805a21..463d67b6 100644 --- a/lib/modules/vyper/index.js +++ b/lib/modules/vyper/index.js @@ -14,63 +14,60 @@ class Vyper { compile_vyper(contractFiles, cb) { let self = this; - async.waterfall([ - function compileContracts(callback) { - self.logger.info("compiling vyper contracts..."); - const compiled_object = {}; - async.each(contractFiles, - function (file, fileCb) { - const className = path.basename(file.filename).split('.')[0]; - compiled_object[className] = {}; - async.parallel([ - function getByteCode(paraCb) { - shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => { - if (stderr) { - return paraCb(stderr); - } - if (code !== 0) { - return paraCb(`Vyper exited with error code ${code}`); - } - if (!stdout) { - return paraCb('Execution returned no bytecode'); - } - const byteCode = stdout.replace(/\n/g, ''); - compiled_object[className].runtimeBytecode = byteCode; - compiled_object[className].realRuntimeBytecode = byteCode; - compiled_object[className].code = byteCode; - paraCb(); - }); - }, - function getABI(paraCb) { - shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => { - if (stderr) { - return paraCb(stderr); - } - if (code !== 0) { - return paraCb(`Vyper exited with error code ${code}`); - } - if (!stdout) { - return paraCb('Execution returned no ABI'); - } - let ABI = []; - try { - ABI = JSON.parse(stdout.replace(/\n/g, '')); - } catch (e) { - return paraCb('ABI is not valid JSON'); - } - compiled_object[className].abiDefinition = ABI; - paraCb(); - }); + if (!contractFiles || !contractFiles.length) { + return cb(); + } + self.logger.info("compiling Vyper contracts..."); + const compiled_object = {}; + async.each(contractFiles, + function (file, fileCb) { + const className = path.basename(file.filename).split('.')[0]; + compiled_object[className] = {}; + async.parallel([ + function getByteCode(paraCb) { + shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => { + if (stderr) { + return paraCb(stderr); } - ], fileCb); + if (code !== 0) { + return paraCb(`Vyper exited with error code ${code}`); + } + if (!stdout) { + return paraCb('Execution returned no bytecode'); + } + const byteCode = stdout.replace(/\n/g, ''); + compiled_object[className].runtimeBytecode = byteCode; + compiled_object[className].realRuntimeBytecode = byteCode; + compiled_object[className].code = byteCode; + paraCb(); + }); }, - function (err) { - callback(err, compiled_object); - }); - } - ], function (err, result) { - cb(err, result); - }); + function getABI(paraCb) { + shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => { + if (stderr) { + return paraCb(stderr); + } + if (code !== 0) { + return paraCb(`Vyper exited with error code ${code}`); + } + if (!stdout) { + return paraCb('Execution returned no ABI'); + } + let ABI = []; + try { + ABI = JSON.parse(stdout.replace(/\n/g, '')); + } catch (e) { + return paraCb('ABI is not valid JSON'); + } + compiled_object[className].abiDefinition = ABI; + paraCb(); + }); + } + ], fileCb); + }, + function (err) { + cb(err, compiled_object); + }); } } From 6f119eeb1b95adeb685efcd5b7a1d6c9f6c144cb Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 17 Apr 2018 11:25:46 -0400 Subject: [PATCH 2/2] move vyper contract compile to static util function --- lib/modules/vyper/index.js | 44 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/modules/vyper/index.js b/lib/modules/vyper/index.js index 463d67b6..04bbd210 100644 --- a/lib/modules/vyper/index.js +++ b/lib/modules/vyper/index.js @@ -12,6 +12,22 @@ class Vyper { embark.registerCompiler(".py", this.compile_vyper.bind(this)); } + static compileVyperContract(filename, compileABI, callback) { + const params = compileABI ? '-f json ' : ''; + shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => { + if (stderr) { + return callback(stderr); + } + if (code !== 0) { + return callback(`Vyper exited with error code ${code}`); + } + if (!stdout) { + return callback('Execution returned no result'); + } + callback(null, stdout.replace(/\n/g, '')); + }); + } + compile_vyper(contractFiles, cb) { let self = this; if (!contractFiles || !contractFiles.length) { @@ -25,17 +41,10 @@ class Vyper { compiled_object[className] = {}; async.parallel([ function getByteCode(paraCb) { - shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => { - if (stderr) { - return paraCb(stderr); + Vyper.compileVyperContract(file.filename, false, (err, byteCode) => { + if (err) { + return paraCb(err); } - if (code !== 0) { - return paraCb(`Vyper exited with error code ${code}`); - } - if (!stdout) { - return paraCb('Execution returned no bytecode'); - } - const byteCode = stdout.replace(/\n/g, ''); compiled_object[className].runtimeBytecode = byteCode; compiled_object[className].realRuntimeBytecode = byteCode; compiled_object[className].code = byteCode; @@ -43,19 +52,13 @@ class Vyper { }); }, function getABI(paraCb) { - shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => { - if (stderr) { - return paraCb(stderr); - } - if (code !== 0) { - return paraCb(`Vyper exited with error code ${code}`); - } - if (!stdout) { - return paraCb('Execution returned no ABI'); + Vyper.compileVyperContract(file.filename, true, (err, ABIString) => { + if (err) { + return paraCb(err); } let ABI = []; try { - ABI = JSON.parse(stdout.replace(/\n/g, '')); + ABI = JSON.parse(ABIString); } catch (e) { return paraCb('ABI is not valid JSON'); } @@ -69,7 +72,6 @@ class Vyper { cb(err, compiled_object); }); } - } module.exports = Vyper;