Skip to content

Commit

Permalink
refactor(useSwapDiscount): migrate from useLidoSWR to useLidoQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Nov 16, 2024
1 parent b6875bc commit b344e45
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions features/stake/swap-discount-banner/use-swap-discount.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useLidoSWR } from '@lido-sdk/react';

import { config } from 'config';
import { STRATEGY_LAZY } from 'consts/swr-strategies';
import { STRATEGY_LAZY } from 'consts/react-query-strategies';
import { useLidoQuery } from 'shared/hooks/use-lido-query';

import { getSwapIntegration } from './integrations';
import type {
Expand All @@ -23,7 +22,6 @@ if (config.enableQaHelpers && typeof window !== 'undefined') {
// we show banner if STETH is considerably cheaper to get on dex than staking
// ETH -> stETH rate > THRESHOLD
const fetchRate = async (
_: string,
integrationKey: StakeSwapDiscountIntegrationKey,
): Promise<FetchRateResult & StakeSwapDiscountIntegrationValue> => {
const integration = getSwapIntegration(integrationKey);
Expand All @@ -43,19 +41,17 @@ const fetchRate = async (
};

export const useSwapDiscount = () => {
return useLidoSWR(
['swr:swap-discount-rate', config.STAKE_SWAP_INTEGRATION],
// @ts-expect-error useLidoSWR has broken fetcher-key type signature
fetchRate,
{
...STRATEGY_LAZY,
onError(error, key) {
console.warn(
`[useSwapDiscount] Error fetching ETH->Steth:`,
key,
error,
);
},
return useLidoQuery({
queryKey: ['swap-discount-rate', config.STAKE_SWAP_INTEGRATION],
queryFn: async () => {
try {
return await fetchRate(config.STAKE_SWAP_INTEGRATION);
} catch (err) {
console.warn(`[useSwapDiscount] Error fetching ETH -> stETH: ${err}`);
// This line is necessary so that useLidoQuery (useQuery) can handle the error and return undefined
throw err;
}
},
);
strategy: STRATEGY_LAZY,
});
};

0 comments on commit b344e45

Please sign in to comment.