2016-08-13 14:48:00 +00:00
|
|
|
var async = require('async');
|
2016-08-14 12:04:34 +00:00
|
|
|
var Compiler = require('./compiler.js');
|
2016-09-25 01:10:47 +00:00
|
|
|
var DeployTracker = require('./deploy_tracker.js');
|
|
|
|
|
|
|
|
var Deploy = function(options) {
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.contractsManager = options.contractsManager;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.env = options.env;
|
|
|
|
|
|
|
|
this.deployTracker = new DeployTracker({
|
|
|
|
logger: options.logger, chainConfig: options.chainConfig, web3: options.web3, env: this.env
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Deploy.prototype.checkAndDeployContract = function(contract, params, callback) {
|
|
|
|
var self = this;
|
2016-09-25 06:30:03 +00:00
|
|
|
var trackedContract = self.deployTracker.getContract(contract.className, contract.code, contract.args);
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2016-09-25 06:30:03 +00:00
|
|
|
if (trackedContract && this.web3.eth.getCode(trackedContract.address) !== "0x") {
|
|
|
|
self.logger.info(contract.className + " already deployed " + trackedContract.address);
|
|
|
|
contract.deployedAddress = trackedContract.address;
|
2016-09-25 01:10:47 +00:00
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
this.deployContract(contract, params, function(err, address) {
|
|
|
|
self.logger.info("not deployed");
|
|
|
|
self.deployTracker.trackContract(contract.className, contract.code, contract.args, address);
|
|
|
|
self.deployTracker.save();
|
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
2016-08-13 14:48:00 +00:00
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Deploy.prototype.deployContract = function(contract, params, callback) {
|
2016-09-17 03:56:25 +00:00
|
|
|
var self = this;
|
2016-08-14 12:04:34 +00:00
|
|
|
var contractObject = this.web3.eth.contract(contract.abiDefinition);
|
2015-10-09 17:20:35 +00:00
|
|
|
|
2016-09-25 01:10:47 +00:00
|
|
|
var contractParams = (params || contract.args).slice();
|
2015-06-28 02:20:07 +00:00
|
|
|
|
2016-09-26 00:51:00 +00:00
|
|
|
console.log("using address" + this.web3.eth.accounts[0]);
|
|
|
|
|
|
|
|
// TODO: probably needs to be defaultAccoun
|
|
|
|
// TODO: it wouldn't necessary be the first address
|
|
|
|
// idea: use defined blockchain address or first address
|
2016-08-13 14:48:00 +00:00
|
|
|
contractParams.push({
|
2016-09-26 00:51:00 +00:00
|
|
|
//from: this.web3.eth.coinbase,
|
|
|
|
from: this.web3.eth.accounts[0],
|
2016-08-14 12:04:34 +00:00
|
|
|
data: contract.code,
|
2016-09-23 09:33:38 +00:00
|
|
|
gas: contract.gas,
|
2016-08-13 14:48:00 +00:00
|
|
|
gasPrice: contract.gasPrice
|
2016-05-30 00:14:27 +00:00
|
|
|
});
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-09-22 22:24:01 +00:00
|
|
|
self.logger.info("deploying " + contract.className);
|
2016-08-14 12:04:34 +00:00
|
|
|
contractParams.push(function(err, transaction) {
|
2016-09-22 22:24:01 +00:00
|
|
|
self.logger.contractsState(self.contractsManager.contractsState());
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
if (err) {
|
2016-09-17 16:28:26 +00:00
|
|
|
self.logger.error("error deploying contract: " + contract.className);
|
|
|
|
self.logger.error(err.toString());
|
2016-08-14 12:04:34 +00:00
|
|
|
callback(new Error(err));
|
|
|
|
} else if (transaction.address !== undefined) {
|
2016-09-22 22:24:01 +00:00
|
|
|
self.logger.info(contract.className + " deployed at " + transaction.address);
|
2016-08-14 12:04:34 +00:00
|
|
|
contract.deployedAddress = transaction.address;
|
|
|
|
callback(null, transaction.address);
|
|
|
|
}
|
|
|
|
});
|
2015-08-04 12:18:04 +00:00
|
|
|
|
2016-08-13 14:48:00 +00:00
|
|
|
contractObject["new"].apply(contractObject, contractParams);
|
2016-08-10 00:50:12 +00:00
|
|
|
};
|
2015-10-09 14:18:18 +00:00
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
Deploy.prototype.deployAll = function(done) {
|
|
|
|
var self = this;
|
2016-09-17 16:28:26 +00:00
|
|
|
this.logger.info("deploying contracts");
|
2016-08-14 12:04:34 +00:00
|
|
|
|
|
|
|
async.eachOfSeries(this.contractsManager.listContracts(),
|
|
|
|
function(contract, key, callback) {
|
2016-09-17 16:28:26 +00:00
|
|
|
self.logger.trace(arguments);
|
2016-09-25 01:10:47 +00:00
|
|
|
self.checkAndDeployContract(contract, null, callback);
|
2016-08-14 12:04:34 +00:00
|
|
|
},
|
|
|
|
function(err, results) {
|
2016-09-17 03:56:25 +00:00
|
|
|
self.logger.info("finished");
|
2016-09-17 16:28:26 +00:00
|
|
|
self.logger.trace(arguments);
|
2016-08-14 12:04:34 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Deploy;
|
2015-06-28 02:20:07 +00:00
|
|
|
|