From b3a81762977dbcfe4d1f2287d0612b38409c2dca Mon Sep 17 00:00:00 2001 From: botond <105208209+notbot00@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:48:42 +0200 Subject: [PATCH] chore: migrate network stats and clean up liquidity lib (#1896) * chore: migrate network stats and clean up liquidity lib * fix: format * fix: test imports * fix: format * fix: market info expiry format tests * fix: format again * fix: build --- .../src/lib/MarketsLiquidity.graphql | 28 +++--- .../LiquidityProvisionMarkets.ts | 77 --------------- .../MarketLiquidity.ts | 0 .../MarketsLiquidity.ts | 34 ++++--- libs/liquidity/src/lib/index.ts | 3 +- .../src/lib/liquidity-data-provider.ts | 4 +- .../src/lib/markets-liquidity-provider.ts | 8 +- .../src/lib/utils/liquidity-utils.ts | 5 +- .../market-expires/market-expires.spec.tsx | 13 +++ .../components/stats-manager/Stats.graphql | 4 +- .../__generated__/NetworkStats.ts | 95 ------------------- .../stats-manager/__generated__/Stats.ts | 63 ++++++++++++ .../stats-manager/__generated___/Stats.ts | 63 ------------ .../stats-manager/stats-manager.tsx | 35 +------ libs/network-stats/src/config/types.ts | 16 ++-- libs/types/apollo.config.js | 2 + 16 files changed, 140 insertions(+), 310 deletions(-) delete mode 100644 libs/liquidity/src/lib/__generated__/LiquidityProvisionMarkets.ts rename libs/liquidity/src/lib/{__generated___ => __generated__}/MarketLiquidity.ts (100%) rename libs/liquidity/src/lib/{__generated___ => __generated__}/MarketsLiquidity.ts (80%) delete mode 100644 libs/network-stats/src/components/stats-manager/__generated__/NetworkStats.ts create mode 100644 libs/network-stats/src/components/stats-manager/__generated__/Stats.ts delete mode 100644 libs/network-stats/src/components/stats-manager/__generated___/Stats.ts diff --git a/libs/liquidity/src/lib/MarketsLiquidity.graphql b/libs/liquidity/src/lib/MarketsLiquidity.graphql index 0bc8cc38d..9d1e009f7 100644 --- a/libs/liquidity/src/lib/MarketsLiquidity.graphql +++ b/libs/liquidity/src/lib/MarketsLiquidity.graphql @@ -1,19 +1,23 @@ +fragment MarketNode on Market { + id + liquidityProvisionsConnection { + edges { + node { + commitmentAmount + fee + } + } + } + data { + targetStake + } +} + query LiquidityProvisionMarkets { marketsConnection { edges { node { - id - liquidityProvisionsConnection { - edges { - node { - commitmentAmount - fee - } - } - } - data { - targetStake - } + ...MarketNode } } } diff --git a/libs/liquidity/src/lib/__generated__/LiquidityProvisionMarkets.ts b/libs/liquidity/src/lib/__generated__/LiquidityProvisionMarkets.ts deleted file mode 100644 index 1e55e07b6..000000000 --- a/libs/liquidity/src/lib/__generated__/LiquidityProvisionMarkets.ts +++ /dev/null @@ -1,77 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL query operation: LiquidityProvisionMarkets -// ==================================================== - -export interface LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection_edges_node { - __typename: "LiquidityProvision"; - /** - * Specified as a unit-less number that represents the amount of settlement asset of the market. - */ - commitmentAmount: string; - /** - * Nominated liquidity fee factor, which is an input to the calculation of liquidity fees on the market, as per setting fees and rewarding liquidity providers. - */ - fee: string; -} - -export interface LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection_edges { - __typename: "LiquidityProvisionsEdge"; - node: LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection_edges_node; -} - -export interface LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection { - __typename: "LiquidityProvisionsConnection"; - edges: (LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection_edges | null)[] | null; -} - -export interface LiquidityProvisionMarkets_marketsConnection_edges_node_data { - __typename: "MarketData"; - /** - * The amount of stake targeted for this market - */ - targetStake: string | null; -} - -export interface LiquidityProvisionMarkets_marketsConnection_edges_node { - __typename: "Market"; - /** - * Market ID - */ - id: string; - /** - * The list of the liquidity provision commitments for this market - */ - liquidityProvisionsConnection: LiquidityProvisionMarkets_marketsConnection_edges_node_liquidityProvisionsConnection | null; - /** - * marketData for the given market - */ - data: LiquidityProvisionMarkets_marketsConnection_edges_node_data | null; -} - -export interface LiquidityProvisionMarkets_marketsConnection_edges { - __typename: "MarketEdge"; - /** - * The market - */ - node: LiquidityProvisionMarkets_marketsConnection_edges_node; -} - -export interface LiquidityProvisionMarkets_marketsConnection { - __typename: "MarketConnection"; - /** - * The markets in this connection - */ - edges: LiquidityProvisionMarkets_marketsConnection_edges[]; -} - -export interface LiquidityProvisionMarkets { - /** - * One or more instruments that are trading on the Vega network - */ - marketsConnection: LiquidityProvisionMarkets_marketsConnection | null; -} diff --git a/libs/liquidity/src/lib/__generated___/MarketLiquidity.ts b/libs/liquidity/src/lib/__generated__/MarketLiquidity.ts similarity index 100% rename from libs/liquidity/src/lib/__generated___/MarketLiquidity.ts rename to libs/liquidity/src/lib/__generated__/MarketLiquidity.ts diff --git a/libs/liquidity/src/lib/__generated___/MarketsLiquidity.ts b/libs/liquidity/src/lib/__generated__/MarketsLiquidity.ts similarity index 80% rename from libs/liquidity/src/lib/__generated___/MarketsLiquidity.ts rename to libs/liquidity/src/lib/__generated__/MarketsLiquidity.ts index a762a2e64..64de1088c 100644 --- a/libs/liquidity/src/lib/__generated___/MarketsLiquidity.ts +++ b/libs/liquidity/src/lib/__generated__/MarketsLiquidity.ts @@ -3,34 +3,40 @@ import { Schema as Types } from '@vegaprotocol/types'; import { gql } from '@apollo/client'; import * as Apollo from '@apollo/client'; const defaultOptions = {} as const; +export type MarketNodeFragment = { __typename?: 'Market', id: string, liquidityProvisionsConnection?: { __typename?: 'LiquidityProvisionsConnection', edges?: Array<{ __typename?: 'LiquidityProvisionsEdge', node: { __typename?: 'LiquidityProvision', commitmentAmount: string, fee: string } } | null> | null } | null, data?: { __typename?: 'MarketData', targetStake?: string | null } | null }; + export type LiquidityProvisionMarketsQueryVariables = Types.Exact<{ [key: string]: never; }>; export type LiquidityProvisionMarketsQuery = { __typename?: 'Query', marketsConnection?: { __typename?: 'MarketConnection', edges: Array<{ __typename?: 'MarketEdge', node: { __typename?: 'Market', id: string, liquidityProvisionsConnection?: { __typename?: 'LiquidityProvisionsConnection', edges?: Array<{ __typename?: 'LiquidityProvisionsEdge', node: { __typename?: 'LiquidityProvision', commitmentAmount: string, fee: string } } | null> | null } | null, data?: { __typename?: 'MarketData', targetStake?: string | null } | null } }> } | null }; - +export const MarketNodeFragmentDoc = gql` + fragment MarketNode on Market { + id + liquidityProvisionsConnection { + edges { + node { + commitmentAmount + fee + } + } + } + data { + targetStake + } +} + `; export const LiquidityProvisionMarketsDocument = gql` query LiquidityProvisionMarkets { marketsConnection { edges { node { - id - liquidityProvisionsConnection { - edges { - node { - commitmentAmount - fee - } - } - } - data { - targetStake - } + ...MarketNode } } } } - `; + ${MarketNodeFragmentDoc}`; /** * __useLiquidityProvisionMarketsQuery__ diff --git a/libs/liquidity/src/lib/index.ts b/libs/liquidity/src/lib/index.ts index 5187c55b5..d9217e9dc 100644 --- a/libs/liquidity/src/lib/index.ts +++ b/libs/liquidity/src/lib/index.ts @@ -1,4 +1,5 @@ -export * from './__generated___/MarketLiquidity'; +export * from './__generated__/MarketLiquidity'; +export * from './__generated__/MarketsLiquidity'; export * from './liquidity-data-provider'; export * from './liquidity-table'; export * from './markets-liquidity-provider'; diff --git a/libs/liquidity/src/lib/liquidity-data-provider.ts b/libs/liquidity/src/lib/liquidity-data-provider.ts index fd67b5d63..5b4d49d54 100644 --- a/libs/liquidity/src/lib/liquidity-data-provider.ts +++ b/libs/liquidity/src/lib/liquidity-data-provider.ts @@ -13,7 +13,7 @@ import { LiquidityProvisionsDocument, LiquidityProvisionsUpdateDocument, MarketLpDocument, -} from './__generated___/MarketLiquidity'; +} from './__generated__/MarketLiquidity'; import type { MarketLpQuery, @@ -23,7 +23,7 @@ import type { LiquidityProvisionFieldsFragment, LiquidityProvisionsQuery, LiquidityProvisionsUpdateSubscription, -} from './__generated___/MarketLiquidity'; +} from './__generated__/MarketLiquidity'; import type { Account } from '@vegaprotocol/accounts'; export const liquidityProvisionsDataProvider = makeDataProvider< LiquidityProvisionsQuery, diff --git a/libs/liquidity/src/lib/markets-liquidity-provider.ts b/libs/liquidity/src/lib/markets-liquidity-provider.ts index 785e1ec65..851e53e07 100644 --- a/libs/liquidity/src/lib/markets-liquidity-provider.ts +++ b/libs/liquidity/src/lib/markets-liquidity-provider.ts @@ -18,8 +18,8 @@ import { marketListProvider, } from '@vegaprotocol/market-list'; -import type { LiquidityProvisionMarkets } from './__generated__/LiquidityProvisionMarkets'; -import { LiquidityProvisionMarketsDocument } from './__generated___/MarketsLiquidity'; +import type { LiquidityProvisionMarketsQuery } from './__generated__/MarketsLiquidity'; +import { LiquidityProvisionMarketsDocument } from './__generated__/MarketsLiquidity'; import { calcDayVolume, @@ -45,7 +45,7 @@ export interface Markets { } const getData = ( - responseData: LiquidityProvisionMarkets + responseData: LiquidityProvisionMarketsQuery ): LiquidityProvisionMarket[] | null => { return ( responseData?.marketsConnection?.edges.map((edge) => { @@ -82,7 +82,7 @@ export const addData = ( }; export const liquidityMarketsProvider = makeDataProvider< - LiquidityProvisionMarkets, + LiquidityProvisionMarketsQuery, LiquidityProvisionMarket[], never, never diff --git a/libs/liquidity/src/lib/utils/liquidity-utils.ts b/libs/liquidity/src/lib/utils/liquidity-utils.ts index 4101c25a2..0ca034afb 100644 --- a/libs/liquidity/src/lib/utils/liquidity-utils.ts +++ b/libs/liquidity/src/lib/utils/liquidity-utils.ts @@ -1,10 +1,9 @@ import BigNumber from 'bignumber.js'; import { addDecimalsFormatNumber } from '@vegaprotocol/react-helpers'; -import type { LiquidityProvisionMarkets_marketsConnection_edges_node } from './../__generated__/LiquidityProvisionMarkets'; +import type { MarketNodeFragment } from './../__generated__/MarketsLiquidity'; -export type LiquidityProvisionMarket = - LiquidityProvisionMarkets_marketsConnection_edges_node; +export type LiquidityProvisionMarket = MarketNodeFragment; export interface Provider { commitmentAmount: string; diff --git a/libs/market-info/src/components/market-expires/market-expires.spec.tsx b/libs/market-info/src/components/market-expires/market-expires.spec.tsx index b06f7f397..eac03ecde 100644 --- a/libs/market-info/src/components/market-expires/market-expires.spec.tsx +++ b/libs/market-info/src/components/market-expires/market-expires.spec.tsx @@ -3,6 +3,19 @@ import React from 'react'; import { MarketExpires } from './market-expires'; +jest.mock('@vegaprotocol/react-helpers', () => ({ + t: jest.fn().mockImplementation((text) => text), + getDateTimeFormat: () => + Intl.DateTimeFormat('en-GB', { + year: 'numeric', + month: 'numeric', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + }), +})); + describe('MarketExpires', () => { describe('should properly parse different tags', () => { it('settlement:date', () => { diff --git a/libs/network-stats/src/components/stats-manager/Stats.graphql b/libs/network-stats/src/components/stats-manager/Stats.graphql index 5730803ce..f84192eae 100644 --- a/libs/network-stats/src/components/stats-manager/Stats.graphql +++ b/libs/network-stats/src/components/stats-manager/Stats.graphql @@ -1,10 +1,9 @@ -query NetworkStats { +query Stats { nodeData { stakedTotal totalNodes inactiveNodes validatingNodes - uptime } statistics { status @@ -19,5 +18,6 @@ query NetworkStats { appVersion chainVersion chainId + genesisTime } } diff --git a/libs/network-stats/src/components/stats-manager/__generated__/NetworkStats.ts b/libs/network-stats/src/components/stats-manager/__generated__/NetworkStats.ts deleted file mode 100644 index 95a0f7fc6..000000000 --- a/libs/network-stats/src/components/stats-manager/__generated__/NetworkStats.ts +++ /dev/null @@ -1,95 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -// @generated -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL query operation: NetworkStats -// ==================================================== - -export interface NetworkStats_nodeData { - __typename: "NodeData"; - /** - * Total staked amount across all nodes - */ - stakedTotal: string; - /** - * Total number of nodes - */ - totalNodes: number; - /** - * Number of inactive nodes - */ - inactiveNodes: number; - /** - * Number of nodes validating - */ - validatingNodes: number; -} - -export interface NetworkStats_statistics { - __typename: "Statistics"; - /** - * Status of the Vega application connection with the chain - */ - status: string; - /** - * Current block number - */ - blockHeight: string; - /** - * Duration of the last block, in nanoseconds - */ - blockDuration: string; - /** - * Number of items in the backlog - */ - backlogLength: string; - /** - * Number of transaction processed per block - */ - txPerBlock: string; - /** - * Number of the trades per seconds - */ - tradesPerSecond: string; - /** - * Number of orders per seconds - */ - ordersPerSecond: string; - /** - * Average number of orders added per blocks - */ - averageOrdersPerBlock: string; - /** - * RFC3339Nano current time of the chain (decided through consensus) - */ - vegaTime: string; - /** - * Version of the Vega node (semver) - */ - appVersion: string; - /** - * Version of the chain (semver) - */ - chainVersion: string; - /** - * Current chain ID - */ - chainId: string; - /** - * RFC3339Nano genesis time of the chain - */ - genesisTime: string; -} - -export interface NetworkStats { - /** - * Returns information about nodes - */ - nodeData: NetworkStats_nodeData | null; - /** - * Get statistics about the Vega node - */ - statistics: NetworkStats_statistics; -} diff --git a/libs/network-stats/src/components/stats-manager/__generated__/Stats.ts b/libs/network-stats/src/components/stats-manager/__generated__/Stats.ts new file mode 100644 index 000000000..5b31a2b3e --- /dev/null +++ b/libs/network-stats/src/components/stats-manager/__generated__/Stats.ts @@ -0,0 +1,63 @@ +import { Schema as Types } from '@vegaprotocol/types'; + +import { gql } from '@apollo/client'; +import * as Apollo from '@apollo/client'; +const defaultOptions = {} as const; +export type StatsQueryVariables = Types.Exact<{ [key: string]: never; }>; + + +export type StatsQuery = { __typename?: 'Query', nodeData?: { __typename?: 'NodeData', stakedTotal: string, totalNodes: number, inactiveNodes: number, validatingNodes: number } | null, statistics: { __typename?: 'Statistics', status: string, blockHeight: string, blockDuration: string, backlogLength: string, txPerBlock: string, tradesPerSecond: string, ordersPerSecond: string, averageOrdersPerBlock: string, vegaTime: string, appVersion: string, chainVersion: string, chainId: string, genesisTime: string } }; + + +export const StatsDocument = gql` + query Stats { + nodeData { + stakedTotal + totalNodes + inactiveNodes + validatingNodes + } + statistics { + status + blockHeight + blockDuration + backlogLength + txPerBlock + tradesPerSecond + ordersPerSecond + averageOrdersPerBlock + vegaTime + appVersion + chainVersion + chainId + genesisTime + } +} + `; + +/** + * __useStatsQuery__ + * + * To run a query within a React component, call `useStatsQuery` and pass it any options that fit your needs. + * When your component renders, `useStatsQuery` 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 } = useStatsQuery({ + * variables: { + * }, + * }); + */ +export function useStatsQuery(baseOptions?: Apollo.QueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useQuery(StatsDocument, options); + } +export function useStatsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { + const options = {...defaultOptions, ...baseOptions} + return Apollo.useLazyQuery(StatsDocument, options); + } +export type StatsQueryHookResult = ReturnType; +export type StatsLazyQueryHookResult = ReturnType; +export type StatsQueryResult = Apollo.QueryResult; \ No newline at end of file diff --git a/libs/network-stats/src/components/stats-manager/__generated___/Stats.ts b/libs/network-stats/src/components/stats-manager/__generated___/Stats.ts deleted file mode 100644 index 2d8764e28..000000000 --- a/libs/network-stats/src/components/stats-manager/__generated___/Stats.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Schema as Types } from '@vegaprotocol/types'; - -import { gql } from '@apollo/client'; -import * as Apollo from '@apollo/client'; -const defaultOptions = {} as const; -export type NetworkStatsQueryVariables = Types.Exact<{ [key: string]: never; }>; - - -export type NetworkStatsQuery = { __typename?: 'Query', nodeData?: { __typename?: 'NodeData', stakedTotal: string, totalNodes: number, inactiveNodes: number, validatingNodes: number, uptime: number } | null, statistics: { __typename?: 'Statistics', status: string, blockHeight: string, blockDuration: string, backlogLength: string, txPerBlock: string, tradesPerSecond: string, ordersPerSecond: string, averageOrdersPerBlock: string, vegaTime: string, appVersion: string, chainVersion: string, chainId: string } }; - - -export const NetworkStatsDocument = gql` - query NetworkStats { - nodeData { - stakedTotal - totalNodes - inactiveNodes - validatingNodes - uptime - } - statistics { - status - blockHeight - blockDuration - backlogLength - txPerBlock - tradesPerSecond - ordersPerSecond - averageOrdersPerBlock - vegaTime - appVersion - chainVersion - chainId - } -} - `; - -/** - * __useNetworkStatsQuery__ - * - * To run a query within a React component, call `useNetworkStatsQuery` and pass it any options that fit your needs. - * When your component renders, `useNetworkStatsQuery` 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 } = useNetworkStatsQuery({ - * variables: { - * }, - * }); - */ -export function useNetworkStatsQuery(baseOptions?: Apollo.QueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useQuery(NetworkStatsDocument, options); - } -export function useNetworkStatsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions) { - const options = {...defaultOptions, ...baseOptions} - return Apollo.useLazyQuery(NetworkStatsDocument, options); - } -export type NetworkStatsQueryHookResult = ReturnType; -export type NetworkStatsLazyQueryHookResult = ReturnType; -export type NetworkStatsQueryResult = Apollo.QueryResult; \ No newline at end of file diff --git a/libs/network-stats/src/components/stats-manager/stats-manager.tsx b/libs/network-stats/src/components/stats-manager/stats-manager.tsx index 53b203236..9d2857229 100644 --- a/libs/network-stats/src/components/stats-manager/stats-manager.tsx +++ b/libs/network-stats/src/components/stats-manager/stats-manager.tsx @@ -1,6 +1,5 @@ import { useEffect } from 'react'; import classnames from 'classnames'; -import { useQuery, gql } from '@apollo/client'; import { useEnvironment } from '@vegaprotocol/environment'; import { statsFields } from '../../config/stats-fields'; import type { @@ -11,39 +10,14 @@ import { Table } from '../table'; import { TableRow } from '../table-row'; import { PromotedStats } from '../promoted-stats'; import { PromotedStatsItem } from '../promoted-stats-item'; -import type { NetworkStats } from './__generated__/NetworkStats'; +import { useStatsQuery } from './__generated__/Stats'; +import type { StatsQuery } from './__generated__/Stats'; interface StatsManagerProps { className?: string; } -const STATS_QUERY = gql` - query NetworkStats { - nodeData { - stakedTotal - totalNodes - inactiveNodes - validatingNodes - } - statistics { - status - blockHeight - blockDuration - backlogLength - txPerBlock - tradesPerSecond - ordersPerSecond - averageOrdersPerBlock - vegaTime - appVersion - chainVersion - chainId - genesisTime - } - } -`; - -const compileData = (data?: NetworkStats) => { +const compileData = (data?: StatsQuery) => { const { nodeData, statistics } = data || {}; const returned = { ...nodeData, ...statistics }; @@ -71,8 +45,7 @@ const compileData = (data?: NetworkStats) => { export const StatsManager = ({ className }: StatsManagerProps) => { const { VEGA_ENV } = useEnvironment(); - const { data, error, startPolling, stopPolling } = - useQuery(STATS_QUERY); + const { data, error, startPolling, stopPolling } = useStatsQuery(); useEffect(() => { startPolling(1000); diff --git a/libs/network-stats/src/config/types.ts b/libs/network-stats/src/config/types.ts index eee026171..e1f7f7f39 100644 --- a/libs/network-stats/src/config/types.ts +++ b/libs/network-stats/src/config/types.ts @@ -1,10 +1,14 @@ -import type { - NetworkStats_nodeData, - NetworkStats_statistics, -} from '../components/stats-manager/__generated__/NetworkStats'; +import type { Schema } from '@vegaprotocol/types'; +import type { StatsQuery } from '../components/stats-manager/__generated__/Stats'; -export type Stats = Omit & - Omit; +type NodeDataKeys = + | 'stakedTotal' + | 'totalNodes' + | 'inactiveNodes' + | 'validatingNodes'; + +export type Stats = Pick & + Omit; // eslint-disable-next-line export type value = any; diff --git a/libs/types/apollo.config.js b/libs/types/apollo.config.js index db4161a92..eea32a900 100644 --- a/libs/types/apollo.config.js +++ b/libs/types/apollo.config.js @@ -22,6 +22,8 @@ module.exports = { '../../libs/environment/**', '../../libs/fills/**', '../../libs/governance/**', + '../../libs/liquidity/**', + '../../libs/network-stats/**', ], }, };