rusty_feeder/exchange/binance/futures/data/
subscription_converter.rs

1//! BinanceSubscriptionConverter - Conversion between standardized options and Binance Futures-specific formats
2//!
3//! This module provides functions to convert from the standardized SubscriptionOptions
4//! structure to Binance Futures-specific subscription formats.
5
6use super::subscription::{
7    WebSocketSubscribeRequest, create_kline_subscription, create_liquidation_subscription,
8    create_mark_price_subscription, create_orderbook_subscription, create_trade_subscription,
9};
10use crate::provider::prelude::*;
11use smallvec::SmallVec;
12use smartstring::alias::String;
13
14/// Convert from standardized SubscriptionOptions to Binance Futures subscription format
15#[inline]
16pub fn options_to_binance_futures_subscription(
17    options: &SubscriptionOptions,
18) -> WebSocketSubscribeRequest {
19    match options.subscription_type {
20        SubscriptionType::Trade => create_trade_subscription(
21            &options.id,
22            options.symbols.clone(),
23            options.request_id,
24            options.api_key.as_deref(),
25            options.test,
26        ),
27        SubscriptionType::OrderBook => create_orderbook_subscription(
28            &options.id,
29            options.symbols.clone(),
30            options.depth.map(|d| d as usize),
31            options.request_id,
32            options.api_key.as_deref(),
33            options.test,
34        ),
35        SubscriptionType::Kline => {
36            if let Some(interval) = &options.interval {
37                create_kline_subscription(
38                    &options.id,
39                    options.symbols.clone(),
40                    interval,
41                    options.request_id,
42                    options.api_key.as_deref(),
43                    options.test,
44                )
45            } else {
46                // Default to 1m if no interval specified
47                create_kline_subscription(
48                    &options.id,
49                    options.symbols.clone(),
50                    "1m",
51                    options.request_id,
52                    options.api_key.as_deref(),
53                    options.test,
54                )
55            }
56        }
57        SubscriptionType::PriceIndex => create_mark_price_subscription(
58            &options.id,
59            options.symbols.clone(),
60            options.request_id,
61            options.api_key.as_deref(),
62            options.test,
63        ),
64        SubscriptionType::LiquidationEvent => create_liquidation_subscription(
65            &options.id,
66            options.symbols.clone(),
67            options.request_id,
68            options.api_key.as_deref(),
69            options.test,
70        ),
71        _ => WebSocketSubscribeRequest {
72            id: options.request_id.unwrap_or(1),
73            method: "SUBSCRIBE".into(),
74            params: SmallVec::<[String; 8]>::new(), // Empty params for unsupported subscription types
75        },
76    }
77}
78
79/// Helper to create stream names with format `<symbol>@<type>`
80#[inline]
81pub fn format_stream_name(symbol: &str, stream_type: &str) -> String {
82    format!("{}@{}", symbol.to_lowercase(), stream_type).into()
83}
84
85/// Helper to create aggregate trade stream name
86#[inline]
87pub fn format_trade_stream(symbol: &str) -> String {
88    format_stream_name(symbol, "aggTrade")
89}
90
91/// Helper to create order book stream name with specific depth
92#[inline]
93pub fn format_orderbook_stream(symbol: &str, depth: Option<usize>) -> String {
94    let symbol = symbol.to_lowercase();
95
96    // Default depth is 10
97    let depth_suffix = match depth {
98        Some(5) => "5",
99        Some(10) => "10",
100        Some(20) => "20",
101        _ => "10",
102    };
103
104    format!("{symbol}@depth{depth_suffix}").into()
105}
106
107/// Helper to create mark price stream name
108#[inline]
109pub fn format_mark_price_stream(symbol: &str) -> String {
110    format!("{}@markPrice@1s", symbol.to_lowercase()).into()
111}
112
113/// Helper to create liquidation order stream name
114#[inline]
115pub fn format_liquidation_stream(symbol: &str) -> String {
116    format!("{}@forceOrder", symbol.to_lowercase()).into()
117}
118
119/// Helper to create kline stream name with interval
120#[inline]
121pub fn format_kline_stream(symbol: &str, interval: &str) -> String {
122    format!("{}@kline_{}", symbol.to_lowercase(), interval).into()
123}