diff --git a/apps/console-lite-e2e/src/support/mocks/generate-positions.ts b/apps/console-lite-e2e/src/support/mocks/generate-positions.ts index 912bd5a79..4e327b901 100644 --- a/apps/console-lite-e2e/src/support/mocks/generate-positions.ts +++ b/apps/console-lite-e2e/src/support/mocks/generate-positions.ts @@ -1,14 +1,15 @@ import merge from 'lodash/merge'; import type { PartialDeep } from 'type-fest'; import type { - Positions, - Positions_party_positionsConnection_edges_node, + PositionsQuery, + PositionFieldsFragment, + MarginsQuery, } from '@vegaprotocol/positions'; export const generatePositions = ( - override?: PartialDeep -): Positions => { - const nodes: Positions_party_positionsConnection_edges_node[] = [ + override?: PartialDeep +): PositionsQuery => { + const nodes: PositionFieldsFragment[] = [ { __typename: 'Position', realisedPNL: '0', @@ -47,7 +48,7 @@ export const generatePositions = ( }, ]; - const defaultResult: Positions = { + const defaultResult: PositionsQuery = { party: { __typename: 'Party', id: Cypress.env('VEGA_PUBLIC_KEY'), @@ -66,7 +67,7 @@ export const generatePositions = ( return merge(defaultResult, override); }; -export const emptyPositions = () => { +export const emptyPositions = (): PositionsQuery => { return { party: { id: Cypress.env('VEGA_PUBLIC_KEY'), @@ -76,7 +77,7 @@ export const emptyPositions = () => { }; }; -export const generateMargins = () => { +export const generateMargins = (): MarginsQuery => { return { party: { id: Cypress.env('VEGA_PUBLIC_KEY'), @@ -119,7 +120,7 @@ export const generateMargins = () => { }, { node: { - m_typename: 'MarginLevels', + __typename: 'MarginLevels', maintenanceLevel: '0', searchLevel: '0', initialLevel: '0', diff --git a/apps/trading-e2e/src/support/mocks/generate-positions.ts b/apps/trading-e2e/src/support/mocks/generate-positions.ts index 666c667b9..b910bd804 100644 --- a/apps/trading-e2e/src/support/mocks/generate-positions.ts +++ b/apps/trading-e2e/src/support/mocks/generate-positions.ts @@ -1,15 +1,15 @@ import merge from 'lodash/merge'; import type { PartialDeep } from 'type-fest'; import type { - Positions, - Positions_party_positionsConnection_edges_node, - Margins, + PositionsQuery, + PositionFieldsFragment, + MarginsQuery, } from '@vegaprotocol/positions'; export const generatePositions = ( - override?: PartialDeep -): Positions => { - const nodes: Positions_party_positionsConnection_edges_node[] = [ + override?: PartialDeep +): PositionsQuery => { + const nodes: PositionFieldsFragment[] = [ { __typename: 'Position', realisedPNL: '0', @@ -48,7 +48,7 @@ export const generatePositions = ( }, ]; - const defaultResult: Positions = { + const defaultResult: PositionsQuery = { party: { __typename: 'Party', id: Cypress.env('VEGA_PUBLIC_KEY'), @@ -67,7 +67,7 @@ export const generatePositions = ( return merge(defaultResult, override); }; -export const generateMargins = (): Margins => { +export const generateMargins = (): MarginsQuery => { return { party: { id: Cypress.env('VEGA_PUBLIC_KEY'), diff --git a/apps/trading/components/select-market/select-market.tsx b/apps/trading/components/select-market/select-market.tsx index 8b607b8ee..71b0b351b 100644 --- a/apps/trading/components/select-market/select-market.tsx +++ b/apps/trading/components/select-market/select-market.tsx @@ -15,7 +15,7 @@ import type { MarketWithData, } from '@vegaprotocol/market-list'; import { useVegaWallet } from '@vegaprotocol/wallet'; -import type { Positions_party_positionsConnection_edges_node } from '@vegaprotocol/positions'; +import type { PositionFieldsFragment } from '@vegaprotocol/positions'; import { positionsDataProvider } from '@vegaprotocol/positions'; import { SelectMarketTableHeader, @@ -79,7 +79,7 @@ export const SelectAllMarketsTableBody = ({ tableColumns = (market) => columns(market, onSelect, onCellClick), }: { markets?: Market[] | null; - positions?: Positions_party_positionsConnection_edges_node[]; + positions?: PositionFieldsFragment[]; title?: string; onSelect: (id: string) => void; onCellClick: OnCellClickHandler; diff --git a/libs/positions/src/index.ts b/libs/positions/src/index.ts index 6f30311d7..db59b5136 100644 --- a/libs/positions/src/index.ts +++ b/libs/positions/src/index.ts @@ -1,7 +1,4 @@ -export * from './lib/__generated__/Positions'; -export * from './lib/__generated__/Margins'; -export * from './lib/__generated__/PositionsSubscription'; -export * from './lib/__generated__/MarginsSubscription'; +export * from './lib/__generated___/Positions'; export * from './lib/positions-container'; export * from './lib/positions-data-providers'; export * from './lib/positions-table'; diff --git a/libs/positions/src/lib/Positions.graphql b/libs/positions/src/lib/Positions.graphql index fee859bd0..d13ade8e0 100644 --- a/libs/positions/src/lib/Positions.graphql +++ b/libs/positions/src/lib/Positions.graphql @@ -4,38 +4,8 @@ fragment PositionFields on Position { unrealisedPNL averageEntryPrice updatedAt - marginsConnection { - edges { - node { - market { - id - } - maintenanceLevel - searchLevel - initialLevel - collateralReleaseLevel - asset { - symbol - } - } - } - } market { id - decimalPlaces - positionDecimalPlaces - tradingMode - tradableInstrument { - instrument { - name - } - } - data { - markPrice - market { - id - } - } } } @@ -62,3 +32,42 @@ subscription PositionsSubscription($partyId: ID!) { marketId } } + +fragment MarginFields on MarginLevels { + maintenanceLevel + searchLevel + initialLevel + collateralReleaseLevel + asset { + id + } + market { + id + } +} + +query Margins($partyId: ID!) { + party(id: $partyId) { + id + marginsConnection { + edges { + node { + ...MarginFields + } + } + } + } +} + +subscription MarginsSubscription($partyId: ID!) { + margins(partyId: $partyId) { + marketId + asset + partyId + maintenanceLevel + searchLevel + initialLevel + collateralReleaseLevel + timestamp + } +} diff --git a/libs/positions/src/lib/__generated__/Margins.ts b/libs/positions/src/lib/__generated__/Margins.ts deleted file mode 100644 index a61171625..000000000 --- a/libs/positions/src/lib/__generated__/Margins.ts +++ /dev/null @@ -1,89 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL query operation: Margins -// ==================================================== - -export interface Margins_party_marginsConnection_edges_node_market { - __typename: "Market"; - /** - * Market ID - */ - id: string; -} - -export interface Margins_party_marginsConnection_edges_node_asset { - __typename: "Asset"; - /** - * The ID of the asset - */ - id: string; -} - -export interface Margins_party_marginsConnection_edges_node { - __typename: "MarginLevels"; - /** - * Market in which the margin is required for this party - */ - market: Margins_party_marginsConnection_edges_node_market; - /** - * Minimal margin for the position to be maintained in the network (unsigned integer) - */ - maintenanceLevel: string; - /** - * If the margin is between maintenance and search, the network will initiate a collateral search (unsigned integer) - */ - searchLevel: string; - /** - * This is the minimum margin required for a party to place a new order on the network (unsigned integer) - */ - initialLevel: string; - /** - * If the margin of the party is greater than this level, then collateral will be released from the margin account into - * the general account of the party for the given asset. - */ - collateralReleaseLevel: string; - /** - * Asset for the current margins - */ - asset: Margins_party_marginsConnection_edges_node_asset; -} - -export interface Margins_party_marginsConnection_edges { - __typename: "MarginEdge"; - node: Margins_party_marginsConnection_edges_node; -} - -export interface Margins_party_marginsConnection { - __typename: "MarginConnection"; - /** - * The margin levels in this connection - */ - edges: Margins_party_marginsConnection_edges[] | null; -} - -export interface Margins_party { - __typename: "Party"; - /** - * Party identifier - */ - id: string; - /** - * Margin levels for a market - */ - marginsConnection: Margins_party_marginsConnection | null; -} - -export interface Margins { - /** - * An entity that is trading on the Vega network - */ - party: Margins_party | null; -} - -export interface MarginsVariables { - partyId: string; -} diff --git a/libs/positions/src/lib/__generated__/MarginsSubscription.ts b/libs/positions/src/lib/__generated__/MarginsSubscription.ts deleted file mode 100644 index 257ea5c6d..000000000 --- a/libs/positions/src/lib/__generated__/MarginsSubscription.ts +++ /dev/null @@ -1,56 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL subscription operation: MarginsSubscription -// ==================================================== - -export interface MarginsSubscription_margins { - __typename: "MarginLevelsUpdate"; - /** - * Market in which the margin is required for this party - */ - marketId: string; - /** - * Asset for the current margins - */ - asset: string; - /** - * The party for this margin - */ - partyId: string; - /** - * Minimal margin for the position to be maintained in the network (unsigned integer) - */ - maintenanceLevel: string; - /** - * If the margin is between maintenance and search, the network will initiate a collateral search (unsigned integer) - */ - searchLevel: string; - /** - * This is the minimum margin required for a party to place a new order on the network (unsigned integer) - */ - initialLevel: string; - /** - * If the margin of the party is greater than this level, then collateral will be released from the margin account into - * the general account of the party for the given asset. - */ - collateralReleaseLevel: string; - /** - * RFC3339Nano time from at which this margin level was relevant - */ - timestamp: string; -} - -export interface MarginsSubscription { - /** - * Subscribe to the margin changes - */ - margins: MarginsSubscription_margins; -} - -export interface MarginsSubscriptionVariables { - partyId: string; -} diff --git a/libs/positions/src/lib/__generated__/PositionFields.ts b/libs/positions/src/lib/__generated__/PositionFields.ts deleted file mode 100644 index 04c13baff..000000000 --- a/libs/positions/src/lib/__generated__/PositionFields.ts +++ /dev/null @@ -1,44 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL fragment: PositionFields -// ==================================================== - -export interface PositionFields_market { - __typename: "Market"; - /** - * Market ID - */ - id: string; -} - -export interface PositionFields { - __typename: "Position"; - /** - * Realised Profit and Loss (int64) - */ - realisedPNL: string; - /** - * Open volume (uint64) - */ - openVolume: string; - /** - * Unrealised Profit and Loss (int64) - */ - unrealisedPNL: string; - /** - * Average entry price for this position - */ - averageEntryPrice: string; - /** - * RFC3339Nano time the position was updated - */ - updatedAt: string | null; - /** - * Market relating to this position - */ - market: PositionFields_market; -} diff --git a/libs/positions/src/lib/__generated__/Positions.ts b/libs/positions/src/lib/__generated__/Positions.ts deleted file mode 100644 index f3458262e..000000000 --- a/libs/positions/src/lib/__generated__/Positions.ts +++ /dev/null @@ -1,83 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL query operation: Positions -// ==================================================== - -export interface Positions_party_positionsConnection_edges_node_market { - __typename: "Market"; - /** - * Market ID - */ - id: string; -} - -export interface Positions_party_positionsConnection_edges_node { - __typename: "Position"; - /** - * Realised Profit and Loss (int64) - */ - realisedPNL: string; - /** - * Open volume (uint64) - */ - openVolume: string; - /** - * Unrealised Profit and Loss (int64) - */ - unrealisedPNL: string; - /** - * Average entry price for this position - */ - averageEntryPrice: string; - /** - * RFC3339Nano time the position was updated - */ - updatedAt: string | null; - /** - * Market relating to this position - */ - market: Positions_party_positionsConnection_edges_node_market; -} - -export interface Positions_party_positionsConnection_edges { - __typename: "PositionEdge"; - /** - * The position - */ - node: Positions_party_positionsConnection_edges_node; -} - -export interface Positions_party_positionsConnection { - __typename: "PositionConnection"; - /** - * The positions in this connection - */ - edges: Positions_party_positionsConnection_edges[] | null; -} - -export interface Positions_party { - __typename: "Party"; - /** - * Party identifier - */ - id: string; - /** - * Trading positions relating to a party - */ - positionsConnection: Positions_party_positionsConnection | null; -} - -export interface Positions { - /** - * An entity that is trading on the Vega network - */ - party: Positions_party | null; -} - -export interface PositionsVariables { - partyId: string; -} diff --git a/libs/positions/src/lib/__generated__/PositionsSubscription.ts b/libs/positions/src/lib/__generated__/PositionsSubscription.ts deleted file mode 100644 index 3a8800c19..000000000 --- a/libs/positions/src/lib/__generated__/PositionsSubscription.ts +++ /dev/null @@ -1,47 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL subscription operation: PositionsSubscription -// ==================================================== - -export interface PositionsSubscription_positions { - __typename: "PositionUpdate"; - /** - * Realised Profit and Loss (int64) - */ - realisedPNL: string; - /** - * Open volume (uint64) - */ - openVolume: string; - /** - * Unrealised Profit and Loss (int64) - */ - unrealisedPNL: string; - /** - * Average entry price for this position - */ - averageEntryPrice: string; - /** - * RFC3339Nano time the position was updated - */ - updatedAt: string | null; - /** - * Market relating to this position - */ - marketId: string; -} - -export interface PositionsSubscription { - /** - * Subscribe to the positions updates - */ - positions: PositionsSubscription_positions[]; -} - -export interface PositionsSubscriptionVariables { - partyId: string; -} diff --git a/libs/positions/src/lib/__generated___/Positions.ts b/libs/positions/src/lib/__generated___/Positions.ts index c7143eee6..d4872de7e 100644 --- a/libs/positions/src/lib/__generated___/Positions.ts +++ b/libs/positions/src/lib/__generated___/Positions.ts @@ -3,14 +3,14 @@ import { Schema as Types } from '@vegaprotocol/types'; import { gql } from '@apollo/client'; import * as Apollo from '@apollo/client'; const defaultOptions = {} as const; -export type PositionFieldsFragment = { __typename?: 'Position', realisedPNL: string, openVolume: string, unrealisedPNL: string, averageEntryPrice: string, updatedAt?: string | null, marginsConnection?: { __typename?: 'MarginConnection', edges?: Array<{ __typename?: 'MarginEdge', node: { __typename?: 'MarginLevels', maintenanceLevel: string, searchLevel: string, initialLevel: string, collateralReleaseLevel: string, market: { __typename?: 'Market', id: string }, asset: { __typename?: 'Asset', symbol: string } } }> | null } | null, market: { __typename?: 'Market', id: string, decimalPlaces: number, positionDecimalPlaces: number, tradingMode: Types.MarketTradingMode, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', name: string } }, data?: { __typename?: 'MarketData', markPrice: string, market: { __typename?: 'Market', id: string } } | null } }; +export type PositionFieldsFragment = { __typename?: 'Position', realisedPNL: string, openVolume: string, unrealisedPNL: string, averageEntryPrice: string, updatedAt?: string | null, market: { __typename?: 'Market', id: string } }; export type PositionsQueryVariables = Types.Exact<{ partyId: Types.Scalars['ID']; }>; -export type PositionsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, positionsConnection?: { __typename?: 'PositionConnection', edges?: Array<{ __typename?: 'PositionEdge', node: { __typename?: 'Position', realisedPNL: string, openVolume: string, unrealisedPNL: string, averageEntryPrice: string, updatedAt?: string | null, marginsConnection?: { __typename?: 'MarginConnection', edges?: Array<{ __typename?: 'MarginEdge', node: { __typename?: 'MarginLevels', maintenanceLevel: string, searchLevel: string, initialLevel: string, collateralReleaseLevel: string, market: { __typename?: 'Market', id: string }, asset: { __typename?: 'Asset', symbol: string } } }> | null } | null, market: { __typename?: 'Market', id: string, decimalPlaces: number, positionDecimalPlaces: number, tradingMode: Types.MarketTradingMode, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', name: string } }, data?: { __typename?: 'MarketData', markPrice: string, market: { __typename?: 'Market', id: string } } | null } } }> | null } | null } | null }; +export type PositionsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, positionsConnection?: { __typename?: 'PositionConnection', edges?: Array<{ __typename?: 'PositionEdge', node: { __typename?: 'Position', realisedPNL: string, openVolume: string, unrealisedPNL: string, averageEntryPrice: string, updatedAt?: string | null, market: { __typename?: 'Market', id: string } } }> | null } | null } | null }; export type PositionsSubscriptionSubscriptionVariables = Types.Exact<{ partyId: Types.Scalars['ID']; @@ -19,6 +19,22 @@ export type PositionsSubscriptionSubscriptionVariables = Types.Exact<{ export type PositionsSubscriptionSubscription = { __typename?: 'Subscription', positions: Array<{ __typename?: 'PositionUpdate', realisedPNL: string, openVolume: string, unrealisedPNL: string, averageEntryPrice: string, updatedAt?: string | null, marketId: string }> }; +export type MarginFieldsFragment = { __typename?: 'MarginLevels', maintenanceLevel: string, searchLevel: string, initialLevel: string, collateralReleaseLevel: string, asset: { __typename?: 'Asset', id: string }, market: { __typename?: 'Market', id: string } }; + +export type MarginsQueryVariables = Types.Exact<{ + partyId: Types.Scalars['ID']; +}>; + + +export type MarginsQuery = { __typename?: 'Query', party?: { __typename?: 'Party', id: string, marginsConnection?: { __typename?: 'MarginConnection', edges?: Array<{ __typename?: 'MarginEdge', node: { __typename?: 'MarginLevels', maintenanceLevel: string, searchLevel: string, initialLevel: string, collateralReleaseLevel: string, asset: { __typename?: 'Asset', id: string }, market: { __typename?: 'Market', id: string } } }> | null } | null } | null }; + +export type MarginsSubscriptionSubscriptionVariables = Types.Exact<{ + partyId: Types.Scalars['ID']; +}>; + + +export type MarginsSubscriptionSubscription = { __typename?: 'Subscription', margins: { __typename?: 'MarginLevelsUpdate', marketId: string, asset: string, partyId: string, maintenanceLevel: string, searchLevel: string, initialLevel: string, collateralReleaseLevel: string, timestamp: string } }; + export const PositionFieldsFragmentDoc = gql` fragment PositionFields on Position { realisedPNL @@ -26,38 +42,22 @@ export const PositionFieldsFragmentDoc = gql` unrealisedPNL averageEntryPrice updatedAt - marginsConnection { - edges { - node { - market { - id - } - maintenanceLevel - searchLevel - initialLevel - collateralReleaseLevel - asset { - symbol - } - } - } + market { + id + } +} + `; +export const MarginFieldsFragmentDoc = gql` + fragment MarginFields on MarginLevels { + maintenanceLevel + searchLevel + initialLevel + collateralReleaseLevel + asset { + id } market { id - decimalPlaces - positionDecimalPlaces - tradingMode - tradableInstrument { - instrument { - name - } - } - data { - markPrice - market { - id - } - } } } `; @@ -137,4 +137,83 @@ export function usePositionsSubscriptionSubscription(baseOptions: Apollo.Subscri return Apollo.useSubscription(PositionsSubscriptionDocument, options); } export type PositionsSubscriptionSubscriptionHookResult = ReturnType; -export type PositionsSubscriptionSubscriptionResult = Apollo.SubscriptionResult; \ No newline at end of file +export type PositionsSubscriptionSubscriptionResult = Apollo.SubscriptionResult; +export const MarginsDocument = gql` + query Margins($partyId: ID!) { + party(id: $partyId) { + id + marginsConnection { + edges { + node { + ...MarginFields + } + } + } + } +} + ${MarginFieldsFragmentDoc}`; + +/** + * __useMarginsQuery__ + * + * To run a query within a React component, call `useMarginsQuery` and pass it any options that fit your needs. + * When your component renders, `useMarginsQuery` returns an object from Apollo Client that contains loading, error, and data properties + * you can use to render your UI. + * + * @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options; + * + * @example + * const { data, loading, error } = useMarginsQuery({ + * variables: { + * partyId: // value for 'partyId' + * }, + * }); + */ +export function useMarginsQuery(baseOptions: Apollo.QueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useQuery(MarginsDocument, options); + } +export function useMarginsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useLazyQuery(MarginsDocument, options); + } +export type MarginsQueryHookResult = ReturnType; +export type MarginsLazyQueryHookResult = ReturnType; +export type MarginsQueryResult = Apollo.QueryResult; +export const MarginsSubscriptionDocument = gql` + subscription MarginsSubscription($partyId: ID!) { + margins(partyId: $partyId) { + marketId + asset + partyId + maintenanceLevel + searchLevel + initialLevel + collateralReleaseLevel + timestamp + } +} + `; + +/** + * __useMarginsSubscriptionSubscription__ + * + * To run a query within a React component, call `useMarginsSubscriptionSubscription` and pass it any options that fit your needs. + * When your component renders, `useMarginsSubscriptionSubscription` returns an object from Apollo Client that contains loading, error, and data properties + * you can use to render your UI. + * + * @param baseOptions options that will be passed into the subscription, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options; + * + * @example + * const { data, loading, error } = useMarginsSubscriptionSubscription({ + * variables: { + * partyId: // value for 'partyId' + * }, + * }); + */ +export function useMarginsSubscriptionSubscription(baseOptions: Apollo.SubscriptionHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useSubscription(MarginsSubscriptionDocument, options); + } +export type MarginsSubscriptionSubscriptionHookResult = ReturnType; +export type MarginsSubscriptionSubscriptionResult = Apollo.SubscriptionResult; \ No newline at end of file diff --git a/libs/positions/src/lib/margin-data-provider.ts b/libs/positions/src/lib/margin-data-provider.ts index 76e21a83a..ae158485d 100644 --- a/libs/positions/src/lib/margin-data-provider.ts +++ b/libs/positions/src/lib/margin-data-provider.ts @@ -1,55 +1,21 @@ -import { gql } from '@apollo/client'; import produce from 'immer'; import { makeDataProvider } from '@vegaprotocol/react-helpers'; +import { + MarginsSubscriptionDocument, + MarginsDocument, +} from './__generated___/Positions'; import type { - MarginsSubscription, - MarginsSubscription_margins, -} from './__generated__/MarginsSubscription'; -import type { Margins, Margins_party } from './__generated__/Margins'; + MarginsQuery, + MarginsSubscriptionSubscription, +} from './__generated___/Positions'; -export const MARGINS_QUERY = gql` - query Margins($partyId: ID!) { - party(id: $partyId) { - id - marginsConnection { - edges { - node { - market { - id - } - maintenanceLevel - searchLevel - initialLevel - collateralReleaseLevel - asset { - id - } - } - } - } - } - } -`; - -export const MARGINS_SUBSCRIPTION = gql` - subscription MarginsSubscription($partyId: ID!) { - margins(partyId: $partyId) { - marketId - asset - partyId - maintenanceLevel - searchLevel - initialLevel - collateralReleaseLevel - timestamp - } - } -`; - -const update = (data: Margins_party, delta: MarginsSubscription_margins) => { +const update = ( + data: MarginsQuery['party'], + delta: MarginsSubscriptionSubscription['margins'] +) => { return produce(data, (draft) => { const { marketId } = delta; - if (marketId && draft.marginsConnection?.edges) { + if (marketId && draft?.marginsConnection?.edges) { const index = draft.marginsConnection.edges.findIndex( (edge) => edge.node.market.id === marketId ); @@ -86,18 +52,18 @@ const update = (data: Margins_party, delta: MarginsSubscription_margins) => { }); }; -const getData = (responseData: Margins) => responseData.party; -const getDelta = (subscriptionData: MarginsSubscription) => +const getData = (responseData: MarginsQuery) => responseData.party; +const getDelta = (subscriptionData: MarginsSubscriptionSubscription) => subscriptionData.margins; export const marginsDataProvider = makeDataProvider< - Margins, - Margins_party, - MarginsSubscription, - MarginsSubscription_margins + MarginsQuery, + MarginsQuery['party'], + MarginsSubscriptionSubscription, + MarginsSubscriptionSubscription['margins'] >({ - query: MARGINS_QUERY, - subscriptionQuery: MARGINS_SUBSCRIPTION, + query: MarginsDocument, + subscriptionQuery: MarginsSubscriptionDocument, update, getData, getDelta, diff --git a/libs/positions/src/lib/positions-data-providers.spec.ts b/libs/positions/src/lib/positions-data-providers.spec.ts index e9cd281bc..8981ac92c 100644 --- a/libs/positions/src/lib/positions-data-providers.spec.ts +++ b/libs/positions/src/lib/positions-data-providers.spec.ts @@ -1,9 +1,8 @@ import { AccountType, MarketTradingMode } from '@vegaprotocol/types'; import type { Account } from '@vegaprotocol/accounts'; -import type { Positions } from './__generated__/Positions'; -import { getMetrics, rejoinPositionData } from './positions-data-providers'; import type { MarketWithData } from '@vegaprotocol/market-list'; -import type { Margins } from './__generated__/Margins'; +import type { PositionsQuery, MarginsQuery } from './__generated___/Positions'; +import { getMetrics, rejoinPositionData } from './positions-data-providers'; const accounts = [ { @@ -64,7 +63,7 @@ const accounts = [ }, ] as Account[]; -const positions: Positions = { +const positions: PositionsQuery = { party: { __typename: 'Party', id: '02eceaba4df2bef76ea10caf728d8a099a2aa846cced25737cccaa9812342f65', @@ -163,7 +162,7 @@ const marketsData = [ }, ] as MarketWithData[]; -const margins: Margins = { +const margins: MarginsQuery = { party: { id: '02eceaba4df2bef76ea10caf728d8a099a2aa846cced25737cccaa9812342f65', marginsConnection: { diff --git a/libs/positions/src/lib/positions-data-providers.ts b/libs/positions/src/lib/positions-data-providers.ts index dc3fd68b7..263032eab 100644 --- a/libs/positions/src/lib/positions-data-providers.ts +++ b/libs/positions/src/lib/positions-data-providers.ts @@ -1,37 +1,31 @@ -import { gql } from '@apollo/client'; import produce from 'immer'; import BigNumber from 'bignumber.js'; import sortBy from 'lodash/sortBy'; import type { Account } from '@vegaprotocol/accounts'; import { accountsDataProvider } from '@vegaprotocol/accounts'; import { toBigNum } from '@vegaprotocol/react-helpers'; -import type { - Positions, - Positions_party, - Positions_party_positionsConnection_edges, -} from './__generated__/Positions'; import { makeDataProvider, makeDerivedDataProvider, } from '@vegaprotocol/react-helpers'; - -import type { - PositionsSubscription, - PositionsSubscription_positions, -} from './__generated__/PositionsSubscription'; - import { AccountType } from '@vegaprotocol/types'; import type { MarketTradingMode } from '@vegaprotocol/types'; import type { MarketWithData } from '@vegaprotocol/market-list'; import { marketsWithDataProvider } from '@vegaprotocol/market-list'; -import { marginsDataProvider } from './margin-data-provider'; import type { - Margins_party, - Margins_party_marginsConnection_edges_node, -} from './__generated__/Margins'; + PositionsQuery, + PositionsSubscriptionSubscription, + MarginsQuery, + MarginFieldsFragment, +} from './__generated___/Positions'; +import { + PositionsDocument, + PositionsSubscriptionDocument, +} from './__generated___/Positions'; +import { marginsDataProvider } from './margin-data-provider'; type PositionMarginLevel = Pick< - Margins_party_marginsConnection_edges_node, + MarginFieldsFragment, 'maintenanceLevel' | 'searchLevel' | 'initialLevel' >; @@ -40,7 +34,7 @@ interface PositionRejoined { openVolume: string; unrealisedPNL: string; averageEntryPrice: string; - updatedAt: string | null; + updatedAt?: string | null; market: MarketWithData | null; margins: PositionMarginLevel | null; } @@ -69,52 +63,10 @@ export interface Position { } export interface Data { - party: Positions_party | null; + party: PositionsQuery['party'] | null; positions: Position[] | null; } -const POSITION_FIELDS = gql` - fragment PositionFields on Position { - realisedPNL - openVolume - unrealisedPNL - averageEntryPrice - updatedAt - market { - id - } - } -`; - -export const POSITIONS_QUERY = gql` - ${POSITION_FIELDS} - query Positions($partyId: ID!) { - party(id: $partyId) { - id - positionsConnection { - edges { - node { - ...PositionFields - } - } - } - } - } -`; - -export const POSITIONS_SUBSCRIPTION = gql` - subscription PositionsSubscription($partyId: ID!) { - positions(partyId: $partyId) { - realisedPNL - openVolume - unrealisedPNL - averageEntryPrice - updatedAt - marketId - } - } -`; - export const getMetrics = ( data: PositionRejoined[] | null, accounts: Account[] | null @@ -221,19 +173,19 @@ export const getMetrics = ( searchPrice: searchPrice .multipliedBy(10 ** marketDecimalPlaces) .toFixed(0), - updatedAt: position.updatedAt, + updatedAt: position.updatedAt || null, }); }); return metrics; }; export const update = ( - data: Positions_party, - deltas: PositionsSubscription_positions[] + data: PositionsQuery['party'], + deltas: PositionsSubscriptionSubscription['positions'] ) => { return produce(data, (draft) => { deltas.forEach((delta) => { - if (!draft.positionsConnection?.edges || !delta) { + if (!draft?.positionsConnection?.edges || !delta) { return; } const index = draft.positionsConnection.edges.findIndex( @@ -267,22 +219,22 @@ export const update = ( }; export const positionsDataProvider = makeDataProvider< - Positions, - Positions_party, - PositionsSubscription, - PositionsSubscription_positions[] + PositionsQuery, + PositionsQuery['party'], + PositionsSubscriptionSubscription, + PositionsSubscriptionSubscription['positions'] >({ - query: POSITIONS_QUERY, - subscriptionQuery: POSITIONS_SUBSCRIPTION, + query: PositionsDocument, + subscriptionQuery: PositionsSubscriptionDocument, update, - getData: (responseData: Positions) => responseData.party, - getDelta: (subscriptionData: PositionsSubscription) => + getData: (responseData: PositionsQuery) => responseData.party, + getDelta: (subscriptionData: PositionsSubscriptionSubscription) => subscriptionData.positions, }); const upgradeMarginsConection = ( marketId: string, - margins: Margins_party | null + margins: MarginsQuery['party'] | null ) => { if (marketId && margins?.marginsConnection?.edges) { const index = @@ -302,26 +254,23 @@ const upgradeMarginsConection = ( }; export const rejoinPositionData = ( - positions: Positions_party | null, + positions: PositionsQuery['party'] | null, marketsData: MarketWithData[] | null, - margins: Margins_party | null + margins: MarginsQuery['party'] | null ): PositionRejoined[] | null => { if (positions?.positionsConnection?.edges && marketsData && margins) { - return positions.positionsConnection.edges.map( - (nodes: Positions_party_positionsConnection_edges) => { - return { - realisedPNL: nodes.node.realisedPNL, - openVolume: nodes.node.openVolume, - unrealisedPNL: nodes.node.unrealisedPNL, - averageEntryPrice: nodes.node.averageEntryPrice, - updatedAt: nodes.node.updatedAt, - market: - marketsData?.find((market) => market.id === nodes.node.market.id) || - null, - margins: upgradeMarginsConection(nodes.node.market.id, margins), - }; - } - ); + return positions.positionsConnection.edges.map(({ node }) => { + return { + realisedPNL: node.realisedPNL, + openVolume: node.openVolume, + unrealisedPNL: node.unrealisedPNL, + averageEntryPrice: node.averageEntryPrice, + updatedAt: node.updatedAt, + market: + marketsData?.find((market) => market.id === node.market.id) || null, + margins: upgradeMarginsConection(node.market.id, margins), + }; + }); } return null; };