Commit 2246afce authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: only poll every 12s (#5110)

* fix: only poll every 12s

* refactor: clarify polling interval

* doc: polling interval

* docs: further clarification

* fix: ctor super first
parent e0767b1c
...@@ -3,10 +3,16 @@ import { deepCopy } from '@ethersproject/properties' ...@@ -3,10 +3,16 @@ import { deepCopy } from '@ethersproject/properties'
// eslint-disable-next-line @typescript-eslint/no-restricted-imports // eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { StaticJsonRpcProvider } from '@ethersproject/providers' import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { isPlain } from '@reduxjs/toolkit' import { isPlain } from '@reduxjs/toolkit'
import ms from 'ms.macro'
import { SupportedChainId } from './chains' import { CHAIN_IDS_TO_NAMES, SupportedChainId } from './chains'
import { RPC_URLS } from './networks' import { RPC_URLS } from './networks'
// NB: Third-party providers (eg MetaMask) will have their own polling intervals,
// which should be left as-is to allow operations (eg transaction confirmation) to resolve faster.
// Network providers (eg AppJsonRpcProvider) need to update less frequently to be considered responsive.
export const POLLING_INTERVAL = ms`12s` // mainnet block frequency - ok for other chains as it is a sane refresh rate
class AppJsonRpcProvider extends StaticJsonRpcProvider { class AppJsonRpcProvider extends StaticJsonRpcProvider {
private _blockCache = new Map<string, Promise<any>>() private _blockCache = new Map<string, Promise<any>>()
get blockCache() { get blockCache() {
...@@ -18,8 +24,10 @@ class AppJsonRpcProvider extends StaticJsonRpcProvider { ...@@ -18,8 +24,10 @@ class AppJsonRpcProvider extends StaticJsonRpcProvider {
return this._blockCache return this._blockCache
} }
constructor(urls: string[]) { constructor(chainId: SupportedChainId) {
super(urls[0]) // Including networkish allows ethers to skip the initial detectNetwork call.
super(RPC_URLS[chainId][0], /* networkish= */ { chainId, name: CHAIN_IDS_TO_NAMES[chainId] })
this.pollingInterval = POLLING_INTERVAL
} }
send(method: string, params: Array<any>): Promise<any> { send(method: string, params: Array<any>): Promise<any> {
...@@ -50,17 +58,17 @@ class AppJsonRpcProvider extends StaticJsonRpcProvider { ...@@ -50,17 +58,17 @@ class AppJsonRpcProvider extends StaticJsonRpcProvider {
* These are the only JsonRpcProviders used directly by the interface. * These are the only JsonRpcProviders used directly by the interface.
*/ */
export const RPC_PROVIDERS: { [key in SupportedChainId]: StaticJsonRpcProvider } = { export const RPC_PROVIDERS: { [key in SupportedChainId]: StaticJsonRpcProvider } = {
[SupportedChainId.MAINNET]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.MAINNET]), [SupportedChainId.MAINNET]: new AppJsonRpcProvider(SupportedChainId.MAINNET),
[SupportedChainId.RINKEBY]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.RINKEBY]), [SupportedChainId.RINKEBY]: new AppJsonRpcProvider(SupportedChainId.RINKEBY),
[SupportedChainId.ROPSTEN]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.ROPSTEN]), [SupportedChainId.ROPSTEN]: new AppJsonRpcProvider(SupportedChainId.ROPSTEN),
[SupportedChainId.GOERLI]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.GOERLI]), [SupportedChainId.GOERLI]: new AppJsonRpcProvider(SupportedChainId.GOERLI),
[SupportedChainId.KOVAN]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.KOVAN]), [SupportedChainId.KOVAN]: new AppJsonRpcProvider(SupportedChainId.KOVAN),
[SupportedChainId.OPTIMISM]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.OPTIMISM]), [SupportedChainId.OPTIMISM]: new AppJsonRpcProvider(SupportedChainId.OPTIMISM),
[SupportedChainId.OPTIMISM_GOERLI]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.OPTIMISM_GOERLI]), [SupportedChainId.OPTIMISM_GOERLI]: new AppJsonRpcProvider(SupportedChainId.OPTIMISM_GOERLI),
[SupportedChainId.ARBITRUM_ONE]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.ARBITRUM_ONE]), [SupportedChainId.ARBITRUM_ONE]: new AppJsonRpcProvider(SupportedChainId.ARBITRUM_ONE),
[SupportedChainId.ARBITRUM_RINKEBY]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.ARBITRUM_RINKEBY]), [SupportedChainId.ARBITRUM_RINKEBY]: new AppJsonRpcProvider(SupportedChainId.ARBITRUM_RINKEBY),
[SupportedChainId.POLYGON]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.POLYGON]), [SupportedChainId.POLYGON]: new AppJsonRpcProvider(SupportedChainId.POLYGON),
[SupportedChainId.POLYGON_MUMBAI]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.POLYGON_MUMBAI]), [SupportedChainId.POLYGON_MUMBAI]: new AppJsonRpcProvider(SupportedChainId.POLYGON_MUMBAI),
[SupportedChainId.CELO]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.CELO]), [SupportedChainId.CELO]: new AppJsonRpcProvider(SupportedChainId.CELO),
[SupportedChainId.CELO_ALFAJORES]: new AppJsonRpcProvider(RPC_URLS[SupportedChainId.CELO_ALFAJORES]), [SupportedChainId.CELO_ALFAJORES]: new AppJsonRpcProvider(SupportedChainId.CELO_ALFAJORES),
} }
...@@ -2,6 +2,7 @@ import { skipToken } from '@reduxjs/toolkit/query/react' ...@@ -2,6 +2,7 @@ import { skipToken } from '@reduxjs/toolkit/query/react'
import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core'
import { IMetric, MetricLoggerUnit, setGlobalMetric } from '@uniswap/smart-order-router' import { IMetric, MetricLoggerUnit, setGlobalMetric } from '@uniswap/smart-order-router'
import { sendTiming } from 'components/analytics' import { sendTiming } from 'components/analytics'
import { POLLING_INTERVAL } from 'constants/providers'
import { useStablecoinAmountFromFiatValue } from 'hooks/useStablecoinPrice' import { useStablecoinAmountFromFiatValue } from 'hooks/useStablecoinPrice'
import { useRoutingAPIArguments } from 'lib/hooks/routing/useRoutingAPIArguments' import { useRoutingAPIArguments } from 'lib/hooks/routing/useRoutingAPIArguments'
import useIsValidBlock from 'lib/hooks/useIsValidBlock' import useIsValidBlock from 'lib/hooks/useIsValidBlock'
...@@ -45,7 +46,7 @@ export function useRoutingAPITrade<TTradeType extends TradeType>( ...@@ -45,7 +46,7 @@ export function useRoutingAPITrade<TTradeType extends TradeType>(
const { isLoading, isError, data, currentData } = useGetQuoteQuery(queryArgs ?? skipToken, { const { isLoading, isError, data, currentData } = useGetQuoteQuery(queryArgs ?? skipToken, {
// Price-fetching is informational and costly, so it's done less frequently. // Price-fetching is informational and costly, so it's done less frequently.
pollingInterval: routerPreference === RouterPreference.PRICE ? ms`2m` : ms`15s`, pollingInterval: routerPreference === RouterPreference.PRICE ? ms`2m` : POLLING_INTERVAL,
}) })
const quoteResult: GetQuoteResult | undefined = useIsValidBlock(Number(data?.blockNumber) || 0) ? data : undefined const quoteResult: GetQuoteResult | undefined = useIsValidBlock(Number(data?.blockNumber) || 0) ? data : undefined
......
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