refactor deployContract method
This commit is contained in:
parent
8eb6298ece
commit
70f72f494d
|
@ -174,128 +174,117 @@ 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) {
|
||||
if (err) {
|
||||
return callback(new Error(err));
|
||||
}
|
||||
|
||||
let contractCode = contract.code;
|
||||
|
||||
// Applying linked contracts
|
||||
let contractsList = self.contractsManager.listContracts();
|
||||
for (let contractObj of contractsList) {
|
||||
let filename = contractObj.filename;
|
||||
let deployedAddress = contractObj.deployedAddress;
|
||||
if (deployedAddress) {
|
||||
deployedAddress = deployedAddress.substr(2);
|
||||
async.waterfall([
|
||||
function getAccounts(next) {
|
||||
self.web3.eth.getAccounts(function (err, _accounts) {
|
||||
if (err) {
|
||||
return callback(new Error(err));
|
||||
}
|
||||
accounts = _accounts;
|
||||
deploymentAccount = deploymentAccount || accounts[0];
|
||||
next();
|
||||
});
|
||||
},
|
||||
function doLinking(next) {
|
||||
// Applying linked contracts
|
||||
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;
|
||||
if (contractCode.indexOf(linkReference) < 0) {
|
||||
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));
|
||||
}
|
||||
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
|
||||
contract.code = contractCode;
|
||||
next();
|
||||
},
|
||||
function applyBeforeDeploy(next) {
|
||||
// selected in deploy_manager
|
||||
let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy');
|
||||
|
||||
// saving code changes back to contract object
|
||||
contract.code = contractCode;
|
||||
//self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length);
|
||||
async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => {
|
||||
self.logger.info("running beforeDeploy plugin " + plugin.name + " .");
|
||||
|
||||
// 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 + " .");
|
||||
|
||||
// calling each beforeDeploy handler declared by the plugin
|
||||
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();
|
||||
// calling each beforeDeploy handler declared by the plugin
|
||||
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 plugins has been processed.');
|
||||
contractCode = contract.code;
|
||||
asyncCallback();
|
||||
//self.logger.info('All beforeDeploy handlers of the plugin has processed.');
|
||||
eachPluginCb();
|
||||
});
|
||||
},
|
||||
(asyncCallback) => {
|
||||
let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
|
||||
let deployObject;
|
||||
}, () => {
|
||||
//self.logger.info('All beforeDeploy plugins has been processed.');
|
||||
contractCode = contract.code;
|
||||
next();
|
||||
});
|
||||
},
|
||||
function createDeployObject(next) {
|
||||
let contractObject = new self.web3.eth.Contract(contract.abiDefinition);
|
||||
|
||||
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;
|
||||
} else {
|
||||
callback(new Error(e));
|
||||
asyncCallback();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
deployObject = contractObject.deploy({arguments: contractParams, data: "0x" + contractCode});
|
||||
} catch(e) {
|
||||
if (e.indexOf('Invalid number of parameters for "undefined"') >= 0) {
|
||||
return next(new Error("attempted to deploy " + contractObject.className + " without specifying parameters"));
|
||||
} else {
|
||||
return next(new Error(e));
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue