2018-05-08 15:30:46 +00:00
|
|
|
const asciiTable = require('ascii-table');
|
2018-06-06 19:37:08 +00:00
|
|
|
const GasEstimator = require('../gasEstimator');
|
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:10:35 +00:00
|
|
|
profile(contractName, contract, cb) {
|
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-08 19:26:32 +00:00
|
|
|
self.gasEstimator.estimateGas(contractName, function(err, gastimates) {
|
2018-06-12 17:10:35 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-08 19:26:32 +00:00
|
|
|
contract.abiDefinition.forEach((abiMethod) => {
|
|
|
|
console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]);
|
|
|
|
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') {
|
|
|
|
self.events.request('contracts:contract', contractName, (contract) => {
|
|
|
|
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;
|