11import { useAtom , useAtomValue } from 'jotai'
22import { useEffect , useLayoutEffect , useRef } from 'react'
33
4- import { useIsOnline , useIsWindowVisible , usePrevious } from '@cowprotocol/common-hooks'
4+ import { useIsOnline , useIsWindowVisible , usePrevious , useSyncedRef } from '@cowprotocol/common-hooks'
55import { getCurrencyAddress } from '@cowprotocol/common-utils'
66
77import ms from 'ms.macro'
88
9+ import { useIsSmartSlippageApplied } from 'modules/tradeSlippage/hooks/useIsSmartSlippageApplied'
10+
911import { usePollQuoteCallback } from './usePollQuoteCallback'
1012import { useQuoteParams } from './useQuoteParams'
1113import { useTradeQuote } from './useTradeQuote'
@@ -21,7 +23,7 @@ import { checkOnlySlippageBpsChanged } from '../utils/quoteParamsChanges'
2123
2224const ONE_SEC = 1000
2325const QUOTE_VALIDATION_INTERVAL = ms `2s`
24- const QUOTE_SLIPPAGE_CHANGE_INTERVAL = ms `1.5s`
26+ const QUOTE_SLIPPAGE_CHANGE_THROTTLE_INTERVAL = ms `1.5s`
2527
2628// eslint-disable-next-line max-lines-per-function
2729export function useTradeQuotePolling ( quotePollingParams : TradeQuotePollingParameters ) : null {
@@ -58,6 +60,8 @@ export function useTradeQuotePolling(quotePollingParams: TradeQuotePollingParame
5860 prevQuoteParamsRef . current = quoteParams
5961 } , [ quoteParams ] )
6062
63+ const isSmartSlippageApplied = useSyncedRef ( useIsSmartSlippageApplied ( ) )
64+
6165 /**
6266 * Reset quote when window is not visible or sell amount has been cleared
6367 */
@@ -83,27 +87,29 @@ export function useTradeQuotePolling(quotePollingParams: TradeQuotePollingParame
8387 */
8488 if ( isConfirmOpen ) return
8589
86- const onlySlippageBpsChanged = checkOnlySlippageBpsChanged (
87- quoteParams ,
88- prevQuoteParamsRef . current ,
89- tradeQuoteRef . current ,
90- )
91-
92- if ( onlySlippageBpsChanged ) {
93- const quoteTimestampDiff = tradeQuoteRef . current . localQuoteTimestamp
94- ? Date . now ( ) - tradeQuoteRef . current . localQuoteTimestamp
95- : undefined
96- // slippageBps updates on every fetch /quote response
97- // so we should throttle duplicated additional requests caused by following slippageBps updates to prevent re-fetch loop (#6675)
98- if ( typeof quoteTimestampDiff === 'number' && quoteTimestampDiff < QUOTE_SLIPPAGE_CHANGE_INTERVAL ) {
99- return
90+ if ( isSmartSlippageApplied . current ) {
91+ const onlySlippageBpsChanged = checkOnlySlippageBpsChanged (
92+ quoteParams ,
93+ prevQuoteParamsRef . current ,
94+ tradeQuoteRef . current ,
95+ )
96+
97+ if ( onlySlippageBpsChanged ) {
98+ const quoteTimestampDiff = tradeQuoteRef . current . localQuoteTimestamp
99+ ? Date . now ( ) - tradeQuoteRef . current . localQuoteTimestamp
100+ : undefined
101+ // in "smart" slippage mode slippageBps updates on every fetch /quote response
102+ // so we should throttle duplicated additional requests caused by following slippageBps updates to prevent re-fetch loop (#6675)
103+ if ( typeof quoteTimestampDiff === 'number' && quoteTimestampDiff < QUOTE_SLIPPAGE_CHANGE_THROTTLE_INTERVAL ) {
104+ return
105+ }
100106 }
101107 }
102108
103109 if ( pollQuoteRef . current ( true ) ) {
104110 resetQuoteCounter ( )
105111 }
106- } , [ isConfirmOpen , isQuoteUpdatePossible , quoteParams , resetQuoteCounter ] )
112+ } , [ isConfirmOpen , isQuoteUpdatePossible , isSmartSlippageApplied , quoteParams , resetQuoteCounter ] )
107113
108114 /**
109115 * Update quote once a QUOTE_POLLING_INTERVAL
0 commit comments