Skip to content

Commit

Permalink
feat: support rq options, remove update on block
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Oct 1, 2023
1 parent 26fcb43 commit 2893203
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/oeth/swap/src/components/SwapRouteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 21 additions & 4 deletions libs/shared/providers/src/gas/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -40,5 +56,6 @@ export const useGasPrice = (gasAmount = 0n) => {
maxGasCostGwei,
};
},
...options,
});
};

0 comments on commit 2893203

Please sign in to comment.