2018-04-12 17:24:54 +00:00
|
|
|
let async = require('../../utils/async_extend.js');
|
|
|
|
const shelljs = require('shelljs');
|
2018-04-12 20:33:01 +00:00
|
|
|
const path = require('path');
|
2018-04-12 17:24:54 +00:00
|
|
|
|
|
|
|
class Vyper {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.contractDirectories = options.contractDirectories;
|
|
|
|
|
|
|
|
embark.registerCompiler(".py", this.compile_vyper.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
compile_vyper(contractFiles, cb) {
|
|
|
|
let self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function compileContracts(callback) {
|
|
|
|
self.logger.info("compiling vyper contracts...");
|
2018-04-12 20:33:01 +00:00
|
|
|
const compiled_object = {};
|
2018-04-12 17:24:54 +00:00
|
|
|
async.each(contractFiles,
|
2018-04-12 20:33:01 +00:00
|
|
|
function (file, fileCb) {
|
2018-04-13 19:48:19 +00:00
|
|
|
const className = path.basename(file.filename).split('.')[0];
|
|
|
|
compiled_object[className] = {};
|
2018-04-12 20:33:01 +00:00
|
|
|
async.parallel([
|
|
|
|
function getByteCode(paraCb) {
|
2018-04-13 19:48:19 +00:00
|
|
|
shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
|
2018-04-12 20:33:01 +00:00
|
|
|
if (stderr) {
|
|
|
|
return paraCb(stderr);
|
|
|
|
}
|
|
|
|
if (code !== 0) {
|
2018-04-13 19:48:19 +00:00
|
|
|
return paraCb(`Vyper exited with error code ${code}`);
|
2018-04-12 20:33:01 +00:00
|
|
|
}
|
|
|
|
if (!stdout) {
|
|
|
|
return paraCb('Execution returned no bytecode');
|
|
|
|
}
|
2018-04-13 19:48:19 +00:00
|
|
|
const byteCode = stdout.replace(/\n/g, '');
|
|
|
|
compiled_object[className].runtimeBytecode = byteCode;
|
|
|
|
compiled_object[className].realRuntimeBytecode = byteCode;
|
|
|
|
compiled_object[className].code = byteCode;
|
2018-04-12 20:33:01 +00:00
|
|
|
paraCb();
|
2018-04-12 17:24:54 +00:00
|
|
|
});
|
2018-04-12 20:33:01 +00:00
|
|
|
},
|
|
|
|
function getABI(paraCb) {
|
2018-04-13 19:48:19 +00:00
|
|
|
shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
|
2018-04-12 20:33:01 +00:00
|
|
|
if (stderr) {
|
|
|
|
return paraCb(stderr);
|
|
|
|
}
|
|
|
|
if (code !== 0) {
|
2018-04-13 19:48:19 +00:00
|
|
|
return paraCb(`Vyper exited with error code ${code}`);
|
2018-04-12 20:33:01 +00:00
|
|
|
}
|
|
|
|
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');
|
|
|
|
}
|
2018-04-13 19:48:19 +00:00
|
|
|
compiled_object[className].abiDefinition = ABI;
|
2018-04-12 20:33:01 +00:00
|
|
|
paraCb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], fileCb);
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
callback(err, compiled_object);
|
|
|
|
});
|
2018-04-12 17:24:54 +00:00
|
|
|
}
|
|
|
|
], function (err, result) {
|
|
|
|
cb(err, result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Vyper;
|