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

fix(swap): fix swap out of market #3576

Merged
merged 3 commits into from
Jan 10, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSetAtom } from 'jotai'
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { useCallback, useEffect, useRef } from 'react'

import { priceOutOfRangeAnalytics } from '@cowprotocol/analytics'
import { useTokensBalances } from '@cowprotocol/balances-and-allowances'
Expand Down Expand Up @@ -44,9 +44,7 @@ export function UnfillableOrdersUpdater(): null {
const updatePendingOrderPrices = useSetAtom(updatePendingOrderPricesAtom)
const isWindowVisible = useIsWindowVisible()

const pendingLimit = useOnlyPendingOrders(chainId, UiOrderType.LIMIT)
const pendingTwap = useOnlyPendingOrders(chainId, UiOrderType.TWAP)
const pending = useMemo(() => pendingLimit.concat(pendingTwap), [pendingLimit, pendingTwap])
const pending = useOnlyPendingOrders(chainId)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We stopped checking for SWAPs since the preparation for TWAP.
This change checks for all order types once again, since we need the indication for all pending orders.


const setIsOrderUnfillable = useSetIsOrderUnfillable()
const strategy = useGetGpPriceStrategy()
Expand Down Expand Up @@ -225,6 +223,9 @@ async function _getOrderPrice(

const amount = getRemainderAmount(order.kind, order)

// Don't quote if there's nothing left to match in this order
if (amount === '0') return null
Comment on lines +226 to +227
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bonus: fix for this issue I observed while testing.

The order would match but still be in the pending list.
This prevent from quoting it and getting unnecessary 400s.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, thanks


if (order.kind === 'sell') {
// this order sell amount is sellAmountAfterFees
// this is an issue as it will be adjusted again in the backend
Expand Down Expand Up @@ -262,6 +263,8 @@ async function _getOrderPrice(
receiver: order.receiver,
isEthFlow,
priceQuality: getPriceQuality({ verifyQuote }),
appData: order.appData ?? undefined,
appDataHash: order.appDataHash ?? undefined,
Comment on lines +266 to +267
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bonus: pass the original appData so we can get more precise quotes.
It could have for example the permit hook, which affects the fee price.

}
try {
return getBestQuote({ strategy, quoteParams, fetchFee: false, isPriceRefresh: false })
Expand Down
9 changes: 3 additions & 6 deletions apps/cowswap-frontend/src/legacy/state/orders/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,16 @@ export const useCombinedPendingOrders = ({
* The difference is that this hook returns only orders that have the status PENDING
* while usePendingOrders aggregates all pending states
*/
export const useOnlyPendingOrders = (chainId: SupportedChainId, uiOrderType: UiOrderType): Order[] => {
export const useOnlyPendingOrders = (chainId: SupportedChainId): Order[] => {
const state = useSelector<AppState, PartialOrdersMap | undefined>(
(state) => chainId && state.orders?.[chainId]?.pending
)

return useMemo(() => {
if (!state) return []

return Object.values(state)
.filter((order) => order && getUiOrderType(order.order) === uiOrderType)
.map(_deserializeOrder)
.filter(isTruthy)
}, [state, uiOrderType])
return Object.values(state).map(_deserializeOrder).filter(isTruthy)
}, [state])
}

export const useCancelledOrders = ({ chainId }: GetOrdersParams): Order[] => {
Expand Down
Loading