@tradejs/app 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import { buildKlinePath } from '#app/lib/marketRoutes';
2
+ import type { DerivativesInterval } from './derivativesViewModel';
3
+
4
+ export type ChartWindow = {
5
+ startTimestamp: number;
6
+ endTimestamp: number;
7
+ };
8
+
9
+ export const HOURS_OPTIONS = [
10
+ { label: 'Last 24h', value: '24' },
11
+ { label: 'Last 7d', value: '168' },
12
+ { label: 'Last 30d', value: '720' },
13
+ { label: 'Last 60d', value: '1440' },
14
+ { label: 'Last 90d', value: '2160' },
15
+ ];
16
+
17
+ export const INTERVAL_OPTIONS = ['15m', '1h'] as const;
18
+ export const FIXED_SYMBOLS = ['BTCUSDT', 'ETHUSDT'] as const;
19
+
20
+ const DERIVATIVES_TO_KLINE_INTERVAL = {
21
+ '15m': '15',
22
+ '1h': '60',
23
+ } as const;
24
+
25
+ export const buildDerivativesDashboardRequest = ({
26
+ hours,
27
+ selectedInterval,
28
+ now = Date.now(),
29
+ }: {
30
+ hours: string;
31
+ selectedInterval: DerivativesInterval;
32
+ now?: number;
33
+ }) => {
34
+ const from = now - Number(hours) * 60 * 60 * 1000;
35
+ const symbols = FIXED_SYMBOLS.join(',');
36
+ const klineInterval = DERIVATIVES_TO_KLINE_INTERVAL[selectedInterval];
37
+
38
+ return {
39
+ summaryPath: `/api/derivatives/summary?hours=${hours}&limit=200&symbols=${symbols}`,
40
+ chartWindow: { startTimestamp: from, endTimestamp: now },
41
+ details: FIXED_SYMBOLS.map((symbol) => ({
42
+ symbol,
43
+ derivativesPath: `/api/derivatives/${symbol}/${selectedInterval}?from=${from}&to=${now}`,
44
+ pricePath: buildKlinePath({
45
+ provider: 'bybit',
46
+ universe: 'crypto',
47
+ symbol,
48
+ interval: klineInterval,
49
+ }),
50
+ priceBody: { start: from, end: now },
51
+ })),
52
+ };
53
+ };
@@ -0,0 +1,74 @@
1
+ import {
2
+ buildDerivativesDashboardRequest,
3
+ type ChartWindow,
4
+ } from './derivativesDashboardConfig';
5
+ import type {
6
+ DerivativesInterval,
7
+ DetailResponse,
8
+ DetailRow,
9
+ PriceResponse,
10
+ PriceRow,
11
+ SummaryResponse,
12
+ } from './derivativesViewModel';
13
+
14
+ export interface DerivativesDashboardClient {
15
+ get<T>(url: string): Promise<T>;
16
+ post<T>(url: string, body: object): Promise<T>;
17
+ }
18
+
19
+ export interface DerivativesDashboardData {
20
+ summary: SummaryResponse;
21
+ detailsBySymbol: Record<string, DetailRow[]>;
22
+ pricesBySymbol: Record<string, PriceRow[]>;
23
+ chartWindow: ChartWindow;
24
+ }
25
+
26
+ export const loadDerivativesDashboardData = async ({
27
+ hours,
28
+ selectedInterval,
29
+ client,
30
+ now,
31
+ }: {
32
+ hours: string;
33
+ selectedInterval: DerivativesInterval;
34
+ client: DerivativesDashboardClient;
35
+ now?: number;
36
+ }): Promise<DerivativesDashboardData> => {
37
+ const request = buildDerivativesDashboardRequest({
38
+ hours,
39
+ selectedInterval,
40
+ now,
41
+ });
42
+ const [summary, details] = await Promise.all([
43
+ client.get<SummaryResponse>(request.summaryPath),
44
+ Promise.all(
45
+ request.details.map(async (detailRequest) => {
46
+ const [derivativesResponse, priceResponse] = await Promise.all([
47
+ client.get<DetailResponse>(detailRequest.derivativesPath),
48
+ client.post<PriceResponse>(
49
+ detailRequest.pricePath,
50
+ detailRequest.priceBody,
51
+ ),
52
+ ]);
53
+ return [
54
+ detailRequest.symbol,
55
+ {
56
+ detailRows: derivativesResponse.rows,
57
+ priceRows: priceResponse.data ?? [],
58
+ },
59
+ ] as const;
60
+ }),
61
+ ),
62
+ ]);
63
+
64
+ return {
65
+ summary,
66
+ detailsBySymbol: Object.fromEntries(
67
+ details.map(([symbol, payload]) => [symbol, payload.detailRows]),
68
+ ),
69
+ pricesBySymbol: Object.fromEntries(
70
+ details.map(([symbol, payload]) => [symbol, payload.priceRows]),
71
+ ),
72
+ chartWindow: request.chartWindow,
73
+ };
74
+ };