diff --git a/src/components/Swap/SwapBestTrade.tsx b/src/components/Swap/SwapBestTrade.tsx index 03b2783cd..c626d5461 100644 --- a/src/components/Swap/SwapBestTrade.tsx +++ b/src/components/Swap/SwapBestTrade.tsx @@ -1051,7 +1051,10 @@ const SwapBestTrade: React.FC<{ const betterPriceFound = await getBetterPrice({ dexOutAmount: optimalRate?.destAmount, allowedSlippage, - skip: liquidityHubDisabled, + skip: + liquidityHubDisabled || + wrapType === WrapType.WRAP || + wrapType === WrapType.UNWRAP, }); if (betterPriceFound) { @@ -1067,6 +1070,7 @@ const SwapBestTrade: React.FC<{ getBetterPrice, isLhPureAggregationMode, onPureAggregationSubmit, + wrapType, ]); const paraRate = optimalRate diff --git a/src/components/Swap/orbs/LiquidityHub/LiquidityHubSwapConfirmation.tsx b/src/components/Swap/orbs/LiquidityHub/LiquidityHubSwapConfirmation.tsx index a9a094a7e..d6035cf34 100644 --- a/src/components/Swap/orbs/LiquidityHub/LiquidityHubSwapConfirmation.tsx +++ b/src/components/Swap/orbs/LiquidityHub/LiquidityHubSwapConfirmation.tsx @@ -390,6 +390,7 @@ const useLiquidityHubSwapCallback = () => { outCurrency, getLatestQuote, onLiquidityHubSwapInProgress, + quote, } = useLiquidityHubConfirmationContext(); const { mutateAsync: signCallback } = useSignEIP712Callback(); const getSteps = useGetStepsCallback(); @@ -431,17 +432,15 @@ const useLiquidityHubSwapCallback = () => { await approvalCallback(); } - const acceptedQuote = getLatestQuote(); + const acceptedQuote = getLatestQuote() || quote; if (!acceptedQuote) { - throw new Error('Failed to fetch quote'); + throw new Error('missing quote'); } + onAcceptQuote(acceptedQuote); updateStore({ currentStep: Steps.SWAP }); - const signature = await promiseWithTimeout( - signCallback(acceptedQuote.permitData), - SIGNATURE_TIMEOUT, - ); + const signature = await signCallback(acceptedQuote.permitData); onSignature(signature); const txHash = await swapCallback({ diff --git a/src/components/Swap/orbs/LiquidityHub/hooks.ts b/src/components/Swap/orbs/LiquidityHub/hooks.ts index 524b8c32c..45c3099f6 100644 --- a/src/components/Swap/orbs/LiquidityHub/hooks.ts +++ b/src/components/Swap/orbs/LiquidityHub/hooks.ts @@ -1,6 +1,6 @@ import { ChainId, Currency } from '@uniswap/sdk'; import { useActiveWeb3React } from 'hooks'; -import { useMemo, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import { getConfig } from 'config'; import { wrappedCurrency } from 'utils/wrappedCurrency'; import { @@ -134,37 +134,36 @@ export const useGetBetterPrice = ( ) => { const [seekingBetterPrice, setSeekingBestPrice] = useState(false); - const mutation = useMutation({ - mutationFn: async ({ - dexOutAmount = '', - allowedSlippage = 0, + const getBetterPrice = useCallback( + async ({ skip, + allowedSlippage = 0, + dexOutAmount = '', }: { - dexOutAmount?: string; + skip: boolean; allowedSlippage?: number; - skip?: boolean; + dexOutAmount?: string; }) => { - if (skip) return false; - setSeekingBestPrice(true); - const quote = await promiseWithTimeout(fetchLiquidityHubQuote(), 5_000); - const dexMinAmountOut = - subtractSlippage(allowedSlippage, dexOutAmount) || 0; - return BN(quote?.userMinOutAmountWithGas || 0).gt(dexMinAmountOut); - }, - onError: (error) => { - console.error('useSeekingBetterPrice', error); - }, - onSettled: () => { - setTimeout(() => { - setSeekingBestPrice(false); - }, 50); + try { + if (skip) return false; + setSeekingBestPrice(true); + const quote = await promiseWithTimeout(fetchLiquidityHubQuote(), 5_000); + const dexMinAmountOut = + subtractSlippage(allowedSlippage, dexOutAmount) || 0; + return BN(quote?.userMinOutAmountWithGas || 0).gt(dexMinAmountOut); + } catch (error) { + console.error('useSeekingBetterPrice', error); + } finally { + setTimeout(() => { + setSeekingBestPrice(false); + }, 50); + } }, - }); + [fetchLiquidityHubQuote], + ); return { seekingBetterPrice, - quote: mutation.data, - error: mutation.error, - getBetterPrice: mutation.mutateAsync, + getBetterPrice, }; };