2015-06-28 20:11:42 +00:00
|
|
|
var readYaml = require('read-yaml');
|
|
|
|
var fs = require('fs');
|
2015-07-01 23:49:49 +00:00
|
|
|
var toposort = require('toposort');
|
2015-06-28 20:11:42 +00:00
|
|
|
|
2015-07-04 03:23:21 +00:00
|
|
|
ContractsConfig = function(blockchainConfig, compiler) {
|
2015-07-03 11:59:35 +00:00
|
|
|
this.blockchainConfig = blockchainConfig;
|
2015-07-04 03:23:21 +00:00
|
|
|
this.compiler = compiler;
|
2015-07-03 11:59:35 +00:00
|
|
|
this.contractFiles = [];
|
|
|
|
}
|
|
|
|
|
2015-08-01 15:19:21 +00:00
|
|
|
ContractsConfig.prototype.init = function(files, env) {
|
2015-06-28 20:11:42 +00:00
|
|
|
this.all_contracts = [];
|
|
|
|
this.contractDB = {};
|
|
|
|
this.contractFiles = files;
|
2015-07-01 23:49:49 +00:00
|
|
|
this.contractDependencies = {};
|
2015-08-01 02:50:51 +00:00
|
|
|
this.contractStubs = {};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
2015-07-04 20:52:05 +00:00
|
|
|
//TODO: have to specify environment otherwise wouldn't work with staging
|
2015-07-04 02:27:17 +00:00
|
|
|
if (this.blockchainConfig.config != undefined) {
|
2015-08-01 15:19:21 +00:00
|
|
|
//this.blockchainConfig = this.blockchainConfig.config('development');
|
|
|
|
this.blockchainConfig = this.blockchainConfig.config(env);
|
2015-07-04 02:27:17 +00:00
|
|
|
}
|
2015-06-28 20:11:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ContractsConfig.prototype.loadConfigFile = function(filename) {
|
|
|
|
try {
|
|
|
|
this.contractConfig = readYaml.sync(filename);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("error reading " + filename);
|
|
|
|
}
|
|
|
|
return this;
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
|
|
|
ContractsConfig.prototype.loadConfig = function(config) {
|
|
|
|
this.contractConfig = config;
|
|
|
|
return this;
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
|
|
|
ContractsConfig.prototype.config = function(env) {
|
|
|
|
return this.contractConfig[env];
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
2015-08-01 02:50:51 +00:00
|
|
|
ContractsConfig.prototype.is_a_token = function(target, compiled_contracts) {
|
|
|
|
for (var className in compiled_contracts) {
|
|
|
|
if (this.contractStubs[className] && this.contractStubs[className].indexOf(target) >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
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);
|
2015-07-04 03:23:21 +00:00
|
|
|
this.compiler.init(env);
|
2015-07-01 23:49:49 +00:00
|
|
|
|
2015-07-04 20:52:05 +00:00
|
|
|
// determine dependencies
|
2015-07-01 23:49:49 +00:00
|
|
|
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-08-01 02:50:51 +00:00
|
|
|
|
|
|
|
this.contractStubs[className] = options.stubs;
|
2015-07-01 23:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-28 20:11:42 +00:00
|
|
|
|
2015-07-04 20:52:05 +00:00
|
|
|
var all_compiled_contracts = {};
|
|
|
|
// compile files
|
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()
|
|
|
|
|
2015-07-04 03:23:21 +00:00
|
|
|
compiled_contracts = this.compiler.compile(source);
|
2015-08-01 02:50:51 +00:00
|
|
|
for (var className in compiled_contracts) {
|
2015-06-28 20:11:42 +00:00
|
|
|
var contract = compiled_contracts[className];
|
2015-08-01 02:50:51 +00:00
|
|
|
|
|
|
|
if (this.is_a_token(className, compiled_contracts)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-04 20:52:05 +00:00
|
|
|
all_compiled_contracts[className] = contract;
|
2015-06-28 20:11:42 +00:00
|
|
|
this.all_contracts.push(className);
|
2015-07-04 20:52:05 +00:00
|
|
|
this.contractDB[className] = {
|
|
|
|
args: [],
|
|
|
|
types: ['file'],
|
2015-07-04 22:59:57 +00:00
|
|
|
gasPrice: this.blockchainConfig.gasPrice,
|
|
|
|
gasLimit: this.blockchainConfig.gasLimit,
|
2015-07-04 20:52:05 +00:00
|
|
|
compiled: contract
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move this
|
|
|
|
for(className in contractsConfig) {
|
|
|
|
var contractConfig = contractsConfig[className];
|
|
|
|
|
|
|
|
var contract;
|
|
|
|
contract = this.contractDB[className];
|
|
|
|
if (contract === undefined) {
|
2015-07-04 22:59:57 +00:00
|
|
|
contract = {
|
|
|
|
args: [],
|
|
|
|
types: ['file'],
|
|
|
|
gasPrice: this.blockchainConfig.gasPrice,
|
|
|
|
gasLimit: this.blockchainConfig.gasLimit,
|
|
|
|
compiled: contract
|
|
|
|
}
|
2015-06-28 20:11:42 +00:00
|
|
|
this.contractDB[className] = contract;
|
|
|
|
}
|
2015-07-04 20:52:05 +00:00
|
|
|
|
2015-07-04 22:59:57 +00:00
|
|
|
contract.gasPrice = contractConfig.gas_price || contract.gasPrice;
|
|
|
|
contract.gasLimit = contractConfig.gas_limit || contract.gasLimit;
|
|
|
|
contract.args = contractConfig.args || [];
|
2015-07-06 12:19:25 +00:00
|
|
|
contract.address = contractConfig.address;
|
2015-08-02 23:43:37 +00:00
|
|
|
contract.onDeploy = contractConfig.onDeploy || [];
|
2015-07-04 20:52:05 +00:00
|
|
|
|
2015-07-04 22:59:57 +00:00
|
|
|
if (contractConfig.instanceOf !== undefined) {
|
2015-07-04 20:52:05 +00:00
|
|
|
contract.types.push('instance');
|
|
|
|
contract.instanceOf = contractConfig.instanceOf;
|
2015-07-04 22:59:57 +00:00
|
|
|
contract.compiled = all_compiled_contracts[contractConfig.instanceOf];
|
2015-07-04 20:52:05 +00:00
|
|
|
}
|
2015-07-06 12:33:26 +00:00
|
|
|
if (contractConfig.address !== undefined) {
|
|
|
|
contract.types.push('static');
|
|
|
|
}
|
2015-07-04 20:52:05 +00:00
|
|
|
|
|
|
|
if (this.all_contracts.indexOf(className) < 0) {
|
|
|
|
this.all_contracts.push(className);
|
|
|
|
}
|
2015-06-28 20:11:42 +00:00
|
|
|
}
|
2015-07-01 23:49:49 +00:00
|
|
|
|
|
|
|
this.sortContracts();
|
2015-07-04 02:41:39 +00:00
|
|
|
};
|
2015-07-01 23:49:49 +00:00
|
|
|
|
|
|
|
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-07-04 02:41:39 +00:00
|
|
|
};
|
2015-06-28 20:11:42 +00:00
|
|
|
|
2015-07-04 02:41:39 +00:00
|
|
|
module.exports = ContractsConfig;
|
2015-06-28 20:11:42 +00:00
|
|
|
|