From dbba7346722f626e232499949edfe803d256adf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Thu, 22 Dec 2022 11:52:09 +0100 Subject: [PATCH] feat: add filters to Orders query (#2446) --- .../order-data-provider/Orders.graphql | 13 ++- .../__generated__/Orders.ts | 10 ++- .../order-list-manager/use-order-list-data.ts | 83 ++++++++++++++++--- libs/types/src/__generated__/types.ts | 82 +----------------- libs/types/src/global-types-mappings.ts | 4 +- 5 files changed, 95 insertions(+), 97 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 d03be5e8f..283681f3f 100644 --- a/libs/orders/src/lib/components/order-data-provider/Orders.graphql +++ b/libs/orders/src/lib/components/order-data-provider/Orders.graphql @@ -22,10 +22,19 @@ fragment OrderFields on Order { } } -query Orders($partyId: ID!, $pagination: Pagination, $dateRange: DateRange) { +query Orders( + $partyId: ID! + $pagination: Pagination + $dateRange: DateRange + $filter: OrderFilter +) { party(id: $partyId) { id - ordersConnection(pagination: $pagination, dateRange: $dateRange) { + ordersConnection( + pagination: $pagination + dateRange: $dateRange + filter: $filter + ) { edges { node { ...OrderFields 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 3201dbc1e..9c0ea9298 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 @@ -9,6 +9,7 @@ export type OrdersQueryVariables = Types.Exact<{ partyId: Types.Scalars['ID']; pagination?: Types.InputMaybe; dateRange?: Types.InputMaybe; + filter?: Types.InputMaybe; }>; @@ -71,10 +72,14 @@ export const OrderUpdateFieldsFragmentDoc = gql` } `; export const OrdersDocument = gql` - query Orders($partyId: ID!, $pagination: Pagination, $dateRange: DateRange) { + query Orders($partyId: ID!, $pagination: Pagination, $dateRange: DateRange, $filter: OrderFilter) { party(id: $partyId) { id - ordersConnection(pagination: $pagination, dateRange: $dateRange) { + ordersConnection( + pagination: $pagination + dateRange: $dateRange + filter: $filter + ) { edges { node { ...OrderFields @@ -107,6 +112,7 @@ export const OrdersDocument = gql` * partyId: // value for 'partyId' * pagination: // value for 'pagination' * dateRange: // value for 'dateRange' + * filter: // value for 'filter' * }, * }); */ 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 ff5a2e567..49f832566 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 @@ -42,6 +42,64 @@ interface Props { scrolledToTop: RefObject; } +const filterOrders = ( + orders: (OrderEdge | null)[] | null, + variables: OrdersQueryVariables & OrdersUpdateSubscriptionVariables +) => { + if (!orders) { + return orders; + } + return orders.filter((order) => { + if (variables.marketId && order?.node.market?.id !== variables.marketId) { + return false; + } + if ( + variables?.filter?.status && + !( + order?.node?.status && + variables.filter.status.includes(order.node.status) + ) + ) { + return false; + } + if ( + variables?.filter?.types && + !(order?.node?.type && variables.filter.types.includes(order.node.type)) + ) { + return false; + } + if ( + variables?.filter?.timeInForce && + !( + order?.node?.timeInForce && + variables.filter.timeInForce.includes(order.node.timeInForce) + ) + ) { + return false; + } + if ( + variables?.dateRange?.start && + !( + (order?.node?.updatedAt || order?.node?.createdAt) && + variables.dateRange.start < + (order.node.updatedAt || order.node.createdAt) + ) + ) { + return false; + } + if ( + variables?.dateRange?.end && + !( + (order?.node?.updatedAt || order?.node?.createdAt) && + variables.dateRange.end > (order.node.updatedAt || order.node.createdAt) + ) + ) { + return false; + } + return true; + }); +}; + export const useOrderListData = ({ partyId, marketId, @@ -57,7 +115,16 @@ export const useOrderListData = ({ const variables = useMemo< OrdersQueryVariables & OrdersUpdateSubscriptionVariables >( - () => ({ partyId, dateRange: filter?.updatedAt?.value, marketId }), + () => ({ + partyId, + dateRange: filter?.updatedAt?.value, + marketId, + filter: { + status: filter?.status?.value, + timeInForce: filter?.timeInForce?.value, + types: filter?.type?.value, + }, + }), [partyId, marketId, filter] ); @@ -88,18 +155,15 @@ export const useOrderListData = ({ ).length; } } + dataRef.current = filterOrders(data, variables); const avoidRerender = !!( (dataRef.current?.length && data?.length) || (!dataRef.current?.length && !data?.length) ); - dataRef.current = - !!marketId && !!data - ? data.filter((d) => d?.node.market?.id === marketId) - : data; gridRef.current?.api?.refreshInfiniteCache(); return avoidRerender; }, - [gridRef, scrolledToTop, marketId] + [gridRef, scrolledToTop, variables] ); const insert = useCallback( @@ -110,14 +174,11 @@ export const useOrderListData = ({ data: (OrderEdge | null)[] | null; totalCount?: number; }) => { - dataRef.current = - !!marketId && !!data - ? data.filter((d) => d?.node.market?.id === marketId) - : data; + dataRef.current = filterOrders(data, variables); totalCountRef.current = totalCount; return true; }, - [marketId] + [variables] ); const { data, error, loading, load, totalCount } = useDataProvider({ diff --git a/libs/types/src/__generated__/types.ts b/libs/types/src/__generated__/types.ts index 66168bd77..70da9c901 100644 --- a/libs/types/src/__generated__/types.ts +++ b/libs/types/src/__generated__/types.ts @@ -473,35 +473,6 @@ export type ContinuousTrading = { tickSize: Scalars['String']; }; -/** Connection type for retrieving cursor-based paginated core snapshot data */ -export type CoreSnapshotConnection = { - __typename?: 'CoreSnapshotConnection'; - /** The positions in this connection */ - edges?: Maybe>; - /** The pagination information */ - pageInfo?: Maybe; -}; - -/** A snapshot taken by the core */ -export type CoreSnapshotData = { - __typename?: 'CoreSnapshotData'; - /** The block hash at the snapshot block height */ - blockHash: Scalars['String']; - /** At which block the snapshot was taken */ - blockHeight: Scalars['String']; - /** The current version of vega core */ - vegaCoreVersion: Scalars['String']; -}; - -/** Edge type containing the core snapshot cursor information */ -export type CoreSnapshotEdge = { - __typename?: 'CoreSnapshotEdge'; - /** Cursor identifying the core snapashot data */ - cursor: Scalars['String']; - /** The core snapshot data */ - node: CoreSnapshotData; -}; - /** A data source contains the data sent by a data source */ export type Data = { __typename?: 'Data'; @@ -890,33 +861,6 @@ export type EpochParticipation = { totalRewards?: Maybe; }; -/** an aggregated reward summary for a combination of epoch/asset/market/reward type */ -export type EpochRewardSummary = { - __typename?: 'EpochRewardSummary'; - /** Total quantity of rewards awarded in this asset/market/reward type in this epoch */ - amount: Scalars['String']; - /** ID of the Asset */ - assetId: Scalars['ID']; - /** The epoch for which summary is generated */ - epoch: Scalars['Int']; - /** ID of the market */ - marketId?: Maybe; - /** Type of the reward */ - rewardType: AccountType; -}; - -export type EpochRewardSummaryConnection = { - __typename?: 'EpochRewardSummaryConnection'; - edges?: Maybe>>; - pageInfo?: Maybe; -}; - -export type EpochRewardSummaryEdge = { - __typename?: 'EpochRewardSummaryEdge'; - cursor: Scalars['String']; - node: EpochRewardSummary; -}; - /** Describes in both human readable and block time when an epoch spans. */ export type EpochTimestamps = { __typename?: 'EpochTimestamps'; @@ -1488,8 +1432,6 @@ export type Market = { liquidityMonitoringParameters: LiquidityMonitoringParameters; /** The list of the liquidity provision commitments for this market */ liquidityProvisionsConnection?: Maybe; - /** Liquidity Provision order price range */ - lpPriceRange: Scalars['String']; /** Timestamps for state changes in the market */ marketTimestamps: MarketTimestamps; /** @@ -1869,8 +1811,6 @@ export type NewMarket = { decimalPlaces: Scalars['Int']; /** New market instrument configuration */ instrument: InstrumentConfiguration; - /** Liquidity Provision order price range */ - lpPriceRange: Scalars['String']; /** Metadata for this instrument, tags */ metadata?: Maybe>; /** New market risk configuration */ @@ -3196,16 +3136,12 @@ export type Query = { assetsConnection?: Maybe; /** Get historical balances for an account within the given date range */ balanceChanges: AggregatedBalanceConnection; - /** List core snapshots */ - coreSnapshots?: Maybe; /** Find a deposit using its ID */ deposit?: Maybe; /** Fetch all deposits */ deposits?: Maybe; /** Get data for a specific epoch, if ID omitted it gets the current epoch. If the string is 'next', fetch the next epoch */ epoch: Epoch; - /** List reward summary per epoch by asset, market, reward type */ - epochRewardSummaries?: Maybe; /** Get the signatures bundle to allowlist an ERC20 token in the collateral bridge */ erc20ListAssetBundle?: Maybe; /** Get the signature bundle to add a particular validator to the signer list of the multisig contract */ @@ -3311,12 +3247,6 @@ export type QuerybalanceChangesArgs = { }; -/** Queries allow a caller to read data and filter data via GraphQL. */ -export type QuerycoreSnapshotsArgs = { - pagination?: InputMaybe; -}; - - /** Queries allow a caller to read data and filter data via GraphQL. */ export type QuerydepositArgs = { id: Scalars['ID']; @@ -3336,14 +3266,6 @@ export type QueryepochArgs = { }; -/** Queries allow a caller to read data and filter data via GraphQL. */ -export type QueryepochRewardSummariesArgs = { - fromEpoch?: InputMaybe; - pagination?: InputMaybe; - toEpoch?: InputMaybe; -}; - - /** Queries allow a caller to read data and filter data via GraphQL. */ export type Queryerc20ListAssetBundleArgs = { assetId: Scalars['ID']; @@ -4343,8 +4265,8 @@ export enum TransferType { TRANSFER_TYPE_MTM_LOSS = 'TRANSFER_TYPE_MTM_LOSS', /** Funds added to margin account after mark to market gain */ TRANSFER_TYPE_MTM_WIN = 'TRANSFER_TYPE_MTM_WIN', - /** Reward payout received */ - TRANSFER_TYPE_REWARD_PAYOUT = 'TRANSFER_TYPE_REWARD_PAYOUT', + /** Staking reward received */ + TRANSFER_TYPE_STAKE_REWARD = 'TRANSFER_TYPE_STAKE_REWARD', /** A network internal instruction for the collateral engine to move funds from the pending transfers pool account into the destination account */ TRANSFER_TYPE_TRANSFER_FUNDS_DISTRIBUTE = 'TRANSFER_TYPE_TRANSFER_FUNDS_DISTRIBUTE', /** A network internal instruction for the collateral engine to move funds from a user's general account into the pending transfers pool */ diff --git a/libs/types/src/global-types-mappings.ts b/libs/types/src/global-types-mappings.ts index 2d4f30020..1b1eea9ac 100644 --- a/libs/types/src/global-types-mappings.ts +++ b/libs/types/src/global-types-mappings.ts @@ -401,7 +401,7 @@ export const TransferTypeMapping: TransferTypeMap = { TRANSFER_TYPE_WITHDRAW: 'Withdrawal', TRANSFER_TYPE_DEPOSIT: 'Deposit', TRANSFER_TYPE_BOND_SLASHING: 'Bond slashed', - TRANSFER_TYPE_REWARD_PAYOUT: 'Rewards funded', + TRANSFER_TYPE_STAKE_REWARD: 'Rewards funded', TRANSFER_TYPE_TRANSFER_FUNDS_SEND: 'Transfer sent', TRANSFER_TYPE_TRANSFER_FUNDS_DISTRIBUTE: 'Transfer received', TRANSFER_TYPE_CLEAR_ACCOUNT: 'Market accounts cleared', @@ -427,7 +427,7 @@ export const DescriptionTransferTypeMapping: TransferTypeMap = { TRANSFER_TYPE_WITHDRAW: `Funds withdrawn from your general account`, TRANSFER_TYPE_DEPOSIT: `Funds deposited to your general account`, TRANSFER_TYPE_BOND_SLASHING: `Bond account penalized when liquidity commitment not met`, - TRANSFER_TYPE_REWARD_PAYOUT: `Collateral deducted from your general account to fund rewards you've set up`, + TRANSFER_TYPE_STAKE_REWARD: `Collateral deducted from your general account to fund rewards you've set up`, TRANSFER_TYPE_TRANSFER_FUNDS_SEND: `Funds deducted from your general account to fulfil a transfer`, TRANSFER_TYPE_TRANSFER_FUNDS_DISTRIBUTE: `Funds added to your general account to fulfil a transfer`, TRANSFER_TYPE_CLEAR_ACCOUNT: `Market-related accounts emptied, and balances moved, because the market has closed`,