From 72d93bb56852085812743719b9c969c17e3ebd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Tue, 9 May 2023 10:40:06 +0200 Subject: [PATCH] feat(trading): update fills queries, fix empty fills list issue (#3636) --- libs/fills/src/lib/Fills.graphql | 27 +++++++------- libs/fills/src/lib/__generated__/Fills.ts | 43 ++++++++++------------- libs/fills/src/lib/fills-data-provider.ts | 13 ++++--- libs/fills/src/lib/fills.mock.ts | 32 ++++++++--------- libs/fills/src/lib/use-fills-list.ts | 10 +++++- 5 files changed, 62 insertions(+), 63 deletions(-) diff --git a/libs/fills/src/lib/Fills.graphql b/libs/fills/src/lib/Fills.graphql index a7062f563..9503e7a20 100644 --- a/libs/fills/src/lib/Fills.graphql +++ b/libs/fills/src/lib/Fills.graphql @@ -34,25 +34,22 @@ fragment FillEdge on TradeEdge { cursor } -query Fills($partyId: ID!, $marketId: ID, $pagination: Pagination) { - party(id: $partyId) { - id - tradesConnection(marketId: $marketId, pagination: $pagination) { - edges { - ...FillEdge - } - pageInfo { - startCursor - endCursor - hasNextPage - hasPreviousPage - } +query Fills($filter: TradesFilter, $pagination: Pagination) { + trades(filter: $filter, pagination: $pagination) { + edges { + ...FillEdge + } + pageInfo { + startCursor + endCursor + hasNextPage + hasPreviousPage } } } -subscription FillsEvent($partyId: ID!) { - trades(partyId: $partyId) { +subscription FillsEvent($filter: TradesSubscriptionFilter!) { + tradesStream(filter: $filter) { id marketId buyOrder diff --git a/libs/fills/src/lib/__generated__/Fills.ts b/libs/fills/src/lib/__generated__/Fills.ts index 9e8cc922a..07d11fd83 100644 --- a/libs/fills/src/lib/__generated__/Fills.ts +++ b/libs/fills/src/lib/__generated__/Fills.ts @@ -8,20 +8,19 @@ export type FillFieldsFragment = { __typename?: 'Trade', id: string, createdAt: export type FillEdgeFragment = { __typename?: 'TradeEdge', cursor: string, node: { __typename?: 'Trade', id: string, createdAt: any, price: string, size: string, buyOrder: string, sellOrder: string, aggressor: Types.Side, market: { __typename?: 'Market', id: string }, buyer: { __typename?: 'Party', id: string }, seller: { __typename?: 'Party', id: string }, buyerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string }, sellerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string } } }; export type FillsQueryVariables = Types.Exact<{ - partyId: Types.Scalars['ID']; - marketId?: Types.InputMaybe; + filter?: Types.InputMaybe; pagination?: Types.InputMaybe; }>; -export type FillsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, tradesConnection?: { __typename?: 'TradeConnection', edges: Array<{ __typename?: 'TradeEdge', cursor: string, node: { __typename?: 'Trade', id: string, createdAt: any, price: string, size: string, buyOrder: string, sellOrder: string, aggressor: Types.Side, market: { __typename?: 'Market', id: string }, buyer: { __typename?: 'Party', id: string }, seller: { __typename?: 'Party', id: string }, buyerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string }, sellerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string } } }>, pageInfo: { __typename?: 'PageInfo', startCursor: string, endCursor: string, hasNextPage: boolean, hasPreviousPage: boolean } } | null } | null }; +export type FillsQuery = { __typename?: 'Query', trades?: { __typename?: 'TradeConnection', edges: Array<{ __typename?: 'TradeEdge', cursor: string, node: { __typename?: 'Trade', id: string, createdAt: any, price: string, size: string, buyOrder: string, sellOrder: string, aggressor: Types.Side, market: { __typename?: 'Market', id: string }, buyer: { __typename?: 'Party', id: string }, seller: { __typename?: 'Party', id: string }, buyerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string }, sellerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string } } }>, pageInfo: { __typename?: 'PageInfo', startCursor: string, endCursor: string, hasNextPage: boolean, hasPreviousPage: boolean } } | null }; export type FillsEventSubscriptionVariables = Types.Exact<{ - partyId: Types.Scalars['ID']; + filter: Types.TradesSubscriptionFilter; }>; -export type FillsEventSubscription = { __typename?: 'Subscription', trades?: Array<{ __typename?: 'TradeUpdate', id: string, marketId: string, buyOrder: string, sellOrder: string, buyerId: string, sellerId: string, aggressor: Types.Side, price: string, size: string, createdAt: any, type: Types.TradeType, buyerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string }, sellerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string } }> | null }; +export type FillsEventSubscription = { __typename?: 'Subscription', tradesStream?: Array<{ __typename?: 'TradeUpdate', id: string, marketId: string, buyOrder: string, sellOrder: string, buyerId: string, sellerId: string, aggressor: Types.Side, price: string, size: string, createdAt: any, type: Types.TradeType, buyerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string }, sellerFee: { __typename?: 'TradeFee', makerFee: string, infrastructureFee: string, liquidityFee: string } }> | null }; export const FillFieldsFragmentDoc = gql` fragment FillFields on Trade { @@ -62,19 +61,16 @@ export const FillEdgeFragmentDoc = gql` } ${FillFieldsFragmentDoc}`; export const FillsDocument = gql` - query Fills($partyId: ID!, $marketId: ID, $pagination: Pagination) { - party(id: $partyId) { - id - tradesConnection(marketId: $marketId, pagination: $pagination) { - edges { - ...FillEdge - } - pageInfo { - startCursor - endCursor - hasNextPage - hasPreviousPage - } + query Fills($filter: TradesFilter, $pagination: Pagination) { + trades(filter: $filter, pagination: $pagination) { + edges { + ...FillEdge + } + pageInfo { + startCursor + endCursor + hasNextPage + hasPreviousPage } } } @@ -92,13 +88,12 @@ export const FillsDocument = gql` * @example * const { data, loading, error } = useFillsQuery({ * variables: { - * partyId: // value for 'partyId' - * marketId: // value for 'marketId' + * filter: // value for 'filter' * pagination: // value for 'pagination' * }, * }); */ -export function useFillsQuery(baseOptions: Apollo.QueryHookOptions) { +export function useFillsQuery(baseOptions?: Apollo.QueryHookOptions) { const options = {...defaultOptions, ...baseOptions} return Apollo.useQuery(FillsDocument, options); } @@ -110,8 +105,8 @@ export type FillsQueryHookResult = ReturnType; export type FillsLazyQueryHookResult = ReturnType; export type FillsQueryResult = Apollo.QueryResult; export const FillsEventDocument = gql` - subscription FillsEvent($partyId: ID!) { - trades(partyId: $partyId) { + subscription FillsEvent($filter: TradesSubscriptionFilter!) { + tradesStream(filter: $filter) { id marketId buyOrder @@ -149,7 +144,7 @@ export const FillsEventDocument = gql` * @example * const { data, loading, error } = useFillsEventSubscription({ * variables: { - * partyId: // value for 'partyId' + * filter: // value for 'filter' * }, * }); */ diff --git a/libs/fills/src/lib/fills-data-provider.ts b/libs/fills/src/lib/fills-data-provider.ts index 9f069edf3..491e2520d 100644 --- a/libs/fills/src/lib/fills-data-provider.ts +++ b/libs/fills/src/lib/fills-data-provider.ts @@ -22,7 +22,7 @@ import type { const update = ( data: FillEdgeFragment[] | null, - delta: FillsEventSubscription['trades'] + delta: FillsEventSubscription['tradesStream'] ) => { return produce(data, (draft) => { orderBy(delta, 'createdAt').forEach((node) => { @@ -36,7 +36,10 @@ const update = ( } } else { const firstNode = draft[0]?.node; - if (firstNode && node.createdAt >= firstNode.createdAt) { + if ( + (firstNode && node.createdAt >= firstNode.createdAt) || + !firstNode + ) { const { buyerId, sellerId, marketId, ...trade } = node; draft.unshift({ node: { @@ -65,13 +68,13 @@ export type Trade = Omit & { export type TradeEdge = Edge; const getData = (responseData: FillsQuery | null): FillEdgeFragment[] => - responseData?.party?.tradesConnection?.edges || []; + responseData?.trades?.edges || []; const getPageInfo = (responseData: FillsQuery | null): PageInfo | null => - responseData?.party?.tradesConnection?.pageInfo || null; + responseData?.trades?.pageInfo || null; const getDelta = (subscriptionData: FillsEventSubscription) => - subscriptionData.trades || []; + subscriptionData.tradesStream || []; export const fillsProvider = makeDataProvider< Parameters['0'], diff --git a/libs/fills/src/lib/fills.mock.ts b/libs/fills/src/lib/fills.mock.ts index 68f665955..05f604bbd 100644 --- a/libs/fills/src/lib/fills.mock.ts +++ b/libs/fills/src/lib/fills.mock.ts @@ -12,24 +12,20 @@ export const fillsQuery = ( vegaPublicKey?: string ): FillsQuery => { const defaultResult: FillsQuery = { - party: { - id: vegaPublicKey || 'vega-0', - tradesConnection: { - __typename: 'TradeConnection', - edges: fills(vegaPublicKey).map((node) => ({ - __typename: 'TradeEdge', - cursor: '3', - node, - })), - pageInfo: { - __typename: 'PageInfo', - startCursor: '1', - endCursor: '2', - hasNextPage: false, - hasPreviousPage: false, - }, + trades: { + __typename: 'TradeConnection', + edges: fills(vegaPublicKey).map((node) => ({ + __typename: 'TradeEdge', + cursor: '3', + node, + })), + pageInfo: { + __typename: 'PageInfo', + startCursor: '1', + endCursor: '2', + hasNextPage: false, + hasPreviousPage: false, }, - __typename: 'Party', }, }; @@ -169,7 +165,7 @@ export const fillsEventSubscription = ( ): FillsEventSubscription => { const defaultResult: FillsEventSubscription = { __typename: 'Subscription', - trades: [ + tradesStream: [ { __typename: 'TradeUpdate', id: '0', diff --git a/libs/fills/src/lib/use-fills-list.ts b/libs/fills/src/lib/use-fills-list.ts index 0e1e60517..2a10ab2dd 100644 --- a/libs/fills/src/lib/use-fills-list.ts +++ b/libs/fills/src/lib/use-fills-list.ts @@ -2,6 +2,7 @@ import type { RefObject } from 'react'; import type { AgGridReact } from 'ag-grid-react'; import { useCallback, useRef } from 'react'; import { makeInfiniteScrollGetRows } from '@vegaprotocol/data-provider'; +import type * as Types from '@vegaprotocol/types'; import { updateGridData } from '@vegaprotocol/datagrid'; import { useDataProvider } from '@vegaprotocol/data-provider'; import type { Trade, TradeEdge } from './fills-data-provider'; @@ -89,11 +90,18 @@ export const useFillsList = ({ [gridRef] ); + const filter: Types.TradesFilter & Types.TradesSubscriptionFilter = { + partyIds: [partyId], + }; + if (marketId) { + filter.marketIds = [marketId]; + } + const { data, error, loading, load, totalCount, reload } = useDataProvider({ dataProvider: fillsWithMarketProvider, update, insert, - variables: { partyId, marketId: marketId || '' }, + variables: { filter }, }); totalCountRef.current = totalCount;