2016-08-14 12:04:34 +00:00
|
|
|
var Compiler = require('./compiler.js');
|
|
|
|
|
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-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() {
|
|
|
|
for(var className in this.compiledContracts) {
|
|
|
|
var contract = this.compiledContracts[className];
|
|
|
|
var contractConfig = this.contractsConfig[className];
|
|
|
|
|
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 || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
contract.className = className;
|
|
|
|
this.contracts[className] = contract;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 = [];
|
|
|
|
for(var className in this.compiledContracts) {
|
|
|
|
var contract = this.compiledContracts[className];
|
|
|
|
contracts.push(contract);
|
|
|
|
}
|
|
|
|
return contracts;
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
(contract.deployedAddress || '...'.green),
|
|
|
|
((contract.deployedAddress !== undefined) ? "\t\tDeployed".green : "\t\tPending".magenta)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
module.exports = ContractsManager;
|
|
|
|
|