finish converting afterDeploy module

This commit is contained in:
Iuri Matias 2018-05-20 12:23:48 -04:00 committed by Jonathan Rainville
parent 2064d26914
commit 31833579a7
5 changed files with 201 additions and 182 deletions

View File

@ -29,6 +29,10 @@ class ContractsManager {
self.events.setCommandHandler('contracts:list', (cb) => {
cb(self.listContracts());
});
self.events.setCommandHandler("contracts:contract", (contractName, cb) => {
cb(self.getContract(contractName));
});
}
build(done) {

View File

@ -17,10 +17,6 @@ class Deploy {
this.chainConfig = options.chainConfig;
this.plugins = options.plugins;
this.gasLimit = options.gasLimit;
this.events.setCommandHandler("contracts:contract", (contractName, cb) => {
cb(this.contractsManager.getContract(contractName));
});
}
determineArguments(suppliedArgs, contract) {

View File

@ -1,4 +1,6 @@
let RunCode = require('../../core/runCode.js');
const stringReplaceAsync = require('string-replace-async');
const async = require('async');
class SpecialConfigs {
@ -13,59 +15,66 @@ class SpecialConfigs {
this.registerAfterDeployAction();
}
replaceWithAddresses(cmd, cb) {
const self = this;
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) => {
if (!referedContract) {
self.logger.error(referedContractName + ' does not exist');
self.logger.error("error running afterDeploy: " + cmd);
return reject(new Error("ReferedContractDoesNotExist"));
}
if (referedContract && referedContract.deploy === false) {
self.logger.error(referedContractName + " exists but has been set to not deploy");
self.logger.error("error running afterDeploy: " + cmd);
return reject(new Error("ReferedContracSetToNotdeploy"));
}
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);
return reject(new Error("ReferedContractAddressNotFound"));
}
return resolve(referedContract.deployedAddress);
});
}));
}).then((result) => {
cb(null, result);
}).catch(cb);
}
registerAfterDeployAction() {
const self = this;
this.embark.registerAfterAllContractsDeploy((cb) => {
let afterDeployCmds = self.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);
if (!referedContract) {
self.logger.error(referedContractName + ' does not exist');
self.logger.error("error running afterDeploy: " + cmd);
withErrors = true;
return;
}
if (referedContract && referedContract.deploy === false) {
self.logger.error(referedContractName + " exists but has been set to not deploy");
self.logger.error("error running afterDeploy: " + cmd);
withErrors = true;
return;
}
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);
withErrors = true;
return;
}
return referedContract.deployedAddress;
});
return realCmd;
});
if (withErrors) {
return cb(new Error("error running afterDeploy"));
}
// TODO: convert to for to avoid repeated callback
for(let cmd of onDeployCode) {
self.logger.info("executing: " + cmd);
try {
RunCode.doEval(cmd, self.blockchain.web3);
} 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));
async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => {
self.replaceWithAddresses(cmd, nextMapCb);
}, (err, result) => {
if (err) {
return cb(new Error("error running afterDeploy"));
}
}
let onDeployCode = result;
cb();
// 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));
}
}
cb();
});
});
}

275
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -61,6 +61,7 @@
"serve-static": "^1.11.1",
"shelljs": "^0.5.0",
"solc": "0.4.23",
"string-replace-async": "^1.2.1",
"style-loader": "^0.19.0",
"tar": "^3.1.5",
"toposort": "^1.0.0",