From 381d9011a0c0c15b796c90e8be4bc714c674c4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Mon, 3 Apr 2023 23:20:31 +0200 Subject: [PATCH] fix(orders): restore filter by marketId (#3342) --- .../order-data-provider/Orders.graphql | 12 ++++-- .../__generated__/Orders.ts | 17 ++++++--- .../order-data-provider.spec.ts | 4 +- .../order-data-provider.ts | 38 ++++++++----------- .../order-list-manager/use-order-list-data.ts | 10 ++--- .../order-hooks/use-pending-orders-volume.ts | 12 +++--- .../src/lib/close-position-dialog/shared.tsx | 4 +- .../src/lib/positions-data-providers.ts | 15 ++++---- .../lib/use-request-close-position-data.ts | 29 ++++---------- 9 files changed, 63 insertions(+), 78 deletions(-) diff --git a/libs/orders/src/lib/components/order-data-provider/Orders.graphql b/libs/orders/src/lib/components/order-data-provider/Orders.graphql index 5a409e326..67ba24909 100644 --- a/libs/orders/src/lib/components/order-data-provider/Orders.graphql +++ b/libs/orders/src/lib/components/order-data-provider/Orders.graphql @@ -32,12 +32,16 @@ query OrderById($orderId: ID!) { query Orders( $partyId: ID! + $marketIds: [ID!] $pagination: Pagination - $filter: OrderByMarketIdsFilter + $filter: OrderFilter ) { party(id: $partyId) { id - ordersConnection(pagination: $pagination, filter: $filter) { + ordersConnection( + pagination: $pagination + filter: { order: $filter, marketIds: $marketIds } + ) { edges { node { ...OrderFields @@ -74,8 +78,8 @@ fragment OrderUpdateFields on OrderUpdate { } } -subscription OrdersUpdate($partyId: ID!) { - orders(filter: { partyIds: [$partyId] }) { +subscription OrdersUpdate($partyId: ID!, $marketIds: [ID!]) { + orders(filter: { partyIds: [$partyId], marketIds: $marketIds }) { ...OrderUpdateFields } } diff --git a/libs/orders/src/lib/components/order-data-provider/__generated__/Orders.ts b/libs/orders/src/lib/components/order-data-provider/__generated__/Orders.ts index 6ea18be70..6b91e2f07 100644 --- a/libs/orders/src/lib/components/order-data-provider/__generated__/Orders.ts +++ b/libs/orders/src/lib/components/order-data-provider/__generated__/Orders.ts @@ -14,8 +14,9 @@ export type OrderByIdQuery = { __typename?: 'Query', orderByID: { __typename?: ' export type OrdersQueryVariables = Types.Exact<{ partyId: Types.Scalars['ID']; + marketIds?: Types.InputMaybe | Types.Scalars['ID']>; pagination?: Types.InputMaybe; - filter?: Types.InputMaybe; + filter?: Types.InputMaybe; }>; @@ -25,6 +26,7 @@ export type OrderUpdateFieldsFragment = { __typename?: 'OrderUpdate', id: string export type OrdersUpdateSubscriptionVariables = Types.Exact<{ partyId: Types.Scalars['ID']; + marketIds?: Types.InputMaybe | Types.Scalars['ID']>; }>; @@ -114,10 +116,13 @@ export type OrderByIdQueryHookResult = ReturnType; export type OrderByIdLazyQueryHookResult = ReturnType; export type OrderByIdQueryResult = Apollo.QueryResult; export const OrdersDocument = gql` - query Orders($partyId: ID!, $pagination: Pagination, $filter: OrderByMarketIdsFilter) { + query Orders($partyId: ID!, $marketIds: [ID!], $pagination: Pagination, $filter: OrderFilter) { party(id: $partyId) { id - ordersConnection(pagination: $pagination, filter: $filter) { + ordersConnection( + pagination: $pagination + filter: {order: $filter, marketIds: $marketIds} + ) { edges { node { ...OrderFields @@ -148,6 +153,7 @@ export const OrdersDocument = gql` * const { data, loading, error } = useOrdersQuery({ * variables: { * partyId: // value for 'partyId' + * marketIds: // value for 'marketIds' * pagination: // value for 'pagination' * filter: // value for 'filter' * }, @@ -165,8 +171,8 @@ export type OrdersQueryHookResult = ReturnType; export type OrdersLazyQueryHookResult = ReturnType; export type OrdersQueryResult = Apollo.QueryResult; export const OrdersUpdateDocument = gql` - subscription OrdersUpdate($partyId: ID!) { - orders(filter: {partyIds: [$partyId]}) { + subscription OrdersUpdate($partyId: ID!, $marketIds: [ID!]) { + orders(filter: {partyIds: [$partyId], marketIds: $marketIds}) { ...OrderUpdateFields } } @@ -185,6 +191,7 @@ export const OrdersUpdateDocument = gql` * const { data, loading, error } = useOrdersUpdateSubscription({ * variables: { * partyId: // value for 'partyId' + * marketIds: // value for 'marketIds' * }, * }); */ diff --git a/libs/orders/src/lib/components/order-data-provider/order-data-provider.spec.ts b/libs/orders/src/lib/components/order-data-provider/order-data-provider.spec.ts index 436967f77..10b650436 100644 --- a/libs/orders/src/lib/components/order-data-provider/order-data-provider.spec.ts +++ b/libs/orders/src/lib/components/order-data-provider/order-data-provider.spec.ts @@ -112,9 +112,7 @@ describe('order data provider', () => { const updatedData = update(data, delta, () => null, { partyId: '0x123', filter: { - order: { - dateRange: { end: new Date('2022-02-01').toISOString() }, - }, + dateRange: { end: new Date('2022-02-01').toISOString() }, }, }); expect( diff --git a/libs/orders/src/lib/components/order-data-provider/order-data-provider.ts b/libs/orders/src/lib/components/order-data-provider/order-data-provider.ts index 7065782a1..833769829 100644 --- a/libs/orders/src/lib/components/order-data-provider/order-data-provider.ts +++ b/libs/orders/src/lib/components/order-data-provider/order-data-provider.ts @@ -35,45 +35,40 @@ const orderMatchFilters = ( return true; } if ( - variables?.filter?.order?.status && - !(order.status && variables.filter.order.status.includes(order.status)) + variables?.filter?.status && + !(order.status && variables.filter.status.includes(order.status)) ) { return false; } if ( - variables?.filter?.order?.types && - !(order.type && variables.filter.order.types.includes(order.type)) + variables?.filter?.types && + !(order.type && variables.filter.types.includes(order.type)) ) { return false; } if ( - variables?.filter?.order?.timeInForce && - !variables.filter.order.timeInForce.includes(order.timeInForce) + variables?.filter?.timeInForce && + !variables.filter.timeInForce.includes(order.timeInForce) ) { return false; } - if ( - variables?.filter?.order?.excludeLiquidity && - order.liquidityProvisionId - ) { + if (variables?.filter?.excludeLiquidity && order.liquidityProvisionId) { return false; } if ( - variables?.filter?.order?.dateRange?.start && + variables?.filter?.dateRange?.start && !( (order.updatedAt || order.createdAt) && - variables.filter.order.dateRange.start < - (order.updatedAt || order.createdAt) + variables.filter.dateRange.start < (order.updatedAt || order.createdAt) ) ) { return false; } if ( - variables?.filter?.order?.dateRange?.end && + variables?.filter?.dateRange?.end && !( (order.updatedAt || order.createdAt) && - variables.filter.order.dateRange.end > - (order.updatedAt || order.createdAt) + variables.filter.dateRange.end > (order.updatedAt || order.createdAt) ) ) { return false; @@ -243,18 +238,17 @@ export const hasActiveOrderProvider = makeDerivedDataProvider< { partyId: string; marketId?: string } >( [ - (callback, client, variables) => + (callback, client, { partyId, marketId }) => hasActiveOrderProviderInternal(callback, client, { + marketIds: marketId ? [marketId] : undefined, filter: { - order: { - status: [OrderStatus.STATUS_ACTIVE], - excludeLiquidity: true, - }, + status: [OrderStatus.STATUS_ACTIVE], + excludeLiquidity: true, }, pagination: { first: 1, }, - ...variables, + partyId, } as OrdersQueryVariables), ], (parts) => parts[0] diff --git a/libs/orders/src/lib/components/order-list-manager/use-order-list-data.ts b/libs/orders/src/lib/components/order-list-manager/use-order-list-data.ts index 00e16c27e..060a9180a 100644 --- a/libs/orders/src/lib/components/order-list-manager/use-order-list-data.ts +++ b/libs/orders/src/lib/components/order-list-manager/use-order-list-data.ts @@ -72,12 +72,10 @@ export const useOrderListData = ({ const allVars: OrdersQueryVariables & OrdersUpdateSubscriptionVariables = { partyId, filter: { - order: { - dateRange: filter?.updatedAt?.value, - status: filter?.status?.value, - timeInForce: filter?.timeInForce?.value, - types: filter?.type?.value, - }, + dateRange: filter?.updatedAt?.value, + status: filter?.status?.value, + timeInForce: filter?.timeInForce?.value, + types: filter?.type?.value, }, pagination: { first: 1000, diff --git a/libs/orders/src/lib/order-hooks/use-pending-orders-volume.ts b/libs/orders/src/lib/order-hooks/use-pending-orders-volume.ts index efcb3513d..988570419 100644 --- a/libs/orders/src/lib/order-hooks/use-pending-orders-volume.ts +++ b/libs/orders/src/lib/order-hooks/use-pending-orders-volume.ts @@ -53,14 +53,12 @@ export const useActiveOrdersVolumeAndMargin = ( update, variables: { partyId: partyId || '', + marketIds: [marketId], filter: { - marketIds: [marketId], - order: { - status: [ - OrderStatus.STATUS_ACTIVE, - OrderStatus.STATUS_PARTIALLY_FILLED, - ], - }, + status: [ + OrderStatus.STATUS_ACTIVE, + OrderStatus.STATUS_PARTIALLY_FILLED, + ], }, }, skip: !partyId, diff --git a/libs/positions/src/lib/close-position-dialog/shared.tsx b/libs/positions/src/lib/close-position-dialog/shared.tsx index e8d6df8aa..57e79f7a1 100644 --- a/libs/positions/src/lib/close-position-dialog/shared.tsx +++ b/libs/positions/src/lib/close-position-dialog/shared.tsx @@ -1,5 +1,5 @@ import type { MarketData, Market } from '@vegaprotocol/market-list'; -import type { Order } from '@vegaprotocol/orders'; +import type { OrderFieldsFragment } from '@vegaprotocol/orders'; import { timeInForceLabel } from '@vegaprotocol/orders'; import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { t } from '@vegaprotocol/i18n'; @@ -50,7 +50,7 @@ export const ActiveOrders = ({ orders, }: { market: Market; - orders: Order[]; + orders: OrderFieldsFragment[]; }) => { const asset = market.tradableInstrument.instrument.product.settlementAsset; diff --git a/libs/positions/src/lib/positions-data-providers.ts b/libs/positions/src/lib/positions-data-providers.ts index 660a749ce..b444d0e6f 100644 --- a/libs/positions/src/lib/positions-data-providers.ts +++ b/libs/positions/src/lib/positions-data-providers.ts @@ -349,16 +349,15 @@ export const volumeAndMarginProvider = makeDerivedDataProvider< PositionsQueryVariables & MarketDataQueryVariables >( [ - (callback, client, variables) => + (callback, client, { partyId, marketId }) => ordersProvider(callback, client, { - ...variables, + partyId, + marketIds: [marketId], filter: { - order: { - status: [ - OrderStatus.STATUS_ACTIVE, - OrderStatus.STATUS_PARTIALLY_FILLED, - ], - }, + status: [ + OrderStatus.STATUS_ACTIVE, + OrderStatus.STATUS_PARTIALLY_FILLED, + ], }, }), (callback, client, variables) => diff --git a/libs/positions/src/lib/use-request-close-position-data.ts b/libs/positions/src/lib/use-request-close-position-data.ts index 8d4414334..c8f1f0b79 100644 --- a/libs/positions/src/lib/use-request-close-position-data.ts +++ b/libs/positions/src/lib/use-request-close-position-data.ts @@ -1,5 +1,5 @@ import { marketDataProvider, marketProvider } from '@vegaprotocol/market-list'; -import { isOrderActive, ordersWithMarketProvider } from '@vegaprotocol/orders'; +import { isOrderActive, ordersProvider } from '@vegaprotocol/orders'; import type { OrdersQueryVariables } from '@vegaprotocol/orders'; import { useDataProvider } from '@vegaprotocol/react-helpers'; import { useMemo } from 'react'; @@ -13,8 +13,11 @@ export const useRequestClosePositionData = ( [marketId] ); const orderVariables = useMemo( - () => ({ partyId: partyId || '' }), - [partyId] + () => ({ + partyId: partyId || '', + marketIds: marketId ? [marketId] : undefined, + }), + [partyId, marketId] ); const { data: market, loading: marketLoading } = useDataProvider({ dataProvider: marketProvider, @@ -26,7 +29,7 @@ export const useRequestClosePositionData = ( variables: marketVariables, }); const { data: orderData, loading: orderDataLoading } = useDataProvider({ - dataProvider: ordersWithMarketProvider, + dataProvider: ordersProvider, variables: orderVariables, skip: !partyId, }); @@ -35,23 +38,7 @@ export const useRequestClosePositionData = ( if (!orderData || !market) return []; return ( orderData - .filter((o) => { - // Filter out orders not on market for position - if ( - !o || - !o.node || - !o.node.market || - o.node.market.id !== market.id - ) { - return false; - } - - if (!isOrderActive(o.node.status)) { - return false; - } - - return true; - }) + .filter((o) => o?.node && isOrderActive(o.node.status)) // @ts-ignore o is never null as its been filtered out above .map((o) => o.node) );