From 289320308beef9d6cdbc0e2f2b53412372825058 Mon Sep 17 00:00:00 2001 From: toniocodo Date: Sun, 1 Oct 2023 21:41:35 +0200 Subject: [PATCH] feat: support rq options, remove update on block --- .../swap/src/components/SwapRouteCard.tsx | 2 +- libs/shared/providers/src/gas/hooks.ts | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libs/oeth/swap/src/components/SwapRouteCard.tsx b/libs/oeth/swap/src/components/SwapRouteCard.tsx index 2a18ee9d0..2e0bf7f83 100644 --- a/libs/oeth/swap/src/components/SwapRouteCard.tsx +++ b/libs/oeth/swap/src/components/SwapRouteCard.tsx @@ -41,7 +41,7 @@ export function SwapRouteCard({ route.gas, ); const { data: approvalGasPrice, isLoading: approvalGasPriceLoading } = - useGasPrice(route.approvalGas); + useGasPrice(route.approvalGas, { refetchInterval: 30e3 }); const { data: allowance } = useSwapRouteAllowance(route); const estimatedAmount = +formatUnits( diff --git a/libs/shared/providers/src/gas/hooks.ts b/libs/shared/providers/src/gas/hooks.ts index 38c498dc9..3d7254b69 100644 --- a/libs/shared/providers/src/gas/hooks.ts +++ b/libs/shared/providers/src/gas/hooks.ts @@ -2,15 +2,31 @@ import { contracts } from '@origin/shared/contracts'; import { useQuery } from '@tanstack/react-query'; import { fetchFeeData, readContract } from '@wagmi/core'; import { formatUnits } from 'viem'; -import { useBlockNumber } from 'wagmi'; + +import type { UseQueryOptions } from '@tanstack/react-query'; const GAS_MARGIN = 1.3; -export const useGasPrice = (gasAmount = 0n) => { - const { data: blockNumber } = useBlockNumber(); +type GasPrice = { + gweiUsd: number; + gasPrice: number; + gasCostUsd: number; + gasCostGwei: number; + maxGasCostUsd: number; + maxGasCostGwei: number; +}; +export const useGasPrice = ( + gasAmount = 0n, + options?: UseQueryOptions< + GasPrice, + Error, + GasPrice, + [['useGasPrice', string]] + >, +) => { return useQuery({ - queryKey: ['useGasPrice', gasAmount?.toString(), blockNumber?.toString()], + queryKey: ['useGasPrice', gasAmount?.toString()] as const, queryFn: async () => { const [price, data] = await Promise.all([ readContract({ @@ -40,5 +56,6 @@ export const useGasPrice = (gasAmount = 0n) => { maxGasCostGwei, }; }, + ...options, }); };