2021-09-21 20:51:50 +03:00
|
|
|
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
2022-01-19 02:40:23 +03:00
|
|
|
import { useMemo } from 'react'
|
2021-12-16 22:44:03 +03:00
|
|
|
import { InterfaceTrade, TradeState } from 'state/routing/types'
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useRoutingAPITrade } from 'state/routing/useRoutingAPITrade'
|
2021-09-22 02:21:28 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
import useAutoRouterSupported from './useAutoRouterSupported'
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useClientSideV3Trade } from './useClientSideV3Trade'
|
|
|
|
import useDebounce from './useDebounce'
|
|
|
|
import useIsWindowVisible from './useIsWindowVisible'
|
|
|
|
|
|
|
|
/**
|
2021-12-16 22:44:03 +03:00
|
|
|
* Returns the best v2+v3 trade for a desired swap.
|
2021-09-21 20:51:50 +03:00
|
|
|
* @param tradeType whether the swap is an exact in/out
|
|
|
|
* @param amountSpecified the exact amount to swap in/out
|
|
|
|
* @param otherCurrency the desired output/payment currency
|
|
|
|
*/
|
2021-12-16 22:44:03 +03:00
|
|
|
export function useBestTrade(
|
2021-09-21 20:51:50 +03:00
|
|
|
tradeType: TradeType,
|
|
|
|
amountSpecified?: CurrencyAmount<Currency>,
|
|
|
|
otherCurrency?: Currency
|
|
|
|
): {
|
2021-12-16 22:44:03 +03:00
|
|
|
state: TradeState
|
|
|
|
trade: InterfaceTrade<Currency, Currency, TradeType> | undefined
|
2021-09-21 20:51:50 +03:00
|
|
|
} {
|
2021-12-16 22:44:03 +03:00
|
|
|
const autoRouterSupported = useAutoRouterSupported()
|
2021-09-21 20:51:50 +03:00
|
|
|
const isWindowVisible = useIsWindowVisible()
|
|
|
|
|
2022-01-19 02:40:23 +03:00
|
|
|
const [debouncedAmount, debouncedOtherCurrency] = useDebounce(
|
|
|
|
useMemo(() => [amountSpecified, otherCurrency], [amountSpecified, otherCurrency]),
|
|
|
|
200
|
|
|
|
)
|
2021-09-21 20:51:50 +03:00
|
|
|
|
|
|
|
const routingAPITrade = useRoutingAPITrade(
|
|
|
|
tradeType,
|
2021-12-16 22:44:03 +03:00
|
|
|
autoRouterSupported && isWindowVisible ? debouncedAmount : undefined,
|
2021-10-22 19:14:04 +03:00
|
|
|
debouncedOtherCurrency
|
2021-09-21 20:51:50 +03:00
|
|
|
)
|
|
|
|
|
2022-03-09 20:19:24 +03:00
|
|
|
const isLoading = routingAPITrade.state === TradeState.LOADING
|
|
|
|
const useFallback = !autoRouterSupported || routingAPITrade.state === TradeState.NO_ROUTE_FOUND
|
2021-09-21 20:51:50 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
// only use client side router if routing api trade failed or is not supported
|
2021-09-21 20:51:50 +03:00
|
|
|
const bestV3Trade = useClientSideV3Trade(
|
|
|
|
tradeType,
|
|
|
|
useFallback ? debouncedAmount : undefined,
|
2021-10-22 19:14:04 +03:00
|
|
|
useFallback ? debouncedOtherCurrency : undefined
|
2021-09-21 20:51:50 +03:00
|
|
|
)
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
// only return gas estimate from api if routing api trade is used
|
2022-01-19 02:40:23 +03:00
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
...(useFallback ? bestV3Trade : routingAPITrade),
|
|
|
|
...(isLoading ? { state: TradeState.LOADING } : {}),
|
|
|
|
}),
|
2022-03-09 20:19:24 +03:00
|
|
|
[bestV3Trade, isLoading, routingAPITrade, useFallback]
|
2022-01-19 02:40:23 +03:00
|
|
|
)
|
2021-09-21 20:51:50 +03:00
|
|
|
}
|