Skip to content

Commit 0390370

Browse files
authored
feat(limit-orders): check permit validity in orders table (#6496)
* feat: add permit validation for lo * refactor: move permit validity updater to lo module * refactor: move updater to order module * feat: add limit orders permit updater * refactor: optimize flags * refactor: typings * feat: add permit validity checking for limit getOrderParams * test: cover getPermitAmount * refactor: decompose check permit amount fn * refactor: check calldata validity before a viewcall * fix: adjust test cases * refactor: add useMemo * chore: adjust import * fix: mark order as unfillable inside group * refactor: use helper for addresses and add type definition * refactor: move trade orders permit updater to ordersTable module * refactor: move the updater * fix: check only pending orders
1 parent 6f649ec commit 0390370

File tree

30 files changed

+768
-179
lines changed

30 files changed

+768
-179
lines changed

apps/cowswap-frontend/src/common/hooks/usePendingOrdersFillability.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

apps/cowswap-frontend/src/common/utils/doesOrderHavePermit.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { doesHookHavePermit } from '@cowprotocol/hook-dapp-lib'
22

3-
import { Order } from 'legacy/state/orders/actions'
4-
53
import { getAppDataHooks } from 'modules/appData'
64

7-
export function doesOrderHavePermit(order: Order): boolean {
5+
import { GenericOrder } from 'common/types'
6+
7+
export function doesOrderHavePermit(order: GenericOrder): boolean {
88
return !!getOrderPermitIfExists(order)
99
}
1010

11-
export function getOrderPermitIfExists(order: Order): string | null {
11+
export function getOrderPermitIfExists(order: GenericOrder): string | null {
1212
const appData = order.fullAppData
1313
const hooks = getAppDataHooks(appData)
1414
if (!hooks?.pre) return null

apps/cowswap-frontend/src/legacy/hooks/useRecentActivity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { EnhancedTransactionDetails } from 'legacy/state/enhancedTransactions/re
1111
import { Order, OrderStatus } from 'legacy/state/orders/actions'
1212
import { useOrder, useOrders, useOrdersById } from 'legacy/state/orders/hooks'
1313

14-
import { OrderFillability, usePendingOrdersFillability } from 'common/hooks/usePendingOrdersFillability'
14+
import { OrderFillability, usePendingOrdersFillability } from 'modules/ordersTable'
15+
1516
import { ActivityStatus, ActivityType } from 'common/types/activity'
1617

1718
/**

apps/cowswap-frontend/src/modules/account/containers/Transaction/ActivityDetails.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { BridgeActivitySummary } from 'modules/bridge'
2222
import { EthFlowStepper } from 'modules/ethFlow'
2323
import { useInjectedWidgetParams } from 'modules/injectedWidget'
2424
import { useGetPendingOrdersPermitValidityState } from 'modules/ordersTable'
25+
import { OrderFillability } from 'modules/ordersTable'
2526
import { useSwapPartialApprovalToggleState } from 'modules/swap/hooks/useSwapSettings'
2627
import { ConfirmDetailsItem } from 'modules/trade'
2728

@@ -30,7 +31,6 @@ import { useCancelOrder } from 'common/hooks/useCancelOrder'
3031
import { isPending } from 'common/hooks/useCategorizeRecentActivity'
3132
import { useEnhancedActivityDerivedState } from 'common/hooks/useEnhancedActivityDerivedState'
3233
import { useGetSurplusData } from 'common/hooks/useGetSurplusFiatValue'
33-
import { OrderFillability } from 'common/hooks/usePendingOrdersFillability'
3434
import { useSwapAndBridgeContext } from 'common/hooks/useSwapAndBridgeContext'
3535
import { CurrencyLogoPair } from 'common/pure/CurrencyLogoPair'
3636
import { CustomRecipientWarningBanner } from 'common/pure/CustomRecipientWarningBanner'

apps/cowswap-frontend/src/modules/account/pure/OrderFillabilityWarning/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ReactNode } from 'react'
33
import { BannerOrientation, StatusColorVariant } from '@cowprotocol/ui'
44
import { CurrencyAmount, Token } from '@uniswap/sdk-core'
55

6-
import { OrderFillability } from 'common/hooks/usePendingOrdersFillability'
6+
import { OrderFillability } from 'modules/ordersTable'
77

88
import {
99
ApproveWrapper,

apps/cowswap-frontend/src/modules/limitOrders/containers/LimitOrdersWidget/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useAtomValue } from 'jotai'
2-
import React, { useCallback, useEffect, useMemo } from 'react'
2+
import React, { ReactElement, useCallback, useEffect, useMemo } from 'react'
33

44
import { useFeatureFlags } from '@cowprotocol/common-hooks'
55
import { isSellOrder } from '@cowprotocol/common-utils'
@@ -60,10 +60,7 @@ const UNLOCK_SCREEN = {
6060
buttonLink: 'https://cow.fi/learn/cow-swap-improves-the-limit-order-experience-with-partially-fillable-limit-orders',
6161
}
6262

63-
// TODO: Break down this large function into smaller functions
64-
// TODO: Add proper return type annotation
65-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
66-
export function LimitOrdersWidget() {
63+
export function LimitOrdersWidget(): ReactElement {
6764
const {
6865
inputCurrency,
6966
outputCurrency,

apps/cowswap-frontend/src/modules/orderProgressBar/updaters/OrderProgressEventsUpdater.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OrderFillability } from 'common/hooks/usePendingOrdersFillability'
1+
import type { OrderFillability } from 'modules/ordersTable'
22

33
import { computeUnfillableOrderIds, getNewlyFillableOrderIds } from './OrderProgressEventsUpdater'
44

apps/cowswap-frontend/src/modules/orderProgressBar/updaters/OrderProgressEventsUpdater.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { WIDGET_EVENT_EMITTER } from 'widgetEventEmitter'
1616

1717
import { useOnlyPendingOrders } from 'legacy/state/orders/hooks'
1818

19-
import { usePendingOrdersFillability, type OrderFillability } from 'common/hooks/usePendingOrdersFillability'
19+
import { usePendingOrdersFillability, type OrderFillability } from 'modules/ordersTable'
2020

2121
import { OrderProgressBarStepName } from '../constants'
2222
import {

apps/cowswap-frontend/src/modules/ordersTable/containers/OrdersTableWidget/hooks/useOrdersTableList.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { BalancesAndAllowances } from '@cowprotocol/balances-and-allowances'
55
import { Order, OrderStatus, PENDING_STATES } from 'legacy/state/orders/actions'
66
import { useSetIsOrderUnfillable } from 'legacy/state/orders/hooks'
77

8+
import { useGetPendingOrdersPermitValidityState } from 'modules/ordersTable'
9+
810
import { getIsComposableCowOrder } from 'utils/orderUtils/getIsComposableCowOrder'
911
import { getIsNotComposableCowOrder } from 'utils/orderUtils/getIsNotComposableCowOrder'
1012

@@ -34,6 +36,8 @@ export function useOrdersTableList(
3436
return groupOrdersTable(allOrders).sort(ordersSorter)
3537
}, [allOrders])
3638

39+
const pendingOrdersPermitValidityState = useGetPendingOrdersPermitValidityState()
40+
3741
// Then, categorize orders into their respective lists
3842
return useMemo(
3943
() =>
@@ -58,13 +62,18 @@ export function useOrdersTableList(
5862
const isSigning = order.status === OrderStatus.PRESIGNATURE_PENDING
5963

6064
// Check if order is unfillable (insufficient balance or allowance)
61-
const params = getOrderParams(chainId, balancesAndAllowances, order)
65+
const params = getOrderParams(chainId, balancesAndAllowances, order, pendingOrdersPermitValidityState)
6266
let isUnfillable = params.hasEnoughBalance === false || params.hasEnoughAllowance === false
6367

6468
// For TWAP orders, also check child orders
6569
if (!isParsedOrder(item) && item.children) {
6670
const hasUnfillableChild = item.children.some((childOrder) => {
67-
const childParams = getOrderParams(chainId, balancesAndAllowances, childOrder)
71+
const childParams = getOrderParams(
72+
chainId,
73+
balancesAndAllowances,
74+
childOrder,
75+
pendingOrdersPermitValidityState,
76+
)
6877
return (
6978
childOrder.status !== OrderStatus.FULFILLED &&
7079
(childOrder.status === OrderStatus.SCHEDULED || childOrder.status === OrderStatus.PENDING) &&
@@ -103,6 +112,14 @@ export function useOrdersTableList(
103112
},
104113
{ open: [], history: [], unfillable: [], signing: [], all: [] },
105114
),
106-
[allSortedOrders, chainId, balancesAndAllowances, orderType, setIsOrderUnfillable, orderLimit],
115+
[
116+
allSortedOrders,
117+
chainId,
118+
balancesAndAllowances,
119+
orderType,
120+
setIsOrderUnfillable,
121+
orderLimit,
122+
pendingOrdersPermitValidityState,
123+
],
107124
)
108125
}

apps/cowswap-frontend/src/modules/ordersTable/containers/OrdersTableWidget/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ export function OrdersTableWidget(props: OrdersTableWidgetProps): ReactNode {
5757
})
5858
}, [isTabWithPending, filteredOrders, currentPageNumber])
5959

60+
const hasPendingOrders = !!pendingOrders?.length
61+
6062
return (
6163
<>
62-
{!!pendingOrders?.length && <UnfillableOrdersUpdater orders={pendingOrders} />}
64+
{hasPendingOrders && <UnfillableOrdersUpdater orders={pendingOrders} />}
6365
<OrdersTableStateUpdater searchTerm={searchTerm} {...stateParams} />
6466
{children}
6567
<OrdersTableContainer searchTerm={searchTerm} isDarkMode={darkMode}>
66-
{!!pendingOrders?.length && <MultipleCancellationMenu pendingOrders={pendingOrders} />}
68+
{hasPendingOrders && <MultipleCancellationMenu pendingOrders={pendingOrders} />}
6769

6870
{/* If account is not connected, don't show the search input */}
6971
{!!account && !!orders?.length && (

0 commit comments

Comments
 (0)