refactor profile caller

This commit is contained in:
Iuri Matias 2018-08-01 15:09:16 -04:00
parent 1504a09a99
commit f877cd06ad

View File

@ -13,42 +13,47 @@ class Profiler {
this.registerApi();
}
profileJSON(contractName, contract, returnCb) {
profileJSON(contractName, returnCb) {
const self = this;
let profileObj = {};
profileObj.name = contractName;
profileObj.methods = [];
self.gasEstimator.estimateGas(contractName, function(err, gastimates, name) {
if (err) {
return returnCb(err);
self.events.request('contracts:contract', contractName, (contract) => {
if (!contract || !contract.deployedAddress) {
return returnCb("-- couldn't profile " + contractName + " - it's not deployed or could be an interface");
}
contract.abiDefinition.forEach((abiMethod) => {
let methodName = abiMethod.name;
if (['constructor', 'fallback'].indexOf(abiMethod.type) >= 0) {
methodName = abiMethod.type;
self.gasEstimator.estimateGas(contractName, function(err, gastimates, name) {
if (err) {
return returnCb(err);
}
profileObj.methods.push({
name: methodName,
payable: abiMethod.payable,
mutability: abiMethod.stateMutability,
inputs: self.formatParams(abiMethod.inputs),
outputs: self.formatParams(abiMethod.outputs),
gasEstimates: gastimates[methodName]
});
});
contract.abiDefinition.forEach((abiMethod) => {
let methodName = abiMethod.name;
if (['constructor', 'fallback'].indexOf(abiMethod.type) >= 0) {
methodName = abiMethod.type;
}
returnCb(null, profileObj);
profileObj.methods.push({
name: methodName,
payable: abiMethod.payable,
mutability: abiMethod.stateMutability,
inputs: self.formatParams(abiMethod.inputs),
outputs: self.formatParams(abiMethod.outputs),
gasEstimates: gastimates[methodName]
});
});
returnCb(null, profileObj);
});
});
}
profile(contractName, contract, returnCb) {
profile(contractName, returnCb) {
const self = this;
this.profileJSON(contractName, contract, (err, profileObj) => {
this.profileJSON(contractName, (err, profileObj) => {
if (err) {
self.logger.error(JSON.stringify(err));
return returnCb(err);
@ -80,17 +85,11 @@ class Profiler {
let cmdName = cmd.split(' ')[0];
let contractName = cmd.split(' ')[1];
if (cmdName === 'profile') {
self.events.request('contracts:contract', contractName, (contract) => {
if (!contract || !contract.deployedAddress) {
self.logger.info("-- couldn't profile " + contractName + " - it's not deployed or could be an interface");
return "";
}
self.logger.info("-- profile for " + contractName);
self.profile(contractName, contract, (err, table) => {
self.logger.info(table);
});
self.logger.info("-- profile for " + contractName);
self.profile(contractName, (err, table) => {
self.logger.info(table);
});
return "";
return "";
}
return false;
});
@ -105,16 +104,12 @@ class Profiler {
'/embark-api/profiler/:contractName',
(req, res) => {
let contractName = req.params.contractName;
self.events.request('contracts:contract', contractName, (contract) => {
if (!contract || !contract.deployedAddress) {
return res.send({error: "-- couldn't profile " + contractName + " - it's not deployed or could be an interface"});
self.profileJSON(contractName, (err, table) => {
if (err) {
return res.send({error: err});
}
self.profile(contractName, contract, (err, table) => {
if (err) {
return res.send({error: err});
}
res.send(table);
});
res.send(table);
});
}
);