Skip to content

Commit

Permalink
feat: optimize client side SOR for widgets (#3294)
Browse files Browse the repository at this point in the history
* start SOR updates

* update pool providers to static

* update router config

* remove log

* udpate defaults for chainId

* small changes
  • Loading branch information
ianlapham authored Feb 14, 2022
1 parent 7de63ab commit 41ef961
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Protocol } from '@uniswap/router-sdk'
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
import { ChainId } from '@uniswap/smart-order-router'
import { useStablecoinAmountFromFiatValue } from 'hooks/useUSDCPrice'
import { useEffect, useMemo, useState } from 'react'
import { GetQuoteResult, InterfaceTrade, TradeState } from 'state/routing/types'
Expand All @@ -9,8 +10,28 @@ import useActiveWeb3React from '../useActiveWeb3React'
import { getClientSideQuote } from './clientSideSmartOrderRouter'
import { useRoutingAPIArguments } from './useRoutingAPIArguments'

const protocols: Protocol[] = [Protocol.V2, Protocol.V3]
const config = { protocols }
/**
* Reduces client-side latency by increasing the minimum percentage of the input token to use for each route in a split route while SOR is used client-side.
* Defaults are defined in https://github.com/Uniswap/smart-order-router/blob/309e6f6603984d3b5aef0733b0cfaf129c29f602/src/routers/alpha-router/config.ts#L83.
*/
const DistributionPercents: { [key: number]: number } = {
[ChainId.MAINNET]: 10,
[ChainId.OPTIMISM]: 10,
[ChainId.OPTIMISTIC_KOVAN]: 10,
[ChainId.ARBITRUM_ONE]: 25,
[ChainId.ARBITRUM_RINKEBY]: 25,
}

const DEFAULT_DISTRIBUTION_PERCENT = 10

function getConfig(chainId: ChainId | undefined) {
return {
// Limit to only V2 and V3.
protocols: [Protocol.V2, Protocol.V3],

distributionPercent: (chainId && DistributionPercents[chainId]) ?? DEFAULT_DISTRIBUTION_PERCENT,
}
}

export default function useClientSideSmartOrderRouterTrade<TTradeType extends TradeType>(
tradeType: TTradeType,
Expand Down Expand Up @@ -49,6 +70,8 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr
error: undefined,
})

const config = useMemo(() => getConfig(chainId), [chainId])

// When arguments update, make a new call to SOR for updated quote
useEffect(() => {
setLoading(true)
Expand All @@ -72,7 +95,7 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr
setLoading(false)
}
}
}, [queryArgs, params])
}, [queryArgs, params, config])

const route = useMemo(
() => computeRoutes(currencyIn, currencyOut, tradeType, quoteResult),
Expand Down

0 comments on commit 41ef961

Please sign in to comment.