From 9b49cab7759ade03e35c7b47c713a8a9693038c3 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Thu, 17 Jun 2021 16:18:22 +0300 Subject: [PATCH] fix: tx gas price --- src/worker.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/worker.js b/src/worker.js index c3a3a21..4d25ddb 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,6 +1,6 @@ const Web3 = require('web3') const { GasPriceOracle } = require('gas-price-oracle') -const { toBN, toWei, fromWei } = require('web3-utils') +const { toBN, toWei, fromWei, toHex } = require('web3-utils') const proxyLightABI = require('../abis/proxyLightABI.json') const { queue } = require('./queue') @@ -8,7 +8,7 @@ const { getInstance, fromDecimals } = require('./utils') const { jobType, status } = require('./constants') const { netId, - gasPrices, + gasPrices: GAS_PRICES, gasLimits, instances, privateKey, @@ -34,7 +34,7 @@ function start() { if (networksWithOracle.includes(netId)) { gasPriceOracleConfig = { chainId: netId, - defaultFallbackGasPrices: gasPrices, + defaultFallbackGasPrices: GAS_PRICES, } gasPriceOracle = new GasPriceOracle(gasPriceOracleConfig) } @@ -53,16 +53,22 @@ function start() { } } +async function getGasPrices() { + let gasPrices = GAS_PRICES + + if (gasPriceOracle) { + gasPrices = await gasPriceOracle.gasPrices() + } + + return gasPrices +} + async function checkTornadoFee({ args, contract }) { const { currency, amount } = getInstance(contract) const { decimals } = instances[currency] const fee = toBN(args[4]) - let { fast } = gasPrices - if (gasPriceOracle) { - // eslint-disable-next-line no-extra-semi - ;({ fast } = await gasPriceOracle.gasPrices()) - } + const { fast } = await getGasPrices() const expense = toBN(toWei(fast.toString(), 'gwei')).mul(toBN(gasLimits[jobType.TORNADO_WITHDRAW])) const feePercent = toBN(fromDecimals(amount, decimals)) @@ -81,17 +87,24 @@ async function checkTornadoFee({ args, contract }) { } } -function getTxObject({ data }) { +async function getTxObject({ data }) { const contract = new web3.eth.Contract(proxyLightABI, proxyLight) const calldata = contract.methods.withdraw(data.contract, data.proof, ...data.args).encodeABI() - return { + const tx = { value: data.args[5], to: contract._address, data: calldata, gasLimit: gasLimits[jobType.TORNADO_WITHDRAW], } + + if (!gasPriceOracle) { + const { fast } = await getGasPrices() + tx.gasPrice = toHex(toWei(fast.toString(), 'gwei')) + } + + return tx } async function processJob(job) {