Commit 6ffbf756 authored by Noah Zinsmeister's avatar Noah Zinsmeister Committed by GitHub

memoize common bases (#853)

* memoize common bases

* improve memoization
parent 10837d7b
import { ChainId, JSBI, Percent, Token, WETH, Pair, TokenAmount } from '@uniswap/sdk' import { ChainId, JSBI, Percent, Token, WETH, Pair, TokenAmount } from '@uniswap/sdk'
import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors' import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors'
export const ROUTER_ADDRESS = '0xf164fC0Ec4E93095b804a4795bBe1e041497b92a' export const ROUTER_ADDRESS = '0xf164fC0Ec4E93095b804a4795bBe1e041497b92a'
// used to construct intermediary pairs for trading // used to construct intermediary pairs for trading
export const BASES_TO_CHECK_TRADES_AGAINST: { readonly [chainId in ChainId]: Token[] } = { export const BASES_TO_CHECK_TRADES_AGAINST: { readonly [chainId in ChainId]: Token[] } = {
[ChainId.MAINNET]: [ [ChainId.MAINNET]: [
WETH[ChainId.MAINNET], WETH[ChainId.MAINNET],
......
...@@ -4,6 +4,7 @@ import flatMap from 'lodash.flatmap' ...@@ -4,6 +4,7 @@ import flatMap from 'lodash.flatmap'
import { useActiveWeb3React } from './index' import { useActiveWeb3React } from './index'
import { usePairs } from '../data/Reserves' import { usePairs } from '../data/Reserves'
import { BASES_TO_CHECK_TRADES_AGAINST } from '../constants' import { BASES_TO_CHECK_TRADES_AGAINST } from '../constants'
function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] { function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] {
...@@ -11,7 +12,8 @@ function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] { ...@@ -11,7 +12,8 @@ function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] {
const bases = useMemo(() => BASES_TO_CHECK_TRADES_AGAINST[chainId as ChainId] ?? [], [chainId]) const bases = useMemo(() => BASES_TO_CHECK_TRADES_AGAINST[chainId as ChainId] ?? [], [chainId])
const allPairs = usePairs([ const allPairCombinations: [Token | undefined, Token | undefined][] = useMemo(
() => [
// the direct pair // the direct pair
[tokenA, tokenB], [tokenA, tokenB],
// token A against all bases // token A against all bases
...@@ -20,7 +22,11 @@ function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] { ...@@ -20,7 +22,11 @@ function useAllCommonPairs(tokenA?: Token, tokenB?: Token): Pair[] {
...bases.map((base): [Token | undefined, Token | undefined] => [tokenB, base]), ...bases.map((base): [Token | undefined, Token | undefined] => [tokenB, base]),
// each base against all bases // each base against all bases
...flatMap(bases, (base): [Token, Token][] => bases.map(otherBase => [base, otherBase])) ...flatMap(bases, (base): [Token, Token][] => bases.map(otherBase => [base, otherBase]))
]) ],
[tokenA, tokenB, bases]
)
const allPairs = usePairs(allPairCombinations)
// only pass along valid pairs, non-duplicated pairs // only pass along valid pairs, non-duplicated pairs
return useMemo( return useMemo(
......
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