diff --git a/lib/core/engine.js b/lib/core/engine.js index b7d7fcf8..a0d12f9f 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -168,7 +168,7 @@ class Engine { this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard}); this.registerModule('vyper'); - this.registerModule('profiler'); + this.registerModule('profiler', {plugins: this.plugins}); this.registerModule('deploytracker'); this.registerModule('specialconfigs'); this.registerModule('console_listener', {ipc: this.ipc}); diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index 4a490400..ff2260a9 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -2,16 +2,18 @@ const asciiTable = require('ascii-table'); const GasEstimator = require('./gasEstimator.js'); class Profiler { - constructor(embark) { + constructor(embark, options) { this.embark = embark; this.logger = embark.logger; this.events = embark.events; + this.plugins = options.plugins; this.gasEstimator = new GasEstimator(embark); this.registerConsoleCommand(); + this.registerApi(); } - profile(contractName, contract) { + profile(contractName, contract, returnCb) { const self = this; let table = new asciiTable(contractName); table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates'); @@ -19,7 +21,7 @@ class Profiler { if (err) { self.logger.error('error found in method: ', name); self.logger.error(JSON.stringify(err)); - return; + return returnCb(err); } contract.abiDefinition.forEach((abiMethod) => { switch(abiMethod.type) { @@ -33,7 +35,7 @@ class Profiler { table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates[abiMethod.name]); } }); - self.logger.info(table.toString()); + return returnCb(null, table.toString()); }); } @@ -60,13 +62,41 @@ class Profiler { return ""; } self.logger.info("-- profile for " + contractName); - this.profile(contractName, contract); + self.profile(contractName, contract, (err, table) => { + self.logger.info(table); + }); }); return ""; } return false; }); } + + registerApi() { + const self = this; + + let plugin = this.plugins.createPlugin('profiler', {}); + plugin.registerAPICall( + 'get', + '/embark-api/profiler/:contractName', + (req, res) => { + let contractName = req.params.contractName; + //self.events.request('contracts:contract', req.params.contractName, res.send.bind(res)); + self.events.request('contracts:contract', contractName, (contract) => { + if (!contract || !contract.deployedAddress) { + return res.send("-- couldn't profile " + contractName + " - it's not deployed or could be an interface"); + } + self.profile(contractName, contract, (err, table) => { + if (err) { + return res.send(err); + } + res.send(table); + }); + }); + } + ); + } + } module.exports = Profiler;