2017-02-24 13:20:03 +00:00
|
|
|
var async = require('async');
|
|
|
|
var Web3 = require('web3');
|
|
|
|
|
|
|
|
var Deploy = require('./deploy.js');
|
|
|
|
var ContractsManager = require('./contracts.js');
|
|
|
|
|
|
|
|
var DeployManager = function(options) {
|
|
|
|
this.config = options.config;
|
|
|
|
this.logger = options.logger;
|
2017-03-01 01:42:03 +00:00
|
|
|
this.blockchainConfig = this.config.blockchainConfig;
|
2017-02-24 13:20:03 +00:00
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.events = options.events;
|
2017-03-03 06:22:12 +00:00
|
|
|
this.web3 = options.web3;
|
|
|
|
// TODO: lol, redo this
|
|
|
|
if (options.trackContracts !== false) {
|
|
|
|
this.chainConfig = this.config.chainTracker;
|
|
|
|
} else {
|
|
|
|
this.chainConfig = false;
|
|
|
|
}
|
2017-02-24 13:20:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DeployManager.prototype.deployContracts = function(done) {
|
|
|
|
var self = this;
|
2017-03-01 01:42:03 +00:00
|
|
|
|
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
self.logger.info("Blockchain component is disabled in the config".underline);
|
2017-03-01 04:29:16 +00:00
|
|
|
self.events.emit('blockchainDisabled', {});
|
2017-03-01 01:42:03 +00:00
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
2017-02-24 13:20:03 +00:00
|
|
|
async.waterfall([
|
2017-02-25 03:49:34 +00:00
|
|
|
function buildContracts(callback) {
|
2017-02-24 13:20:03 +00:00
|
|
|
var contractsManager = new ContractsManager({
|
|
|
|
contractFiles: self.config.contractsFiles,
|
|
|
|
contractsConfig: self.config.contractsConfig,
|
|
|
|
logger: self.logger,
|
|
|
|
plugins: self.plugins
|
|
|
|
});
|
|
|
|
contractsManager.build(callback);
|
|
|
|
},
|
2017-03-03 06:22:12 +00:00
|
|
|
function deployContracts2(contractsManager, callback) {
|
2017-02-24 13:20:03 +00:00
|
|
|
|
|
|
|
//TODO: figure out where to put this since the web3 can be passed along if needed
|
|
|
|
// perhaps it should go into the deploy object itself
|
|
|
|
// TODO: should come from the config object
|
2017-03-03 06:22:12 +00:00
|
|
|
var web3;
|
|
|
|
if (self.web3) {
|
|
|
|
web3 = self.web3;
|
|
|
|
} else {
|
|
|
|
web3 = new Web3();
|
|
|
|
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
|
|
|
|
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
|
2017-02-24 13:20:03 +00:00
|
|
|
|
2017-03-03 06:22:12 +00:00
|
|
|
if (!web3.isConnected()) {
|
|
|
|
self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red);
|
|
|
|
self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta);
|
|
|
|
return callback(Error("error connecting to blockchain node"));
|
|
|
|
}
|
2017-02-24 13:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
web3.eth.getAccounts(function(err, accounts) {
|
|
|
|
if (err) {
|
|
|
|
return callback(new Error(err));
|
|
|
|
}
|
|
|
|
web3.eth.defaultAccount = accounts[0];
|
|
|
|
|
|
|
|
var deploy = new Deploy({
|
|
|
|
web3: web3,
|
|
|
|
contractsManager: contractsManager,
|
|
|
|
logger: self.logger,
|
2017-03-03 06:22:12 +00:00
|
|
|
chainConfig: self.chainConfig,
|
2017-02-24 13:20:03 +00:00
|
|
|
env: self.config.env
|
|
|
|
});
|
|
|
|
deploy.deployAll(function() {
|
2017-03-01 04:29:16 +00:00
|
|
|
self.events.emit('contractsDeployed', contractsManager);
|
2017-03-02 02:09:55 +00:00
|
|
|
callback(null, contractsManager);
|
2017-02-24 13:20:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
done(err, null);
|
|
|
|
} else {
|
|
|
|
done(null, result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DeployManager;
|
|
|
|
|