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) {
let self = this;
let accounts = [];
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) {
return callback(new Error(err));
}
let contractCode = contract.code;
accounts = _accounts;
deploymentAccount = deploymentAccount || accounts[0];
next();
});
},
function doLinking(next) {
// Applying linked contracts
let contractsList = self.contractsManager.listContracts();
for (let contractObj of contractsList) {
@ -197,25 +205,23 @@ class Deploy {
continue;
}
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);
if (deployedAddress === undefined) {
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);
}
// saving code changes back to contract object
contract.code = contractCode;
next();
},
function applyBeforeDeploy(next) {
// selected in deploy_manager
let deploymentAccount = self.web3.eth.defaultAccount || accounts[0];
let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy');
async.waterfall([
(asyncCallback) => {
//self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length);
async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => {
self.logger.info("running beforeDeploy plugin " + plugin.name + " .");
@ -240,27 +246,24 @@ class Deploy {
}, () => {
//self.logger.info('All beforeDeploy plugins has been processed.');
contractCode = contract.code;
asyncCallback();
next();
});
},
(asyncCallback) => {
function createDeployObject(next) {
let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
let deployObject;
try {
deployObject = contractObject.deploy({arguments: contractParams, data: "0x" + contractCode});
} catch(e) {
if (e.indexOf('Invalid number of parameters for "undefined"') >= 0) {
callback(new Error("attempted to deploy " + contractObject.className + " without specifying parameters"));
asyncCallback();
return;
return next(new Error("attempted to deploy " + contractObject.className + " without specifying parameters"));
} else {
callback(new Error(e));
asyncCallback();
return;
return next(new Error(e));
}
}
next();
},
function deployTheContract(next) {
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);
deployObject.send({
@ -268,34 +271,20 @@ class Deploy {
gas: contract.gas,
gasPrice: contract.gasPrice
}).on('receipt', function(receipt) {
if (err) {
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) {
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;
self.logger.contractsState(self.contractsManager.contractsState());
return callback(null, receipt.contractAddress);
return next(null, receipt.contractAddress);
}
self.logger.contractsState(self.contractsManager.contractsState());
}).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) {