Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(L2): ensure chainIds match before fetching pool data #2652

Merged
merged 3 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/hooks/useAllCurrencyCombinations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Currency, Token } from '@uniswap/sdk-core'
import { useMemo } from 'react'

import { ADDITIONAL_BASES, BASES_TO_CHECK_TRADES_AGAINST, CUSTOM_BASES } from '../constants/routing'
import { useActiveWeb3React } from './web3'

export function useAllCurrencyCombinations(currencyA?: Currency, currencyB?: Currency): [Token, Token][] {
const { chainId } = useActiveWeb3React()
const chainId = currencyA?.chainId

const [tokenA, tokenB] = chainId ? [currencyA?.wrapped, currencyB?.wrapped] : [undefined, undefined]

const bases: Token[] = useMemo(() => {
if (!chainId) return []
if (!chainId || chainId !== tokenB?.chainId) return []

const common = BASES_TO_CHECK_TRADES_AGAINST[chainId] ?? []
const additionalA = tokenA ? ADDITIONAL_BASES[chainId]?.[tokenA.address] ?? [] : []
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/useBestV3Trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export function useBestV3Trade(
const routingAPIEnabled = useRoutingAPIEnabled()
const isWindowVisible = useIsWindowVisible()

const debouncedAmount = useDebounce(amountSpecified, 100)
const debouncedAmount = useDebounce(amountSpecified, 200)
const debouncedOtherCurrency = useDebounce(otherCurrency, 200)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you considered debouncing these together?

useDebounce([amountSpecified, otherCurrency], 200)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, didn't occur to me that would work!


const routingAPITrade = useRoutingAPITrade(
tradeType,
routingAPIEnabled && isWindowVisible ? debouncedAmount : undefined,
otherCurrency
debouncedOtherCurrency
)

const isLoading = amountSpecified !== undefined && debouncedAmount === undefined
Expand All @@ -43,18 +44,18 @@ export function useBestV3Trade(
(tradeType === TradeType.EXACT_INPUT
? !routingAPITrade.trade.inputAmount.equalTo(amountSpecified) ||
!amountSpecified.currency.equals(routingAPITrade.trade.inputAmount.currency) ||
!otherCurrency?.equals(routingAPITrade.trade.outputAmount.currency)
!debouncedOtherCurrency?.equals(routingAPITrade.trade.outputAmount.currency)
: !routingAPITrade.trade.outputAmount.equalTo(amountSpecified) ||
!amountSpecified.currency.equals(routingAPITrade.trade.outputAmount.currency) ||
!otherCurrency?.equals(routingAPITrade.trade.inputAmount.currency))
!debouncedOtherCurrency?.equals(routingAPITrade.trade.inputAmount.currency))

const useFallback = !routingAPIEnabled || (!debouncing && routingAPITrade.state === V3TradeState.NO_ROUTE_FOUND)

// only use client side router if routing api trade failed
const bestV3Trade = useClientSideV3Trade(
tradeType,
useFallback ? debouncedAmount : undefined,
useFallback ? otherCurrency : undefined
useFallback ? debouncedOtherCurrency : undefined
)

return {
Expand Down