2018-05-08 15:30:46 +00:00
|
|
|
const asciiTable = require('ascii-table');
|
2018-07-07 13:30:47 +00:00
|
|
|
const GasEstimator = require('./gasEstimator.js');
|
2018-05-08 15:30:46 +00:00
|
|
|
|
|
|
|
class Profiler {
|
2018-05-08 15:41:38 +00:00
|
|
|
constructor(embark) {
|
2018-05-08 15:30:46 +00:00
|
|
|
this.embark = embark;
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
2018-06-06 19:37:08 +00:00
|
|
|
this.gasEstimator = new GasEstimator(embark);
|
2018-05-08 15:30:46 +00:00
|
|
|
|
|
|
|
this.registerConsoleCommand();
|
|
|
|
}
|
|
|
|
|
2018-06-12 17:12:37 +00:00
|
|
|
profile(contractName, contract) {
|
2018-05-08 15:30:46 +00:00
|
|
|
const self = this;
|
|
|
|
let table = new asciiTable(contractName);
|
2018-06-06 19:37:08 +00:00
|
|
|
table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates');
|
2018-06-15 20:22:27 +00:00
|
|
|
self.gasEstimator.estimateGas(contractName, function(err, gastimates, name) {
|
2018-06-12 17:10:35 +00:00
|
|
|
if (err) {
|
2018-06-15 20:22:27 +00:00
|
|
|
self.logger.error('error found in method: ', name);
|
|
|
|
self.logger.error(JSON.stringify(err));
|
2018-06-12 17:10:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-08 19:26:32 +00:00
|
|
|
contract.abiDefinition.forEach((abiMethod) => {
|
|
|
|
switch(abiMethod.type) {
|
|
|
|
case "constructor":
|
2018-06-12 15:31:26 +00:00
|
|
|
table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates['constructor']);
|
2018-06-08 19:26:32 +00:00
|
|
|
break;
|
2018-06-12 17:10:35 +00:00
|
|
|
case "fallback":
|
|
|
|
table.addRow("fallback", abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates['fallback']);
|
|
|
|
break;
|
2018-06-08 19:26:32 +00:00
|
|
|
default:
|
2018-06-12 15:31:26 +00:00
|
|
|
table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates[abiMethod.name]);
|
2018-06-08 19:26:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
self.logger.info(table.toString());
|
2018-05-08 15:30:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
formatParams(params) {
|
2018-05-08 15:53:34 +00:00
|
|
|
if (!params || !params.length) {
|
|
|
|
return "()";
|
2018-05-08 15:30:46 +00:00
|
|
|
}
|
2018-05-08 15:53:34 +00:00
|
|
|
let paramString = "(";
|
|
|
|
let mappedParams = params.map(param => param.type);
|
|
|
|
paramString += mappedParams.join(',');
|
|
|
|
paramString += ")";
|
|
|
|
return paramString;
|
2018-05-08 15:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
registerConsoleCommand() {
|
|
|
|
const self = this;
|
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
let cmdName = cmd.split(' ')[0];
|
|
|
|
let contractName = cmd.split(' ')[1];
|
|
|
|
if (cmdName === 'profile') {
|
2018-06-12 17:24:21 +00:00
|
|
|
self.events.request('contracts:contract', contractName, (contract) => {
|
|
|
|
if (!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);
|
|
|
|
this.profile(contractName, contract);
|
|
|
|
});
|
2018-05-18 15:41:23 +00:00
|
|
|
return "";
|
2018-05-08 15:30:46 +00:00
|
|
|
}
|
2018-05-18 15:41:23 +00:00
|
|
|
return false;
|
2018-05-08 15:30:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:41:38 +00:00
|
|
|
module.exports = Profiler;
|