From 7cebbec7b680efb7bd739609a13b99a44b168377 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 21 May 2018 12:29:18 -0400 Subject: [PATCH] make determine arguments async --- lib/contracts/deploy.js | 64 ++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 72525091..934f5fcf 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -17,8 +17,10 @@ class Deploy { this.gasLimit = options.gasLimit; } - determineArguments(suppliedArgs, contract) { - let realArgs = [], l, arg, contractName, referedContract; + // TODO: determining the arguments could also be in a module since it's not + // part of ta 'normal' contract deployment + determineArguments(suppliedArgs, contract, callback) { + let realArgs = [], contractName, referedContract; let args = suppliedArgs; @@ -35,8 +37,8 @@ class Deploy { } } - for (l = 0; l < args.length; l++) { - arg = args[l]; + async.eachLimit(args, 1, (arg, nextEachCb) => { + if (arg[0] === "$") { contractName = arg.substr(1); referedContract = this.contractsManager.getContract(contractName); @@ -56,9 +58,13 @@ class Deploy { } else { realArgs.push(arg); } - } - return realArgs; + + nextEachCb(); + + }, () => { + callback(realArgs); + }); } checkAndDeployContract(contract, params, callback) { @@ -71,9 +77,11 @@ class Deploy { } async.waterfall([ - function determineArguments(next) { - contract.realArgs = self.determineArguments(params || contract.args, contract); - next(); + function _determineArguments(next) { + self.determineArguments(params || contract.args, contract, (realArgs) => { + contract.realArgs = realArgs; + next(); + }); }, function deployIt(next) { if (contract.address !== undefined) { @@ -126,27 +134,31 @@ class Deploy { contractToDeploy(contract, params, callback) { const self = this; - contract.realArgs = self.determineArguments(params || contract.args, contract); - this.deployContract(contract, contract.realArgs, function (err, address) { - if (err) { - self.events.emit("deploy:contract:error", contract); - return callback(new Error(err)); - } - contract.address = address; - self.events.emit("deploy:contract:deployed", contract); + // TODO: refactor to async + self.determineArguments(params || contract.args, contract, (realArgs) => { + contract.realArgs = realArgs; - // always run contractCode so other functionality like 'afterDeploy' can also work - let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager}); - let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); - RunCode.doEval(contractCode, {web3: self.web3}); + this.deployContract(contract, contract.realArgs, function (err, address) { + if (err) { + self.events.emit("deploy:contract:error", contract); + return callback(new Error(err)); + } + contract.address = address; + self.events.emit("deploy:contract:deployed", contract); - let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); + // always run contractCode so other functionality like 'afterDeploy' can also work + let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager}); + let contractCode = codeGenerator.generateContractCode(contract, self.gasLimit); + RunCode.doEval(contractCode, {web3: self.web3}); - async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { - plugin.call(plugin, contract, nextEach); - }, () => { - callback(); + let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); + + async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { + plugin.call(plugin, contract, nextEach); + }, () => { + callback(); + }); }); }); }