From a8e29976b7bd0a1d5d6980d6aec17d398bbbb734 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 18 May 2018 20:26:21 -0400 Subject: [PATCH] move deploy to blockchain component --- lib/contracts/blockchain.js | 20 ++++++++++++++++++++ lib/contracts/deploy.js | 20 +++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 27463d19..677fe900 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -94,6 +94,26 @@ class Blockchain { return new this.web3.eth.Contract(params.abi); } + deployContractObject(contractObject, params) { + return contractObject.deploy({arguments: params.arguments, data: params.data}); + } + + estimateDeployContractGas(deployObject, cb) { + return deployObject.estimateGas().then((gasValue) => { + cb(null, gasValue); + }).catch(cb); + } + + deployContractFromObject(deployContractObject, params, cb) { + deployContractObject.send({ + from: params.from, gas: params.gas, gasPrice: params.gasPrice + }).on('receipt', function(receipt) { + if (receipt.contractAddress !== undefined) { + cb(null, receipt); + } + }).on('error', cb); + } + } module.exports = Blockchain; diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 08b122ee..16d6b33a 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -312,7 +312,7 @@ class Deploy { try { const dataCode = contractCode.startsWith('0x') ? contractCode : "0x" + contractCode; - deployObject = contractObject.deploy({arguments: contractParams, data: dataCode}); + deployObject = self.blockchain.deployContractObject(contractObject, {arguments: contractParams, data: dataCode}); } catch(e) { if (e.message.indexOf('Invalid number of parameters for "undefined"') >= 0) { return next(new Error(__("attempted to deploy %s without specifying parameters", contract.className))); @@ -334,22 +334,20 @@ class Deploy { function deployTheContract(next) { self.logger.info(__("deploying") + " " + contract.className.bold.cyan + " " + __("with").green + " " + contract.gas + " " + __("gas").green); - deployObject.send({ + self.blockchain.deployContractFromObject(deployObject, { from: deploymentAccount, gas: contract.gas, gasPrice: contract.gasPrice - }).on('receipt', function(receipt) { - if (receipt.contractAddress !== undefined) { - self.logger.info(contract.className.bold.cyan + " " + __("deployed at").green + " " + receipt.contractAddress.bold.cyan); - contract.deployedAddress = receipt.contractAddress; - contract.transactionHash = receipt.transactionHash; + }, function(error, receipt) { + if (error) { self.events.emit('contractsState', self.contractsManager.contractsState()); - return next(null, receipt.contractAddress); + 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; self.events.emit('contractsState', self.contractsManager.contractsState()); - }).on('error', function(error) { - self.events.emit('contractsState', self.contractsManager.contractsState()); - return next(new Error(__("error deploying") + " =" + contract.className + "= " + __("due to error") + ": " + error.message)); + return next(null, receipt.contractAddress); }); } ], callback);