Add new methods to PriceFeed class to get statistics

This commit is contained in:
kuduzow
2022-12-01 22:57:58 +03:00
parent f6623f9bec
commit 16b9310e38
3 changed files with 48 additions and 7 deletions

View File

@@ -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 };

View File

@@ -1 +1,5 @@
export { default as candlesSchema } from './candlesSchema';
export {
statisticsOverviewSchema,
topPairsStatisticsSchema,
} from './statisticsSchema';

View File

@@ -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,
}),
),
});