From cf13f098ac12691ded7006ee7802d69879a8d978 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 22 May 2018 15:37:04 -0400 Subject: [PATCH] refactor deploy method --- lib/contracts/deploy.js | 55 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 959971b1..695103a8 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -102,14 +102,14 @@ class Deploy { // TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { if (!trackedContract) { - return self.contractToDeploy(contract, params, next); + return self.deployContract(contract, next); } self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) { if (codeInChain !== "0x") { self.contractAlreadyDeployed(contract, trackedContract, next); } else { - self.contractToDeploy(contract, params, next); + self.deployContract(contract, next); } }); }); @@ -131,38 +131,10 @@ class Deploy { }); } - contractToDeploy(contract, params, callback) { - const self = this; - - // TODO: this whole callback is almost pointless and should be moved to deployContract - 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: can be moved into a afterDeploy event - // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { - self.events.request('runcode:eval', contractCode); - - let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); - - async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { - plugin.call(plugin, contract, nextEach); - }, () => { - callback(); - }); - }); - }); - } - - deployContract(contract, params, callback) { + deployContract(contract, callback) { let self = this; let accounts = []; - let contractParams = (params || contract.args).slice(); + let contractParams = (contract.realArgs || contract.args).slice(); let contractCode = contract.code; let deploymentAccount = self.blockchain.defaultAccount(); let deployObject; @@ -284,12 +256,29 @@ class Deploy { gasPrice: contract.gasPrice }, function(error, receipt) { if (error) { + contract.error = error.message; + self.events.emit("deploy:contract:error", contract); return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message)); } self.logger.info(contract.className.bold.cyan + " " + __("deployed at").green + " " + receipt.contractAddress.bold.cyan); contract.deployedAddress = receipt.contractAddress; contract.transactionHash = receipt.transactionHash; - return next(null, receipt.contractAddress); + self.events.emit("deploy:contract:receipt", receipt); + self.events.emit("deploy:contract:deployed", contract); + + // TODO: can be moved into a afterDeploy event + // just need to figure out the gasLimit coupling issue + self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { + self.events.request('runcode:eval', contractCode); + + let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); + + async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { + plugin.call(plugin, contract, nextEach); + }, () => { + return next(null, receipt); + }); + }); }); } ], callback);