embark/lib/contracts/deploy_manager.js

123 lines
3.5 KiB
JavaScript
Raw Normal View History

let async = require('async');
const _ = require('underscore');
2017-03-30 11:12:39 +00:00
class DeployManager {
constructor(options) {
const self = this;
2017-03-30 11:12:39 +00:00
this.config = options.config;
this.logger = options.logger;
this.blockchainConfig = this.config.blockchainConfig;
2017-12-21 16:21:36 +00:00
2017-03-31 11:34:43 +00:00
this.events = options.events;
2017-03-30 11:12:39 +00:00
this.plugins = options.plugins;
2018-05-18 22:31:47 +00:00
this.blockchain = options.blockchain;
2018-01-13 16:38:10 +00:00
this.gasLimit = false;
this.fatalErrors = false;
this.deployOnlyOnConfig = false;
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
this.events.setCommandHandler('deploy:contracts', (cb) => {
self.deployContracts(cb);
});
}
deployAll(done) {
let self = this;
self.events.request('contracts:list', (err, contracts) => {
if (err) {
return done(err);
}
self.logger.info(__("deploying contracts"));
self.events.emit("deploy:beforeAll");
const contractsPerDependencyCount = _.groupBy(contracts, 'dependencyCount');
async.eachSeries(contractsPerDependencyCount,
function (parallelGroups, callback) {
async.each(parallelGroups, (contract, eachCb) => {
contract._gasLimit = self.gasLimit;
self.events.request('deploy:contract', contract, (err) => {
eachCb(err);
});
}, callback);
},
function (err, _results) {
if (err) {
self.logger.error(__("error deploying contracts"));
self.logger.error(err.message);
self.logger.debug(err.stack);
}
if (contracts.length === 0) {
self.logger.info(__("no contracts found"));
return done();
}
self.logger.info(__("finished deploying contracts"));
done(err);
}
);
});
}
2017-03-30 11:12:39 +00:00
deployContracts(done) {
let self = this;
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
2018-05-08 21:49:46 +00:00
self.logger.info(__("Blockchain component is disabled in the config").underline);
2017-03-31 11:34:43 +00:00
this.events.emit('blockchainDisabled', {});
2017-03-30 11:12:39 +00:00
return done();
}
2017-03-30 11:12:39 +00:00
async.waterfall([
function buildContracts(callback) {
self.events.request("contracts:build", self.deployOnlyOnConfig, (err) => {
callback(err);
});
2017-03-30 11:12:39 +00:00
},
// TODO: shouldn't be necessary
function checkCompileOnly(callback){
if(self.onlyCompile){
2018-05-30 10:56:51 +00:00
self.events.emit('contractsDeployed');
return done();
}
return callback();
},
2018-05-20 10:46:12 +00:00
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(callback) {
self.blockchain.onReady(() => {
self.blockchain.assertNodeConnection(callback);
2018-01-05 20:10:47 +00:00
});
2017-03-30 11:12:39 +00: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 11:12:39 +00:00
});
},
2018-05-20 10:46:12 +00:00
function deployAllContracts(callback) {
self.deployAll(function (err) {
2018-05-19 02:40:47 +00:00
if (!err) {
2018-05-30 10:56:51 +00:00
self.events.emit('contractsDeployed');
2018-05-19 02:40:47 +00:00
}
if (err && self.fatalErrors) {
return callback(err);
}
callback();
2017-03-30 11:12:39 +00:00
});
2017-12-21 16:21:36 +00:00
},
function runAfterDeploy(callback) {
2018-05-30 12:00:31 +00:00
self.plugins.emitAndRunActionsForEvent('contracts:deploy:afterAll', callback);
2017-02-24 13:20:03 +00:00
}
], function (err, _result) {
done(err);
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;