Extracted gas estimate to individual function

This commit is contained in:
Richard Ramos 2018-04-20 21:14:50 -04:00
parent 56879acc00
commit 67e1c4939b
1 changed files with 16 additions and 14 deletions

View File

@ -94,6 +94,19 @@ class MessageProcessor {
}
_estimateGas(input){
const web3Sim = new Web3(ganache.provider({fork: `${this.config.node.protocol}://${this.config.node.host}:${this.config.node.port}`}));
const simAccounts = await web3Sim.eth.getAccounts();
let simulatedReceipt = await web3Sim.eth.sendTransaction({
from: simAccounts[0],
to: input.address,
value: 0,
data: input.payload
});
return web3Sim.utils.toBN(simulatedReceipt.gasUsed);
}
async process(error, message){
if(error){
@ -143,24 +156,13 @@ class MessageProcessor {
}
// Estimate costs
const web3Sim = new Web3(ganache.provider({fork: `${config.node.protocol}://${config.node.host}:${config.node.port}`}));
const simAccounts = await web3Sim.eth.getAccounts();
let simulatedReceipt = await web3Sim.eth.sendTransaction({
from: simAccounts[0],
to: input.address,
value: 0,
data: input.payload
});
const estimatedGas = web3.utils.toBN(simulatedReceipt.gasUsed);
const estimatedGas = this._estimateGas(input);
if(gasLimit.lt(estimatedGas)) {
return this._reply("Gas limit below estimated gas", message);
}
this.web3.eth.sendTransaction({
from: config.node.blockchain.account,
from: this.config.node.blockchain.account,
to: address,
value: 0,
data: input.payload,