From f8f33e0b397f08c790999a55cdb522cc0021769b Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Wed, 20 Oct 2021 00:31:19 +1000 Subject: [PATCH] fix: gas price --- src/constants/contracts.ts | 2 +- src/services/gas-price.service.ts | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/constants/contracts.ts b/src/constants/contracts.ts index 72c858b..c2a1aff 100644 --- a/src/constants/contracts.ts +++ b/src/constants/contracts.ts @@ -11,7 +11,7 @@ export const RPC_LIST: { [chainId in ChainId]: string } = { [ChainId.MAINNET]: 'https://mainnet.infura.io/v3/eb6a84e726614079948e0b1efce5baa5', [ChainId.GOERLI]: 'https://eth-goerli.alchemyapi.io/v2/hlSj0EqPUuLGyyTExs6UqFKnXDrc_eOh', [ChainId.OPTIMISM]: 'https://optimism-kovan.infura.io/v3/8f786b96d16046b78e0287fa61c6fcf8', - [ChainId.XDAI]: 'https://xdai-rpc.ztake.org', + [ChainId.XDAI]: 'https://rpc.xdaichain.com/tornado', }; export const OFF_CHAIN_ORACLE = '0x07D91f5fb9Bf7798734C3f606dB065549F6893bb'; diff --git a/src/services/gas-price.service.ts b/src/services/gas-price.service.ts index e4b5fa7..a72d47c 100644 --- a/src/services/gas-price.service.ts +++ b/src/services/gas-price.service.ts @@ -7,6 +7,16 @@ import { GasPriceOracle } from 'gas-price-oracle'; import { toWei } from '@/utilities'; import { RPC_LIST } from '@/constants'; +const bump = (gas: BigNumber, percent: number) => gas.mul(percent).div(100).toHexString(); +const gweiToWei = (value: number) => toWei(String(value), 'gwei'); + +const percentBump = { + INSTANT: 150, + FAST: 130, + STANDARD: 85, + LOW: 50, +}; + @Injectable() export class GasPriceService { private readonly chainId: number; @@ -21,15 +31,13 @@ export class GasPriceService { defaultRpc: RPC_LIST[this.chainId], }); - const fast = await instance.fetchGasPriceFromRpc(); - - const bnGas = BigNumber.from(toWei(String(fast), 'gwei')); + const result = await instance.gasPrices(); return { - instant: bnGas.mul(150).div(100).toHexString(), - fast: bnGas.mul(130).div(100).toHexString(), - standard: bnGas.mul(85).div(100).toHexString(), - low: bnGas.mul(50).div(100).toHexString(), + instant: bump(gweiToWei(result.instant), percentBump.INSTANT), + fast: bump(gweiToWei(result.instant), percentBump.FAST), + standard: bump(gweiToWei(result.standard), percentBump.STANDARD), + low: bump(gweiToWei(result.low), percentBump.LOW), }; } }