feat: lazy-load smart-order-router (#7317)
This commit is contained in:
parent
2506c95816
commit
adaa7f1a0d
15
.eslintrc.js
15
.eslintrc.js
@ -28,7 +28,20 @@ module.exports = {
|
|||||||
{
|
{
|
||||||
files: ['**/*.ts', '**/*.tsx'],
|
files: ['**/*.ts', '**/*.tsx'],
|
||||||
rules: {
|
rules: {
|
||||||
'@typescript-eslint/no-restricted-imports': ['error', restrictedImports],
|
'@typescript-eslint/no-restricted-imports': [
|
||||||
|
'error',
|
||||||
|
{
|
||||||
|
...restrictedImports,
|
||||||
|
paths: [
|
||||||
|
...restrictedImports.paths,
|
||||||
|
{
|
||||||
|
name: '@uniswap/smart-order-router',
|
||||||
|
message: 'Only import types, unless you are in the client-side SOR, to preserve lazy-loading.',
|
||||||
|
allowTypeImports: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
'import/no-restricted-paths': [
|
'import/no-restricted-paths': [
|
||||||
'error',
|
'error',
|
||||||
{
|
{
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { BigNumber } from '@ethersproject/bignumber'
|
import { BigNumber } from '@ethersproject/bignumber'
|
||||||
import { t } from '@lingui/macro'
|
import { t } from '@lingui/macro'
|
||||||
import { ChainId, Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
import { ChainId, Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
||||||
import { nativeOnChain } from '@uniswap/smart-order-router'
|
|
||||||
import UniswapXBolt from 'assets/svg/bolt.svg'
|
import UniswapXBolt from 'assets/svg/bolt.svg'
|
||||||
import { SupportedLocale } from 'constants/locales'
|
import { SupportedLocale } from 'constants/locales'
|
||||||
|
import { nativeOnChain } from 'constants/tokens'
|
||||||
import { TransactionStatus } from 'graphql/data/__generated__/types-and-hooks'
|
import { TransactionStatus } from 'graphql/data/__generated__/types-and-hooks'
|
||||||
import { ChainTokenMap, useAllTokensMultichain } from 'hooks/Tokens'
|
import { ChainTokenMap, useAllTokensMultichain } from 'hooks/Tokens'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES as V3NFT_ADDRESSES,
|
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES as V3NFT_ADDRESSES,
|
||||||
Token,
|
Token,
|
||||||
} from '@uniswap/sdk-core'
|
} from '@uniswap/sdk-core'
|
||||||
import { AddressMap } from '@uniswap/smart-order-router'
|
import type { AddressMap } from '@uniswap/smart-order-router'
|
||||||
import MulticallJSON from '@uniswap/v3-periphery/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json'
|
import MulticallJSON from '@uniswap/v3-periphery/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json'
|
||||||
import NFTPositionManagerJSON from '@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json'
|
import NFTPositionManagerJSON from '@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json'
|
||||||
import { useWeb3React } from '@web3-react/core'
|
import { useWeb3React } from '@web3-react/core'
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { ChainId } from '@uniswap/sdk-core'
|
import { ChainId } from '@uniswap/sdk-core'
|
||||||
import { DAI_ARBITRUM } from '@uniswap/smart-order-router'
|
import { BRIDGED_USDC_ARBITRUM, DAI, DAI_ARBITRUM_ONE, USDC_MAINNET } from 'constants/tokens'
|
||||||
import { BRIDGED_USDC_ARBITRUM, DAI, USDC_MAINNET } from 'constants/tokens'
|
|
||||||
import { render } from 'test-utils/render'
|
import { render } from 'test-utils/render'
|
||||||
|
|
||||||
import { PortfolioLogo } from './PortfolioLogo'
|
import { PortfolioLogo } from './PortfolioLogo'
|
||||||
@ -13,7 +12,7 @@ describe('PortfolioLogo', () => {
|
|||||||
|
|
||||||
it('renders with L2 icon', () => {
|
it('renders with L2 icon', () => {
|
||||||
const { container } = render(
|
const { container } = render(
|
||||||
<PortfolioLogo chainId={ChainId.ARBITRUM_ONE} currencies={[DAI_ARBITRUM, BRIDGED_USDC_ARBITRUM]} />
|
<PortfolioLogo chainId={ChainId.ARBITRUM_ONE} currencies={[DAI_ARBITRUM_ONE, BRIDGED_USDC_ARBITRUM]} />
|
||||||
)
|
)
|
||||||
expect(container).toMatchSnapshot()
|
expect(container).toMatchSnapshot()
|
||||||
})
|
})
|
||||||
|
@ -1,12 +1,30 @@
|
|||||||
import { BigintIsh, CurrencyAmount, Token, TradeType } from '@uniswap/sdk-core'
|
import { BigintIsh, ChainId, CurrencyAmount, Token, TradeType } from '@uniswap/sdk-core'
|
||||||
// This file is lazy-loaded, so the import of smart-order-router is intentional.
|
// This file is lazy-loaded, so the import of smart-order-router is intentional.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||||
import { AlphaRouter, AlphaRouterConfig } from '@uniswap/smart-order-router'
|
import { AlphaRouter, AlphaRouterConfig } from '@uniswap/smart-order-router'
|
||||||
|
import { asSupportedChain } from 'constants/chains'
|
||||||
|
import { RPC_PROVIDERS } from 'constants/providers'
|
||||||
import { nativeOnChain } from 'constants/tokens'
|
import { nativeOnChain } from 'constants/tokens'
|
||||||
import JSBI from 'jsbi'
|
import JSBI from 'jsbi'
|
||||||
import { GetQuoteArgs, QuoteResult, QuoteState, SwapRouterNativeAssets } from 'state/routing/types'
|
import { GetQuoteArgs, QuoteResult, QuoteState, SwapRouterNativeAssets } from 'state/routing/types'
|
||||||
import { transformSwapRouteToGetQuoteResult } from 'utils/transformSwapRouteToGetQuoteResult'
|
import { transformSwapRouteToGetQuoteResult } from 'utils/transformSwapRouteToGetQuoteResult'
|
||||||
|
|
||||||
|
const routers = new Map<ChainId, AlphaRouter>()
|
||||||
|
export function getRouter(chainId: ChainId): AlphaRouter {
|
||||||
|
const router = routers.get(chainId)
|
||||||
|
if (router) return router
|
||||||
|
|
||||||
|
const supportedChainId = asSupportedChain(chainId)
|
||||||
|
if (supportedChainId) {
|
||||||
|
const provider = RPC_PROVIDERS[supportedChainId]
|
||||||
|
const router = new AlphaRouter({ chainId, provider })
|
||||||
|
routers.set(chainId, router)
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Router does not support this chain (chainId: ${chainId}).`)
|
||||||
|
}
|
||||||
|
|
||||||
async function getQuote(
|
async function getQuote(
|
||||||
{
|
{
|
||||||
tradeType,
|
tradeType,
|
||||||
|
@ -3,7 +3,6 @@ import { Protocol } from '@uniswap/router-sdk'
|
|||||||
import { TradeType } from '@uniswap/sdk-core'
|
import { TradeType } from '@uniswap/sdk-core'
|
||||||
import { sendAnalyticsEvent } from 'analytics'
|
import { sendAnalyticsEvent } from 'analytics'
|
||||||
import { isUniswapXSupportedChain } from 'constants/chains'
|
import { isUniswapXSupportedChain } from 'constants/chains'
|
||||||
import { getClientSideQuote } from 'lib/hooks/routing/clientSideSmartOrderRouter'
|
|
||||||
import ms from 'ms'
|
import ms from 'ms'
|
||||||
import { logSwapQuoteRequest } from 'tracing/swapFlowLoggers'
|
import { logSwapQuoteRequest } from 'tracing/swapFlowLoggers'
|
||||||
import { trace } from 'tracing/trace'
|
import { trace } from 'tracing/trace'
|
||||||
@ -20,7 +19,7 @@ import {
|
|||||||
URAQuoteResponse,
|
URAQuoteResponse,
|
||||||
URAQuoteType,
|
URAQuoteType,
|
||||||
} from './types'
|
} from './types'
|
||||||
import { getRouter, isExactInput, shouldUseAPIRouter, transformRoutesToTrade } from './utils'
|
import { isExactInput, shouldUseAPIRouter, transformRoutesToTrade } from './utils'
|
||||||
|
|
||||||
const UNISWAP_API_URL = process.env.REACT_APP_UNISWAP_API_URL
|
const UNISWAP_API_URL = process.env.REACT_APP_UNISWAP_API_URL
|
||||||
if (UNISWAP_API_URL === undefined) {
|
if (UNISWAP_API_URL === undefined) {
|
||||||
@ -182,6 +181,7 @@ export const routingApi = createApi({
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const method = fellBack ? QuoteMethod.CLIENT_SIDE_FALLBACK : QuoteMethod.CLIENT_SIDE
|
const method = fellBack ? QuoteMethod.CLIENT_SIDE_FALLBACK : QuoteMethod.CLIENT_SIDE
|
||||||
|
const { getRouter, getClientSideQuote } = await import('lib/hooks/routing/clientSideSmartOrderRouter')
|
||||||
const router = getRouter(args.tokenInChainId)
|
const router = getRouter(args.tokenInChainId)
|
||||||
const quoteResult = await getClientSideQuote(args, router, CLIENT_PARAMS)
|
const quoteResult = await getClientSideQuote(args, router, CLIENT_PARAMS)
|
||||||
if (quoteResult.state === QuoteState.SUCCESS) {
|
if (quoteResult.state === QuoteState.SUCCESS) {
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import { BigNumber } from '@ethersproject/bignumber'
|
import { BigNumber } from '@ethersproject/bignumber'
|
||||||
import { MixedRouteSDK } from '@uniswap/router-sdk'
|
import { MixedRouteSDK } from '@uniswap/router-sdk'
|
||||||
import { ChainId, Currency, CurrencyAmount, Token, TradeType } from '@uniswap/sdk-core'
|
import { Currency, CurrencyAmount, Token, TradeType } from '@uniswap/sdk-core'
|
||||||
import { AlphaRouter } from '@uniswap/smart-order-router'
|
|
||||||
import { DutchOrderInfo, DutchOrderInfoJSON } from '@uniswap/uniswapx-sdk'
|
import { DutchOrderInfo, DutchOrderInfoJSON } from '@uniswap/uniswapx-sdk'
|
||||||
import { Pair, Route as V2Route } from '@uniswap/v2-sdk'
|
import { Pair, Route as V2Route } from '@uniswap/v2-sdk'
|
||||||
import { FeeAmount, Pool, Route as V3Route } from '@uniswap/v3-sdk'
|
import { FeeAmount, Pool, Route as V3Route } from '@uniswap/v3-sdk'
|
||||||
import { asSupportedChain } from 'constants/chains'
|
|
||||||
import { RPC_PROVIDERS } from 'constants/providers'
|
|
||||||
import { isAvalanche, isBsc, isMatic, nativeOnChain } from 'constants/tokens'
|
import { isAvalanche, isBsc, isMatic, nativeOnChain } from 'constants/tokens'
|
||||||
import { toSlippagePercent } from 'utils/slippage'
|
import { toSlippagePercent } from 'utils/slippage'
|
||||||
|
|
||||||
@ -39,22 +36,6 @@ interface RouteResult {
|
|||||||
outputAmount: CurrencyAmount<Currency>
|
outputAmount: CurrencyAmount<Currency>
|
||||||
}
|
}
|
||||||
|
|
||||||
const routers = new Map<ChainId, AlphaRouter>()
|
|
||||||
export function getRouter(chainId: ChainId): AlphaRouter {
|
|
||||||
const router = routers.get(chainId)
|
|
||||||
if (router) return router
|
|
||||||
|
|
||||||
const supportedChainId = asSupportedChain(chainId)
|
|
||||||
if (supportedChainId) {
|
|
||||||
const provider = RPC_PROVIDERS[supportedChainId]
|
|
||||||
const router = new AlphaRouter({ chainId, provider })
|
|
||||||
routers.set(chainId, router)
|
|
||||||
return router
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Router does not support this chain (chainId: ${chainId}).`)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a Routing API quote into an array of routes that can be used to
|
* Transforms a Routing API quote into an array of routes that can be used to
|
||||||
* create a `Trade`.
|
* create a `Trade`.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { ChainId, CurrencyAmount, Percent, Token, TradeType } from '@uniswap/sdk-core'
|
import { ChainId, CurrencyAmount, Percent, Token, TradeType } from '@uniswap/sdk-core'
|
||||||
|
// This is a test file, so the import of smart-order-router is allowed.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||||
import { V3Route } from '@uniswap/smart-order-router'
|
import { V3Route } from '@uniswap/smart-order-router'
|
||||||
import { FeeAmount, Pool } from '@uniswap/v3-sdk'
|
import { FeeAmount, Pool } from '@uniswap/v3-sdk'
|
||||||
import { ZERO_PERCENT } from 'constants/misc'
|
import { ZERO_PERCENT } from 'constants/misc'
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { Protocol } from '@uniswap/router-sdk'
|
import { Protocol } from '@uniswap/router-sdk'
|
||||||
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
||||||
|
// This file is lazy-loaded, so the import of smart-order-router is intentional.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||||
import { routeAmountsToString, SwapRoute } from '@uniswap/smart-order-router'
|
import { routeAmountsToString, SwapRoute } from '@uniswap/smart-order-router'
|
||||||
import { Pool } from '@uniswap/v3-sdk'
|
import { Pool } from '@uniswap/v3-sdk'
|
||||||
import { QuoteResult, QuoteState, URAQuoteType } from 'state/routing/types'
|
import { QuoteResult, QuoteState, URAQuoteType } from 'state/routing/types'
|
||||||
|
Loading…
Reference in New Issue
Block a user