embark/lib/modules/profiler/index.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

const asciiTable = require('ascii-table');
const GasEstimator = require('./gasEstimator.js');
class Profiler {
2018-08-01 17:27:50 +00:00
constructor(embark, options) {
this.embark = embark;
this.logger = embark.logger;
this.events = embark.events;
2018-08-01 17:27:50 +00:00
this.plugins = options.plugins;
this.gasEstimator = new GasEstimator(embark);
this.registerConsoleCommand();
2018-08-01 17:27:50 +00:00
this.registerApi();
}
2018-08-08 12:42:45 +00:00
profile(contractName, contract, callback) {
const self = this;
2018-08-01 18:10:02 +00:00
2018-08-01 19:09:16 +00:00
this.profileJSON(contractName, (err, profileObj) => {
if (err) {
2018-08-10 13:22:45 +00:00
return callback(null, "error found in method: " + name + " error: " + JSON.stringify(err));
}
2018-08-01 18:10:02 +00:00
let table = new asciiTable(contractName);
table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates');
profileObj.methods.forEach((method) => {
2018-08-01 19:16:41 +00:00
table.addRow(method.name, method.payable, method.mutability, self.formatParams(method.inputs), self.formatParams(method.outputs), method.gasEstimates);
});
2018-08-08 12:42:45 +00:00
callback(null, table.toString());
});
}
formatParams(params) {
2018-08-01 19:16:41 +00:00
return "(" + (params || []).map(param => param.type).join(',') + ")";
}
registerConsoleCommand() {
const self = this;
self.embark.registerConsoleCommand((cmd, _options) => {
let cmdName = cmd.split(' ')[0];
let contractName = cmd.split(' ')[1];
2018-08-08 12:42:45 +00:00
return {
match: () => cmdName === 'profile',
process: (callback) => {
self.events.request('contracts:contract', contractName, (contract) => {
if (!contract || !contract.deployedAddress) {
2018-08-10 13:22:45 +00:00
return callback(null, "-- couldn't profile " + contractName + " - it's not deployed or could be an interface");
2018-08-08 12:42:45 +00:00
}
this.profile(contractName, contract, callback);
});
}
};
});
}
2018-08-01 17:27:50 +00:00
registerApi() {
const self = this;
let plugin = this.plugins.createPlugin('profiler', {});
plugin.registerAPICall(
'get',
'/embark-api/profiler/:contractName',
(req, res) => {
let contractName = req.params.contractName;
2018-08-01 19:09:16 +00:00
self.profileJSON(contractName, (err, table) => {
if (err) {
return res.send({error: err});
2018-08-01 17:27:50 +00:00
}
2018-08-01 19:09:16 +00:00
res.send(table);
2018-08-01 17:27:50 +00:00
});
}
);
}
}
module.exports = Profiler;