Skip to content

Commit

Permalink
fix: lazily instantiate supported router providers (#3348)
Browse files Browse the repository at this point in the history
* fix: iterate over enum values

* fix: lazily instantiate router providers
  • Loading branch information
zzmp authored Feb 23, 2022
1 parent 369f8c9 commit 248bc07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/lib/hooks/routing/clientSideSmartOrderRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import JSBI from 'jsbi'
import { GetQuoteResult } from 'state/routing/types'
import { transformSwapRouteToGetQuoteResult } from 'utils/transformSwapRouteToGetQuoteResult'

export const AUTO_ROUTER_SUPPORTED_CHAINS: ChainId[] = Object.values(ChainId) as number[]
export const AUTO_ROUTER_SUPPORTED_CHAINS: ChainId[] = Object.values(ChainId).filter((chainId): chainId is ChainId =>
Number.isInteger(chainId)
)

async function getQuote(
{
Expand Down
30 changes: 15 additions & 15 deletions src/state/routing/slice.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { JsonRpcProvider } from '@ethersproject/providers'
import { BaseProvider, JsonRpcProvider } from '@ethersproject/providers'
import { createApi, fetchBaseQuery, FetchBaseQueryError } from '@reduxjs/toolkit/query/react'
import { Protocol } from '@uniswap/router-sdk'
import { ChainId } from '@uniswap/smart-order-router'
import { AlphaRouterParams } from '@uniswap/smart-order-router'
import { SupportedChainId } from 'constants/chains'
import { INFURA_NETWORK_URLS } from 'constants/infura'
import { AUTO_ROUTER_SUPPORTED_CHAINS, getClientSideQuote } from 'lib/hooks/routing/clientSideSmartOrderRouter'
import ms from 'ms.macro'
import qs from 'qs'

import { GetQuoteResult } from './types'

const routerParams = AUTO_ROUTER_SUPPORTED_CHAINS.reduce<{ [chainId in SupportedChainId]?: AlphaRouterParams }>(
(params, chainId) => ({
...params,
[chainId]: {
chainId,
provider: new JsonRpcProvider(INFURA_NETWORK_URLS[chainId]),
},
}),
{}
)
const routerProviders = new Map<ChainId, BaseProvider>()
function getRouterProvider(chainId: ChainId): BaseProvider {
const provider = routerProviders.get(chainId)
if (provider) return provider

if (AUTO_ROUTER_SUPPORTED_CHAINS.includes(chainId)) {
const provider = new JsonRpcProvider(INFURA_NETWORK_URLS[chainId])
routerProviders.set(chainId, provider)
return provider
}

throw new Error(`Router does not support this chain (chainId: ${chainId}).`)
}

const protocols: Protocol[] = [Protocol.V2, Protocol.V3]

Expand Down Expand Up @@ -62,8 +63,7 @@ export const routingApi = createApi({
try {
if (useClientSideRouter) {
const chainId = args.tokenInChainId
const params = routerParams[chainId]
if (!params) throw new Error(`Router does not support this chain (chainId: ${chainId}).`)
const params = { chainId, provider: getRouterProvider(chainId) }
result = await getClientSideQuote(args, params, { protocols })
} else {
const query = qs.stringify({
Expand Down

0 comments on commit 248bc07

Please sign in to comment.