2016-08-14 12:04:34 +00:00
|
|
|
var Compiler = require('./compiler.js');
|
2016-09-27 04:55:35 +00:00
|
|
|
var toposort = require('toposort');
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-09-28 01:04:40 +00:00
|
|
|
// TODO: create a contract object
|
|
|
|
|
2016-08-22 03:40:05 +00:00
|
|
|
var ContractsManager = function(options) {
|
|
|
|
this.contractFiles = options.contractFiles;
|
|
|
|
this.contractsConfig = options.contractsConfig;
|
2016-08-14 12:04:34 +00:00
|
|
|
this.contracts = {};
|
2016-09-22 22:24:01 +00:00
|
|
|
this.logger = options.logger;
|
2016-09-27 04:55:35 +00:00
|
|
|
|
|
|
|
this.contractDependencies = {};
|
2016-08-14 12:04:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ContractsManager.prototype.init = function() {
|
|
|
|
this.compiledContracts = this.compileContracts();
|
|
|
|
};
|
|
|
|
|
|
|
|
ContractsManager.prototype.compileContracts = function() {
|
|
|
|
var compiler = new Compiler();
|
|
|
|
return compiler.compile_solidity(this.contractFiles);
|
|
|
|
};
|
|
|
|
|
|
|
|
ContractsManager.prototype.build = function() {
|
2016-09-27 04:55:35 +00:00
|
|
|
|
2016-09-28 01:04:40 +00:00
|
|
|
// =======================
|
|
|
|
// =======================
|
|
|
|
// TODO: this should be going through the contract config, not just the
|
|
|
|
// compiled list
|
|
|
|
// =======================
|
|
|
|
// =======================
|
2016-09-27 04:55:35 +00:00
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
for(var className in this.compiledContracts) {
|
|
|
|
var contract = this.compiledContracts[className];
|
2016-09-23 09:59:08 +00:00
|
|
|
var contractConfig = this.contractsConfig.contracts[className];
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-09-23 09:33:38 +00:00
|
|
|
if (this.contractsConfig.gas === 'auto') {
|
|
|
|
var maxGas = Math.max(contract.gasEstimates.creation[0], contract.gasEstimates.creation[1], 500000);
|
|
|
|
var adjustedGas = Math.round(maxGas * 1.01);
|
|
|
|
contract.gas = adjustedGas;
|
|
|
|
} else {
|
|
|
|
contract.gas = this.contractsConfig.gas;
|
|
|
|
}
|
2016-08-14 12:04:34 +00:00
|
|
|
contract.gasPrice = this.contractsConfig.gasPrice;
|
|
|
|
|
|
|
|
if (contractConfig === undefined) {
|
|
|
|
contract.args = [];
|
|
|
|
} else {
|
|
|
|
contract.args = contractConfig.args || [];
|
|
|
|
}
|
|
|
|
|
2016-09-28 01:04:40 +00:00
|
|
|
contract.type = 'file';
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
contract.className = className;
|
2016-09-28 01:13:54 +00:00
|
|
|
contract.address = contractConfig.address;
|
2016-09-28 01:04:40 +00:00
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
this.contracts[className] = contract;
|
|
|
|
}
|
2016-09-28 01:04:40 +00:00
|
|
|
|
|
|
|
for(var className in this.contractsConfig.contracts) {
|
|
|
|
var contractConfig = this.contractsConfig.contracts[className];
|
|
|
|
var contract;
|
|
|
|
|
|
|
|
if (contractConfig.instanceOf !== undefined) {
|
|
|
|
// TODO: should merge with parent object
|
|
|
|
var parentContractName = contractConfig.instanceOf;
|
|
|
|
var parentContract = this.contractsConfig.contracts[parentContractName];
|
|
|
|
var parentContractObject = this.contracts[parentContractName];
|
|
|
|
contract = JSON.parse(JSON.stringify(parentContractObject));
|
|
|
|
contract.deploy = true;
|
|
|
|
contract.className = className;
|
|
|
|
contract.args = (contractConfig.args || parentContract.args);
|
|
|
|
contract.gas = (contractConfig.gas || parentContract.gas);
|
|
|
|
contract.gasPrice = (contractConfig.gasPrice || parentContract.gasPrice);
|
|
|
|
|
|
|
|
this.contracts[className] = contract;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine dependencies
|
|
|
|
for (var className in this.compiledContracts) {
|
|
|
|
var contract = this.compiledContracts[className];
|
|
|
|
var contractConfig = this.contractsConfig.contracts[className];
|
|
|
|
|
|
|
|
if (this.contractsConfig.args === null || this.contractsConfig.args === []) continue;
|
|
|
|
|
|
|
|
var ref = contractConfig.args; //get arguments
|
|
|
|
for (var j = 0; j < ref.length; j++) {
|
|
|
|
var arg = ref[j];
|
|
|
|
if (arg[0] === "$") {
|
|
|
|
if (this.contractDependencies[className] === void 0) {
|
|
|
|
this.contractDependencies[className] = [];
|
|
|
|
}
|
|
|
|
this.contractDependencies[className].push(arg.substr(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
};
|
|
|
|
|
2016-09-27 04:55:35 +00:00
|
|
|
ContractsManager.prototype.getContract = function(className) {
|
|
|
|
return this.compiledContracts[className];
|
|
|
|
};
|
|
|
|
|
|
|
|
ContractsManager.prototype.sortContracts = function(contractList) {
|
|
|
|
var converted_dependencies = [], i;
|
|
|
|
|
|
|
|
for(var contract in this.contractDependencies) {
|
|
|
|
var dependencies = this.contractDependencies[contract];
|
|
|
|
for(var i=0; i < dependencies.length; i++) {
|
|
|
|
converted_dependencies.push([contract, dependencies[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderedDependencies = toposort(converted_dependencies).reverse();
|
|
|
|
|
|
|
|
var newList = contractList.sort(function(a,b) {
|
|
|
|
var order_a = orderedDependencies.indexOf(a.className);
|
|
|
|
var order_b = orderedDependencies.indexOf(b.className);
|
|
|
|
return order_a - order_b;
|
|
|
|
});
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
};
|
|
|
|
|
2016-09-22 22:24:01 +00:00
|
|
|
// TODO: should be built contracts
|
2016-08-14 12:04:34 +00:00
|
|
|
ContractsManager.prototype.listContracts = function() {
|
|
|
|
var contracts = [];
|
2016-09-28 01:04:40 +00:00
|
|
|
for(var className in this.contracts) {
|
|
|
|
var contract = this.contracts[className];
|
2016-08-14 12:04:34 +00:00
|
|
|
contracts.push(contract);
|
|
|
|
}
|
2016-09-27 04:55:35 +00:00
|
|
|
return this.sortContracts(contracts);
|
2016-08-14 12:04:34 +00:00
|
|
|
};
|
|
|
|
|
2016-09-22 22:24:01 +00:00
|
|
|
ContractsManager.prototype.contractsState = function() {
|
|
|
|
var data = [];
|
|
|
|
|
|
|
|
for(var className in this.contracts) {
|
|
|
|
var contract = this.contracts[className];
|
|
|
|
|
|
|
|
data.push([
|
|
|
|
className.green,
|
2016-09-25 02:41:55 +00:00
|
|
|
(contract.deployedAddress || '...').green,
|
2016-09-22 22:24:01 +00:00
|
|
|
((contract.deployedAddress !== undefined) ? "\t\tDeployed".green : "\t\tPending".magenta)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
module.exports = ContractsManager;
|
|
|
|
|