create some hooks for using in v3 swap routing
This commit is contained in:
parent
77fa61495f
commit
aa742f415d
62
src/hooks/useAllCurrencyCombinations.ts
Normal file
62
src/hooks/useAllCurrencyCombinations.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { Currency, Token } from '@uniswap/sdk-core'
|
||||
import flatMap from 'lodash.flatmap'
|
||||
import { useMemo } from 'react'
|
||||
import { ADDITIONAL_BASES, BASES_TO_CHECK_TRADES_AGAINST, CUSTOM_BASES } from '../constants'
|
||||
import { wrappedCurrency } from '../utils/wrappedCurrency'
|
||||
import { useActiveWeb3React } from './index'
|
||||
|
||||
export function useAllCurrencyCombinations(currencyA?: Currency, currencyB?: Currency): [Token, Token][] {
|
||||
const { chainId } = useActiveWeb3React()
|
||||
|
||||
const [tokenA, tokenB] = chainId
|
||||
? [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)]
|
||||
: [undefined, undefined]
|
||||
|
||||
const bases: Token[] = useMemo(() => {
|
||||
if (!chainId) return []
|
||||
|
||||
const common = BASES_TO_CHECK_TRADES_AGAINST[chainId] ?? []
|
||||
const additionalA = tokenA ? ADDITIONAL_BASES[chainId]?.[tokenA.address] ?? [] : []
|
||||
const additionalB = tokenB ? ADDITIONAL_BASES[chainId]?.[tokenB.address] ?? [] : []
|
||||
|
||||
return [...common, ...additionalA, ...additionalB]
|
||||
}, [chainId, tokenA, tokenB])
|
||||
|
||||
const basePairs: [Token, Token][] = useMemo(
|
||||
() => flatMap(bases, (base): [Token, Token][] => bases.map((otherBase) => [base, otherBase])),
|
||||
[bases]
|
||||
)
|
||||
|
||||
return useMemo(
|
||||
() =>
|
||||
tokenA && tokenB
|
||||
? [
|
||||
// the direct pair
|
||||
[tokenA, tokenB],
|
||||
// token A against all bases
|
||||
...bases.map((base): [Token, Token] => [tokenA, base]),
|
||||
// token B against all bases
|
||||
...bases.map((base): [Token, Token] => [tokenB, base]),
|
||||
// each base against all bases
|
||||
...basePairs,
|
||||
]
|
||||
.filter((tokens): tokens is [Token, Token] => Boolean(tokens[0] && tokens[1]))
|
||||
.filter(([t0, t1]) => t0.address !== t1.address)
|
||||
.filter(([tokenA, tokenB]) => {
|
||||
if (!chainId) return true
|
||||
const customBases = CUSTOM_BASES[chainId]
|
||||
|
||||
const customBasesA: Token[] | undefined = customBases?.[tokenA.address]
|
||||
const customBasesB: Token[] | undefined = customBases?.[tokenB.address]
|
||||
|
||||
if (!customBasesA && !customBasesB) return true
|
||||
|
||||
if (customBasesA && !customBasesA.find((base) => tokenB.equals(base))) return false
|
||||
if (customBasesB && !customBasesB.find((base) => tokenA.equals(base))) return false
|
||||
|
||||
return true
|
||||
})
|
||||
: [],
|
||||
[tokenA, tokenB, bases, basePairs, chainId]
|
||||
)
|
||||
}
|
@ -13,10 +13,10 @@ import { Interface } from '@ethersproject/abi'
|
||||
const POOL_STATE_INTERFACE = new Interface(IUniswapV3PoolStateABI) as IUniswapV3PoolStateInterface
|
||||
|
||||
export enum PoolState {
|
||||
LOADING = 'LOADING',
|
||||
NOT_EXISTS = 'NOT_EXISTS',
|
||||
EXISTS = 'EXISTS',
|
||||
INVALID = 'INVALID',
|
||||
LOADING,
|
||||
NOT_EXISTS,
|
||||
EXISTS,
|
||||
INVALID,
|
||||
}
|
||||
|
||||
export function usePools(
|
||||
|
@ -1,75 +1,16 @@
|
||||
import { Currency, CurrencyAmount, Token } from '@uniswap/sdk-core'
|
||||
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
|
||||
import { Pair, Trade } from '@uniswap/v2-sdk'
|
||||
import flatMap from 'lodash.flatmap'
|
||||
import { useMemo } from 'react'
|
||||
import { useUserSingleHopOnly } from 'state/user/hooks'
|
||||
import { isTradeBetter } from 'utils/trades'
|
||||
import {
|
||||
ADDITIONAL_BASES,
|
||||
BASES_TO_CHECK_TRADES_AGAINST,
|
||||
BETTER_TRADE_LESS_HOPS_THRESHOLD,
|
||||
CUSTOM_BASES,
|
||||
} from '../constants'
|
||||
import { BETTER_TRADE_LESS_HOPS_THRESHOLD } from '../constants'
|
||||
import { useAllCurrencyCombinations } from './useAllCurrencyCombinations'
|
||||
import { PairState, useV2Pairs } from './useV2Pairs'
|
||||
import { wrappedCurrency } from '../utils/wrappedCurrency'
|
||||
import { useActiveWeb3React } from './index'
|
||||
|
||||
function useAllCommonPairs(currencyA?: Currency, currencyB?: Currency): Pair[] {
|
||||
const { chainId } = useActiveWeb3React()
|
||||
const allCurrencyCombinations = useAllCurrencyCombinations(currencyA, currencyB)
|
||||
|
||||
const [tokenA, tokenB] = chainId
|
||||
? [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)]
|
||||
: [undefined, undefined]
|
||||
|
||||
const bases: Token[] = useMemo(() => {
|
||||
if (!chainId) return []
|
||||
|
||||
const common = BASES_TO_CHECK_TRADES_AGAINST[chainId] ?? []
|
||||
const additionalA = tokenA ? ADDITIONAL_BASES[chainId]?.[tokenA.address] ?? [] : []
|
||||
const additionalB = tokenB ? ADDITIONAL_BASES[chainId]?.[tokenB.address] ?? [] : []
|
||||
|
||||
return [...common, ...additionalA, ...additionalB]
|
||||
}, [chainId, tokenA, tokenB])
|
||||
|
||||
const basePairs: [Token, Token][] = useMemo(
|
||||
() => flatMap(bases, (base): [Token, Token][] => bases.map((otherBase) => [base, otherBase])),
|
||||
[bases]
|
||||
)
|
||||
|
||||
const allPairCombinations: [Token, Token][] = useMemo(
|
||||
() =>
|
||||
tokenA && tokenB
|
||||
? [
|
||||
// the direct pair
|
||||
[tokenA, tokenB],
|
||||
// token A against all bases
|
||||
...bases.map((base): [Token, Token] => [tokenA, base]),
|
||||
// token B against all bases
|
||||
...bases.map((base): [Token, Token] => [tokenB, base]),
|
||||
// each base against all bases
|
||||
...basePairs,
|
||||
]
|
||||
.filter((tokens): tokens is [Token, Token] => Boolean(tokens[0] && tokens[1]))
|
||||
.filter(([t0, t1]) => t0.address !== t1.address)
|
||||
.filter(([tokenA, tokenB]) => {
|
||||
if (!chainId) return true
|
||||
const customBases = CUSTOM_BASES[chainId]
|
||||
|
||||
const customBasesA: Token[] | undefined = customBases?.[tokenA.address]
|
||||
const customBasesB: Token[] | undefined = customBases?.[tokenB.address]
|
||||
|
||||
if (!customBasesA && !customBasesB) return true
|
||||
|
||||
if (customBasesA && !customBasesA.find((base) => tokenB.equals(base))) return false
|
||||
if (customBasesB && !customBasesB.find((base) => tokenA.equals(base))) return false
|
||||
|
||||
return true
|
||||
})
|
||||
: [],
|
||||
[tokenA, tokenB, bases, basePairs, chainId]
|
||||
)
|
||||
|
||||
const allPairs = useV2Pairs(allPairCombinations)
|
||||
const allPairs = useV2Pairs(allCurrencyCombinations)
|
||||
|
||||
// only pass along valid pairs, non-duplicated pairs
|
||||
return useMemo(
|
45
src/hooks/useV3SwapPools.ts
Normal file
45
src/hooks/useV3SwapPools.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { Currency, Token } from '@uniswap/sdk-core'
|
||||
import { FeeAmount, Pool } from '@uniswap/v3-sdk'
|
||||
import { useMemo } from 'react'
|
||||
import { useAllCurrencyCombinations } from './useAllCurrencyCombinations'
|
||||
import { PoolState, usePools } from './usePools'
|
||||
|
||||
/**
|
||||
* Returns all the existing pools that should be considered for swapping between an input currency and an output currency
|
||||
* @param currencyIn the input currency
|
||||
* @param currencyOut the output currency
|
||||
*/
|
||||
export function useV3SwapPools(
|
||||
currencyIn?: Currency,
|
||||
currencyOut?: Currency
|
||||
): {
|
||||
pools: Pool[]
|
||||
loading: boolean
|
||||
} {
|
||||
const allCurrencyCombinations = useAllCurrencyCombinations(currencyIn, currencyOut)
|
||||
|
||||
const allCurrencyCombinationsWithAllFees: [Token, Token, FeeAmount][] = useMemo(
|
||||
() =>
|
||||
allCurrencyCombinations.reduce<[Token, Token, FeeAmount][]>((list, [tokenA, tokenB]) => {
|
||||
return list.concat([
|
||||
[tokenA, tokenB, FeeAmount.LOW],
|
||||
[tokenA, tokenB, FeeAmount.MEDIUM],
|
||||
[tokenA, tokenB, FeeAmount.HIGH],
|
||||
])
|
||||
}, []),
|
||||
[allCurrencyCombinations]
|
||||
)
|
||||
|
||||
const pools = usePools(allCurrencyCombinationsWithAllFees)
|
||||
|
||||
return useMemo(() => {
|
||||
return {
|
||||
pools: pools
|
||||
.filter((tuple): tuple is [PoolState.EXISTS, Pool] => {
|
||||
return tuple[0] === PoolState.EXISTS && tuple[1] !== null
|
||||
})
|
||||
.map(([, pool]) => pool),
|
||||
loading: pools.some(([state]) => state === PoolState.LOADING),
|
||||
}
|
||||
}, [pools])
|
||||
}
|
@ -7,7 +7,7 @@ import { useCallback, useEffect, useState } from 'react'
|
||||
import { useDispatch, useSelector } from 'react-redux'
|
||||
import { useActiveWeb3React } from '../../hooks'
|
||||
import { useCurrency } from '../../hooks/Tokens'
|
||||
import { useV2TradeExactIn, useV2TradeExactOut } from '../../hooks/v2Trades'
|
||||
import { useV2TradeExactIn, useV2TradeExactOut } from '../../hooks/useV2Trade'
|
||||
import useParsedQueryString from '../../hooks/useParsedQueryString'
|
||||
import { isAddress } from '../../utils'
|
||||
import { AppDispatch, AppState } from '../index'
|
||||
|
Loading…
Reference in New Issue
Block a user