Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce queries from unfillable updater #3107

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/cowswap-frontend/src/legacy/state/orders/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const ContractDeploymentBlocks: Partial<Record<ChainId, number>> = {
export const MARKET_OPERATOR_API_POLL_INTERVAL = ms`2s`
// We can have lots of limit orders and it creates a high load, so we poll them no so ofter as market orders
export const LIMIT_OPERATOR_API_POLL_INTERVAL = ms`15s`
export const PENDING_ORDERS_PRICE_CHECK_POLL_INTERVAL = ms`15s`
export const PENDING_ORDERS_PRICE_CHECK_POLL_INTERVAL = ms`30s`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to poll this often

export const SPOT_PRICE_CHECK_POLL_INTERVAL = ms`15s`
export const EXPIRED_ORDERS_CHECK_POLL_INTERVAL = ms`15s`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export function UnfillableOrdersUpdater(): null {
[setIsOrderUnfillable, updateOrderMarketPriceCallback]
)

const balancesRef = useRef(balances)
balancesRef.current = balances
const updatePending = useCallback(() => {
if (!chainId || !account || isUpdating.current || !isWindowVisible) {
return
Expand All @@ -144,7 +146,11 @@ export function UnfillableOrdersUpdater(): null {
console.debug(`[UnfillableOrdersUpdater] Check order`, order)

const currencyAmount = CurrencyAmount.fromRawAmount(order.inputToken, order.sellAmount)
const enoughBalance = hasEnoughBalanceAndAllowance({ account, amount: currencyAmount, balances })
const enoughBalance = hasEnoughBalanceAndAllowance({
account,
amount: currencyAmount,
balances: balancesRef.current,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now balances uses a ref to not re-render the function that often, but always use the latest version of the balances

})
const verifiedQuote = verifiedQuotesEnabled && enoughBalance

_getOrderPrice(chainId, order, verifiedQuote, strategy)
Expand Down Expand Up @@ -185,22 +191,24 @@ export function UnfillableOrdersUpdater(): null {
strategy,
updateIsUnfillableFlag,
isWindowVisible,
balances,
updatePendingOrderPrices,
verifiedQuotesEnabled,
])

const updatePendingRef = useRef(updatePending)
updatePendingRef.current = updatePending

useEffect(() => {
if (!chainId || !account || !isWindowVisible) {
console.debug('[UnfillableOrdersUpdater] No need to fetch unfillable orders')
return
}

console.debug('[UnfillableOrdersUpdater] Periodically check for unfillable orders')
updatePending()
const interval = setInterval(updatePending, PENDING_ORDERS_PRICE_CHECK_POLL_INTERVAL)
updatePendingRef.current()
const interval = setInterval(() => updatePendingRef.current(), PENDING_ORDERS_PRICE_CHECK_POLL_INTERVAL)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updatePending function is the culpit of the re-renders that cause too many queries

return () => clearInterval(interval)
}, [updatePending, chainId, account, isWindowVisible])
}, [chainId, account, isWindowVisible])

return null
}
Expand Down
Loading