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,128 +174,117 @@ 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([
if (err) { function getAccounts(next) {
return callback(new Error(err)); self.web3.eth.getAccounts(function (err, _accounts) {
} if (err) {
return callback(new Error(err));
let contractCode = contract.code; }
accounts = _accounts;
// Applying linked contracts deploymentAccount = deploymentAccount || accounts[0];
let contractsList = self.contractsManager.listContracts(); next();
for (let contractObj of contractsList) { });
let filename = contractObj.filename; },
let deployedAddress = contractObj.deployedAddress; function doLinking(next) {
if (deployedAddress) { // Applying linked contracts
deployedAddress = deployedAddress.substr(2); let contractsList = self.contractsManager.listContracts();
for (let contractObj of contractsList) {
let filename = contractObj.filename;
let deployedAddress = contractObj.deployedAddress;
if (deployedAddress) {
deployedAddress = deployedAddress.substr(2);
}
let linkReference = '__' + filename + ":" + contractObj.className;
if (contractCode.indexOf(linkReference) < 0) {
continue;
}
if (linkReference.length > 40) {
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 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);
} }
let linkReference = '__' + filename + ":" + contractObj.className; // saving code changes back to contract object
if (contractCode.indexOf(linkReference) < 0) { contract.code = contractCode;
continue; next();
} },
if (linkReference.length > 40) { function applyBeforeDeploy(next) {
return callback(new Error(linkReference + " is too long, try reducing the path of the contract (" + filename + ") and/or its name " + contractObj.className)); // selected in deploy_manager
} let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy');
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?"));
}
contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress);
}
// saving code changes back to contract object //self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length);
contract.code = contractCode; async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => {
self.logger.info("running beforeDeploy plugin " + plugin.name + " .");
// selected in deploy_manager // calling each beforeDeploy handler declared by the plugin
let deploymentAccount = self.web3.eth.defaultAccount || accounts[0]; async.eachSeries(plugin.beforeDeploy, (beforeDeployFn, eachCb) => {
let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy'); beforeDeployFn({
embarkDeploy: self,
async.waterfall([ pluginConfig: plugin.pluginConfig,
(asyncCallback) => { deploymentAccount: deploymentAccount,
//self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length); contract: contract,
async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => { callback:
self.logger.info("running beforeDeploy plugin " + plugin.name + " ."); (function(resObj){
contract.code = resObj.contractCode;
// calling each beforeDeploy handler declared by the plugin eachCb();
async.eachSeries(plugin.beforeDeploy, (beforeDeployFn, eachCb) => { })
beforeDeployFn({
embarkDeploy: self,
pluginConfig: plugin.pluginConfig,
deploymentAccount: deploymentAccount,
contract: contract,
callback:
(function(resObj){
contract.code = resObj.contractCode;
eachCb();
})
});
}, () => {
//self.logger.info('All beforeDeploy handlers of the plugin has processed.');
eachPluginCb();
}); });
}, () => { }, () => {
//self.logger.info('All beforeDeploy plugins has been processed.'); //self.logger.info('All beforeDeploy handlers of the plugin has processed.');
contractCode = contract.code; eachPluginCb();
asyncCallback();
}); });
}, }, () => {
(asyncCallback) => { //self.logger.info('All beforeDeploy plugins has been processed.');
let contractObject = new self.web3.eth.Contract(contract.abiDefinition); contractCode = contract.code;
let deployObject; next();
});
},
function createDeployObject(next) {
let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
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(); } else {
return; return next(new Error(e));
} else {
callback(new Error(e));
asyncCallback();
return;
}
} }
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);
deployObject.send({
from: deploymentAccount,
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) {
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);
}
self.logger.contractsState(self.contractsManager.contractsState());
}).on('error', function(error) {
return callback(new Error("error deploying =" + contract.className + "= due to error: " + error.message));
});
asyncCallback();
} }
]); // end of async.waterfall next();
},
function deployTheContract(next) {
self.logger.info("deploying " + contract.className.bold.cyan + " with ".green + contract.gas + " gas".green);
deployObject.send({
}); 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;
self.logger.contractsState(self.contractsManager.contractsState());
return next(null, receipt.contractAddress);
}
self.logger.contractsState(self.contractsManager.contractsState());
}).on('error', function(error) {
self.logger.contractsState(self.contractsManager.contractsState());
return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message));
});
}
], callback);
} }
deployAll(done) { deployAll(done) {