2015-07-01 23:49:49 +00:00
|
|
|
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
2015-06-28 20:11:42 +00:00
|
|
|
var readYaml = require('read-yaml');
|
|
|
|
var fs = require('fs');
|
|
|
|
var Blockchain = require('./blockchain.js');
|
2015-07-01 23:49:49 +00:00
|
|
|
var toposort = require('toposort');
|
2015-06-28 20:11:42 +00:00
|
|
|
|
|
|
|
ContractsConfig = function(files, blockchainConfig, web3) {
|
|
|
|
this.all_contracts = [];
|
|
|
|
this.contractDB = {};
|
|
|
|
this.contractFiles = files;
|
|
|
|
this.web3 = web3;
|
2015-07-01 23:49:49 +00:00
|
|
|
this.contractDependencies = {};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort));
|
|
|
|
primaryAddress = this.web3.eth.coinbase;
|
|
|
|
this.web3.eth.defaultAccount = primaryAddress;
|
|
|
|
} catch (_error) {
|
|
|
|
e = _error;
|
|
|
|
throw new Error("can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running");
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("address is : " + primaryAddress);
|
|
|
|
};
|
|
|
|
|
|
|
|
ContractsConfig.prototype.loadConfigFile = function(filename) {
|
|
|
|
try {
|
|
|
|
this.contractConfig = readYaml.sync(filename);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("error reading " + filename);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContractsConfig.prototype.loadConfig = function(config) {
|
|
|
|
this.contractConfig = config;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContractsConfig.prototype.config = function(env) {
|
|
|
|
return this.contractConfig[env];
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:49:49 +00:00
|
|
|
ContractsConfig.prototype.compileContracts = function(env) {
|
2015-06-28 20:11:42 +00:00
|
|
|
var contractFile, source, j;
|
2015-07-01 23:49:49 +00:00
|
|
|
var contractsConfig = this.config(env);
|
|
|
|
|
|
|
|
if (contractsConfig != null) {
|
|
|
|
for (className in contractsConfig) {
|
|
|
|
options = contractsConfig[className];
|
|
|
|
if (options.args == null) continue;
|
|
|
|
|
|
|
|
ref = options.args;
|
|
|
|
for (j = 0; j < ref.length; j++) {
|
|
|
|
arg = ref[j];
|
|
|
|
if (arg[0] === "$") {
|
|
|
|
if (this.contractDependencies[className] === void 0) {
|
|
|
|
this.contractDependencies[className] = [];
|
|
|
|
}
|
|
|
|
this.contractDependencies[className].push(arg.substr(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 20:11:42 +00:00
|
|
|
|
|
|
|
for (j = 0; j < this.contractFiles.length; j++) {
|
|
|
|
contractFile = this.contractFiles[j];
|
|
|
|
source = fs.readFileSync(contractFile).toString()
|
|
|
|
|
|
|
|
console.log("compiling " + contractFile);
|
|
|
|
compiled_contracts = this.web3.eth.compile.solidity(source);
|
|
|
|
for (className in compiled_contracts) {
|
|
|
|
var contract = compiled_contracts[className];
|
|
|
|
this.all_contracts.push(className);
|
|
|
|
this.contractDB[className] = contract;
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 23:49:49 +00:00
|
|
|
|
|
|
|
this.sortContracts();
|
|
|
|
}
|
|
|
|
|
|
|
|
ContractsConfig.prototype.sortContracts = function() {
|
|
|
|
var converted_dependencies = [], i;
|
|
|
|
|
|
|
|
for(contract in this.contractDependencies) {
|
|
|
|
var dependencies = this.contractDependencies[contract];
|
|
|
|
for(i=0; i < dependencies.length; i++) {
|
|
|
|
converted_dependencies.push([contract, dependencies[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderedDependencies = toposort(converted_dependencies).reverse();
|
|
|
|
|
|
|
|
this.all_contracts = this.all_contracts.sort(function(a,b) {
|
|
|
|
var order_a = orderedDependencies.indexOf(a);
|
|
|
|
var order_b = orderedDependencies.indexOf(b);
|
|
|
|
return order_a - order_b;
|
|
|
|
});;
|
2015-06-28 20:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ContractsConfig
|
|
|
|
|