refactor deployContract method

This commit is contained in:
Iuri Matias 2018-01-18 14:41:33 -05:00
parent 8eb6298ece
commit 70f72f494d
1 changed files with 99 additions and 110 deletions

View File

@ -174,16 +174,24 @@ class Deploy {
deployContract(contract, params, callback) { deployContract(contract, params, callback) {
let self = this; let self = this;
let accounts = [];
let contractParams = (params || contract.args).slice(); let contractParams = (params || contract.args).slice();
let contractCode = contract.code;
let deploymentAccount = self.web3.eth.defaultAccount;
let deployObject;
this.web3.eth.getAccounts(function (err, accounts) { async.waterfall([
function getAccounts(next) {
self.web3.eth.getAccounts(function (err, _accounts) {
if (err) { if (err) {
return callback(new Error(err)); return callback(new Error(err));
} }
accounts = _accounts;
let contractCode = contract.code; deploymentAccount = deploymentAccount || accounts[0];
next();
});
},
function doLinking(next) {
// Applying linked contracts // Applying linked contracts
let contractsList = self.contractsManager.listContracts(); let contractsList = self.contractsManager.listContracts();
for (let contractObj of contractsList) { for (let contractObj of contractsList) {
@ -197,25 +205,23 @@ class Deploy {
continue; continue;
} }
if (linkReference.length > 40) { if (linkReference.length > 40) {
return callback(new Error(linkReference + " is too long, try reducing the path of the contract (" + filename + ") and/or its name " + contractObj.className)); return next(new Error(linkReference + " is too long, try reducing the path of the contract (" + filename + ") and/or its name " + contractObj.className));
} }
let toReplace = linkReference + "_".repeat(40 - linkReference.length); let toReplace = linkReference + "_".repeat(40 - linkReference.length);
if (deployedAddress === undefined) { if (deployedAddress === undefined) {
let libraryName = contractObj.className; let libraryName = contractObj.className;
return callback(new Error(contract.className + " needs " + libraryName + " but an address was not found, did you deploy it or configured an address?")); return next(new Error(contract.className + " needs " + libraryName + " but an address was not found, did you deploy it or configured an address?"));
} }
contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress); contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress);
} }
// saving code changes back to contract object // saving code changes back to contract object
contract.code = contractCode; contract.code = contractCode;
next();
},
function applyBeforeDeploy(next) {
// selected in deploy_manager // selected in deploy_manager
let deploymentAccount = self.web3.eth.defaultAccount || accounts[0];
let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy'); let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy');
async.waterfall([
(asyncCallback) => {
//self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length); //self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length);
async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => { async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => {
self.logger.info("running beforeDeploy plugin " + plugin.name + " ."); self.logger.info("running beforeDeploy plugin " + plugin.name + " .");
@ -240,27 +246,24 @@ class Deploy {
}, () => { }, () => {
//self.logger.info('All beforeDeploy plugins has been processed.'); //self.logger.info('All beforeDeploy plugins has been processed.');
contractCode = contract.code; contractCode = contract.code;
asyncCallback(); next();
}); });
}, },
(asyncCallback) => { function createDeployObject(next) {
let contractObject = new self.web3.eth.Contract(contract.abiDefinition); let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
let deployObject;
try { try {
deployObject = contractObject.deploy({arguments: contractParams, data: "0x" + contractCode}); deployObject = contractObject.deploy({arguments: contractParams, data: "0x" + contractCode});
} catch(e) { } catch(e) {
if (e.indexOf('Invalid number of parameters for "undefined"') >= 0) { if (e.indexOf('Invalid number of parameters for "undefined"') >= 0) {
callback(new Error("attempted to deploy " + contractObject.className + " without specifying parameters")); return next(new Error("attempted to deploy " + contractObject.className + " without specifying parameters"));
asyncCallback();
return;
} else { } else {
callback(new Error(e)); return next(new Error(e));
asyncCallback();
return;
} }
} }
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({ deployObject.send({
@ -268,34 +271,20 @@ class Deploy {
gas: contract.gas, gas: contract.gas,
gasPrice: contract.gasPrice gasPrice: contract.gasPrice
}).on('receipt', function(receipt) { }).on('receipt', function(receipt) {
if (err) { if (receipt.contractAddress !== undefined) {
self.logger.error("error deploying contract: " + contract.className.cyan);
let errMsg = err.toString();
if (errMsg === 'Error: The contract code couldn\'t be stored, please check your gas amount.') {
errMsg = 'The contract code couldn\'t be stored, out of gas or constructor error';
}
self.logger.error(errMsg);
contract.error = errMsg;
self.logger.contractsState(self.contractsManager.contractsState());
return callback(new Error(err));
} else if (receipt.contractAddress !== undefined) {
self.logger.info(contract.className.bold.cyan + " deployed at ".green + receipt.contractAddress.bold.cyan); self.logger.info(contract.className.bold.cyan + " deployed at ".green + receipt.contractAddress.bold.cyan);
contract.deployedAddress = receipt.contractAddress; contract.deployedAddress = receipt.contractAddress;
contract.transactionHash = receipt.transactionHash; contract.transactionHash = receipt.transactionHash;
self.logger.contractsState(self.contractsManager.contractsState()); self.logger.contractsState(self.contractsManager.contractsState());
return callback(null, receipt.contractAddress); return next(null, receipt.contractAddress);
} }
self.logger.contractsState(self.contractsManager.contractsState()); self.logger.contractsState(self.contractsManager.contractsState());
}).on('error', function(error) { }).on('error', function(error) {
return callback(new Error("error deploying =" + contract.className + "= due to error: " + error.message)); self.logger.contractsState(self.contractsManager.contractsState());
return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message));
}); });
asyncCallback();
} }
]); // end of async.waterfall ], callback);
});
} }
deployAll(done) { deployAll(done) {