embark/lib/contracts/deploy_manager.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

let async = require('async');
2018-01-05 15:10:47 -05:00
//require("../utils/debug_util.js")(__filename, async);
let Deploy = require('./deploy.js');
2017-03-30 20:12:39 +09:00
class DeployManager {
constructor(options) {
this.config = options.config;
this.logger = options.logger;
this.blockchainConfig = this.config.blockchainConfig;
2017-12-21 11:21:36 -05:00
2017-03-31 07:34:43 -04:00
this.events = options.events;
2017-03-30 20:12:39 +09:00
this.plugins = options.plugins;
2018-05-18 18:31:47 -04:00
this.blockchain = options.blockchain;
2017-03-30 20:12:39 +09:00
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
this.contractsManager = options.contractsManager;
2018-01-13 11:38:10 -05:00
this.gasLimit = false;
this.fatalErrors = false;
this.deployOnlyOnConfig = false;
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
}
2017-03-30 20:12:39 +09:00
deployContracts(done) {
let self = this;
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
2018-05-08 17:49:46 -04:00
self.logger.info(__("Blockchain component is disabled in the config").underline);
2017-03-31 07:34:43 -04:00
this.events.emit('blockchainDisabled', {});
2017-03-30 20:12:39 +09:00
return done();
}
2017-03-30 20:12:39 +09:00
async.waterfall([
function buildContracts(callback) {
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
self.contractsManager.build(() => {
callback();
});
2017-03-30 20:12:39 +09:00
},
// TODO: shouldn't be necessary
function checkCompileOnly(callback){
if(self.onlyCompile){
self.events.emit('contractsDeployed', self.contractsManager);
return done();
}
return callback();
},
2018-05-20 06:46:12 -04:00
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(callback) {
self.blockchain.assertNodeConnection((err) => {
callback(err);
2018-01-05 15:10:47 -05:00
});
2017-03-30 20:12:39 +09:00
},
// TODO: this can be done on the fly or as part of the initialization
function determineDefaultAccount(callback) {
self.blockchain.determineDefaultAccount((err) => {
callback(err);
2017-03-30 20:12:39 +09:00
});
},
2018-05-20 06:46:12 -04:00
function deployAllContracts(callback) {
2017-03-30 20:12:39 +09:00
let deploy = new Deploy({
2018-05-18 18:31:47 -04:00
blockchain: self.blockchain,
contractsManager: self.contractsManager,
2017-03-30 20:12:39 +09:00
logger: self.logger,
events: self.events,
2017-03-30 20:12:39 +09:00
chainConfig: self.chainConfig,
2018-01-13 11:38:10 -05:00
env: self.config.env,
plugins: self.plugins,
2018-01-13 11:38:10 -05:00
gasLimit: self.gasLimit
2017-03-30 20:12:39 +09:00
});
2018-01-05 15:10:47 -05:00
2018-05-18 22:40:47 -04:00
deploy.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', self.contractsManager);
2018-05-18 22:40:47 -04:00
}
if (err && self.fatalErrors) {
return callback(err);
}
callback();
2017-03-30 20:12:39 +09:00
});
2017-12-21 11:21:36 -05:00
},
function runAfterDeploy(callback) {
let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions');
2017-12-21 11:21:36 -05:00
async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) {
plugin.call(plugin, nextEach);
}, () => {
callback();
2017-12-21 11:21:36 -05:00
});
2017-02-24 08:20:03 -05:00
}
], function (err, _result) {
done(err);
2017-02-24 08:20:03 -05:00
});
}
2017-03-30 20:12:39 +09:00
}
2017-02-24 08:20:03 -05:00
module.exports = DeployManager;