2021-04-25 01:43:57 +03:00
|
|
|
import { ChainId, Currency } from '@uniswap/sdk-core'
|
|
|
|
import { Pool, Route } from '@uniswap/v3-sdk'
|
|
|
|
import { useMemo } from 'react'
|
2021-04-27 18:35:09 +03:00
|
|
|
import { useUserSingleHopOnly } from '../state/user/hooks'
|
2021-04-25 01:43:57 +03:00
|
|
|
import { wrappedCurrency } from '../utils/wrappedCurrency'
|
2021-05-18 18:31:22 +03:00
|
|
|
import { useActiveWeb3React } from './web3'
|
2021-04-25 01:43:57 +03:00
|
|
|
import { useV3SwapPools } from './useV3SwapPools'
|
|
|
|
|
|
|
|
function computeAllRoutes(
|
|
|
|
currencyIn: Currency,
|
|
|
|
currencyOut: Currency,
|
|
|
|
pools: Pool[],
|
|
|
|
chainId: ChainId,
|
|
|
|
currentPath: Pool[] = [],
|
2021-05-12 16:52:17 +03:00
|
|
|
allPaths: Route<Currency, Currency>[] = [],
|
2021-04-25 01:43:57 +03:00
|
|
|
startCurrencyIn: Currency = currencyIn,
|
|
|
|
maxHops = 2
|
2021-05-12 16:52:17 +03:00
|
|
|
): Route<Currency, Currency>[] {
|
2021-04-25 01:43:57 +03:00
|
|
|
const tokenIn = wrappedCurrency(currencyIn, chainId)
|
|
|
|
const tokenOut = wrappedCurrency(currencyOut, chainId)
|
2021-05-12 16:52:17 +03:00
|
|
|
if (!tokenIn || !tokenOut) throw new Error('Missing tokenIn/tokenOut')
|
2021-04-25 01:43:57 +03:00
|
|
|
|
|
|
|
for (const pool of pools) {
|
|
|
|
if (currentPath.indexOf(pool) !== -1 || !pool.involvesToken(tokenIn)) continue
|
|
|
|
|
|
|
|
const outputToken = pool.token0.equals(tokenIn) ? pool.token1 : pool.token0
|
|
|
|
if (outputToken.equals(tokenOut)) {
|
|
|
|
allPaths.push(new Route([...currentPath, pool], startCurrencyIn, currencyOut))
|
|
|
|
} else if (maxHops > 1) {
|
|
|
|
computeAllRoutes(
|
|
|
|
outputToken,
|
|
|
|
currencyOut,
|
|
|
|
pools,
|
|
|
|
chainId,
|
|
|
|
[...currentPath, pool],
|
|
|
|
allPaths,
|
|
|
|
startCurrencyIn,
|
|
|
|
maxHops - 1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allPaths
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the routes from an input currency to an output currency
|
|
|
|
* @param currencyIn the input currency
|
|
|
|
* @param currencyOut the output currency
|
|
|
|
*/
|
2021-05-12 16:52:17 +03:00
|
|
|
export function useAllV3Routes(
|
|
|
|
currencyIn?: Currency,
|
|
|
|
currencyOut?: Currency
|
|
|
|
): { loading: boolean; routes: Route<Currency, Currency>[] } {
|
2021-04-25 01:43:57 +03:00
|
|
|
const { chainId } = useActiveWeb3React()
|
2021-04-26 20:17:37 +03:00
|
|
|
const { pools, loading: poolsLoading } = useV3SwapPools(currencyIn, currencyOut)
|
2021-04-25 01:43:57 +03:00
|
|
|
|
2021-04-27 18:35:09 +03:00
|
|
|
const [singleHopOnly] = useUserSingleHopOnly()
|
|
|
|
|
2021-04-25 01:43:57 +03:00
|
|
|
return useMemo(() => {
|
2021-04-26 20:17:37 +03:00
|
|
|
if (poolsLoading || !chainId || !pools || !currencyIn || !currencyOut) return { loading: true, routes: [] }
|
2021-04-25 01:43:57 +03:00
|
|
|
|
2021-04-27 18:35:09 +03:00
|
|
|
const routes = computeAllRoutes(currencyIn, currencyOut, pools, chainId, [], [], currencyIn, singleHopOnly ? 1 : 2)
|
2021-04-26 20:17:37 +03:00
|
|
|
return { loading: false, routes }
|
2021-04-27 18:35:09 +03:00
|
|
|
}, [chainId, currencyIn, currencyOut, pools, poolsLoading, singleHopOnly])
|
2021-04-25 01:43:57 +03:00
|
|
|
}
|