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();
|
|
|
|
}
|
|
|
|
|
|
|
|
profile(contractName, contract) {
|
|
|
|
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) {
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
contract.abiDefinition.forEach((abiMethod) => {
|
|
|
|
console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]);
|
|
|
|
switch(abiMethod.type) {
|
|
|
|
case "constructor":
|
|
|
|
table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates[abiMethod.name]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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;
|