compile using js solc

This commit is contained in:
Iuri Matias 2015-10-01 23:04:15 -04:00
parent 02a52f718f
commit 32ac41a9cb
2 changed files with 9 additions and 26 deletions

View File

@ -1,6 +1,8 @@
var shelljs = require('shelljs'); var shelljs = require('shelljs');
var shelljs_global = require('shelljs/global'); var shelljs_global = require('shelljs/global');
var web3 = require('web3'); var web3 = require('web3');
var fs = require('fs');
var solc = require('solc');
Compiler = function(blockchainConfig) { Compiler = function(blockchainConfig) {
this.blockchainConfig = blockchainConfig; this.blockchainConfig = blockchainConfig;
@ -21,44 +23,24 @@ Compiler.prototype.init = function(env) {
}; };
Compiler.prototype.compile_solidity = function(contractFile) { Compiler.prototype.compile_solidity = function(contractFile) {
var cmd, result, output, version, json, compiled_object; var source = fs.readFileSync(contractFile).toString();
var output = solc.compile(source, 1);
cmd = "solc --version"; var json = output.contracts;
result = exec(cmd, {silent: true});
output = result.output;
version = output.split('\n')[1].split(' ')[1].slice(0,5);
if (version == '0.1.1') {
cmd = "solc --input-file " + contractFile + " --combined-json binary,json-abi";
}
else {
cmd = "solc --input-file " + contractFile + " --combined-json bin,abi";
}
result = exec(cmd, {silent: true});
output = result.output;
if (result.code === 1) {
if (version == '0.1.1' || version == '0.1.0'){
throw new Error(result.output);
}
}
json = JSON.parse(output).contracts;
compiled_object = {} compiled_object = {}
for (var className in json) { for (var className in json) {
var contract = json[className]; var contract = json[className];
compiled_object[className] = {}; compiled_object[className] = {};
compiled_object[className].code = contract.binary || contract.bin; compiled_object[className].code = contract.bytecode;
compiled_object[className].info = {}; compiled_object[className].info = {};
compiled_object[className].info.abiDefinition = JSON.parse(contract["abi"] || contract["json-abi"]); compiled_object[className].info.abiDefinition = JSON.parse(contract.interface);
} }
return compiled_object; return compiled_object;
} };
Compiler.prototype.compile_serpent = function(contractFile) { Compiler.prototype.compile_serpent = function(contractFile) {
var cmd, result, output, json, compiled_object; var cmd, result, output, json, compiled_object;

View File

@ -24,6 +24,7 @@
"python": "^0.0.4", "python": "^0.0.4",
"read-yaml": "^1.0.0", "read-yaml": "^1.0.0",
"shelljs": "^0.5.0", "shelljs": "^0.5.0",
"solc": "^0.1.3-2",
"sync-me": "^0.1.1", "sync-me": "^0.1.1",
"toposort": "^0.2.10", "toposort": "^0.2.10",
"web3": "^0.8.1", "web3": "^0.8.1",