Commit 91de599f authored by Justin Domingue's avatar Justin Domingue Committed by GitHub

fix: de-duplicate intermediate pairs (#2396)

* make useAllCurrencyCombinations return uniques

* consolidate useBestV3Trade* functions

* consolidate useBestV2Trade

* typo

* reverted changes to trade hooks

* reverted changes to trade hooks
Co-authored-by: default avatarNoah Zinsmeister <noahwz@gmail.com>
parent 284a4351
......@@ -19,7 +19,11 @@ export function useAllCurrencyCombinations(currencyA?: Currency, currencyB?: Cur
}, [chainId, tokenA, tokenB])
const basePairs: [Token, Token][] = useMemo(
() => bases.flatMap((base): [Token, Token][] => bases.map((otherBase) => [base, otherBase])),
() =>
bases
.flatMap((base): [Token, Token][] => bases.map((otherBase) => [base, otherBase]))
// though redundant with the first filter below, that expression runs more often, so this is probably worthwhile
.filter(([t0, t1]) => !t0.equals(t1)),
[bases]
)
......@@ -28,7 +32,7 @@ export function useAllCurrencyCombinations(currencyA?: Currency, currencyB?: Cur
tokenA && tokenB
? [
// the direct pair
[tokenA, tokenB],
[tokenA, tokenB] as [Token, Token],
// token A against all bases
...bases.map((base): [Token, Token] => [tokenA, base]),
// token B against all bases
......@@ -36,8 +40,18 @@ export function useAllCurrencyCombinations(currencyA?: Currency, currencyB?: Cur
// 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 out invalid pairs comprised of the same asset (e.g. WETH<>WETH)
.filter(([t0, t1]) => !t0.equals(t1))
// filter out duplicate pairs
.filter(([t0, t1], i, otherPairs) => {
// find the first index in the array at which there are the same 2 tokens as the current
const firstIndexInOtherPairs = otherPairs.findIndex(([t0Other, t1Other]) => {
return (t0.equals(t0Other) && t1.equals(t1Other)) || (t0.equals(t1Other) && t1.equals(t0Other))
})
// only accept the first occurence of the same 2 tokens
return firstIndexInOtherPairs === i
})
// optionally filter out some pairs for tokens with custom bases defined
.filter(([tokenA, tokenB]) => {
if (!chainId) return true
const customBases = CUSTOM_BASES[chainId]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment