move deploy to blockchain component

This commit is contained in:
Iuri Matias 2018-05-18 20:26:21 -04:00 committed by Jonathan Rainville
parent 9b1a78cdaa
commit a8e29976b7
2 changed files with 29 additions and 11 deletions

View File

@ -94,6 +94,26 @@ class Blockchain {
return new this.web3.eth.Contract(params.abi); 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; module.exports = Blockchain;

View File

@ -312,7 +312,7 @@ class Deploy {
try { try {
const dataCode = contractCode.startsWith('0x') ? contractCode : "0x" + contractCode; 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) { } catch(e) {
if (e.message.indexOf('Invalid number of parameters for "undefined"') >= 0) { if (e.message.indexOf('Invalid number of parameters for "undefined"') >= 0) {
return next(new Error(__("attempted to deploy %s without specifying parameters", contract.className))); return next(new Error(__("attempted to deploy %s without specifying parameters", contract.className)));
@ -334,22 +334,20 @@ class Deploy {
function deployTheContract(next) { function deployTheContract(next) {
self.logger.info(__("deploying") + " " + contract.className.bold.cyan + " " + __("with").green + " " + contract.gas + " " + __("gas").green); self.logger.info(__("deploying") + " " + contract.className.bold.cyan + " " + __("with").green + " " + contract.gas + " " + __("gas").green);
deployObject.send({ self.blockchain.deployContractFromObject(deployObject, {
from: deploymentAccount, from: deploymentAccount,
gas: contract.gas, gas: contract.gas,
gasPrice: contract.gasPrice gasPrice: contract.gasPrice
}).on('receipt', function(receipt) { }, function(error, receipt) {
if (receipt.contractAddress !== undefined) { if (error) {
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()); 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()); self.events.emit('contractsState', self.contractsManager.contractsState());
}).on('error', function(error) { return next(null, receipt.contractAddress);
self.events.emit('contractsState', self.contractsManager.contractsState());
return next(new Error(__("error deploying") + " =" + contract.className + "= " + __("due to error") + ": " + error.message));
}); });
} }
], callback); ], callback);