2015-08-31 01:35:34 +00:00
|
|
|
var shelljs = require('shelljs');
|
|
|
|
var shelljs_global = require('shelljs/global');
|
2015-07-04 03:23:21 +00:00
|
|
|
var web3 = require('web3');
|
2015-10-02 03:04:15 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var solc = require('solc');
|
2015-07-04 03:23:21 +00:00
|
|
|
|
|
|
|
Compiler = function(blockchainConfig) {
|
|
|
|
this.blockchainConfig = blockchainConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
Compiler.prototype.init = function(env) {
|
|
|
|
var config = this.blockchainConfig.config(env);
|
|
|
|
};
|
|
|
|
|
2015-08-31 02:10:45 +00:00
|
|
|
Compiler.prototype.compile_solidity = function(contractFile) {
|
2015-10-02 03:04:15 +00:00
|
|
|
var source = fs.readFileSync(contractFile).toString();
|
|
|
|
var output = solc.compile(source, 1);
|
2015-09-15 01:01:42 +00:00
|
|
|
|
2015-10-14 19:37:45 +00:00
|
|
|
if(output.errors && output.errors.length>0){
|
|
|
|
throw new Error(output.errors[0]);
|
|
|
|
}
|
|
|
|
|
2015-10-02 03:04:15 +00:00
|
|
|
var json = output.contracts;
|
2015-08-31 01:35:34 +00:00
|
|
|
|
|
|
|
compiled_object = {}
|
|
|
|
|
|
|
|
for (var className in json) {
|
|
|
|
var contract = json[className];
|
|
|
|
|
|
|
|
compiled_object[className] = {};
|
2015-10-02 03:04:15 +00:00
|
|
|
compiled_object[className].code = contract.bytecode;
|
2015-08-31 01:35:34 +00:00
|
|
|
compiled_object[className].info = {};
|
2015-10-02 03:04:15 +00:00
|
|
|
compiled_object[className].info.abiDefinition = JSON.parse(contract.interface);
|
2015-08-31 01:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return compiled_object;
|
2015-10-02 03:04:15 +00:00
|
|
|
};
|
2015-08-31 02:10:45 +00:00
|
|
|
|
|
|
|
Compiler.prototype.compile_serpent = function(contractFile) {
|
|
|
|
var cmd, result, output, json, compiled_object;
|
|
|
|
|
|
|
|
cmd = "serpent compile " + contractFile;
|
|
|
|
|
|
|
|
result = exec(cmd, {silent: true});
|
|
|
|
code = result.output;
|
|
|
|
|
|
|
|
if (result.code === 1) {
|
|
|
|
throw new Error(result.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = "serpent mk_full_signature " + contractFile;
|
|
|
|
result = exec(cmd, {silent: true});
|
|
|
|
|
|
|
|
if (result.code === 1) {
|
|
|
|
throw new Error(result.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
json = JSON.parse(result.output.trim());
|
|
|
|
className = contractFile.split('.')[0].split("/").pop();
|
|
|
|
|
2015-08-31 12:24:09 +00:00
|
|
|
for (var i=0; i < json.length; i++) {
|
|
|
|
var elem = json[i];
|
|
|
|
|
|
|
|
if (elem.outputs.length > 0) {
|
|
|
|
elem.constant = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 02:10:45 +00:00
|
|
|
compiled_object = {}
|
|
|
|
compiled_object[className] = {};
|
|
|
|
compiled_object[className].code = code.trim();
|
|
|
|
compiled_object[className].info = {};
|
|
|
|
compiled_object[className].info.abiDefinition = json;
|
|
|
|
|
|
|
|
return compiled_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Compiler.prototype.compile = function(contractFile) {
|
|
|
|
var extension = contractFile.split('.')[1];
|
|
|
|
|
|
|
|
if (extension === 'sol') {
|
|
|
|
return this.compile_solidity(contractFile);
|
|
|
|
}
|
|
|
|
else if (extension === 'se') {
|
|
|
|
return this.compile_serpent(contractFile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("extension not known");
|
|
|
|
}
|
2015-07-04 03:23:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Compiler;
|