rusty_feeder/exchange/bithumb/data/
subscription_converter.rs

1//! BithumbSubscriptionConverter - Conversion between standardized options and Bithumb-specific formats
2//!
3//! This module provides functions to convert from the standardized SubscriptionOptions
4//! structure to Bithumb-specific subscription formats.
5
6use rusty_common::json::Value;
7use simd_json;
8use smallvec::SmallVec;
9use smartstring::alias::String;
10use uuid::Uuid;
11
12use super::subscription::{BithumbSubscription, ResponseFormat};
13use crate::provider::prelude::*;
14
15/// Convert from standardized SubscriptionOptions to Bithumb subscription format
16#[inline]
17pub fn options_to_bithumb_subscription(options: &SubscriptionOptions) -> Vec<Value> {
18    match options.subscription_type {
19        SubscriptionType::Trade => create_trade_subscription(
20            &options.id,
21            options.symbols.clone(),
22            options.snapshot_only,
23            options.realtime_only,
24            options.format.as_deref() == Some("SIMPLE"),
25        ),
26        SubscriptionType::OrderBook => create_orderbook_subscription(
27            &options.id,
28            options.symbols.clone(),
29            options.depth,
30            options.snapshot_only,
31            options.realtime_only,
32            options.format.as_deref() == Some("SIMPLE"),
33        ),
34        SubscriptionType::Ticker => create_ticker_subscription(
35            &options.id,
36            options.symbols.clone(),
37            options.snapshot_only,
38            options.realtime_only,
39            options.format.as_deref() == Some("SIMPLE"),
40        ),
41        _ => Vec::new(), // Bithumb doesn't support other subscription types natively
42    }
43}
44
45/// Create a trade subscription array for Bithumb WebSocket
46#[inline]
47pub fn create_trade_subscription(
48    ticket: &str,
49    symbols: SmallVec<[String; 8]>,
50    is_only_snapshot: bool,
51    is_only_realtime: bool,
52    use_simple_format: bool,
53) -> Vec<Value> {
54    let mut result = SmallVec::<[Value; 2]>::new();
55
56    // Use provided ticket or generate a new one
57    let _ticket_str = if ticket.is_empty() {
58        String::from(Uuid::new_v4().to_string())
59    } else {
60        ticket.into()
61    };
62
63    // Add format if using simple format
64    if use_simple_format {
65        result.push(simd_json::serde::to_owned_value(ResponseFormat::simple()).unwrap());
66    }
67
68    // Create trade subscription
69    let sub = BithumbSubscription::new(
70        super::subscription::SubscriptionType::Trade,
71        symbols.to_vec(),
72        None,
73        is_only_snapshot,
74        is_only_realtime,
75    );
76
77    result.push(simd_json::serde::to_owned_value(sub).unwrap());
78
79    result.into_vec()
80}
81
82/// Create an orderbook subscription array for Bithumb WebSocket
83#[inline]
84pub fn create_orderbook_subscription(
85    ticket: &str,
86    symbols: SmallVec<[String; 8]>,
87    depth: Option<u32>,
88    is_only_snapshot: bool,
89    is_only_realtime: bool,
90    use_simple_format: bool,
91) -> Vec<Value> {
92    let mut result = SmallVec::<[Value; 2]>::new();
93
94    // Use provided ticket or generate a new one
95    let _ticket_str = if ticket.is_empty() {
96        String::from(Uuid::new_v4().to_string())
97    } else {
98        ticket.into()
99    };
100
101    // Add format if using simple format
102    if use_simple_format {
103        result.push(simd_json::serde::to_owned_value(ResponseFormat::simple()).unwrap());
104    }
105
106    // Convert depth to level (Bithumb uses level parameter for aggregation)
107    let level = depth.map(|d| d as f64);
108
109    // Create orderbook subscription
110    let sub = BithumbSubscription::new(
111        super::subscription::SubscriptionType::Orderbook,
112        symbols.to_vec(),
113        level,
114        is_only_snapshot,
115        is_only_realtime,
116    );
117
118    result.push(simd_json::serde::to_owned_value(sub).unwrap());
119
120    result.into_vec()
121}
122
123/// Create a ticker subscription array for Bithumb WebSocket
124#[inline]
125pub fn create_ticker_subscription(
126    ticket: &str,
127    symbols: SmallVec<[String; 8]>,
128    is_only_snapshot: bool,
129    is_only_realtime: bool,
130    use_simple_format: bool,
131) -> Vec<Value> {
132    let mut result = SmallVec::<[Value; 2]>::new();
133
134    // Use provided ticket or generate a new one
135    let _ticket_str = if ticket.is_empty() {
136        String::from(Uuid::new_v4().to_string())
137    } else {
138        ticket.into()
139    };
140
141    // Add format if using simple format
142    if use_simple_format {
143        result.push(simd_json::serde::to_owned_value(ResponseFormat::simple()).unwrap());
144    }
145
146    // Create ticker subscription
147    let sub = BithumbSubscription::new(
148        super::subscription::SubscriptionType::Ticker,
149        symbols.to_vec(),
150        None,
151        is_only_snapshot,
152        is_only_realtime,
153    );
154
155    result.push(simd_json::serde::to_owned_value(sub).unwrap());
156
157    result.into_vec()
158}