2017-03-29 15:37:30 +00:00
|
|
|
let async = require('async');
|
|
|
|
let Deploy = require('./deploy.js');
|
|
|
|
let ContractsManager = require('./contracts.js');
|
2017-03-30 11:12:39 +00:00
|
|
|
let EventEmitter = require('events');
|
2017-03-01 01:42:03 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class DeployManager {
|
|
|
|
constructor(options) {
|
|
|
|
this.config = options.config;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.blockchainConfig = this.config.blockchainConfig;
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
|
|
|
|
Object.create(EventEmitter.prototype);
|
2017-03-01 01:42:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
deployContracts(done) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
|
|
|
self.logger.info("Blockchain component is disabled in the config".underline);
|
|
|
|
this.emit('blockchainDisabled', {});
|
|
|
|
return done();
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function buildContracts(callback) {
|
|
|
|
let contractsManager = new ContractsManager({
|
|
|
|
contractFiles: self.config.contractsFiles,
|
|
|
|
contractsConfig: self.config.contractsConfig,
|
|
|
|
logger: self.logger,
|
|
|
|
plugins: self.plugins
|
|
|
|
});
|
|
|
|
contractsManager.build(callback);
|
|
|
|
},
|
|
|
|
function checkWeb3IsConnected(contractsManager, callback) {
|
|
|
|
if (!self.web3) {
|
|
|
|
return callback(Error("no web3 instance found"));
|
|
|
|
}
|
|
|
|
if (self.web3.currentProvider.isConnected !== undefined && !self.web3.isConnected()) {
|
|
|
|
self.logger.error(("Couldn't connect to an Ethereum node 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);
|
2017-03-11 15:29:45 +00:00
|
|
|
return callback(Error("error connecting to blockchain node"));
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
if (self.web3.currentProvider.isConnected === undefined) {
|
|
|
|
self.web3.version.getNode(function (err, version) {
|
|
|
|
if (err) {
|
|
|
|
return callback(Error("error connecting to blockchain node"));
|
|
|
|
}
|
|
|
|
return callback(null, contractsManager, self.web3);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return callback(null, contractsManager, self.web3);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function setDefaultAccount(contractsManager, web3, callback) {
|
|
|
|
web3.eth.getAccounts(function (err, accounts) {
|
|
|
|
if (err) {
|
|
|
|
return callback(new Error(err));
|
|
|
|
}
|
|
|
|
let accountConfig = self.config.blockchainConfig.account;
|
|
|
|
let selectedAccount = accountConfig && accountConfig.address;
|
|
|
|
web3.eth.defaultAccount = (selectedAccount || accounts[0]);
|
|
|
|
callback(null, contractsManager, web3);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function deployAllContracts(contractsManager, web3, callback) {
|
|
|
|
let deploy = new Deploy({
|
|
|
|
web3: web3,
|
|
|
|
contractsManager: contractsManager,
|
|
|
|
logger: self.logger,
|
|
|
|
chainConfig: self.chainConfig,
|
|
|
|
env: self.config.env
|
|
|
|
});
|
|
|
|
deploy.deployAll(function () {
|
|
|
|
self.emit('contractsDeployed', contractsManager);
|
|
|
|
callback(null, contractsManager);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function (err, result) {
|
2017-02-24 13:20:03 +00:00
|
|
|
if (err) {
|
2017-03-30 11:12:39 +00:00
|
|
|
done(err, null);
|
|
|
|
} else {
|
|
|
|
done(null, result);
|
2017-02-24 13:20:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
}
|
2017-02-24 13:20:03 +00:00
|
|
|
|
|
|
|
module.exports = DeployManager;
|