embark-area-51/lib/contracts/deploy_manager.js

124 lines
3.6 KiB
JavaScript

let async = require('async');
// TODO: remove contractsManager dependency
// * use events
// * in the engine use the instance instead
class DeployManager {
constructor(options) {
const self = this;
this.config = options.config;
this.logger = options.logger;
this.blockchainConfig = this.config.blockchainConfig;
this.events = options.events;
this.plugins = options.plugins;
this.blockchain = options.blockchain;
this.contractsManager = options.contractsManager;
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;
this.logger.info(__("deploying contracts"));
this.events.emit("deploy:beforeAll");
self.events.request('contracts:list', (contracts) => {
async.eachOfSeries(contracts,
function (contract, key, callback) {
contract._gasLimit = self.gasLimit;
self.events.request('deploy:contract', contract, () => {
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"));
self.logger.trace(arguments);
done(err);
}
);
});
}
deployContracts(done) {
let self = this;
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
self.logger.info(__("Blockchain component is disabled in the config").underline);
this.events.emit('blockchainDisabled', {});
return done();
}
async.waterfall([
function buildContracts(callback) {
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
self.contractsManager.build(() => {
callback();
});
},
// TODO: shouldn't be necessary
function checkCompileOnly(callback){
if(self.onlyCompile){
self.events.emit('contractsDeployed', self.contractsManager);
return done();
}
return callback();
},
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(callback) {
self.blockchain.onReady(() => {
self.blockchain.assertNodeConnection((err) => {
callback(err);
});
});
},
// TODO: this can be done on the fly or as part of the initialization
function determineDefaultAccount(callback) {
self.blockchain.determineDefaultAccount((err) => {
callback(err);
});
},
function deployAllContracts(callback) {
self.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', self.contractsManager);
}
if (err && self.fatalErrors) {
return callback(err);
}
callback();
});
},
function runAfterDeploy(callback) {
self.events.emit('contracts:deploy:afterAll');
self.plugins.runActionsForEvent('contracts:deploy:afterAll', callback);
}
], function (err, _result) {
done(err);
});
}
}
module.exports = DeployManager;