Skip to content

Commit

Permalink
chore: reduce queries from unfillable updater (#3107)
Browse files Browse the repository at this point in the history
* chore: uipdate query time to 30s

* chore: do not clear intervals because of a change in the update function

* fix: make sure we invoke the latest update function

* chore: reduce the times the update function is recreated

* Remove ref from dependencies

Co-authored-by: Leandro <[email protected]>

* chore: remove unecesary log

---------

Co-authored-by: Leandro <[email protected]>
  • Loading branch information
anxolin and alfetopito authored Sep 7, 2023
1 parent cadc7ad commit 5b7c5a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
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`
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,
})
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)
return () => clearInterval(interval)
}, [updatePending, chainId, account, isWindowVisible])
}, [chainId, account, isWindowVisible])

return null
}
Expand Down

0 comments on commit 5b7c5a4

Please sign in to comment.