Skip to content

Commit

Permalink
fix(swap): periodically refresh unsupported tokens state (#3252)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Oct 19, 2023
1 parent 80cb391 commit f5986a3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion apps/cowswap-frontend/src/common/updaters/FeesUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { OrderKind } from '@cowprotocol/cow-sdk'
import { useENSAddress } from '@cowprotocol/ens'
import { useWalletInfo } from '@cowprotocol/wallet'

import ms from 'ms.macro'

import { useRefetchQuoteCallback } from 'legacy/hooks/useRefetchPriceCallback'
import { useIsUnsupportedTokenGp } from 'legacy/state/lists/hooks'
import { useAllQuotes, useIsBestQuoteLoading, useSetQuoteError } from 'legacy/state/price/hooks'
Expand All @@ -29,6 +31,14 @@ export const TYPED_VALUE_DEBOUNCE_TIME = 350
const REFETCH_CHECK_INTERVAL = 10000 // Every 10s
const RENEW_FEE_QUOTES_BEFORE_EXPIRATION_TIME = 30000 // Will renew the quote if there's less than 30 seconds left for the quote to expire
const WAITING_TIME_BETWEEN_EQUAL_REQUESTS = 5000 // Prevents from sending the same request to often (max, every 5s)
const UNSUPPORTED_TOKEN_TTL = ms`1h`

/**
* Since a token might become supported, we should periodically (once in 1h) refresh its state
*/
const isUnsupportedTokenExpired = ({ dateAdded }: { dateAdded: number }) => {
return dateAdded + UNSUPPORTED_TOKEN_TTL < Date.now()
}

type FeeQuoteParams = Omit<LegacyFeeQuoteParams, 'validTo'>

Expand Down Expand Up @@ -233,7 +243,9 @@ export function FeesUpdater(): null {
// Callback to re-fetch both the fee and the price
const refetchQuoteIfRequired = () => {
// if no token is unsupported and needs refetching
const hasToRefetch = !unsupportedToken && isRefetchQuoteRequired(isLoading, quoteParams, quoteInfo)
const hasToRefetch =
(!unsupportedToken || isUnsupportedTokenExpired(unsupportedToken)) &&
isRefetchQuoteRequired(isLoading, quoteParams, quoteInfo)

if (hasToRefetch) {
// Decide if this is a new quote, or just a refresh
Expand Down

0 comments on commit f5986a3

Please sign in to comment.