From 67e1c4939b117aa7237bb55d9dfd750144083f40 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 20 Apr 2018 21:14:50 -0400 Subject: [PATCH] Extracted gas estimate to individual function --- app/gas-relayer/src/message-processor.js | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/gas-relayer/src/message-processor.js b/app/gas-relayer/src/message-processor.js index d210259..765100e 100644 --- a/app/gas-relayer/src/message-processor.js +++ b/app/gas-relayer/src/message-processor.js @@ -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){ @@ -142,25 +155,14 @@ class MessageProcessor { return; } - - - // 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,