From 16b9310e388e7e3a69289f69692e24fa99ea1237 Mon Sep 17 00:00:00 2001 From: kuduzow Date: Thu, 1 Dec 2022 22:57:58 +0300 Subject: [PATCH] Add new methods to PriceFeed class to get statistics --- src/services/PriceFeed/index.ts | 30 ++++++++++++++----- src/services/PriceFeed/schemas/index.ts | 4 +++ .../PriceFeed/schemas/statisticsSchema.ts | 21 +++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/services/PriceFeed/schemas/statisticsSchema.ts diff --git a/src/services/PriceFeed/index.ts b/src/services/PriceFeed/index.ts index 098a8bf..263eedc 100644 --- a/src/services/PriceFeed/index.ts +++ b/src/services/PriceFeed/index.ts @@ -1,4 +1,5 @@ import fetchWithValidation from '../../fetchWithValidation'; +import { statisticsOverviewSchema, topPairsStatisticsSchema } from './schemas'; import candlesSchema from './schemas/candlesSchema'; import { PriceFeedWS } from './ws'; @@ -12,6 +13,8 @@ class PriceFeed { this.ws = new PriceFeedWS(this.wsUrl); this.getCandles = this.getCandles.bind(this); + this.getStatisticsOverview = this.getStatisticsOverview.bind(this); + this.getTopPairStatistics = this.getTopPairStatistics.bind(this); } getCandles = ( @@ -28,12 +31,23 @@ class PriceFeed { url.searchParams.append('interval', interval); url.searchParams.append('exchange', exchange); - return fetchWithValidation( - url.toString(), - candlesSchema, - ); + return fetchWithValidation(url.toString(), candlesSchema); }; + getStatisticsOverview(exchange = 'ALL') { + const url = new URL(`${this.statisticsUrl}/overview`); + url.searchParams.append('exchange', exchange); + + return fetchWithValidation(url.toString(), statisticsOverviewSchema); + } + + getTopPairStatistics(exchange = 'ALL') { + const url = new URL(`${this.statisticsUrl}/top-pairs`); + url.searchParams.append('exchange', exchange); + + return fetchWithValidation(url.toString(), topPairsStatisticsSchema); + } + get wsUrl() { const url = new URL(this.apiUrl); const wsProtocol = url.protocol === 'https:' ? 'wss' : 'ws'; @@ -43,10 +57,12 @@ class PriceFeed { get candlesUrl() { return `${this.apiUrl}/api/v1/candles`; } + + get statisticsUrl() { + return `${this.apiUrl}/api/v1/statistics`; + } } export * as schemas from './schemas'; -export { - PriceFeed, -}; +export { PriceFeed }; diff --git a/src/services/PriceFeed/schemas/index.ts b/src/services/PriceFeed/schemas/index.ts index d9cd832..2e7c242 100644 --- a/src/services/PriceFeed/schemas/index.ts +++ b/src/services/PriceFeed/schemas/index.ts @@ -1 +1,5 @@ export { default as candlesSchema } from './candlesSchema'; +export { + statisticsOverviewSchema, + topPairsStatisticsSchema, +} from './statisticsSchema'; diff --git a/src/services/PriceFeed/schemas/statisticsSchema.ts b/src/services/PriceFeed/schemas/statisticsSchema.ts new file mode 100644 index 0000000..05b869c --- /dev/null +++ b/src/services/PriceFeed/schemas/statisticsSchema.ts @@ -0,0 +1,21 @@ +import { z } from 'zod'; + +const statisticsOverview = z.object({ + volume24h: z.number(), + volume7d: z.number(), +}); + +export const statisticsOverviewSchema = z.object({ + time: z.number(), + statisticsOverview, +}); + +export const topPairsStatisticsSchema = z.object({ + time: z.number(), + topPairs: z.array( + z.object({ + assetPair: z.string(), + statisticsOverview, + }), + ), +});