2018-05-08 15:30:46 +00:00
|
|
|
const asciiTable = require('ascii-table');
|
2018-07-07 13:30:47 +00:00
|
|
|
const GasEstimator = require('./gasEstimator.js');
|
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-08-08 12:42:45 +00:00
|
|
|
profile(contractName, contract, callback) {
|
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-15 20:22:27 +00:00
|
|
|
self.gasEstimator.estimateGas(contractName, function(err, gastimates, name) {
|
2018-06-12 17:10:35 +00:00
|
|
|
if (err) {
|
2018-08-10 13:22:45 +00:00
|
|
|
return callback(null, "error found in method: " + name + " error: " + JSON.stringify(err));
|
2018-06-12 17:10:35 +00:00
|
|
|
}
|
2018-06-08 19:26:32 +00:00
|
|
|
contract.abiDefinition.forEach((abiMethod) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2018-08-08 12:42:45 +00:00
|
|
|
callback(null, 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];
|
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-05-08 15:30:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:41:38 +00:00
|
|
|
module.exports = Profiler;
|