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