From 936ed3f9e79d1ad0b3f01e191cd7e8f1a3af53bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Thu, 24 Mar 2022 17:48:34 +0100 Subject: [PATCH] [#128] Add positions data provider --- libs/graphql/apollo.config.js | 2 +- .../src/__generated__/PositionDetails.ts | 149 +++++++++++++++ .../src/__generated__/positionSubscribe.ts | 160 ++++++++++++++++ libs/graphql/src/__generated__/positions.ts | 172 ++++++++++++++++++ libs/graphql/src/data-providers/index.ts | 1 + .../data-providers/positions-data-provider.ts | 98 ++++++++++ 6 files changed, 581 insertions(+), 1 deletion(-) create mode 100644 libs/graphql/src/__generated__/PositionDetails.ts create mode 100644 libs/graphql/src/__generated__/positionSubscribe.ts create mode 100644 libs/graphql/src/__generated__/positions.ts create mode 100644 libs/graphql/src/data-providers/positions-data-provider.ts diff --git a/libs/graphql/apollo.config.js b/libs/graphql/apollo.config.js index 0a0c2499e..c18c10d12 100644 --- a/libs/graphql/apollo.config.js +++ b/libs/graphql/apollo.config.js @@ -4,6 +4,6 @@ module.exports = { name: 'vega', url: process.env.NX_VEGA_URL, }, - includes: ['../../{apps,lib}/**/*.{ts,tsx,js,jsx,graphql}'], + includes: ['../../{apps,libs}/**/*.{ts,tsx,js,jsx,graphql}'], }, }; diff --git a/libs/graphql/src/__generated__/PositionDetails.ts b/libs/graphql/src/__generated__/PositionDetails.ts new file mode 100644 index 000000000..562c99e86 --- /dev/null +++ b/libs/graphql/src/__generated__/PositionDetails.ts @@ -0,0 +1,149 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL fragment: PositionDetails +// ==================================================== + +export interface PositionDetails_market_data { + __typename: "MarketData"; + /** + * the mark price (actually an unsigned int) + */ + markPrice: string; +} + +export interface PositionDetails_market_tradableInstrument_instrument_metadata { + __typename: "InstrumentMetadata"; + /** + * An arbitrary list of tags to associated to associate to the Instrument (string list) + */ + tags: string[] | null; +} + +export interface PositionDetails_market_tradableInstrument_instrument_product_settlementAsset { + __typename: "Asset"; + /** + * The id of the asset + */ + id: string; + /** + * The symbol of the asset (e.g: GBP) + */ + symbol: string; + /** + * The full name of the asset (e.g: Great British Pound) + */ + name: string; + /** + * The precision of the asset + */ + decimals: number; +} + +export interface PositionDetails_market_tradableInstrument_instrument_product { + __typename: "Future"; + /** + * The name of the asset (string) + */ + settlementAsset: PositionDetails_market_tradableInstrument_instrument_product_settlementAsset; + /** + * String representing the quote (e.g. BTCUSD -> USD is quote) + */ + quoteName: string; +} + +export interface PositionDetails_market_tradableInstrument_instrument { + __typename: "Instrument"; + /** + * Uniquely identify an instrument across all instruments available on Vega (string) + */ + id: string; + /** + * Full and fairly descriptive name for the instrument + */ + name: string; + /** + * Metadata for this instrument + */ + metadata: PositionDetails_market_tradableInstrument_instrument_metadata; + /** + * A short non necessarily unique code used to easily describe the instrument (e.g: FX:BTCUSD/DEC18) (string) + */ + code: string; + /** + * A reference to or instance of a fully specified product, including all required product parameters for that product (Product union) + */ + product: PositionDetails_market_tradableInstrument_instrument_product; +} + +export interface PositionDetails_market_tradableInstrument { + __typename: "TradableInstrument"; + /** + * An instance of or reference to a fully specified instrument. + */ + instrument: PositionDetails_market_tradableInstrument_instrument; +} + +export interface PositionDetails_market { + __typename: "Market"; + /** + * Market ID + */ + id: string; + /** + * Market full name + */ + name: string; + /** + * marketData for the given market + */ + data: PositionDetails_market_data | null; + /** + * decimalPlaces indicates the number of decimal places that an integer must be shifted by in order to get a correct + * number denominated in the currency of the Market. (uint64) + * + * Examples: + * Currency Balance decimalPlaces Real Balance + * GBP 100 0 GBP 100 + * GBP 100 2 GBP 1.00 + * GBP 100 4 GBP 0.01 + * GBP 1 4 GBP 0.0001 ( 0.01p ) + * + * GBX (pence) 100 0 GBP 1.00 (100p ) + * GBX (pence) 100 2 GBP 0.01 ( 1p ) + * GBX (pence) 100 4 GBP 0.0001 ( 0.01p ) + * GBX (pence) 1 4 GBP 0.000001 ( 0.0001p) + */ + decimalPlaces: number; + /** + * An instance of or reference to a tradable instrument. + */ + tradableInstrument: PositionDetails_market_tradableInstrument; +} + +export interface PositionDetails { + __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; + /** + * Market relating to this position + */ + market: PositionDetails_market; +} diff --git a/libs/graphql/src/__generated__/positionSubscribe.ts b/libs/graphql/src/__generated__/positionSubscribe.ts new file mode 100644 index 000000000..0963955ed --- /dev/null +++ b/libs/graphql/src/__generated__/positionSubscribe.ts @@ -0,0 +1,160 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL subscription operation: positionSubscribe +// ==================================================== + +export interface positionSubscribe_positions_market_data { + __typename: "MarketData"; + /** + * the mark price (actually an unsigned int) + */ + markPrice: string; +} + +export interface positionSubscribe_positions_market_tradableInstrument_instrument_metadata { + __typename: "InstrumentMetadata"; + /** + * An arbitrary list of tags to associated to associate to the Instrument (string list) + */ + tags: string[] | null; +} + +export interface positionSubscribe_positions_market_tradableInstrument_instrument_product_settlementAsset { + __typename: "Asset"; + /** + * The id of the asset + */ + id: string; + /** + * The symbol of the asset (e.g: GBP) + */ + symbol: string; + /** + * The full name of the asset (e.g: Great British Pound) + */ + name: string; + /** + * The precision of the asset + */ + decimals: number; +} + +export interface positionSubscribe_positions_market_tradableInstrument_instrument_product { + __typename: "Future"; + /** + * The name of the asset (string) + */ + settlementAsset: positionSubscribe_positions_market_tradableInstrument_instrument_product_settlementAsset; + /** + * String representing the quote (e.g. BTCUSD -> USD is quote) + */ + quoteName: string; +} + +export interface positionSubscribe_positions_market_tradableInstrument_instrument { + __typename: "Instrument"; + /** + * Uniquely identify an instrument across all instruments available on Vega (string) + */ + id: string; + /** + * Full and fairly descriptive name for the instrument + */ + name: string; + /** + * Metadata for this instrument + */ + metadata: positionSubscribe_positions_market_tradableInstrument_instrument_metadata; + /** + * A short non necessarily unique code used to easily describe the instrument (e.g: FX:BTCUSD/DEC18) (string) + */ + code: string; + /** + * A reference to or instance of a fully specified product, including all required product parameters for that product (Product union) + */ + product: positionSubscribe_positions_market_tradableInstrument_instrument_product; +} + +export interface positionSubscribe_positions_market_tradableInstrument { + __typename: "TradableInstrument"; + /** + * An instance of or reference to a fully specified instrument. + */ + instrument: positionSubscribe_positions_market_tradableInstrument_instrument; +} + +export interface positionSubscribe_positions_market { + __typename: "Market"; + /** + * Market ID + */ + id: string; + /** + * Market full name + */ + name: string; + /** + * marketData for the given market + */ + data: positionSubscribe_positions_market_data | null; + /** + * decimalPlaces indicates the number of decimal places that an integer must be shifted by in order to get a correct + * number denominated in the currency of the Market. (uint64) + * + * Examples: + * Currency Balance decimalPlaces Real Balance + * GBP 100 0 GBP 100 + * GBP 100 2 GBP 1.00 + * GBP 100 4 GBP 0.01 + * GBP 1 4 GBP 0.0001 ( 0.01p ) + * + * GBX (pence) 100 0 GBP 1.00 (100p ) + * GBX (pence) 100 2 GBP 0.01 ( 1p ) + * GBX (pence) 100 4 GBP 0.0001 ( 0.01p ) + * GBX (pence) 1 4 GBP 0.000001 ( 0.0001p) + */ + decimalPlaces: number; + /** + * An instance of or reference to a tradable instrument. + */ + tradableInstrument: positionSubscribe_positions_market_tradableInstrument; +} + +export interface positionSubscribe_positions { + __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; + /** + * Market relating to this position + */ + market: positionSubscribe_positions_market; +} + +export interface positionSubscribe { + /** + * Subscribe to the positions updates + */ + positions: positionSubscribe_positions; +} + +export interface positionSubscribeVariables { + partyId: string; +} diff --git a/libs/graphql/src/__generated__/positions.ts b/libs/graphql/src/__generated__/positions.ts new file mode 100644 index 000000000..d19e79a58 --- /dev/null +++ b/libs/graphql/src/__generated__/positions.ts @@ -0,0 +1,172 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: positions +// ==================================================== + +export interface positions_party_positions_market_data { + __typename: "MarketData"; + /** + * the mark price (actually an unsigned int) + */ + markPrice: string; +} + +export interface positions_party_positions_market_tradableInstrument_instrument_metadata { + __typename: "InstrumentMetadata"; + /** + * An arbitrary list of tags to associated to associate to the Instrument (string list) + */ + tags: string[] | null; +} + +export interface positions_party_positions_market_tradableInstrument_instrument_product_settlementAsset { + __typename: "Asset"; + /** + * The id of the asset + */ + id: string; + /** + * The symbol of the asset (e.g: GBP) + */ + symbol: string; + /** + * The full name of the asset (e.g: Great British Pound) + */ + name: string; + /** + * The precision of the asset + */ + decimals: number; +} + +export interface positions_party_positions_market_tradableInstrument_instrument_product { + __typename: "Future"; + /** + * The name of the asset (string) + */ + settlementAsset: positions_party_positions_market_tradableInstrument_instrument_product_settlementAsset; + /** + * String representing the quote (e.g. BTCUSD -> USD is quote) + */ + quoteName: string; +} + +export interface positions_party_positions_market_tradableInstrument_instrument { + __typename: "Instrument"; + /** + * Uniquely identify an instrument across all instruments available on Vega (string) + */ + id: string; + /** + * Full and fairly descriptive name for the instrument + */ + name: string; + /** + * Metadata for this instrument + */ + metadata: positions_party_positions_market_tradableInstrument_instrument_metadata; + /** + * A short non necessarily unique code used to easily describe the instrument (e.g: FX:BTCUSD/DEC18) (string) + */ + code: string; + /** + * A reference to or instance of a fully specified product, including all required product parameters for that product (Product union) + */ + product: positions_party_positions_market_tradableInstrument_instrument_product; +} + +export interface positions_party_positions_market_tradableInstrument { + __typename: "TradableInstrument"; + /** + * An instance of or reference to a fully specified instrument. + */ + instrument: positions_party_positions_market_tradableInstrument_instrument; +} + +export interface positions_party_positions_market { + __typename: "Market"; + /** + * Market ID + */ + id: string; + /** + * Market full name + */ + name: string; + /** + * marketData for the given market + */ + data: positions_party_positions_market_data | null; + /** + * decimalPlaces indicates the number of decimal places that an integer must be shifted by in order to get a correct + * number denominated in the currency of the Market. (uint64) + * + * Examples: + * Currency Balance decimalPlaces Real Balance + * GBP 100 0 GBP 100 + * GBP 100 2 GBP 1.00 + * GBP 100 4 GBP 0.01 + * GBP 1 4 GBP 0.0001 ( 0.01p ) + * + * GBX (pence) 100 0 GBP 1.00 (100p ) + * GBX (pence) 100 2 GBP 0.01 ( 1p ) + * GBX (pence) 100 4 GBP 0.0001 ( 0.01p ) + * GBX (pence) 1 4 GBP 0.000001 ( 0.0001p) + */ + decimalPlaces: number; + /** + * An instance of or reference to a tradable instrument. + */ + tradableInstrument: positions_party_positions_market_tradableInstrument; +} + +export interface positions_party_positions { + __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; + /** + * Market relating to this position + */ + market: positions_party_positions_market; +} + +export interface positions_party { + __typename: "Party"; + /** + * Party identifier + */ + id: string; + /** + * Trading positions relating to a party + */ + positions: positions_party_positions[] | 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/graphql/src/data-providers/index.ts b/libs/graphql/src/data-providers/index.ts index 8da2b0152..bd7cccd3c 100644 --- a/libs/graphql/src/data-providers/index.ts +++ b/libs/graphql/src/data-providers/index.ts @@ -1 +1,2 @@ export * from './markets-data-provider'; +export * from './positions-data-provider'; diff --git a/libs/graphql/src/data-providers/positions-data-provider.ts b/libs/graphql/src/data-providers/positions-data-provider.ts new file mode 100644 index 000000000..b0b137f33 --- /dev/null +++ b/libs/graphql/src/data-providers/positions-data-provider.ts @@ -0,0 +1,98 @@ +import { gql } from '@apollo/client'; +import { + positions, + positions_party_positions, +} from '../__generated__/positions'; +import { makeDataProvider } from './generic-data-provider'; + +import { + positionSubscribe, + positionSubscribe_positions, +} from '../__generated__/positionSubscribe'; + +const POSITIONS_FRAGMENT = gql` + fragment PositionDetails on Position { + realisedPNL + openVolume + unrealisedPNL + averageEntryPrice + market { + id + name + data { + markPrice + } + decimalPlaces + tradableInstrument { + instrument { + id + name + metadata { + tags + } + code + product { + ... on Future { + settlementAsset { + id + symbol + name + decimals + } + quoteName + } + } + } + } + } + } +`; + +const POSITION_QUERY = gql` + ${POSITIONS_FRAGMENT} + query positions($partyId: ID!) { + party(id: $partyId) { + id + positions { + ...PositionDetails + } + } + } +`; + +export const POSITIONS_SUB = gql` + ${POSITIONS_FRAGMENT} + subscription positionSubscribe($partyId: ID!) { + positions(partyId: $partyId) { + ...PositionDetails + } + } +`; + +export const positionsDataProvider = makeDataProvider< + positions, + positions_party_positions, + positionSubscribe, + positionSubscribe_positions +>( + POSITION_QUERY, + POSITIONS_SUB, + ( + draft: positions_party_positions[] | null, + delta: positionSubscribe_positions + ) => { + if (!draft) { + return; + } + const index = draft.findIndex((m) => m.market.id === delta.market.id); + if (index !== -1) { + draft[index] = delta; + } else { + draft.push(delta); + } + }, + (responseData: positions): positions_party_positions[] | null => + responseData.party ? responseData.party.positions : null, + (subscriptionData: positionSubscribe): positionSubscribe_positions => + subscriptionData.positions +);