Skip to content

Commit

Permalink
chore: 🤖 allow all chain to use shadow query
Browse files Browse the repository at this point in the history
  • Loading branch information
chef-ryan committed Nov 13, 2024
1 parent 0aaa84f commit ef2fccd
Showing 1 changed file with 5 additions and 40 deletions.
45 changes: 5 additions & 40 deletions apps/web/src/hooks/useBestAMMTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ import { publicClient } from 'utils/wagmi'
import { EXPERIMENTAL_FEATURES } from 'config/experimentalFeatures'
import useNativeCurrency from 'hooks/useNativeCurrency'

import {
QUOTING_API,
QUOTING_API_PREFIX,
QUOTING_API_PREFIX_OPTIMIZED,
QUOTING_API_PREFIX_ORIGINAL,
} from 'config/constants/endpoints'
import { QUOTING_API, QUOTING_API_PREFIX_OPTIMIZED, QUOTING_API_PREFIX_ORIGINAL } from 'config/constants/endpoints'
import {
CommonPoolsParams,
PoolsWithState,
Expand Down Expand Up @@ -679,7 +674,6 @@ export function useBestTradeFromApiShadow(
// baseCurrency,
amount,
currency,
enabled,
maxHops,
maxSplits,
stableSwap,
Expand Down Expand Up @@ -712,7 +706,7 @@ export function useBestTradeFromApiShadow(
return types
}, [v2Swap, v3Swap, stableSwap])

const poolPreFetch = useTradeApiPrefetchShadow(
const poolPreFetch = useTradeApiPrefetch(
{
currencyA: amount?.currency,
currencyB: currency,
Expand All @@ -734,10 +728,10 @@ export function useBestTradeFromApiShadow(
const { address } = useAccount()
const { gasPrice } = useFeeDataWithGasPrice()

const previousEnabled = usePreviousValue(enabled)
const previousEnabled = usePreviousValue(Boolean(currency?.chainId))

return useQuery({
enabled: featureFlag && !!(amount && currency && deferQuotient && enabled && poolTypes?.length),
enabled: featureFlag && !!(amount && currency?.chainId && deferQuotient && poolTypes?.length),
refetchInterval: POOLS_FAST_REVALIDATE[currency?.chainId as keyof typeof POOLS_FAST_REVALIDATE] ?? 10_000,
queryKey: [
queryType,
Expand Down Expand Up @@ -948,7 +942,7 @@ type PrefetchParams = {
function getCurrencyIdentifierForApi(currency: Currency) {
return currency.isNative ? zeroAddress : currency.address
}
export function useTradeApiPrefetchShadow(
export function useTradeApiPrefetch(
{ currencyA, currencyB, poolTypes, enabled = true }: PrefetchParams,
prefix: string,
queryType: string,
Expand Down Expand Up @@ -980,32 +974,3 @@ export function useTradeApiPrefetchShadow(
refetchInterval: currencyA?.chainId ? POOLS_FAST_REVALIDATE[currencyA.chainId] : 0,
})
}

export function useTradeApiPrefetch({ currencyA, currencyB, poolTypes, enabled = true }: PrefetchParams) {
return useQuery({
enabled: !!(currencyA && currencyB && poolTypes?.length && enabled),
queryKey: ['quote-api-prefetch', currencyA?.chainId, currencyA?.symbol, currencyB?.symbol, poolTypes] as const,
queryFn: async ({ signal }) => {
if (!currencyA || !currencyB || !poolTypes?.length) {
throw new Error('Invalid prefetch params')
}

const serverRes = await fetch(
`${QUOTING_API_PREFIX}/_pools/${currencyA.chainId}/${getCurrencyIdentifierForApi(
currencyA,
)}/${getCurrencyIdentifierForApi(currencyB)}?${qs.stringify({ protocols: poolTypes.map(getPoolTypeKey) })}`,
{
method: 'GET',
signal,
},
)
const res = await serverRes.json()
if (!res.success) {
throw new Error(res.message)
}
return res
},
staleTime: currencyA?.chainId ? POOLS_FAST_REVALIDATE[currencyA.chainId] : 0,
refetchInterval: currencyA?.chainId ? POOLS_FAST_REVALIDATE[currencyA.chainId] : 0,
})
}

0 comments on commit ef2fccd

Please sign in to comment.