embark/lib/contracts/deploy_manager.js

143 lines
4.9 KiB
JavaScript
Raw Normal View History

let async = require('async');
2018-01-05 20:10:47 +00:00
//require("../utils/debug_util.js")(__filename, async);
let Deploy = require('./deploy.js');
2017-12-21 16:21:36 +00:00
let RunCode = require('../core/runCode.js');
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;
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;
2017-03-30 11:12:39 +00:00
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
this.contractsManager = options.contractsManager;
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;
}
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.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
self.contractsManager.build(() => {
callback();
});
2017-03-30 11:12:39 +00:00
},
function checkCompileOnly(callback){
if(self.onlyCompile){
self.events.emit('contractsDeployed', self.contractsManager);
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.assertNodeConnection((err) => {
callback(err);
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) {
2017-03-30 11:12:39 +00:00
let deploy = new Deploy({
2018-05-18 22:31:47 +00:00
blockchain: self.blockchain,
contractsManager: self.contractsManager,
2017-03-30 11:12:39 +00:00
logger: self.logger,
events: self.events,
2017-03-30 11:12:39 +00:00
chainConfig: self.chainConfig,
2018-01-13 16:38:10 +00:00
env: self.config.env,
plugins: self.plugins,
2018-01-13 16:38:10 +00:00
gasLimit: self.gasLimit
2017-03-30 11:12:39 +00:00
});
2018-01-05 20:10:47 +00:00
2018-05-19 02:40:47 +00:00
deploy.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', self.contractsManager);
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 runAfterDeployCommands(callback) {
2018-05-20 10:46:12 +00:00
// TODO: should instead emit a afterDeploy event and/or run a afterDeploy plugin
2017-12-21 16:21:36 +00:00
let afterDeployCmds = self.config.contractsConfig.afterDeploy || [];
let withErrors = false;
let regex = /\$\w+/g;
let onDeployCode = afterDeployCmds.map((cmd) => {
let realCmd = cmd.replace(regex, (match) => {
let referedContractName = match.slice(1);
let referedContract = self.contractsManager.getContract(referedContractName);
2017-12-21 16:21:36 +00:00
if (!referedContract) {
self.logger.error(referedContractName + ' does not exist');
2018-05-08 21:49:46 +00:00
self.logger.error(__("error running afterDeploy: ") + cmd);
2017-12-21 16:21:36 +00:00
withErrors = true;
return;
}
if (referedContract && referedContract.deploy === false) {
self.logger.error(referedContractName + " exists but has been set to not deploy");
2018-05-08 21:49:46 +00:00
self.logger.error(__("error running afterDeploy: ") + cmd);
2017-12-21 16:21:36 +00:00
withErrors = true;
return;
}
if (referedContract && !referedContract.deployedAddress) {
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
2018-05-08 21:49:46 +00:00
self.logger.error(__("error running afterDeploy: ") + cmd);
2017-12-21 16:21:36 +00:00
withErrors = true;
return;
}
return referedContract.deployedAddress;
});
return realCmd;
});
if (withErrors) {
return callback(new Error("error running afterDeploy"));
}
2017-12-27 18:07:13 +00:00
// TODO: convert to for to avoid repeated callback
for(let cmd of onDeployCode) {
2018-05-08 21:49:46 +00:00
self.logger.info(__("executing") + ": " + cmd);
2017-12-27 18:07:13 +00:00
try {
RunCode.doEval(cmd, {web3: self.blockchain.web3});
2017-12-27 18:07:13 +00:00
} catch(e) {
if (e.message.indexOf("invalid opcode") >= 0) {
2018-05-08 21:49:46 +00:00
self.logger.error(__('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'));
2017-12-27 18:07:13 +00:00
}
return callback(new Error(e));
}
}
2017-12-21 16:21:36 +00:00
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;