2018-05-20 14:46:36 +00:00
|
|
|
let RunCode = require('../../core/runCode.js');
|
2018-05-20 16:23:48 +00:00
|
|
|
const stringReplaceAsync = require('string-replace-async');
|
|
|
|
const async = require('async');
|
2018-05-20 14:46:36 +00:00
|
|
|
|
|
|
|
class SpecialConfigs {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
|
|
|
this.addCheck = options.addCheck;
|
|
|
|
this.embark = embark;
|
|
|
|
this.contractsConfig = embark.config.contractsConfig;
|
|
|
|
|
|
|
|
this.registerAfterDeployAction();
|
|
|
|
}
|
|
|
|
|
2018-05-20 16:23:48 +00:00
|
|
|
replaceWithAddresses(cmd, cb) {
|
2018-05-20 14:46:36 +00:00
|
|
|
const self = this;
|
2018-05-20 16:23:48 +00:00
|
|
|
let regex = /\$\w+/g;
|
|
|
|
stringReplaceAsync.seq(cmd, regex, (match) => {
|
|
|
|
return (new Promise((resolve, reject) => {
|
|
|
|
let referedContractName = match.slice(1);
|
|
|
|
self.events.request('contracts:contract', referedContractName, (referedContract) => {
|
2018-05-20 14:46:36 +00:00
|
|
|
if (!referedContract) {
|
|
|
|
self.logger.error(referedContractName + ' does not exist');
|
|
|
|
self.logger.error("error running afterDeploy: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContractDoesNotExist"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
if (referedContract && referedContract.deploy === false) {
|
|
|
|
self.logger.error(referedContractName + " exists but has been set to not deploy");
|
|
|
|
self.logger.error("error running afterDeploy: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContracSetToNotdeploy"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
if (referedContract && !referedContract.deployedAddress) {
|
|
|
|
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
|
|
|
|
self.logger.error("error running afterDeploy: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContractAddressNotFound"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
2018-05-20 16:23:48 +00:00
|
|
|
return resolve(referedContract.deployedAddress);
|
2018-05-20 14:46:36 +00:00
|
|
|
});
|
2018-05-20 16:23:48 +00:00
|
|
|
}));
|
|
|
|
}).then((result) => {
|
|
|
|
cb(null, result);
|
|
|
|
}).catch(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerAfterDeployAction() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.embark.registerAfterAllContractsDeploy((cb) => {
|
|
|
|
let afterDeployCmds = self.contractsConfig.afterDeploy || [];
|
2018-05-20 14:46:36 +00:00
|
|
|
|
2018-05-20 16:23:48 +00:00
|
|
|
async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => {
|
|
|
|
self.replaceWithAddresses(cmd, nextMapCb);
|
|
|
|
}, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(new Error("error running afterDeploy"));
|
|
|
|
}
|
|
|
|
let onDeployCode = result;
|
2018-05-20 14:46:36 +00:00
|
|
|
|
2018-05-20 16:23:48 +00:00
|
|
|
// TODO: convert to for to avoid repeated callback
|
|
|
|
for(let cmd of onDeployCode) {
|
|
|
|
self.logger.info("==== executing: " + cmd);
|
|
|
|
try {
|
|
|
|
// TODO: request and re-add web3 object if necessary
|
|
|
|
//RunCode.doEval(cmd, self.blockchain.web3);
|
|
|
|
RunCode.doEval(cmd);
|
|
|
|
} catch(e) {
|
|
|
|
if (e.message.indexOf("invalid opcode") >= 0) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
return cb(new Error(e));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 16:23:48 +00:00
|
|
|
cb();
|
|
|
|
});
|
2018-05-20 14:46:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SpecialConfigs;
|