refactor deploy method

This commit is contained in:
Iuri Matias 2018-05-22 15:37:04 -04:00
parent f4010bd66e
commit cf13f098ac
1 changed files with 22 additions and 33 deletions

View File

@ -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);