2018-06-08 17:46:57 +00:00
|
|
|
const async = require('async');
|
2018-07-07 13:30:47 +00:00
|
|
|
const ContractFuzzer = require('./fuzzer.js');
|
2019-11-13 18:59:10 +00:00
|
|
|
const Web3 = require('web3');
|
2018-06-06 19:37:08 +00:00
|
|
|
|
|
|
|
class GasEstimator {
|
|
|
|
constructor(embark) {
|
|
|
|
this.embark = embark;
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.fuzzer = new ContractFuzzer(embark);
|
|
|
|
}
|
|
|
|
|
2018-06-08 17:46:57 +00:00
|
|
|
estimateGas(contractName, cb) {
|
2018-06-06 20:30:29 +00:00
|
|
|
const self = this;
|
2018-06-06 19:37:08 +00:00
|
|
|
let gasMap = {};
|
2019-11-13 18:59:10 +00:00
|
|
|
self.events.request("blockchain:client:provider", "ethereum", (err, provider) => {
|
|
|
|
const web3 = new Web3(provider);
|
|
|
|
self.events.request('contracts:contract', contractName, (err, contract) => {
|
|
|
|
if (err) return cb(err);
|
2019-01-22 14:57:08 +00:00
|
|
|
let fuzzMap = self.fuzzer.generateFuzz(3, contract);
|
|
|
|
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
|
|
|
|
async.each(contract.abiDefinition.filter((x) => x.type !== "event"),
|
|
|
|
(abiMethod, gasCb) => {
|
|
|
|
let name = abiMethod.name;
|
|
|
|
if (abiMethod.type === "constructor") {
|
|
|
|
// already provided for us
|
2019-11-13 18:59:10 +00:00
|
|
|
gasMap['constructor'] = parseFloat(contract.gasEstimates.creation.totalCost.toString());
|
2019-01-22 14:57:08 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
|
|
|
} else if (abiMethod.type === "fallback") {
|
2019-11-13 18:59:10 +00:00
|
|
|
gasMap['fallback'] = parseFloat(contract.gasEstimates.external[""].toString());
|
2019-01-22 14:57:08 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
|
|
|
} else if (
|
|
|
|
(abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0)
|
|
|
|
) {
|
|
|
|
// just run it and register it
|
|
|
|
contractObj.methods[name]
|
|
|
|
.apply(contractObj.methods[name], [])
|
|
|
|
.estimateGas((err, gasAmount) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err);
|
|
|
|
return gasCb(null, name, abiMethod.type);
|
|
|
|
}
|
|
|
|
gasMap[name] = gasAmount;
|
2018-12-20 18:16:33 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
2019-01-22 14:57:08 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// async concatenate all the fuzz values and their gas cost outputs and check for equality
|
|
|
|
async.concat(fuzzMap[name], (values, getVarianceCb) => {
|
|
|
|
contractObj.methods[name].apply(contractObj.methods[name], values)
|
|
|
|
.estimateGas((err, gasAmount) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err);
|
|
|
|
}
|
|
|
|
getVarianceCb(null, gasAmount);
|
|
|
|
});
|
|
|
|
}, (err, variance) => {
|
|
|
|
if (variance.every(v => v === variance[0])) {
|
|
|
|
gasMap[name] = variance[0];
|
|
|
|
} else {
|
|
|
|
// get average
|
|
|
|
let sum = variance.reduce(function(memo, num) { return memo + num; });
|
|
|
|
gasMap[name] = sum / variance.length;
|
2018-12-20 18:16:33 +00:00
|
|
|
}
|
2019-01-22 14:57:08 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
2018-06-08 19:26:32 +00:00
|
|
|
});
|
2019-01-22 14:57:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(err, name, type) => {
|
|
|
|
if (err) {
|
|
|
|
if (type === "constructor" || type === "fallback") name = type;
|
|
|
|
return cb(err, null, name);
|
|
|
|
}
|
|
|
|
cb(null, gasMap, null);
|
2018-06-15 20:22:27 +00:00
|
|
|
}
|
2019-01-22 14:57:08 +00:00
|
|
|
);
|
|
|
|
});
|
2018-06-06 19:37:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GasEstimator;
|