1+ import { NOTIFICATION_TYPE } from "../constants" ;
2+ import { OrderBookTop , OrderBookTopRaw , PriceRate , PriceRateRaw , WSCandle , WSCandleRaw , WSOrderBook , WSOrderBookRaw , WSTicker , WSTickerRaw , WSTrade , WSTradeRaw } from "../models" ;
3+
4+ type JsonObject < T > = { [ x : string ] : T } ;
5+ type MapNotificationCallback < T > = ( notification : JsonObject < T > , notificationType : NOTIFICATION_TYPE ) => any ;
6+ type ParseFn < TRaw , T > = ( raw : TRaw ) => T
7+
8+ export const parseMapListInterceptor = < TRaw , T > (
9+ callback : MapNotificationCallback < T [ ] > , parseFn : ParseFn < TRaw , T > ) => (
10+ notification : JsonObject < TRaw [ ] > , notificationType : NOTIFICATION_TYPE ) => {
11+ const parsedEntries = Object . entries ( notification ) . map ( ( [ key , rawValueList ] ) => [ key , rawValueList . map ( parseFn ) ] )
12+ const parsedNotification = Object . fromEntries ( parsedEntries )
13+ return callback ( parsedNotification , notificationType )
14+ }
15+
16+ export const parseMapInterceptor = < TRaw , T > (
17+ callback : MapNotificationCallback < T > , parseFn : ParseFn < TRaw , T > ) => (
18+ notification : JsonObject < TRaw > , notificationType : NOTIFICATION_TYPE ) => {
19+ const parsedEntries = Object . entries ( notification ) . map ( ( [ key , rawValue ] ) => [ key , parseFn ( rawValue ) ] )
20+ const parsedNotification = Object . fromEntries ( parsedEntries )
21+ return callback ( parsedNotification , notificationType )
22+ }
23+
24+
25+ export const parseWSTrade = ( wsTradeRaw : WSTradeRaw ) : WSTrade => {
26+ return {
27+ timestamp : wsTradeRaw . t ,
28+ id : wsTradeRaw . i ,
29+ price : wsTradeRaw . p ,
30+ quantity : wsTradeRaw . q ,
31+ side : wsTradeRaw . s ,
32+ }
33+ }
34+
35+ export const parseWSCandle = ( wsCandleRaw : WSCandleRaw ) : WSCandle => {
36+ return {
37+ timestamp : wsCandleRaw . t ,
38+ openPrice : wsCandleRaw . o ,
39+ closePrice : wsCandleRaw . c ,
40+ highPrice : wsCandleRaw . h ,
41+ lowPrice : wsCandleRaw . l ,
42+ baseVolume : wsCandleRaw . v ,
43+ quoteVolume : wsCandleRaw . q ,
44+ }
45+ }
46+
47+
48+ export const parseWSTicker = ( wsTickerRaw : WSTickerRaw ) : WSTicker => {
49+ return {
50+ timestamp : wsTickerRaw . t ,
51+ bestAsk : wsTickerRaw . a ,
52+ bestAskQuantity : wsTickerRaw . A ,
53+ bestBid : wsTickerRaw . b ,
54+ bestBidQuantity : wsTickerRaw . B ,
55+ closePrice : wsTickerRaw . c ,
56+ openPrice : wsTickerRaw . o ,
57+ highPrice : wsTickerRaw . h ,
58+ lowPrice : wsTickerRaw . l ,
59+ baseVolume : wsTickerRaw . v ,
60+ quoteVolume : wsTickerRaw . q ,
61+ priceChange : wsTickerRaw . p ,
62+ PriceChangePercent : wsTickerRaw . P ,
63+ lastTradeId : wsTickerRaw . L ,
64+ }
65+ }
66+
67+
68+ export const parseWSOrderbook = ( wsOrderbookRaw : WSOrderBookRaw ) : WSOrderBook => {
69+ return {
70+ timestamp : wsOrderbookRaw . t ,
71+ sequence : wsOrderbookRaw . s ,
72+ asks : wsOrderbookRaw . a ,
73+ bids : wsOrderbookRaw . b ,
74+ }
75+ }
76+
77+ export const parseOrderbookTop = ( orderbookTopRaw : OrderBookTopRaw ) : OrderBookTop => {
78+ return {
79+ timestamp : orderbookTopRaw . t ,
80+ bestAsk : orderbookTopRaw . a ,
81+ bestAskQuantity : orderbookTopRaw . A ,
82+ bestBid : orderbookTopRaw . b ,
83+ bestBidQuantity : orderbookTopRaw . B ,
84+ }
85+ }
86+
87+ export const parsePriceRate = ( priceRateRaw : PriceRateRaw ) : PriceRate => {
88+ return {
89+ timestamp : priceRateRaw . t ,
90+ rate : priceRateRaw . r ,
91+ }
92+ }
0 commit comments