1use std::{
19 future::Future,
20 sync::{
21 Arc,
22 atomic::{AtomicBool, Ordering},
23 },
24 time::Duration,
25};
26
27use ahash::{AHashMap, AHashSet};
28use anyhow::Context;
29use dashmap::DashMap;
30use futures_util::{StreamExt, pin_mut};
31use nautilus_common::{
32 clients::{DataClient, execute_bar_subscriptions, execute_instrument_subscriptions},
33 live::{dst::time::Instant, runner::get_data_event_sender, sender::EventSender},
34 messages::{
35 DataEvent,
36 data::{
37 BarsResponse, BookResponse, DataResponse, FundingRatesResponse, InstrumentResponse,
38 InstrumentsResponse, OptionChainReferencePriceResponse, RequestBars,
39 RequestBookSnapshot, RequestFundingRates, RequestInstrument, RequestInstruments,
40 RequestOptionChainReferencePrice, RequestTrades, SubscribeBars, SubscribeBookDeltas,
41 SubscribeCustomData, SubscribeFundingRates, SubscribeIndexPrices, SubscribeInstrument,
42 SubscribeInstrumentStatus, SubscribeInstruments, SubscribeMarkPrices,
43 SubscribeOptionGreeks, SubscribeQuotes, SubscribeTrades, TradesResponse,
44 UnsubscribeBars, UnsubscribeBookDeltas, UnsubscribeCustomData, UnsubscribeFundingRates,
45 UnsubscribeIndexPrices, UnsubscribeInstrument, UnsubscribeInstrumentStatus,
46 UnsubscribeInstruments, UnsubscribeMarkPrices, UnsubscribeOptionGreeks,
47 UnsubscribeQuotes, UnsubscribeTrades,
48 },
49 },
50};
51use nautilus_core::{
52 AtomicMap, AtomicSet, UnixNanos,
53 datetime::datetime_to_unix_nanos,
54 time::{AtomicTime, get_atomic_clock_realtime},
55};
56use nautilus_live::{
57 SocketControlFactory,
58 task::{TaskGroup, TaskGroupGuard, TaskSpawner},
59};
60use nautilus_model::{
61 data::{BarType, CustomData, Data, DataType, QuoteTick},
62 enums::{BookType, MarketStatusAction},
63 identifiers::{ClientId, InstrumentId, Venue},
64 instruments::{Instrument, InstrumentAny},
65 orderbook::book::OrderBook,
66 types::Price,
67};
68use rust_decimal::Decimal;
69use tokio_util::sync::CancellationToken;
70use ustr::Ustr;
71
72use crate::{
73 book::{
74 BookSequenceOutcome,
75 recovery::{reset_on_reconnect, spawn_subscription_task, start_recovery},
76 sync::BookSyncTracker,
77 },
78 common::{
79 consts::{
80 BYBIT_BOOK_DEPTHS, BYBIT_DEFAULT_ORDERBOOK_DEPTH, BYBIT_OPTION_BOOK_DEPTHS,
81 BYBIT_OPTION_DEFAULT_ORDERBOOK_DEPTH, BYBIT_QUOTE_DEPTH, BYBIT_VENUE,
82 },
83 enums::BybitProductType,
84 instruments::diff_and_emit_instruments,
85 parse::{extract_raw_symbol, make_bybit_symbol},
86 status::{diff_and_emit_statuses, emit_status},
87 },
88 config::BybitDataClientConfig,
89 data_types::register_bybit_custom_data,
90 http::client::BybitHttpClient,
91 websocket::{
92 client::BybitWebSocketClient,
93 messages::{BybitWsMessage, BybitWsOrderbookDepthMsg},
94 parse::{
95 parse_kline_topic, parse_millis_i64, parse_orderbook_deltas, parse_orderbook_quote,
96 parse_orderbook_topic, parse_ticker_linear_funding, parse_ticker_linear_index_price,
97 parse_ticker_linear_mark_price, parse_ticker_option_greeks,
98 parse_ticker_option_index_price, parse_ticker_option_mark_price,
99 parse_ticker_option_quote, parse_ws_kline_bar, parse_ws_liquidation,
100 parse_ws_trade_tick,
101 },
102 },
103};
104
105#[derive(Debug)]
107pub struct BybitDataClient {
108 client_id: ClientId,
109 config: BybitDataClientConfig,
110 http_client: BybitHttpClient,
111 ws_clients: Vec<BybitWebSocketClient>,
112 session_established: AtomicBool,
113 cancellation_token: CancellationToken,
114 session_tasks: TaskGroup,
115 command_tasks: TaskGroup,
116 shutdown_errors: Vec<String>,
117 data_sender: EventSender<DataEvent>,
118 instruments: Arc<AtomicMap<InstrumentId, InstrumentAny>>,
119 book_depths: Arc<AtomicMap<InstrumentId, u32>>,
120 book_sync: BookSyncTracker,
121 quote_subs: Arc<AtomicSet<InstrumentId>>,
122 last_quotes: Arc<DashMap<InstrumentId, QuoteTick>>,
123 ticker_subs: Arc<AtomicMap<InstrumentId, AHashSet<&'static str>>>,
124 trade_subs: Arc<AtomicSet<InstrumentId>>,
125 option_greeks_subs: Arc<AtomicSet<InstrumentId>>,
126 liquidation_subs: Arc<AtomicMap<InstrumentId, Arc<DataType>>>,
127 instrument_status_subs: Arc<AtomicSet<InstrumentId>>,
128 status_cache: Arc<AtomicMap<InstrumentId, MarketStatusAction>>,
129 instrument_subs: Arc<AtomicSet<InstrumentId>>,
130 subscribe_all_instruments: Arc<AtomicBool>,
131 clock: &'static AtomicTime,
132}
133
134impl BybitDataClient {
135 pub fn new(client_id: ClientId, config: BybitDataClientConfig) -> anyhow::Result<Self> {
146 config.validate()?;
147
148 let clock = get_atomic_clock_realtime();
149 let data_sender = get_data_event_sender();
150 let socket_factory = SocketControlFactory::new(client_id, Some(*BYBIT_VENUE));
151 let proxy_url = config
152 .proxy_url
153 .as_ref()
154 .map(|value| value.expose_secret().to_owned());
155
156 let http_client = if let Some((api_key, api_secret)) = config.resolved_api_credentials() {
157 BybitHttpClient::with_credentials(
158 api_key,
159 api_secret,
160 Some(config.http_base_url()),
161 config.http_timeout_secs,
162 config.max_retries,
163 config.retry_delay_initial_ms,
164 config.retry_delay_max_ms,
165 config.recv_window_ms,
166 proxy_url.clone(),
167 )?
168 } else {
169 BybitHttpClient::new(
170 Some(config.http_base_url()),
171 config.http_timeout_secs,
172 config.max_retries,
173 config.retry_delay_initial_ms,
174 config.retry_delay_max_ms,
175 config.recv_window_ms,
176 proxy_url.clone(),
177 )?
178 };
179
180 let product_types = if config.product_types.is_empty() {
182 vec![BybitProductType::Linear]
183 } else {
184 config.product_types.clone()
185 };
186
187 let ws_clients: Vec<BybitWebSocketClient> = product_types
188 .iter()
189 .map(|product_type| {
190 BybitWebSocketClient::new_public_with(
191 *product_type,
192 config.environment,
193 Some(config.ws_public_url_for(*product_type)),
194 config.heartbeat_interval_secs,
195 config.transport_backend,
196 proxy_url.clone(),
197 )
198 .with_socket_control(
199 socket_factory.control(format!("bybit-{}-data-streams", product_type.as_str())),
200 )
201 })
202 .collect();
203
204 let session_tasks = TaskGroup::new();
205 let command_tasks = TaskGroup::new();
206
207 Ok(Self {
208 client_id,
209 config,
210 http_client,
211 ws_clients,
212 session_established: AtomicBool::new(false),
213 cancellation_token: session_tasks.cancellation_token(),
214 session_tasks,
215 command_tasks,
216 shutdown_errors: Vec::new(),
217 data_sender,
218 instruments: Arc::new(AtomicMap::new()),
219 book_depths: Arc::new(AtomicMap::new()),
220 book_sync: BookSyncTracker::default(),
221 quote_subs: Arc::new(AtomicSet::new()),
222 last_quotes: Arc::new(DashMap::new()),
223 ticker_subs: Arc::new(AtomicMap::new()),
224 trade_subs: Arc::new(AtomicSet::new()),
225 option_greeks_subs: Arc::new(AtomicSet::new()),
226 liquidation_subs: Arc::new(AtomicMap::new()),
227 instrument_status_subs: Arc::new(AtomicSet::new()),
228 status_cache: Arc::new(AtomicMap::new()),
229 instrument_subs: Arc::new(AtomicSet::new()),
230 subscribe_all_instruments: Arc::new(AtomicBool::new(false)),
231 clock,
232 })
233 }
234
235 fn venue(&self) -> Venue {
236 *BYBIT_VENUE
237 }
238
239 fn get_ws_client_for_product(
240 &self,
241 product_type: BybitProductType,
242 ) -> Option<&BybitWebSocketClient> {
243 self.ws_clients
244 .iter()
245 .find(|ws| ws.product_type() == Some(product_type))
246 }
247
248 fn get_product_type_for_instrument(
249 &self,
250 instrument_id: InstrumentId,
251 ) -> Option<BybitProductType> {
252 let guard = self.instruments.load();
253 guard
254 .get(&instrument_id)
255 .and_then(|_| BybitProductType::from_suffix(instrument_id.symbol.as_str()))
256 }
257
258 fn spawn_command<F>(&self, future: F)
259 where
260 F: Future<Output = ()> + Send + 'static,
261 {
262 if let Err(e) = self.command_tasks.spawn(future) {
263 log::warn!("Skipping Bybit data command after shutdown began: {e}");
264 }
265 }
266
267 async fn finish_tasks(&self) -> anyhow::Result<()> {
268 let (session_result, command_result) = tokio::join!(
269 self.session_tasks
270 .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
271 self.command_tasks
272 .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
273 );
274 let mut errors = Vec::new();
275 if let Err(e) = session_result {
276 errors.push(format!("failed to finish Bybit data session tasks: {e}"));
277 }
278
279 if let Err(e) = command_result {
280 errors.push(format!("failed to finish Bybit data command tasks: {e}"));
281 }
282
283 if errors.is_empty() {
284 Ok(())
285 } else {
286 anyhow::bail!(errors.join("; "))
287 }
288 }
289
290 async fn prepare_task_groups(&mut self) -> anyhow::Result<()> {
291 if !self.session_tasks.is_open() || !self.command_tasks.is_open() {
292 self.teardown_partial_connect().await?;
293 self.session_tasks
294 .start_generation()
295 .context("failed to start Bybit data session task generation")?;
296 self.command_tasks
297 .start_generation()
298 .context("failed to start Bybit data command task generation")?;
299 self.cancellation_token = self.session_tasks.cancellation_token();
300 }
301 Ok(())
302 }
303
304 fn spawn_ws<F>(&self, fut: F, context: &'static str)
305 where
306 F: Future<Output = anyhow::Result<()>> + Send + 'static,
307 {
308 let future = async move {
309 if let Err(e) = fut.await {
310 log::error!("{context}: {e:?}");
311 }
312 };
313
314 if let Err(e) = self.command_tasks.spawn(future) {
315 log::warn!("Skipping Bybit {context} after shutdown began: {e}");
316 }
317 }
318
319 fn spawn_instrument_polling(
320 &self,
321 product_types: &[BybitProductType],
322 poll_secs: u64,
323 ) -> anyhow::Result<()> {
324 let http = self.http_client.clone();
325 let sender = self.data_sender.clone();
326 let instruments = self.instruments.clone();
327 let status_cache = self.status_cache.clone();
328 let status_subs = self.instrument_status_subs.clone();
329 let instrument_subs = self.instrument_subs.clone();
330 let subscribe_all_instruments = self.subscribe_all_instruments.clone();
331 let cancel = self.cancellation_token.clone();
332 let clock = self.clock;
333 let product_types = product_types.to_vec();
334
335 let future = async move {
336 let mut interval = tokio::time::interval(Duration::from_secs(poll_secs));
337 interval.tick().await; loop {
340 tokio::select! {
341 _ = interval.tick() => {
342 let all_flag = subscribe_all_instruments.load(Ordering::Relaxed);
343 let want_instruments = all_flag || !instrument_subs.is_empty();
344 let want_statuses = !status_subs.is_empty();
345 if !want_instruments && !want_statuses {
346 continue;
347 }
348
349 let mut all_statuses = AHashMap::new();
350
351 if want_instruments {
352 let subs: Option<AHashSet<InstrumentId>> = if all_flag {
353 None
354 } else {
355 Some((**instrument_subs.load()).clone())
356 };
357 let mut inst_cache = (**instruments.load()).clone();
358
359 for &pt in &product_types {
360 match http.request_instruments_with_statuses(pt).await {
361 Ok((fetched, statuses)) => {
362 diff_and_emit_instruments(
363 &fetched, &mut inst_cache, subs.as_ref(), &sender,
364 );
365
366 for (id, action) in statuses {
367 if inst_cache.contains_key(&id) {
368 all_statuses.insert(id, action);
369 }
370 }
371 }
372 Err(e) => {
373 log::warn!("Bybit instrument poll failed for {pt:?}: {e}");
374 }
375 }
376 }
377
378 instruments.store(inst_cache);
379 } else {
380 for &pt in &product_types {
381 match http.request_instrument_statuses(pt).await {
382 Ok(new_statuses) => {
383 let inst_guard = instruments.load();
384 for (id, action) in new_statuses {
385 if inst_guard.contains_key(&id) {
386 all_statuses.insert(id, action);
387 }
388 }
389 }
390 Err(e) => {
391 log::warn!("Bybit instrument status poll failed for {pt:?}: {e}");
392 }
393 }
394 }
395 }
396
397 if want_statuses {
398 let ts = clock.get_time_ns();
399 let mut cache = (**status_cache.load()).clone();
400 let subs_guard = status_subs.load();
401 diff_and_emit_statuses(
402 &all_statuses, &mut cache, Some(&subs_guard), &sender, ts, ts,
403 );
404 status_cache.store(cache);
405 }
406 }
407 () = cancel.cancelled() => {
408 log::debug!("Bybit instrument polling task cancelled");
409 break;
410 }
411 }
412 }
413 };
414
415 self.session_tasks
416 .spawn(future)
417 .context("failed to register Bybit instrument polling task")?;
418 log::debug!("Instrument polling started: interval={poll_secs}s");
419 Ok(())
420 }
421
422 async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {
423 self.session_tasks.begin_shutdown();
424 self.command_tasks.begin_shutdown();
425 for ws_client in &self.ws_clients {
426 ws_client.begin_shutdown();
427 }
428
429 for ws_client in &mut self.ws_clients {
430 if let Err(e) = ws_client.close().await {
431 self.shutdown_errors.push(e.to_string());
432 }
433 }
434
435 if let Err(e) = self.finish_tasks().await {
436 self.shutdown_errors.push(e.to_string());
437 }
438 self.session_established.store(false, Ordering::Release);
439
440 if self.shutdown_errors.is_empty() {
441 Ok(())
442 } else {
443 let errors = std::mem::take(&mut self.shutdown_errors);
444 anyhow::bail!("Bybit data shutdown failed: {}", errors.join("; "))
445 }
446 }
447}
448
449fn send_data(sender: &EventSender<DataEvent>, data: Data) {
450 if let Err(e) = sender.send(DataEvent::Data(data)) {
451 log::error!("Failed to emit data event: {e}");
452 }
453}
454
455fn validate_orderbook_depth(depth: u32, product_type: BybitProductType) -> anyhow::Result<()> {
456 let valid_depths: &[u32] = if product_type == BybitProductType::Option {
457 &BYBIT_OPTION_BOOK_DEPTHS
458 } else {
459 &BYBIT_BOOK_DEPTHS
460 };
461
462 if !valid_depths.contains(&depth) {
463 anyhow::bail!("invalid depth {depth}; valid values are {valid_depths:?}");
464 }
465
466 Ok(())
467}
468
469fn liquidation_instrument_id(data_type: &DataType, venue: Venue) -> anyhow::Result<InstrumentId> {
470 let instrument_id = data_type
471 .instrument_id()?
472 .context("BybitLiquidation requires `instrument_id` metadata")?;
473
474 if instrument_id.venue != venue {
475 anyhow::bail!("BybitLiquidation requires a {venue} instrument, received {instrument_id}");
476 }
477
478 Ok(instrument_id)
479}
480
481fn liquidation_product_type(instrument_id: InstrumentId) -> anyhow::Result<BybitProductType> {
482 match BybitProductType::from_suffix(instrument_id.symbol.as_str()) {
483 Some(product_type @ (BybitProductType::Linear | BybitProductType::Inverse)) => {
484 Ok(product_type)
485 }
486 Some(product_type) => {
487 anyhow::bail!("Liquidations not available for {product_type:?} instruments")
488 }
489 None => anyhow::bail!(
490 "Liquidations require a -LINEAR or -INVERSE instrument, received {instrument_id}"
491 ),
492 }
493}
494
495fn remove_owned_liquidation_sub(
499 subs: &AtomicMap<InstrumentId, Arc<DataType>>,
500 instrument_id: InstrumentId,
501 owned: &Arc<DataType>,
502) -> bool {
503 let mut removed = false;
504
505 subs.rcu(|map| {
506 removed = map
507 .get(&instrument_id)
508 .is_some_and(|current| Arc::ptr_eq(current, owned));
509
510 if removed {
511 map.remove(&instrument_id);
512 }
513 });
514
515 removed
516}
517
518type FundingCacheEntry = (Option<String>, Option<String>, Option<String>);
520
521#[expect(clippy::too_many_arguments)]
522fn handle_ws_message(
523 message: &BybitWsMessage,
524 data_sender: &EventSender<DataEvent>,
525 instruments: &AHashMap<Ustr, InstrumentAny>,
526 product_type: Option<BybitProductType>,
527 trade_subs: &Arc<AtomicSet<InstrumentId>>,
528 ticker_subs: &Arc<AtomicMap<InstrumentId, AHashSet<&'static str>>>,
529 quote_subs: &Arc<AtomicSet<InstrumentId>>,
530 book_depths: &Arc<AtomicMap<InstrumentId, u32>>,
531 book_sync: &BookSyncTracker,
532 ws_client: &BybitWebSocketClient,
533 option_greeks_subs: &Arc<AtomicSet<InstrumentId>>,
534 liquidation_subs: &Arc<AtomicMap<InstrumentId, Arc<DataType>>>,
535 bar_types_cache: &Arc<AtomicMap<String, BarType>>,
536 bars_timestamp_on_close: bool,
537 last_quotes: &DashMap<InstrumentId, QuoteTick>,
538 funding_cache: &mut AHashMap<Ustr, FundingCacheEntry>,
539 snapshot_timeout: Duration,
540 tasks: &TaskSpawner,
541 clock: &AtomicTime,
542) {
543 let ts_init = clock.get_time_ns();
544 let resolve = |raw_symbol: &Ustr| -> Option<&InstrumentAny> {
545 let key = product_type.map_or(*raw_symbol, |pt| make_bybit_symbol(raw_symbol, pt));
546 instruments.get(&key)
547 };
548
549 match message {
550 BybitWsMessage::Orderbook(msg) => {
551 let Some(instrument) = resolve(&msg.data.s) else {
552 log::warn!("Unknown symbol in orderbook update: {}", msg.data.s);
553 return;
554 };
555 let instrument_id = instrument.id();
556
557 let Ok((depth, symbol)) = parse_orderbook_topic(msg.topic.as_str()) else {
558 log::warn!("Invalid orderbook topic: {}", msg.topic);
559 return;
560 };
561
562 if symbol != msg.data.s.as_str() {
563 log::warn!(
564 "Orderbook topic symbol does not match payload: {}",
565 msg.topic
566 );
567 return;
568 }
569
570 if book_depths.load().get(&instrument_id) == Some(&depth) {
571 handle_orderbook_deltas(
572 msg,
573 instrument,
574 data_sender,
575 book_depths,
576 book_sync,
577 ws_client,
578 snapshot_timeout,
579 tasks,
580 ts_init,
581 );
582 }
583
584 if depth == BYBIT_QUOTE_DEPTH && quote_subs.contains(&instrument_id) {
585 match parse_orderbook_quote(msg, instrument, ts_init) {
586 Ok(quote) => {
587 send_data(data_sender, Data::Quote(quote));
588 }
589 Err(e) => log::error!("Failed to parse orderbook quote: {e}"),
590 }
591 }
592 }
593 BybitWsMessage::Trade(msg) => {
594 for trade in &msg.data {
595 let Some(instrument) = resolve(&trade.s) else {
596 continue;
597 };
598 let instrument_id = instrument.id();
599 if !trade_subs.contains(&instrument_id) {
600 continue;
601 }
602
603 match parse_ws_trade_tick(trade, instrument, ts_init) {
604 Ok(tick) => send_data(data_sender, Data::Trade(tick)),
605 Err(e) => log::error!("Failed to parse trade tick: {e}"),
606 }
607 }
608 }
609 BybitWsMessage::Liquidation(msg) => {
610 let subs = liquidation_subs.load();
611
612 for liquidation in &msg.data {
613 let Some(instrument) = resolve(&liquidation.s) else {
614 continue;
615 };
616
617 let Some(data_type) = subs.get(&instrument.id()) else {
619 continue;
620 };
621
622 match parse_ws_liquidation(liquidation, instrument, ts_init) {
623 Ok(liquidation) => {
624 let custom =
625 CustomData::new(Arc::new(liquidation), DataType::clone(data_type));
626 send_data(data_sender, Data::Custom(custom));
627 }
628 Err(e) => log::error!("Failed to parse liquidation: {e}"),
629 }
630 }
631 }
632 BybitWsMessage::Kline(msg) => {
633 let Ok((_, raw_symbol)) = parse_kline_topic(msg.topic.as_str()) else {
634 log::warn!("Invalid kline topic: {}", msg.topic);
635 return;
636 };
637 let ustr_symbol = Ustr::from(raw_symbol);
638 let Some(instrument) = resolve(&ustr_symbol) else {
639 log::warn!("Unknown symbol in kline update: {raw_symbol}");
640 return;
641 };
642 let topic_key = msg.topic.as_str();
643 let Some(bar_type) = bar_types_cache.load().get(topic_key).copied() else {
644 log::debug!("No bar type cached for kline topic: {topic_key}");
645 return;
646 };
647
648 for kline in &msg.data {
649 if !kline.confirm {
650 continue;
651 }
652
653 match parse_ws_kline_bar(
654 kline,
655 instrument,
656 bar_type,
657 bars_timestamp_on_close,
658 ts_init,
659 ) {
660 Ok(bar) => send_data(data_sender, Data::Bar(bar)),
661 Err(e) => log::error!("Failed to parse kline bar: {e}"),
662 }
663 }
664 }
665 BybitWsMessage::TickerLinear(msg) => {
666 let Some(instrument) = resolve(&msg.data.symbol) else {
667 log::warn!("Unknown symbol in ticker update: {}", msg.data.symbol);
668 return;
669 };
670 let instrument_id = instrument.id();
671 let subs = ticker_subs.load();
672 let sub_set = subs.get(&instrument_id);
673
674 let ts_event = match parse_millis_i64(msg.ts, "ticker.ts") {
675 Ok(ts) => ts,
676 Err(e) => {
677 log::error!("Failed to parse ticker timestamp: {e}");
678 return;
679 }
680 };
681
682 if sub_set.is_some_and(|s| s.contains("funding"))
683 && matches!(
684 instrument,
685 InstrumentAny::CryptoPerpetual(_) | InstrumentAny::PerpetualContract(_)
686 )
687 {
688 let cache_entry = funding_cache
689 .entry(msg.data.symbol)
690 .or_insert((None, None, None));
691 let mut changed = false;
692
693 if let Some(rate) = &msg.data.funding_rate
694 && cache_entry.0.as_ref() != Some(rate)
695 {
696 cache_entry.0 = Some(rate.clone());
697 changed = true;
698 }
699
700 if let Some(next_time) = &msg.data.next_funding_time
701 && cache_entry.1.as_ref() != Some(next_time)
702 {
703 cache_entry.1 = Some(next_time.clone());
704 changed = true;
705 }
706
707 if let Some(interval) = &msg.data.funding_interval_hour {
708 cache_entry.2 = Some(interval.clone());
709 }
710
711 if changed && cache_entry.0.is_some() {
712 let mut merged = msg.data.clone();
713
714 if merged.funding_rate.is_none() {
715 merged.funding_rate.clone_from(&cache_entry.0);
716 }
717
718 if merged.next_funding_time.is_none() {
719 merged.next_funding_time.clone_from(&cache_entry.1);
720 }
721
722 if merged.funding_interval_hour.is_none() {
723 merged.funding_interval_hour.clone_from(&cache_entry.2);
724 }
725
726 match parse_ticker_linear_funding(&merged, instrument_id, ts_event, ts_init) {
727 Ok(update) => {
728 if let Err(e) =
729 data_sender.send(DataEvent::Data(Data::FundingRate(update)))
730 {
731 log::error!("Failed to emit funding rate event: {e}");
732 }
733 }
734 Err(e) => log::error!("Failed to parse ticker linear funding: {e}"),
735 }
736 }
737 }
738
739 if sub_set.is_some_and(|s| s.contains("mark_prices")) && msg.data.mark_price.is_some() {
740 match parse_ticker_linear_mark_price(&msg.data, instrument, ts_event, ts_init) {
741 Ok(update) => send_data(data_sender, Data::MarkPrice(update)),
742 Err(e) => log::debug!("Skipping mark price update: {e}"),
743 }
744 }
745
746 if sub_set.is_some_and(|s| s.contains("index_prices")) && msg.data.index_price.is_some()
747 {
748 match parse_ticker_linear_index_price(&msg.data, instrument, ts_event, ts_init) {
749 Ok(update) => send_data(data_sender, Data::IndexPrice(update)),
750 Err(e) => log::debug!("Skipping index price update: {e}"),
751 }
752 }
753 }
754 BybitWsMessage::TickerOption(msg) => {
755 let Some(instrument) = resolve(&msg.data.symbol) else {
756 log::warn!(
757 "Unknown symbol in option ticker update: {}",
758 msg.data.symbol
759 );
760 return;
761 };
762 let instrument_id = instrument.id();
763 let subs = ticker_subs.load();
764 let sub_set = subs.get(&instrument_id);
765
766 if sub_set.is_some_and(|s| s.contains("quotes")) {
767 match parse_ticker_option_quote(msg, instrument, ts_init) {
768 Ok(quote) => {
769 let last = last_quotes.get(&instrument_id).map(|quote| *quote);
770 if last.is_none_or(|q| !q.has_same_bid_ask("e)) {
771 last_quotes.insert(instrument_id, quote);
772 send_data(data_sender, Data::Quote(quote));
773 }
774 }
775 Err(e) => log::error!("Failed to parse ticker option quote: {e}"),
776 }
777 }
778
779 if sub_set.is_some_and(|s| s.contains("mark_prices")) {
780 match parse_ticker_option_mark_price(msg, instrument, ts_init) {
781 Ok(update) => send_data(data_sender, Data::MarkPrice(update)),
782 Err(e) => log::error!("Failed to parse ticker option mark price: {e}"),
783 }
784 }
785
786 if sub_set.is_some_and(|s| s.contains("index_prices")) {
787 match parse_ticker_option_index_price(msg, instrument, ts_init) {
788 Ok(update) => send_data(data_sender, Data::IndexPrice(update)),
789 Err(e) => log::error!("Failed to parse ticker option index price: {e}"),
790 }
791 }
792
793 if option_greeks_subs.contains(&instrument_id) {
794 match parse_ticker_option_greeks(msg, instrument, ts_init) {
795 Ok(greeks) => {
796 if let Err(e) =
797 data_sender.send(DataEvent::Data(Data::OptionGreeks(greeks)))
798 {
799 log::error!("Failed to send option greeks: {e}");
800 }
801 }
802 Err(e) => log::error!("Failed to parse option greeks: {e}"),
803 }
804 }
805 }
806 BybitWsMessage::Reconnected => {
807 reset_option_quotes(last_quotes, product_type);
808 funding_cache.clear();
809 log::info!("WebSocket reconnected, cleared caches");
810
811 if let Some(product_type) = product_type {
812 reset_on_reconnect(
813 product_type,
814 book_depths,
815 book_sync,
816 ws_client,
817 snapshot_timeout,
818 tasks,
819 );
820 }
821 }
822 BybitWsMessage::Error(e) => {
823 log::warn!(
824 "Bybit WebSocket error: code={} message={}",
825 e.code,
826 e.message
827 );
828 }
829 BybitWsMessage::Auth(_)
830 | BybitWsMessage::OrderResponse(_)
831 | BybitWsMessage::OrderNotSent { .. }
832 | BybitWsMessage::AccountOrder(_)
833 | BybitWsMessage::AccountExecution(_)
834 | BybitWsMessage::AccountExecutionFast(_)
835 | BybitWsMessage::AccountWallet(_)
836 | BybitWsMessage::AccountPosition(_) => {}
837 }
838}
839
840#[expect(clippy::too_many_arguments)]
842fn handle_orderbook_deltas(
843 msg: &BybitWsOrderbookDepthMsg,
844 instrument: &InstrumentAny,
845 data_sender: &EventSender<DataEvent>,
846 book_depths: &AtomicMap<InstrumentId, u32>,
847 book_sync: &BookSyncTracker,
848 ws_client: &BybitWebSocketClient,
849 snapshot_timeout: Duration,
850 tasks: &TaskSpawner,
851 ts_init: UnixNanos,
852) {
853 let instrument_id = instrument.id();
854 let is_snapshot = msg.msg_type.eq_ignore_ascii_case("snapshot");
855
856 let outcome = match u64::try_from(msg.data.u) {
857 Ok(update_id) => book_sync.validate(instrument_id, is_snapshot, update_id, Instant::now()),
858 Err(e) => {
859 log::error!(
860 "Invalid orderbook update ID {} for {instrument_id}: {e}",
861 msg.data.u
862 );
863 BookSequenceOutcome::Recover
864 }
865 };
866
867 match outcome {
868 BookSequenceOutcome::Accept => {}
869 BookSequenceOutcome::Suppress => return,
870 BookSequenceOutcome::Recover => {
871 start_recovery(
872 instrument_id,
873 book_depths,
874 book_sync,
875 ws_client,
876 snapshot_timeout,
877 tasks,
878 );
879 return;
880 }
881 }
882
883 if !is_snapshot && msg.data.b.is_empty() && msg.data.a.is_empty() {
884 return;
885 }
886
887 match parse_orderbook_deltas(msg, instrument, ts_init) {
888 Ok(deltas) => send_data(data_sender, Data::BookDeltas(Box::new(deltas))),
889 Err(e) => {
890 log::error!("Failed to parse orderbook deltas for {instrument_id}: {e}");
891 start_recovery(
892 instrument_id,
893 book_depths,
894 book_sync,
895 ws_client,
896 snapshot_timeout,
897 tasks,
898 );
899 }
900 }
901}
902
903fn upsert_instrument(
904 cache: &Arc<AtomicMap<InstrumentId, InstrumentAny>>,
905 instrument: InstrumentAny,
906) {
907 cache.insert(instrument.id(), instrument);
908}
909
910fn reset_option_quotes(
911 last_quotes: &DashMap<InstrumentId, QuoteTick>,
912 product_type: Option<BybitProductType>,
913) {
914 if product_type == Some(BybitProductType::Option) {
915 last_quotes.clear();
916 }
917}
918
919#[async_trait::async_trait(?Send)]
920impl DataClient for BybitDataClient {
921 fn client_id(&self) -> ClientId {
922 self.client_id
923 }
924
925 fn venue(&self) -> Option<Venue> {
926 Some(self.venue())
927 }
928
929 fn start(&mut self) -> anyhow::Result<()> {
930 log::info!(
931 "Started: client_id={}, product_types={:?}, environment={:?}, proxy_url={:?}",
932 self.client_id,
933 self.config.product_types,
934 self.config.environment,
935 self.config.proxy_url,
936 );
937 Ok(())
938 }
939
940 fn stop(&mut self) -> anyhow::Result<()> {
941 log::info!("Stopping {id}", id = self.client_id);
942 self.session_tasks.begin_shutdown();
943 self.command_tasks.begin_shutdown();
944 for ws_client in &self.ws_clients {
945 ws_client.begin_shutdown();
946 }
947 self.session_established.store(false, Ordering::Relaxed);
948 Ok(())
949 }
950
951 fn reset(&mut self) -> anyhow::Result<()> {
952 log::debug!("Resetting {id}", id = self.client_id);
953 self.session_tasks.begin_shutdown();
954 self.command_tasks.begin_shutdown();
955 for ws_client in &self.ws_clients {
956 ws_client.begin_shutdown();
957 }
958 self.session_established.store(false, Ordering::Relaxed);
959 self.book_depths.store(AHashMap::new());
960 self.book_sync.clear();
961 self.quote_subs.store(AHashSet::new());
962 self.last_quotes.clear();
963 self.ticker_subs.store(AHashMap::new());
964 self.option_greeks_subs.store(AHashSet::new());
965 self.liquidation_subs.store(AHashMap::new());
966 self.instrument_status_subs.store(AHashSet::new());
967 self.status_cache.store(AHashMap::new());
968 self.instrument_subs.store(AHashSet::new());
969 self.subscribe_all_instruments
970 .store(false, Ordering::Relaxed);
971 Ok(())
972 }
973
974 fn dispose(&mut self) -> anyhow::Result<()> {
975 log::debug!("Disposing {id}", id = self.client_id);
976 self.stop()
977 }
978
979 async fn connect(&mut self) -> anyhow::Result<()> {
980 if self.session_established.load(Ordering::Relaxed)
981 && self.session_tasks.is_open()
982 && self.command_tasks.is_open()
983 {
984 return Ok(());
985 }
986
987 register_bybit_custom_data();
988
989 self.prepare_task_groups().await?;
990 self.last_quotes.clear();
991 let ws_clients = self.ws_clients.clone();
992 let setup_guard =
993 TaskGroupGuard::new(&[&self.session_tasks, &self.command_tasks], move || {
994 for ws_client in ws_clients {
995 ws_client.begin_shutdown();
996 }
997 });
998
999 let product_types = if self.config.product_types.is_empty() {
1000 vec![BybitProductType::Linear]
1001 } else {
1002 self.config.product_types.clone()
1003 };
1004
1005 let mut all_instruments = Vec::new();
1006
1007 for product_type in &product_types {
1008 let fetched = self
1009 .http_client
1010 .request_instruments(*product_type, None, None)
1011 .await
1012 .with_context(|| {
1013 format!("failed to request Bybit instruments for {product_type:?}")
1014 })?;
1015
1016 self.http_client.cache_instruments(&fetched);
1017
1018 self.instruments.rcu(|m| {
1019 for instrument in &fetched {
1020 m.insert(instrument.id(), instrument.clone());
1021 }
1022 });
1023
1024 all_instruments.extend(fetched);
1025 }
1026
1027 if self
1029 .config
1030 .instrument_poll_interval_secs
1031 .is_some_and(|s| s > 0)
1032 {
1033 let mut collected_statuses = Vec::new();
1035
1036 for product_type in &product_types {
1037 match self
1038 .http_client
1039 .request_instrument_statuses(*product_type)
1040 .await
1041 {
1042 Ok(statuses) => collected_statuses.push(statuses),
1043 Err(e) => {
1044 log::warn!(
1045 "Failed to seed instrument status cache for {product_type:?}: {e}"
1046 );
1047 }
1048 }
1049 }
1050
1051 let inst_guard = self.instruments.load();
1052 let mut status_map = AHashMap::new();
1053
1054 for statuses in collected_statuses {
1055 for (id, action) in statuses {
1056 if inst_guard.contains_key(&id) {
1057 status_map.insert(id, action);
1058 }
1059 }
1060 }
1061 log::debug!(
1062 "Seeded instrument status cache with {} entries",
1063 status_map.len()
1064 );
1065 self.status_cache.store(status_map);
1066 }
1067
1068 for instrument in all_instruments {
1069 if let Err(e) = self.data_sender.send(DataEvent::Data(instrument.into())) {
1070 log::warn!("Failed to send instrument: {e}");
1071 }
1072 }
1073
1074 let instruments_by_symbol: Arc<AHashMap<Ustr, InstrumentAny>> = {
1076 let guard = self.instruments.load();
1077 let mut map = AHashMap::new();
1078 for instrument in guard.values() {
1079 map.insert(instrument.id().symbol.inner(), instrument.clone());
1080 }
1081 Arc::new(map)
1082 };
1083
1084 let session_result = async {
1085 for ws_client in &mut self.ws_clients {
1086 ws_client
1087 .connect()
1088 .await
1089 .context("failed to connect Bybit WebSocket")?;
1090 ws_client
1091 .wait_until_active(10.0)
1092 .await
1093 .context("WebSocket did not become active")?;
1094
1095 let stream = ws_client.stream();
1096 let product_type = ws_client.product_type();
1097 let sender = self.data_sender.clone();
1098 let trade_subs = self.trade_subs.clone();
1099 let ticker_subs = self.ticker_subs.clone();
1100 let quote_subs = self.quote_subs.clone();
1101 let last_quotes = self.last_quotes.clone();
1102 let book_depths = self.book_depths.clone();
1103 let book_sync = self.book_sync.clone();
1104 let snapshot_timeout = Duration::from_secs(self.config.book_snapshot_timeout_secs);
1105 let tasks = self
1106 .command_tasks
1107 .spawner()
1108 .context("Bybit data command task admission is closed")?;
1109 let option_greeks_subs = self.option_greeks_subs.clone();
1110 let liquidation_subs = self.liquidation_subs.clone();
1111 let bar_types_cache = ws_client.bar_types_cache().clone();
1112 let bars_timestamp_on_close = self.config.bars_timestamp_on_close;
1113 let instruments = Arc::clone(&instruments_by_symbol);
1114 let clock = self.clock;
1115 let cancel = self.cancellation_token.clone();
1116 let ws_client = ws_client.clone();
1117
1118 let future = async move {
1119 let mut funding_cache: AHashMap<Ustr, FundingCacheEntry> = AHashMap::new();
1120
1121 pin_mut!(stream);
1122
1123 loop {
1124 tokio::select! {
1125 Some(message) = stream.next() => {
1126 handle_ws_message(
1127 &message,
1128 &sender,
1129 &instruments,
1130 product_type,
1131 &trade_subs,
1132 &ticker_subs,
1133 "e_subs,
1134 &book_depths,
1135 &book_sync,
1136 &ws_client,
1137 &option_greeks_subs,
1138 &liquidation_subs,
1139 &bar_types_cache,
1140 bars_timestamp_on_close,
1141 &last_quotes,
1142 &mut funding_cache,
1143 snapshot_timeout,
1144 &tasks,
1145 clock,
1146 );
1147 }
1148 () = cancel.cancelled() => {
1149 log::debug!("WebSocket stream task cancelled");
1150 break;
1151 }
1152 }
1153 }
1154 };
1155 self.session_tasks
1156 .spawn(future)
1157 .context("failed to register Bybit WebSocket stream task")?;
1158 }
1159
1160 if let Some(poll_secs) = self.config.instrument_poll_interval_secs
1161 && poll_secs > 0
1162 {
1163 self.spawn_instrument_polling(&product_types, poll_secs)?;
1164 }
1165
1166 Ok::<(), anyhow::Error>(())
1167 }
1168 .await;
1169
1170 if let Err(e) = session_result {
1171 if let Err(teardown_error) = self.teardown_partial_connect().await {
1172 return Err(e.context(format!(
1173 "Bybit data startup teardown failed: {teardown_error}"
1174 )));
1175 }
1176 return Err(e);
1177 }
1178
1179 setup_guard.disarm();
1180 self.session_established.store(true, Ordering::Release);
1181 log::info!("Connected: client_id={}", self.client_id);
1182 Ok(())
1183 }
1184
1185 async fn disconnect(&mut self) -> anyhow::Result<()> {
1186 self.session_tasks.begin_shutdown();
1187 self.command_tasks.begin_shutdown();
1188 for ws_client in &self.ws_clients {
1189 ws_client.begin_shutdown();
1190 }
1191
1192 for ws_client in &mut self.ws_clients {
1193 if let Err(e) = ws_client.close().await {
1194 self.shutdown_errors.push(e.to_string());
1195 }
1196 }
1197
1198 tokio::time::sleep(Duration::from_millis(500)).await;
1200
1201 if let Err(e) = self.finish_tasks().await {
1202 self.shutdown_errors.push(e.to_string());
1203 }
1204
1205 self.book_depths.store(AHashMap::new());
1206 self.book_sync.clear();
1207 self.quote_subs.store(AHashSet::new());
1208 self.last_quotes.clear();
1209 self.ticker_subs.store(AHashMap::new());
1210 self.trade_subs.store(AHashSet::new());
1211 self.option_greeks_subs.store(AHashSet::new());
1212 self.liquidation_subs.store(AHashMap::new());
1213 self.instrument_status_subs.store(AHashSet::new());
1214 self.status_cache.store(AHashMap::new());
1215 self.instrument_subs.store(AHashSet::new());
1216 self.subscribe_all_instruments
1217 .store(false, Ordering::Relaxed);
1218 self.session_established.store(false, Ordering::Release);
1219 log::info!("Disconnected: client_id={}", self.client_id);
1220
1221 if self.shutdown_errors.is_empty() {
1222 Ok(())
1223 } else {
1224 let errors = std::mem::take(&mut self.shutdown_errors);
1225 anyhow::bail!("Bybit data shutdown failed: {}", errors.join("; "))
1226 }
1227 }
1228
1229 fn is_connected(&self) -> bool {
1230 self.session_established.load(Ordering::Relaxed)
1231 && self.ws_clients.iter().all(BybitWebSocketClient::is_active)
1232 }
1233
1234 fn is_disconnected(&self) -> bool {
1235 !self.is_connected()
1236 }
1237
1238 fn subscribe(&mut self, cmd: SubscribeCustomData) -> anyhow::Result<()> {
1239 let data_type = cmd.data_type.type_name();
1240 if data_type != "BybitLiquidation" {
1241 log::warn!("Unsupported custom data subscription: {data_type}");
1242 return Ok(());
1243 }
1244
1245 let instrument_id = liquidation_instrument_id(&cmd.data_type, self.venue())?;
1246 let product_type = liquidation_product_type(instrument_id)?;
1247
1248 let ws = self
1249 .get_ws_client_for_product(product_type)
1250 .context("no WebSocket client for product type")?
1251 .clone();
1252
1253 if let Some(subscribed) = self.liquidation_subs.get_cloned(&instrument_id) {
1254 anyhow::ensure!(
1255 *subscribed == cmd.data_type,
1256 "Already subscribed to liquidations for {instrument_id} as {subscribed}"
1257 );
1258 return Ok(());
1259 }
1260
1261 let subscription = Arc::new(cmd.data_type);
1262 self.liquidation_subs
1263 .insert(instrument_id, Arc::clone(&subscription));
1264 let liquidation_subs = Arc::clone(&self.liquidation_subs);
1265
1266 self.spawn_ws(
1267 async move {
1268 if let Err(e) = ws.subscribe_liquidations(instrument_id).await {
1269 if remove_owned_liquidation_sub(&liquidation_subs, instrument_id, &subscription)
1271 && let Err(e) = ws.unsubscribe_liquidations(instrument_id).await
1272 {
1273 log::warn!(
1274 "Failed to unsubscribe after liquidation subscription error: {e}"
1275 );
1276 }
1277
1278 return Err(e).context("liquidations subscription");
1279 }
1280
1281 Ok(())
1282 },
1283 "liquidation subscription",
1284 );
1285
1286 Ok(())
1287 }
1288
1289 fn subscribe_book_deltas(&mut self, cmd: SubscribeBookDeltas) -> anyhow::Result<()> {
1290 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1291 let cmd = cmd.with_instrument_ids(instrument_id);
1292 if cmd.book_type != BookType::L2_MBP {
1293 anyhow::bail!("Bybit only supports L2_MBP order book deltas");
1294 }
1295
1296 let product_type = self
1297 .get_product_type_for_instrument(instrument_id)
1298 .unwrap_or(BybitProductType::Linear);
1299
1300 let default_depth = if product_type == BybitProductType::Option {
1301 BYBIT_OPTION_DEFAULT_ORDERBOOK_DEPTH
1302 } else {
1303 BYBIT_DEFAULT_ORDERBOOK_DEPTH
1304 };
1305
1306 let depth = cmd.depth.map_or(default_depth, |d| d.get() as u32);
1307
1308 validate_orderbook_depth(depth, product_type)?;
1309
1310 let ws_client = self
1311 .get_ws_client_for_product(product_type)
1312 .context("no WebSocket client for product type")?
1313 .clone();
1314
1315 if let Some(subscribed_depth) = self.book_depths.load().get(&instrument_id) {
1316 anyhow::ensure!(
1317 *subscribed_depth == depth,
1318 "Already subscribed to book depth {subscribed_depth} for {instrument_id}"
1319 );
1320 return Ok(());
1321 }
1322
1323 let tasks = match self.command_tasks.spawner() {
1324 Ok(tasks) => tasks,
1325 Err(e) => {
1326 log::warn!("Skipping Bybit data command after shutdown began: {e}");
1327 return Ok(());
1328 }
1329 };
1330
1331 spawn_subscription_task(
1332 instrument_id,
1333 depth,
1334 Arc::clone(&self.book_depths),
1335 self.book_sync.clone(),
1336 ws_client,
1337 Duration::from_secs(self.config.book_snapshot_timeout_secs),
1338 &tasks,
1339 );
1340
1341 Ok(())
1342 })
1343 }
1344
1345 fn subscribe_quotes(&mut self, cmd: SubscribeQuotes) -> anyhow::Result<()> {
1346 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1347 self.subscribe_quotes_member(instrument_id)
1348 })
1349 }
1350
1351 fn subscribe_trades(&mut self, cmd: SubscribeTrades) -> anyhow::Result<()> {
1352 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1353 let product_type = self
1354 .get_product_type_for_instrument(instrument_id)
1355 .unwrap_or(BybitProductType::Linear);
1356
1357 self.trade_subs.insert(instrument_id);
1358
1359 let ws = self
1360 .get_ws_client_for_product(product_type)
1361 .context("no WebSocket client for product type")?
1362 .clone();
1363
1364 self.spawn_ws(
1365 async move {
1366 ws.subscribe_trades(instrument_id)
1367 .await
1368 .context("trades subscription")
1369 },
1370 "trade subscription",
1371 );
1372
1373 Ok(())
1374 })
1375 }
1376
1377 fn subscribe_funding_rates(&mut self, cmd: SubscribeFundingRates) -> anyhow::Result<()> {
1378 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1379 let product_type = self
1380 .get_product_type_for_instrument(instrument_id)
1381 .unwrap_or(BybitProductType::Linear);
1382
1383 if product_type == BybitProductType::Spot || product_type == BybitProductType::Option {
1384 anyhow::bail!("Funding rates not available for {product_type:?} instruments");
1385 }
1386
1387 let guard = self.instruments.load();
1388 if let Some(instrument) = guard.get(&instrument_id)
1389 && !matches!(
1390 instrument,
1391 InstrumentAny::CryptoPerpetual(_) | InstrumentAny::PerpetualContract(_)
1392 )
1393 {
1394 anyhow::bail!("Funding rates only available for perpetuals, not {instrument_id}");
1395 }
1396
1397 let mut should_subscribe = false;
1398 self.ticker_subs.rcu(|m| {
1399 let entry = m.entry(instrument_id).or_default();
1400 should_subscribe = entry.is_empty();
1401 entry.insert("funding");
1402 });
1403
1404 if should_subscribe {
1405 let ws = self
1406 .get_ws_client_for_product(product_type)
1407 .context("no WebSocket client for product type")?
1408 .clone();
1409
1410 self.spawn_ws(
1411 async move {
1412 ws.subscribe_ticker(instrument_id)
1413 .await
1414 .context("ticker subscription for funding rates")
1415 },
1416 "funding rate subscription",
1417 );
1418 }
1419
1420 Ok(())
1421 })
1422 }
1423
1424 fn subscribe_mark_prices(&mut self, cmd: SubscribeMarkPrices) -> anyhow::Result<()> {
1425 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1426 let product_type = self
1427 .get_product_type_for_instrument(instrument_id)
1428 .unwrap_or(BybitProductType::Linear);
1429
1430 if product_type == BybitProductType::Spot {
1431 anyhow::bail!("Mark prices not available for Spot instruments");
1432 }
1433
1434 let mut should_subscribe = false;
1435 self.ticker_subs.rcu(|m| {
1436 let entry = m.entry(instrument_id).or_default();
1437 should_subscribe = entry.is_empty();
1438 entry.insert("mark_prices");
1439 });
1440
1441 if should_subscribe {
1442 let ws = self
1443 .get_ws_client_for_product(product_type)
1444 .context("no WebSocket client for product type")?
1445 .clone();
1446
1447 self.spawn_ws(
1448 async move {
1449 ws.subscribe_ticker(instrument_id)
1450 .await
1451 .context("ticker subscription for mark prices")
1452 },
1453 "mark price subscription",
1454 );
1455 }
1456
1457 Ok(())
1458 })
1459 }
1460
1461 fn subscribe_index_prices(&mut self, cmd: SubscribeIndexPrices) -> anyhow::Result<()> {
1462 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1463 let product_type = self
1464 .get_product_type_for_instrument(instrument_id)
1465 .unwrap_or(BybitProductType::Linear);
1466
1467 if product_type == BybitProductType::Spot {
1468 anyhow::bail!("Index prices not available for Spot instruments");
1469 }
1470
1471 let mut should_subscribe = false;
1472 self.ticker_subs.rcu(|m| {
1473 let entry = m.entry(instrument_id).or_default();
1474 should_subscribe = entry.is_empty();
1475 entry.insert("index_prices");
1476 });
1477
1478 if should_subscribe {
1479 let ws = self
1480 .get_ws_client_for_product(product_type)
1481 .context("no WebSocket client for product type")?
1482 .clone();
1483
1484 self.spawn_ws(
1485 async move {
1486 ws.subscribe_ticker(instrument_id)
1487 .await
1488 .context("ticker subscription for index prices")
1489 },
1490 "index price subscription",
1491 );
1492 }
1493
1494 Ok(())
1495 })
1496 }
1497
1498 fn subscribe_bars(&mut self, cmd: SubscribeBars) -> anyhow::Result<()> {
1499 execute_bar_subscriptions(&cmd.bar_types, |bar_type| {
1500 let instrument_id = bar_type.instrument_id();
1501 let product_type = self
1502 .get_product_type_for_instrument(instrument_id)
1503 .unwrap_or(BybitProductType::Linear);
1504
1505 if product_type == BybitProductType::Option {
1506 anyhow::bail!("Bybit does not support kline/bar data for options");
1507 }
1508
1509 let ws = self
1510 .get_ws_client_for_product(product_type)
1511 .context("no WebSocket client for product type")?
1512 .clone();
1513
1514 self.spawn_ws(
1515 async move {
1516 ws.subscribe_bars(bar_type)
1517 .await
1518 .context("bars subscription")
1519 },
1520 "bar subscription",
1521 );
1522
1523 Ok(())
1524 })
1525 }
1526
1527 fn unsubscribe(&mut self, cmd: &UnsubscribeCustomData) -> anyhow::Result<()> {
1528 let data_type = cmd.data_type.type_name();
1529 if data_type != "BybitLiquidation" {
1530 log::warn!("Unsupported custom data unsubscription: {data_type}");
1531 return Ok(());
1532 }
1533
1534 let instrument_id = liquidation_instrument_id(&cmd.data_type, self.venue())?;
1535
1536 let Some(subscription) = self
1537 .liquidation_subs
1538 .get_cloned(&instrument_id)
1539 .filter(|subscribed| **subscribed == cmd.data_type)
1540 else {
1541 return Ok(());
1542 };
1543
1544 let product_type = liquidation_product_type(instrument_id)?;
1545
1546 let ws = self
1547 .get_ws_client_for_product(product_type)
1548 .context("no WebSocket client for product type")?
1549 .clone();
1550
1551 if !remove_owned_liquidation_sub(&self.liquidation_subs, instrument_id, &subscription) {
1553 return Ok(());
1554 }
1555
1556 self.spawn_ws(
1557 async move {
1558 ws.unsubscribe_liquidations(instrument_id)
1559 .await
1560 .context("liquidations unsubscribe")
1561 },
1562 "liquidation unsubscribe",
1563 );
1564
1565 Ok(())
1566 }
1567
1568 fn unsubscribe_book_deltas(&mut self, cmd: &UnsubscribeBookDeltas) -> anyhow::Result<()> {
1569 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1570 let Some(depth) = self.book_depths.load().get(&instrument_id).copied() else {
1571 return Ok(());
1572 };
1573
1574 self.book_depths.remove(&instrument_id);
1575 self.book_sync.remove(instrument_id);
1576
1577 let product_type = self
1578 .get_product_type_for_instrument(instrument_id)
1579 .unwrap_or(BybitProductType::Linear);
1580
1581 let ws = self
1582 .get_ws_client_for_product(product_type)
1583 .context("no WebSocket client for product type")?
1584 .clone();
1585
1586 self.spawn_ws(
1587 async move {
1588 ws.unsubscribe_orderbook(instrument_id, depth)
1589 .await
1590 .context("orderbook unsubscribe")
1591 },
1592 "order book unsubscribe",
1593 );
1594
1595 Ok(())
1596 })
1597 }
1598
1599 fn unsubscribe_quotes(&mut self, cmd: &UnsubscribeQuotes) -> anyhow::Result<()> {
1600 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1601 self.unsubscribe_quotes_member(instrument_id)
1602 })
1603 }
1604
1605 fn unsubscribe_trades(&mut self, cmd: &UnsubscribeTrades) -> anyhow::Result<()> {
1606 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1607 let product_type = self
1608 .get_product_type_for_instrument(instrument_id)
1609 .unwrap_or(BybitProductType::Linear);
1610
1611 self.trade_subs.remove(&instrument_id);
1612
1613 let ws = self
1614 .get_ws_client_for_product(product_type)
1615 .context("no WebSocket client for product type")?
1616 .clone();
1617
1618 self.spawn_ws(
1619 async move {
1620 ws.unsubscribe_trades(instrument_id)
1621 .await
1622 .context("trades unsubscribe")
1623 },
1624 "trade unsubscribe",
1625 );
1626
1627 Ok(())
1628 })
1629 }
1630
1631 fn unsubscribe_funding_rates(&mut self, cmd: &UnsubscribeFundingRates) -> anyhow::Result<()> {
1632 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1633 let product_type = self
1634 .get_product_type_for_instrument(instrument_id)
1635 .unwrap_or(BybitProductType::Linear);
1636
1637 let mut should_unsubscribe = false;
1638 self.ticker_subs.rcu(|m| {
1639 if let Some(entry) = m.get_mut(&instrument_id) {
1640 entry.remove("funding");
1641 if entry.is_empty() {
1642 m.remove(&instrument_id);
1643 should_unsubscribe = true;
1644 } else {
1645 should_unsubscribe = false;
1646 }
1647 } else {
1648 should_unsubscribe = false;
1649 }
1650 });
1651
1652 if should_unsubscribe {
1653 let ws = self
1654 .get_ws_client_for_product(product_type)
1655 .context("no WebSocket client for product type")?
1656 .clone();
1657
1658 self.spawn_ws(
1659 async move {
1660 ws.unsubscribe_ticker(instrument_id)
1661 .await
1662 .context("ticker unsubscribe for funding rates")
1663 },
1664 "funding rate unsubscribe",
1665 );
1666 }
1667
1668 Ok(())
1669 })
1670 }
1671
1672 fn unsubscribe_mark_prices(&mut self, cmd: &UnsubscribeMarkPrices) -> anyhow::Result<()> {
1673 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1674 let product_type = self
1675 .get_product_type_for_instrument(instrument_id)
1676 .unwrap_or(BybitProductType::Linear);
1677
1678 let mut should_unsubscribe = false;
1679 self.ticker_subs.rcu(|m| {
1680 if let Some(entry) = m.get_mut(&instrument_id) {
1681 entry.remove("mark_prices");
1682 if entry.is_empty() {
1683 m.remove(&instrument_id);
1684 should_unsubscribe = true;
1685 } else {
1686 should_unsubscribe = false;
1687 }
1688 } else {
1689 should_unsubscribe = false;
1690 }
1691 });
1692
1693 if should_unsubscribe {
1694 let ws = self
1695 .get_ws_client_for_product(product_type)
1696 .context("no WebSocket client for product type")?
1697 .clone();
1698
1699 self.spawn_ws(
1700 async move {
1701 ws.unsubscribe_ticker(instrument_id)
1702 .await
1703 .context("ticker unsubscribe for mark prices")
1704 },
1705 "mark price unsubscribe",
1706 );
1707 }
1708
1709 Ok(())
1710 })
1711 }
1712
1713 fn unsubscribe_index_prices(&mut self, cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> {
1714 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1715 let product_type = self
1716 .get_product_type_for_instrument(instrument_id)
1717 .unwrap_or(BybitProductType::Linear);
1718
1719 let mut should_unsubscribe = false;
1720 self.ticker_subs.rcu(|m| {
1721 if let Some(entry) = m.get_mut(&instrument_id) {
1722 entry.remove("index_prices");
1723 if entry.is_empty() {
1724 m.remove(&instrument_id);
1725 should_unsubscribe = true;
1726 } else {
1727 should_unsubscribe = false;
1728 }
1729 } else {
1730 should_unsubscribe = false;
1731 }
1732 });
1733
1734 if should_unsubscribe {
1735 let ws = self
1736 .get_ws_client_for_product(product_type)
1737 .context("no WebSocket client for product type")?
1738 .clone();
1739
1740 self.spawn_ws(
1741 async move {
1742 ws.unsubscribe_ticker(instrument_id)
1743 .await
1744 .context("ticker unsubscribe for index prices")
1745 },
1746 "index price unsubscribe",
1747 );
1748 }
1749
1750 Ok(())
1751 })
1752 }
1753
1754 fn unsubscribe_bars(&mut self, cmd: &UnsubscribeBars) -> anyhow::Result<()> {
1755 execute_bar_subscriptions(&cmd.bar_types, |bar_type| {
1756 let instrument_id = bar_type.instrument_id();
1757 let product_type = self
1758 .get_product_type_for_instrument(instrument_id)
1759 .unwrap_or(BybitProductType::Linear);
1760
1761 let ws = self
1762 .get_ws_client_for_product(product_type)
1763 .context("no WebSocket client for product type")?
1764 .clone();
1765
1766 self.spawn_ws(
1767 async move {
1768 ws.unsubscribe_bars(bar_type)
1769 .await
1770 .context("bars unsubscribe")
1771 },
1772 "bar unsubscribe",
1773 );
1774
1775 Ok(())
1776 })
1777 }
1778
1779 fn subscribe_option_greeks(&mut self, cmd: SubscribeOptionGreeks) -> anyhow::Result<()> {
1780 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1781 self.option_greeks_subs.insert(instrument_id);
1782
1783 let mut should_subscribe = false;
1784 self.ticker_subs.rcu(|m| {
1785 let entry = m.entry(instrument_id).or_default();
1786 should_subscribe = entry.is_empty();
1787 entry.insert("option_greeks");
1788 });
1789
1790 if should_subscribe {
1791 let product_type = self
1792 .get_product_type_for_instrument(instrument_id)
1793 .unwrap_or(BybitProductType::Option);
1794
1795 let ws = self
1796 .get_ws_client_for_product(product_type)
1797 .context("no WebSocket client for product type")?
1798 .clone();
1799
1800 self.spawn_ws(
1801 async move {
1802 ws.subscribe_ticker(instrument_id)
1803 .await
1804 .context("ticker subscription for option greeks")
1805 },
1806 "option greeks subscription",
1807 );
1808 }
1809
1810 Ok(())
1811 })
1812 }
1813
1814 fn unsubscribe_option_greeks(&mut self, cmd: &UnsubscribeOptionGreeks) -> anyhow::Result<()> {
1815 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1816 self.option_greeks_subs.remove(&instrument_id);
1817
1818 let mut should_unsubscribe = false;
1819 self.ticker_subs.rcu(|m| {
1820 if let Some(entry) = m.get_mut(&instrument_id) {
1821 entry.remove("option_greeks");
1822 if entry.is_empty() {
1823 m.remove(&instrument_id);
1824 should_unsubscribe = true;
1825 } else {
1826 should_unsubscribe = false;
1827 }
1828 } else {
1829 should_unsubscribe = false;
1830 }
1831 });
1832
1833 if should_unsubscribe {
1834 let product_type = self
1835 .get_product_type_for_instrument(instrument_id)
1836 .unwrap_or(BybitProductType::Option);
1837
1838 let ws = self
1839 .get_ws_client_for_product(product_type)
1840 .context("no WebSocket client for product type")?
1841 .clone();
1842
1843 self.spawn_ws(
1844 async move {
1845 ws.unsubscribe_ticker(instrument_id)
1846 .await
1847 .context("ticker unsubscribe for option greeks")
1848 },
1849 "option greeks unsubscribe",
1850 );
1851 }
1852
1853 Ok(())
1854 })
1855 }
1856
1857 fn subscribe_instruments(&mut self, cmd: SubscribeInstruments) -> anyhow::Result<()> {
1858 log::debug!(
1859 "subscribe_instruments: {venue} (definition updates detected via periodic instrument info polling)",
1860 venue = cmd.venue,
1861 );
1862 self.subscribe_all_instruments
1863 .store(true, Ordering::Relaxed);
1864 Ok(())
1865 }
1866
1867 fn subscribe_instrument(&mut self, cmd: SubscribeInstrument) -> anyhow::Result<()> {
1868 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1869 log::debug!(
1870 "subscribe_instrument: {instrument_id} (definition updates detected via periodic instrument info polling)",
1871 );
1872 self.instrument_subs.insert(instrument_id);
1873 Ok(())
1874 })
1875 }
1876
1877 fn unsubscribe_instruments(&mut self, cmd: &UnsubscribeInstruments) -> anyhow::Result<()> {
1878 log::debug!("unsubscribe_instruments: {venue}", venue = cmd.venue);
1879 self.subscribe_all_instruments
1880 .store(false, Ordering::Relaxed);
1881 Ok(())
1882 }
1883
1884 fn unsubscribe_instrument(&mut self, cmd: &UnsubscribeInstrument) -> anyhow::Result<()> {
1885 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1886 log::debug!("unsubscribe_instrument: {instrument_id}");
1887 self.instrument_subs.remove(&instrument_id);
1888 Ok(())
1889 })
1890 }
1891
1892 fn subscribe_instrument_status(
1893 &mut self,
1894 cmd: SubscribeInstrumentStatus,
1895 ) -> anyhow::Result<()> {
1896 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1897 log::debug!(
1898 "subscribe_instrument_status: {instrument_id} (status changes detected via periodic instrument info polling)",
1899 );
1900 self.instrument_status_subs.insert(instrument_id);
1901
1902 if let Some(action) = self.status_cache.load().get(&instrument_id).copied() {
1903 let ts = self.clock.get_time_ns();
1904 emit_status(&self.data_sender, instrument_id, action, ts, ts);
1905 }
1906
1907 Ok(())
1908 })
1909 }
1910
1911 fn unsubscribe_instrument_status(
1912 &mut self,
1913 cmd: &UnsubscribeInstrumentStatus,
1914 ) -> anyhow::Result<()> {
1915 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1916 log::debug!("unsubscribe_instrument_status: {instrument_id}",);
1917 self.instrument_status_subs.remove(&instrument_id);
1918 Ok(())
1919 })
1920 }
1921
1922 fn request_instruments(&self, request: RequestInstruments) -> anyhow::Result<()> {
1923 let http = self.http_client.clone();
1924 let sender = self.data_sender.clone();
1925 let instruments_cache = self.instruments.clone();
1926 let request_id = request.request_id;
1927 let client_id = request.client_id.unwrap_or(self.client_id);
1928 let venue = self.venue();
1929 let start = request.start;
1930 let end = request.end;
1931 let params = request.params;
1932 let clock = self.clock;
1933 let start_nanos = datetime_to_unix_nanos(start);
1934 let end_nanos = datetime_to_unix_nanos(end);
1935 let product_types = if self.config.product_types.is_empty() {
1936 vec![BybitProductType::Linear]
1937 } else {
1938 self.config.product_types.clone()
1939 };
1940
1941 self.spawn_command(async move {
1942 let mut all_instruments = Vec::new();
1943
1944 for product_type in product_types {
1945 match http.request_instruments(product_type, None, None).await {
1946 Ok(instruments) => {
1947 instruments_cache.rcu(|cache| {
1948 for instrument in &instruments {
1949 cache.insert(instrument.id(), instrument.clone());
1950 }
1951 });
1952
1953 all_instruments.extend(instruments);
1954 }
1955 Err(e) => {
1956 log::error!("Failed to fetch instruments for {product_type:?}: {e:?}");
1957 }
1958 }
1959 }
1960
1961 let response = DataResponse::Instruments(InstrumentsResponse::new(
1962 request_id,
1963 client_id,
1964 venue,
1965 all_instruments,
1966 start_nanos,
1967 end_nanos,
1968 clock.get_time_ns(),
1969 params,
1970 ));
1971
1972 if let Err(e) = sender.send(DataEvent::Response(response)) {
1973 log::error!("Failed to send instruments response: {e}");
1974 }
1975 });
1976
1977 Ok(())
1978 }
1979
1980 fn request_instrument(&self, request: RequestInstrument) -> anyhow::Result<()> {
1981 let instrument_id = request.single_instrument_id()?;
1982
1983 let http = self.http_client.clone();
1984 let sender = self.data_sender.clone();
1985 let instruments = self.instruments.clone();
1986 let request_id = request.request_id;
1987 let client_id = request.client_id.unwrap_or(self.client_id);
1988 let start = request.start;
1989 let end = request.end;
1990 let params = request.params;
1991 let clock = self.clock;
1992 let start_nanos = datetime_to_unix_nanos(start);
1993 let end_nanos = datetime_to_unix_nanos(end);
1994
1995 let product_type = BybitProductType::from_suffix(instrument_id.symbol.as_str())
1996 .unwrap_or(BybitProductType::Linear);
1997 let raw_symbol = extract_raw_symbol(instrument_id.symbol.as_str()).to_string();
1998
1999 self.spawn_command(async move {
2000 match http
2001 .request_instruments(product_type, Some(raw_symbol), None)
2002 .await
2003 .context("fetch instrument from API")
2004 {
2005 Ok(fetched) => {
2006 if let Some(instrument) = fetched.into_iter().find(|i| i.id() == instrument_id)
2007 {
2008 upsert_instrument(&instruments, instrument.clone());
2009
2010 let response = DataResponse::Instrument(Box::new(InstrumentResponse::new(
2011 request_id,
2012 client_id,
2013 instrument.id(),
2014 instrument,
2015 start_nanos,
2016 end_nanos,
2017 clock.get_time_ns(),
2018 params,
2019 )));
2020
2021 if let Err(e) = sender.send(DataEvent::Response(response)) {
2022 log::error!("Failed to send instrument response: {e}");
2023 }
2024 } else {
2025 log::error!("Instrument not found: {instrument_id}");
2026 }
2027 }
2028 Err(e) => log::error!("Instrument request failed: {e:?}"),
2029 }
2030 });
2031
2032 Ok(())
2033 }
2034
2035 fn request_book_snapshot(&self, request: RequestBookSnapshot) -> anyhow::Result<()> {
2036 let instrument_id = request.single_instrument_id()?;
2037
2038 let http = self.http_client.clone();
2039 let sender = self.data_sender.clone();
2040 let depth = request.depth.map(|n| n.get() as u32);
2041 let request_id = request.request_id;
2042 let client_id = request.client_id.unwrap_or(self.client_id);
2043 let params = request.params;
2044 let clock = self.clock;
2045
2046 let product_type = BybitProductType::from_suffix(instrument_id.symbol.as_str())
2047 .unwrap_or(BybitProductType::Linear);
2048
2049 self.spawn_command(async move {
2050 match http
2051 .request_orderbook_snapshot(product_type, instrument_id, depth)
2052 .await
2053 .context("failed to request book snapshot from Bybit")
2054 {
2055 Ok(deltas) => {
2056 let mut book = OrderBook::new(instrument_id, BookType::L2_MBP);
2057 if let Err(e) = book.apply_deltas(&deltas) {
2058 log::error!("Failed to apply book deltas for {instrument_id}: {e}");
2059 return;
2060 }
2061
2062 let response = DataResponse::Book(BookResponse::new(
2063 request_id,
2064 client_id,
2065 instrument_id,
2066 book,
2067 None,
2068 None,
2069 clock.get_time_ns(),
2070 params,
2071 ));
2072
2073 if let Err(e) = sender.send(DataEvent::Response(response)) {
2074 log::error!("Failed to send book snapshot response: {e}");
2075 }
2076 }
2077 Err(e) => log::error!("Book snapshot request failed for {instrument_id}: {e:?}"),
2078 }
2079 });
2080
2081 Ok(())
2082 }
2083
2084 fn request_trades(&self, request: RequestTrades) -> anyhow::Result<()> {
2085 let instrument_id = request.single_instrument_id()?;
2086
2087 let http = self.http_client.clone();
2088 let sender = self.data_sender.clone();
2089 let start = request.start;
2090 let end = request.end;
2091 let limit = request.limit.map(|n| n.get() as u32);
2092 let request_id = request.request_id;
2093 let client_id = request.client_id.unwrap_or(self.client_id);
2094 let params = request.params;
2095 let clock = self.clock;
2096 let start_nanos = datetime_to_unix_nanos(start);
2097 let end_nanos = datetime_to_unix_nanos(end);
2098
2099 let product_type = BybitProductType::from_suffix(instrument_id.symbol.as_str())
2100 .unwrap_or(BybitProductType::Linear);
2101
2102 self.spawn_command(async move {
2103 match http
2104 .request_trades(product_type, instrument_id, limit)
2105 .await
2106 .context("failed to request trades from Bybit")
2107 {
2108 Ok(trades) => {
2109 let response = DataResponse::Trades(TradesResponse::new(
2110 request_id,
2111 client_id,
2112 instrument_id,
2113 trades,
2114 start_nanos,
2115 end_nanos,
2116 clock.get_time_ns(),
2117 params,
2118 ));
2119
2120 if let Err(e) = sender.send(DataEvent::Response(response)) {
2121 log::error!("Failed to send trades response: {e}");
2122 }
2123 }
2124 Err(e) => log::error!("Trade request failed: {e:?}"),
2125 }
2126 });
2127
2128 Ok(())
2129 }
2130
2131 fn request_bars(&self, request: RequestBars) -> anyhow::Result<()> {
2132 let bar_type = request.single_bar_type()?;
2133
2134 let http = self.http_client.clone();
2135 let bars_timestamp_on_close = self.config.bars_timestamp_on_close;
2136 let sender = self.data_sender.clone();
2137 let start = request.start;
2138 let end = request.end;
2139 let limit = request.limit.map(|n| n.get() as u32);
2140 let request_id = request.request_id;
2141 let client_id = request.client_id.unwrap_or(self.client_id);
2142 let params = request.params;
2143 let clock = self.clock;
2144 let start_nanos = datetime_to_unix_nanos(start);
2145 let end_nanos = datetime_to_unix_nanos(end);
2146
2147 let instrument_id = bar_type.instrument_id();
2148 let product_type = BybitProductType::from_suffix(instrument_id.symbol.as_str())
2149 .unwrap_or(BybitProductType::Linear);
2150
2151 self.spawn_command(async move {
2152 match http
2153 .request_bars(
2154 product_type,
2155 bar_type,
2156 start,
2157 end,
2158 limit,
2159 bars_timestamp_on_close,
2160 )
2161 .await
2162 .context("failed to request bars from Bybit")
2163 {
2164 Ok(bars) => {
2165 let response = DataResponse::Bars(BarsResponse::new(
2166 request_id,
2167 client_id,
2168 bar_type,
2169 bars,
2170 start_nanos,
2171 end_nanos,
2172 clock.get_time_ns(),
2173 params,
2174 ));
2175
2176 if let Err(e) = sender.send(DataEvent::Response(response)) {
2177 log::error!("Failed to send bars response: {e}");
2178 }
2179 }
2180 Err(e) => log::error!("Bar request failed: {e:?}"),
2181 }
2182 });
2183
2184 Ok(())
2185 }
2186
2187 fn request_funding_rates(&self, request: RequestFundingRates) -> anyhow::Result<()> {
2188 let instrument_id = request.single_instrument_id()?;
2189
2190 let http = self.http_client.clone();
2191 let sender = self.data_sender.clone();
2192 let start = request.start;
2193 let end = request.end;
2194 let limit = request.limit.map(|n| n.get() as u32);
2195 let request_id = request.request_id;
2196 let client_id = request.client_id.unwrap_or(self.client_id);
2197 let params = request.params;
2198 let clock = self.clock;
2199 let start_nanos = datetime_to_unix_nanos(start);
2200 let end_nanos = datetime_to_unix_nanos(end);
2201
2202 let product_type = BybitProductType::from_suffix(instrument_id.symbol.as_str())
2203 .unwrap_or(BybitProductType::Linear);
2204
2205 if product_type == BybitProductType::Spot || product_type == BybitProductType::Option {
2206 anyhow::bail!("Funding rates not available for {product_type} instruments");
2207 }
2208
2209 self.spawn_command(async move {
2210 match http
2211 .request_funding_rates(product_type, instrument_id, start, end, limit)
2212 .await
2213 .context("failed to request funding rates from Bybit")
2214 {
2215 Ok(funding_rates) => {
2216 let response = DataResponse::FundingRates(FundingRatesResponse::new(
2217 request_id,
2218 client_id,
2219 instrument_id,
2220 funding_rates,
2221 start_nanos,
2222 end_nanos,
2223 clock.get_time_ns(),
2224 params,
2225 ));
2226
2227 if let Err(e) = sender.send(DataEvent::Response(response)) {
2228 log::error!("Failed to send funding rates response: {e}");
2229 }
2230 }
2231 Err(e) => log::error!("Funding rates request failed for {instrument_id}: {e:?}"),
2232 }
2233 });
2234
2235 Ok(())
2236 }
2237
2238 fn request_option_chain_reference_price(
2239 &self,
2240 request: RequestOptionChainReferencePrice,
2241 ) -> anyhow::Result<()> {
2242 let series_id = request.series_id;
2243 let instrument_id = request.instrument_id;
2244 let http_client = self.http_client.clone();
2245 let sender = self.data_sender.clone();
2246 let request_id = request.request_id;
2247 let client_id = request.client_id.unwrap_or(self.client_id());
2248 let params = request.params;
2249 let clock = self.clock;
2250
2251 self.spawn_command(async move {
2252 let raw_symbol = extract_raw_symbol(instrument_id.symbol.as_str()).to_string();
2253 let query = crate::http::query::BybitTickersParams {
2254 category: BybitProductType::Option,
2255 symbol: Some(raw_symbol),
2256 base_coin: None,
2257 exp_date: None,
2258 };
2259 let price = match http_client.request_option_tickers_raw_with_params(&query).await {
2260 Ok(tickers) => tickers.into_iter().find_map(|ticker| {
2261 let decimal = ticker.underlying_price.parse::<Decimal>().ok()?;
2262 if decimal <= Decimal::ZERO {
2263 return None;
2264 }
2265
2266 match Price::from_decimal(decimal) {
2267 Ok(price) => Some(price),
2268 Err(e) => {
2269 log::warn!(
2270 "Invalid Bybit option-chain reference price for {instrument_id}: {e}"
2271 );
2272 None
2273 }
2274 }
2275 }),
2276 Err(e) => {
2277 log::error!(
2278 "Option-chain reference price request failed for {series_id}: {e:?}"
2279 );
2280 None
2281 }
2282 };
2283 let response = DataResponse::OptionChainReferencePrice(
2284 OptionChainReferencePriceResponse::new(
2285 request_id,
2286 client_id,
2287 series_id,
2288 price,
2289 clock.get_time_ns(),
2290 params,
2291 ),
2292 );
2293
2294 if let Err(e) = sender.send(DataEvent::Response(response)) {
2295 log::error!("Failed to send option-chain reference price response: {e}");
2296 }
2297 });
2298
2299 Ok(())
2300 }
2301}
2302
2303impl BybitDataClient {
2304 fn subscribe_quotes_member(&self, instrument_id: InstrumentId) -> anyhow::Result<()> {
2305 let product_type = self
2306 .get_product_type_for_instrument(instrument_id)
2307 .unwrap_or(BybitProductType::Linear);
2308
2309 let ws = self
2310 .get_ws_client_for_product(product_type)
2311 .context("no WebSocket client for product type")?
2312 .clone();
2313
2314 if product_type == BybitProductType::Option {
2315 self.last_quotes.remove(&instrument_id);
2316 let mut should_subscribe = false;
2317 self.ticker_subs.rcu(|m| {
2318 let entry = m.entry(instrument_id).or_default();
2319 should_subscribe = entry.is_empty();
2320 entry.insert("quotes");
2321 });
2322
2323 if should_subscribe {
2324 self.spawn_ws(
2325 async move {
2326 ws.subscribe_ticker(instrument_id)
2327 .await
2328 .context("ticker subscription")
2329 },
2330 "quote subscription",
2331 );
2332 }
2333 } else {
2334 if self.quote_subs.contains(&instrument_id) {
2335 return Ok(());
2336 }
2337
2338 self.quote_subs.insert(instrument_id);
2339 let quote_subs = Arc::clone(&self.quote_subs);
2340
2341 self.spawn_ws(
2342 async move {
2343 if let Err(e) = ws
2344 .subscribe_orderbook(instrument_id, BYBIT_QUOTE_DEPTH)
2345 .await
2346 {
2347 let mut removed = false;
2351 quote_subs.rcu(|subs| removed = subs.remove(&instrument_id));
2352
2353 if removed
2354 && let Err(e) = ws
2355 .unsubscribe_orderbook(instrument_id, BYBIT_QUOTE_DEPTH)
2356 .await
2357 {
2358 log::warn!("Failed to unsubscribe after quote subscription error: {e}");
2359 }
2360
2361 return Err(e).context("orderbook subscription for quotes");
2362 }
2363
2364 Ok(())
2365 },
2366 "quote subscription (orderbook)",
2367 );
2368 }
2369
2370 Ok(())
2371 }
2372
2373 fn unsubscribe_quotes_member(&self, instrument_id: InstrumentId) -> anyhow::Result<()> {
2374 let product_type = self
2375 .get_product_type_for_instrument(instrument_id)
2376 .unwrap_or(BybitProductType::Linear);
2377
2378 let ws = self
2379 .get_ws_client_for_product(product_type)
2380 .context("no WebSocket client for product type")?
2381 .clone();
2382
2383 if product_type == BybitProductType::Option {
2384 self.last_quotes.remove(&instrument_id);
2385 let mut should_unsubscribe = false;
2386 self.ticker_subs.rcu(|m| {
2387 if let Some(entry) = m.get_mut(&instrument_id) {
2388 entry.remove("quotes");
2389 if entry.is_empty() {
2390 m.remove(&instrument_id);
2391 should_unsubscribe = true;
2392 } else {
2393 should_unsubscribe = false;
2394 }
2395 } else {
2396 should_unsubscribe = false;
2397 }
2398 });
2399
2400 if should_unsubscribe {
2401 self.spawn_ws(
2402 async move {
2403 ws.unsubscribe_ticker(instrument_id)
2404 .await
2405 .context("ticker unsubscribe")
2406 },
2407 "quote unsubscribe",
2408 );
2409 }
2410 } else {
2411 if !self.quote_subs.contains(&instrument_id) {
2412 return Ok(());
2413 }
2414
2415 self.quote_subs.remove(&instrument_id);
2416 self.spawn_ws(
2417 async move {
2418 ws.unsubscribe_orderbook(instrument_id, BYBIT_QUOTE_DEPTH)
2419 .await
2420 .context("orderbook unsubscribe for quotes")
2421 },
2422 "quote unsubscribe (orderbook)",
2423 );
2424 }
2425
2426 Ok(())
2427 }
2428}
2429
2430#[cfg(test)]
2431#[path = "../benches"]
2432pub(crate) mod tests {
2433 #[allow(dead_code)]
2435 #[path = "quotes/mod.rs"]
2436 pub(crate) mod benchmarks;
2437
2438 use std::{num::NonZeroUsize, process::Command, sync::Arc, time::Duration};
2439
2440 use ahash::{AHashMap, AHashSet};
2441 use dashmap::DashMap;
2442 use nautilus_common::{
2443 clients::DataClient,
2444 live::{dst::time::Instant, runner::set_data_event_sender},
2445 messages::{
2446 DataEvent,
2447 data::{
2448 SubscribeBookDeltas, SubscribeCustomData, SubscribeFundingRates, SubscribeQuotes,
2449 SubscriptionError, UnsubscribeBookDeltas, UnsubscribeCustomData,
2450 UnsubscribeFundingRates, UnsubscribeQuotes,
2451 },
2452 },
2453 testing::wait_until_async,
2454 };
2455 use nautilus_core::{
2456 AtomicMap, AtomicSet, Params, UUID4, UnixNanos,
2457 time::{AtomicTime, get_atomic_clock_realtime},
2458 };
2459 use nautilus_live::{
2460 book::snapshot::SnapshotGate,
2461 task::{TaskGroup, TaskSpawner},
2462 };
2463 use nautilus_model::{
2464 data::{BarType, CustomData, Data, DataType, MarkPriceUpdate, QuoteTick},
2465 enums::{AggressorSide, BookAction, BookType, PositionSide},
2466 identifiers::InstrumentId,
2467 instruments::{Instrument, InstrumentAny},
2468 types::{Price, Quantity},
2469 };
2470 #[cfg(feature = "python")]
2471 use pyo3::types::{PyAnyMethods, PyDictMethods};
2472 use rstest::rstest;
2473 use ustr::Ustr;
2474
2475 use super::{
2476 BybitDataClient, handle_orderbook_deltas, handle_ws_message, remove_owned_liquidation_sub,
2477 validate_orderbook_depth,
2478 };
2479 use crate::{
2480 book::{BookSequenceOutcome, sync::BookSyncTracker},
2481 common::{
2482 consts::BYBIT_CLIENT_ID,
2483 credential::Credential,
2484 enums::{BybitEnvironment, BybitProductType, BybitSymbolType},
2485 parse::{parse_linear_instrument, parse_option_instrument},
2486 rate_limit::BYBIT_OPTION_SUBSCRIPTION_LIMIT,
2487 testing::load_test_json,
2488 },
2489 config::BybitDataClientConfig,
2490 data_types::BybitLiquidation,
2491 http::models::{BybitInstrumentLinearResponse, BybitInstrumentOptionResponse},
2492 websocket::{
2493 client::BybitWebSocketClient,
2494 messages::{
2495 BybitWsKlineMsg, BybitWsLiquidationMsg, BybitWsMessage, BybitWsOrderbookDepthMsg,
2496 BybitWsTickerLinearMsg, BybitWsTickerOptionMsg, BybitWsTradeMsg,
2497 },
2498 },
2499 };
2500
2501 fn linear_instrument() -> InstrumentAny {
2502 let json = load_test_json("http_get_instruments_linear.json");
2503 let response: BybitInstrumentLinearResponse = serde_json::from_str(&json).unwrap();
2504 let instrument = &response.result.list[0];
2505 let ts = UnixNanos::new(1_700_000_000_000_000_000);
2506 parse_linear_instrument(instrument, ts, ts).unwrap()
2507 }
2508
2509 fn tradfi_linear_instrument() -> InstrumentAny {
2510 let json = load_test_json("http_get_instruments_linear.json");
2511 let response: BybitInstrumentLinearResponse = serde_json::from_str(&json).unwrap();
2512 let mut instrument = response.result.list[0].clone();
2513 instrument.symbol_type = Some(BybitSymbolType::Stock);
2514 let ts = UnixNanos::new(1_700_000_000_000_000_000);
2515 parse_linear_instrument(&instrument, ts, ts).unwrap()
2516 }
2517
2518 fn linear_instruments() -> Vec<InstrumentAny> {
2519 let json = load_test_json("http_get_instruments_linear.json");
2520 let response: BybitInstrumentLinearResponse = serde_json::from_str(&json).unwrap();
2521 let ts = UnixNanos::new(1_700_000_000_000_000_000);
2522 response
2523 .result
2524 .list
2525 .iter()
2526 .map(|instrument| parse_linear_instrument(instrument, ts, ts).unwrap())
2527 .collect()
2528 }
2529
2530 fn option_instrument() -> InstrumentAny {
2531 let json = load_test_json("http_get_instruments_option.json");
2532 let response: BybitInstrumentOptionResponse = serde_json::from_str(&json).unwrap();
2533 let instrument = &response.result.list[0];
2534 let ts = UnixNanos::new(1_700_000_000_000_000_000);
2535 parse_option_instrument(instrument, ts, ts).unwrap()
2536 }
2537
2538 fn build_instruments(instruments: &[InstrumentAny]) -> AHashMap<Ustr, InstrumentAny> {
2539 let mut map = AHashMap::new();
2540 for inst in instruments {
2541 map.insert(inst.id().symbol.inner(), inst.clone());
2542 }
2543 map
2544 }
2545
2546 #[expect(clippy::type_complexity)]
2547 fn empty_subs() -> (
2548 Arc<AtomicSet<InstrumentId>>,
2549 Arc<AtomicMap<InstrumentId, AHashSet<&'static str>>>,
2550 Arc<AtomicSet<InstrumentId>>,
2551 Arc<AtomicMap<InstrumentId, u32>>,
2552 Arc<AtomicSet<InstrumentId>>,
2553 Arc<AtomicMap<String, BarType>>,
2554 ) {
2555 (
2556 Arc::new(AtomicSet::new()),
2557 Arc::new(AtomicMap::new()),
2558 Arc::new(AtomicSet::new()),
2559 Arc::new(AtomicMap::new()),
2560 Arc::new(AtomicSet::new()),
2561 Arc::new(AtomicMap::new()),
2562 )
2563 }
2564
2565 fn book_context() -> (BookSyncTracker, BybitWebSocketClient, TaskSpawner) {
2566 let tasks = TaskGroup::new().spawner().unwrap();
2567 let ws_client = BybitWebSocketClient::new_public(None, 20);
2568 (BookSyncTracker::default(), ws_client, tasks)
2569 }
2570
2571 fn liquidation_data_type(instrument_id: InstrumentId, identifier: Option<&str>) -> DataType {
2572 let metadata = Params::new(
2573 [(
2574 "instrument_id".to_string(),
2575 serde_json::Value::String(instrument_id.to_string()),
2576 )]
2577 .into(),
2578 );
2579
2580 DataType::new(
2581 "BybitLiquidation",
2582 Some(metadata),
2583 identifier.map(ToString::to_string),
2584 )
2585 }
2586
2587 fn subscribe_custom_data(data_type: DataType) -> SubscribeCustomData {
2588 SubscribeCustomData::new(
2589 Some(*BYBIT_CLIENT_ID),
2590 None,
2591 data_type,
2592 UUID4::new(),
2593 UnixNanos::default(),
2594 None,
2595 None,
2596 )
2597 }
2598
2599 fn unsubscribe_custom_data(data_type: DataType) -> UnsubscribeCustomData {
2600 UnsubscribeCustomData::new(
2601 Some(*BYBIT_CLIENT_ID),
2602 None,
2603 data_type,
2604 UUID4::new(),
2605 UnixNanos::default(),
2606 None,
2607 None,
2608 )
2609 }
2610
2611 fn handle_liquidation_message(
2612 msg: BybitWsLiquidationMsg,
2613 instruments: &AHashMap<Ustr, InstrumentAny>,
2614 liquidation_subs: &Arc<AtomicMap<InstrumentId, Arc<DataType>>>,
2615 ts_init: UnixNanos,
2616 ) -> Vec<CustomData> {
2617 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
2618 empty_subs();
2619 let (book_sync, ws_client, tasks) = book_context();
2620
2621 let mut funding_cache = AHashMap::new();
2622 let clock = AtomicTime::new(false, ts_init);
2623 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
2624
2625 handle_ws_message(
2626 &BybitWsMessage::Liquidation(msg),
2627 &tx.into(),
2628 instruments,
2629 Some(BybitProductType::Linear),
2630 &trade_subs,
2631 &ticker_subs,
2632 "e_subs,
2633 &book_depths,
2634 &book_sync,
2635 &ws_client,
2636 &greeks_subs,
2637 liquidation_subs,
2638 &bar_types,
2639 true,
2640 &DashMap::new(),
2641 &mut funding_cache,
2642 Duration::ZERO,
2643 &tasks,
2644 &clock,
2645 );
2646
2647 let mut events = Vec::new();
2648
2649 while let Ok(event) = rx.try_recv() {
2650 match event {
2651 DataEvent::Data(Data::Custom(custom)) => events.push(custom),
2652 other => panic!("Expected custom data event, found {other:?}"),
2653 }
2654 }
2655
2656 events
2657 }
2658
2659 fn as_liquidation(custom: &CustomData) -> &BybitLiquidation {
2660 custom
2661 .data
2662 .as_any()
2663 .downcast_ref::<BybitLiquidation>()
2664 .expect("expected BybitLiquidation")
2665 }
2666
2667 #[tokio::test]
2668 async fn test_failed_book_subscription_releases_registration() {
2669 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2670 set_data_event_sender(tx);
2671 let config = BybitDataClientConfig {
2672 product_types: vec![BybitProductType::Option],
2673 ..Default::default()
2674 };
2675 let mut client = BybitDataClient::new(*BYBIT_CLIENT_ID, config).unwrap();
2676 let ws = client.ws_clients[0].clone();
2677 let topics = (0..BYBIT_OPTION_SUBSCRIPTION_LIMIT)
2678 .map(|index| format!("tickers.OPTION-{index}"))
2679 .collect();
2680
2681 ws.subscribe(topics).await.unwrap_err();
2683 let instrument_id = InstrumentId::from("ETH-26JUN26-16000-P-OPTION.BYBIT");
2684 let expected_error = format!(
2685 "Client error: Option WebSocket subscription limit is {BYBIT_OPTION_SUBSCRIPTION_LIMIT} arguments per connection, requested {}",
2686 BYBIT_OPTION_SUBSCRIPTION_LIMIT + 1,
2687 );
2688 assert_eq!(
2689 ws.subscribe_orderbook(instrument_id, 25)
2690 .await
2691 .unwrap_err()
2692 .to_string(),
2693 expected_error,
2694 );
2695 client
2696 .instruments
2697 .insert(instrument_id, option_instrument());
2698 let command = SubscribeBookDeltas::new(
2699 instrument_id,
2700 BookType::L2_MBP,
2701 Some(*BYBIT_CLIENT_ID),
2702 None,
2703 UUID4::new(),
2704 UnixNanos::default(),
2705 None,
2706 false,
2707 None,
2708 None,
2709 );
2710
2711 for _ in 0..2 {
2712 client.subscribe_book_deltas(command.clone()).unwrap();
2713 wait_until_async(
2714 || async { !client.book_depths.contains_key(&instrument_id) },
2715 Duration::from_secs(2),
2716 )
2717 .await;
2718 assert!(!client.book_depths.contains_key(&instrument_id));
2719 assert_eq!(
2720 ws.subscribe_orderbook(instrument_id, 25)
2721 .await
2722 .unwrap_err()
2723 .to_string(),
2724 expected_error,
2725 );
2726 }
2727 }
2728
2729 #[tokio::test]
2730 async fn test_failed_book_subscription_releases_transport_reference() {
2731 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2732 set_data_event_sender(tx);
2733 let mut client =
2734 BybitDataClient::new(*BYBIT_CLIENT_ID, BybitDataClientConfig::default()).unwrap();
2735 let ws = client.ws_clients[0].clone();
2736 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2737 client
2738 .subscribe_book_deltas(SubscribeBookDeltas::new(
2739 instrument_id,
2740 BookType::L2_MBP,
2741 Some(*BYBIT_CLIENT_ID),
2742 None,
2743 UUID4::new(),
2744 UnixNanos::default(),
2745 None,
2746 false,
2747 None,
2748 None,
2749 ))
2750 .unwrap();
2751 wait_until_async(
2752 || async { !client.book_depths.contains_key(&instrument_id) },
2753 Duration::from_secs(2),
2754 )
2755 .await;
2756
2757 let error = ws.subscribe_orderbook(instrument_id, 50).await.unwrap_err();
2758
2759 assert_eq!(
2760 error.to_string(),
2761 "WebSocket send error: Failed to send subscribe command: channel closed",
2762 );
2763 assert!(!client.book_depths.contains_key(&instrument_id));
2764 }
2765
2766 #[tokio::test]
2767 async fn test_failed_liquidation_subscription_releases_registration() {
2768 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2769 set_data_event_sender(tx);
2770 let mut client =
2771 BybitDataClient::new(*BYBIT_CLIENT_ID, BybitDataClientConfig::default()).unwrap();
2772 let ws = client.ws_clients[0].clone();
2773 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2774 let command = subscribe_custom_data(liquidation_data_type(instrument_id, None));
2775
2776 for _ in 0..2 {
2778 client.subscribe(command.clone()).unwrap();
2779 wait_until_async(
2780 || async { !client.liquidation_subs.contains_key(&instrument_id) },
2781 Duration::from_secs(2),
2782 )
2783 .await;
2784 }
2785
2786 let error = ws.subscribe_liquidations(instrument_id).await.unwrap_err();
2787
2788 assert_eq!(
2789 error.to_string(),
2790 "WebSocket send error: Failed to send subscribe command: channel closed",
2791 );
2792 assert!(!client.liquidation_subs.contains_key(&instrument_id));
2793 }
2794
2795 #[rstest]
2796 #[tokio::test]
2797 async fn test_failed_quote_subscription_releases_registration() {
2798 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2799 set_data_event_sender(tx);
2800 let mut client =
2801 BybitDataClient::new(*BYBIT_CLIENT_ID, BybitDataClientConfig::default()).unwrap();
2802 let ws = client.ws_clients[0].clone();
2803 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2804 let command = SubscribeQuotes::new(
2805 instrument_id,
2806 Some(*BYBIT_CLIENT_ID),
2807 None,
2808 UUID4::new(),
2809 UnixNanos::default(),
2810 None,
2811 None,
2812 );
2813
2814 for _ in 0..2 {
2816 client.subscribe_quotes(command.clone()).unwrap();
2817 wait_until_async(
2818 || async { !client.quote_subs.contains(&instrument_id) },
2819 Duration::from_secs(2),
2820 )
2821 .await;
2822 }
2823
2824 let error = ws.subscribe_orderbook(instrument_id, 1).await.unwrap_err();
2825
2826 assert_eq!(
2827 error.to_string(),
2828 "WebSocket send error: Failed to send subscribe command: channel closed",
2829 );
2830 assert!(!client.quote_subs.contains(&instrument_id));
2831 }
2832
2833 #[rstest]
2834 fn test_liquidation_subscription_keeps_first_data_type() {
2835 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2836 set_data_event_sender(tx);
2837 let mut client =
2838 BybitDataClient::new(*BYBIT_CLIENT_ID, BybitDataClientConfig::default()).unwrap();
2839 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2840 let data_type = liquidation_data_type(instrument_id, None);
2841 let other_data_type = liquidation_data_type(instrument_id, Some("other"));
2842 let subscription = Arc::new(data_type.clone());
2843 client
2844 .liquidation_subs
2845 .insert(instrument_id, Arc::clone(&subscription));
2846
2847 let error = client
2848 .subscribe(subscribe_custom_data(other_data_type.clone()))
2849 .unwrap_err();
2850 client
2851 .unsubscribe(&unsubscribe_custom_data(other_data_type))
2852 .unwrap();
2853 client
2854 .subscribe(subscribe_custom_data(data_type.clone()))
2855 .unwrap();
2856
2857 assert_eq!(
2858 error.to_string(),
2859 format!(
2860 "Already subscribed to liquidations for BTCUSDT-LINEAR.BYBIT as {}",
2861 data_type.topic()
2862 ),
2863 );
2864 assert!(
2865 client
2866 .liquidation_subs
2867 .get_cloned(&instrument_id)
2868 .is_some_and(|current| Arc::ptr_eq(¤t, &subscription))
2869 );
2870 }
2871
2872 #[rstest]
2873 fn test_remove_owned_liquidation_sub_removes_own_entry() {
2874 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2875 let owned = Arc::new(liquidation_data_type(instrument_id, None));
2876 let subs = AtomicMap::new();
2877 subs.insert(instrument_id, Arc::clone(&owned));
2878
2879 let removed = remove_owned_liquidation_sub(&subs, instrument_id, &owned);
2880
2881 assert!(removed);
2882 assert!(subs.is_empty());
2883 }
2884
2885 #[rstest]
2886 fn test_remove_owned_liquidation_sub_skips_entry_removed_by_unsubscribe() {
2887 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2888 let owned = Arc::new(liquidation_data_type(instrument_id, None));
2889 let subs = AtomicMap::new();
2890
2891 let removed = remove_owned_liquidation_sub(&subs, instrument_id, &owned);
2892
2893 assert!(!removed);
2894 assert!(subs.is_empty());
2895 }
2896
2897 #[rstest]
2898 #[case::identical_data_type(None)]
2899 #[case::other_data_type(Some("other"))]
2900 fn test_remove_owned_liquidation_sub_keeps_later_subscription(
2901 #[case] identifier: Option<&str>,
2902 ) {
2903 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
2904 let owned = Arc::new(liquidation_data_type(instrument_id, None));
2905 let later = Arc::new(liquidation_data_type(instrument_id, identifier));
2906 let subs = AtomicMap::new();
2907 subs.insert(instrument_id, Arc::clone(&later));
2908
2909 let removed = remove_owned_liquidation_sub(&subs, instrument_id, &owned);
2910
2911 assert!(!removed);
2912 assert_eq!(subs.len(), 1);
2913 assert!(
2914 subs.get_cloned(&instrument_id)
2915 .is_some_and(|current| Arc::ptr_eq(¤t, &later))
2916 );
2917 }
2918
2919 #[rstest]
2920 #[case::linear_1(BybitProductType::Linear, 1)]
2921 #[case::linear_50(BybitProductType::Linear, 50)]
2922 #[case::linear_200(BybitProductType::Linear, 200)]
2923 #[case::linear_1000(BybitProductType::Linear, 1000)]
2924 #[case::inverse_1(BybitProductType::Inverse, 1)]
2925 #[case::inverse_50(BybitProductType::Inverse, 50)]
2926 #[case::inverse_200(BybitProductType::Inverse, 200)]
2927 #[case::inverse_1000(BybitProductType::Inverse, 1000)]
2928 #[case::spot_1(BybitProductType::Spot, 1)]
2929 #[case::spot_50(BybitProductType::Spot, 50)]
2930 #[case::spot_200(BybitProductType::Spot, 200)]
2931 #[case::spot_1000(BybitProductType::Spot, 1000)]
2932 #[case::option_25(BybitProductType::Option, 25)]
2933 #[case::option_100(BybitProductType::Option, 100)]
2934 fn test_validate_orderbook_depth_accepts_supported(
2935 #[case] product_type: BybitProductType,
2936 #[case] depth: u32,
2937 ) {
2938 assert!(validate_orderbook_depth(depth, product_type).is_ok());
2939 }
2940
2941 #[rstest]
2942 #[case::linear_25(BybitProductType::Linear, 25, "[1, 50, 200, 1000]")]
2943 #[case::inverse_100(BybitProductType::Inverse, 100, "[1, 50, 200, 1000]")]
2944 #[case::spot_500(BybitProductType::Spot, 500, "[1, 50, 200, 1000]")]
2945 #[case::option_1(BybitProductType::Option, 1, "[25, 100]")]
2946 #[case::option_50(BybitProductType::Option, 50, "[25, 100]")]
2947 #[case::option_200(BybitProductType::Option, 200, "[25, 100]")]
2948 #[case::option_1000(BybitProductType::Option, 1000, "[25, 100]")]
2949 #[case::option_500(BybitProductType::Option, 500, "[25, 100]")]
2950 fn test_validate_orderbook_depth_rejects_unsupported(
2951 #[case] product_type: BybitProductType,
2952 #[case] depth: u32,
2953 #[case] valid_depths: &str,
2954 ) {
2955 let e = validate_orderbook_depth(depth, product_type).unwrap_err();
2956
2957 assert_eq!(
2958 e.to_string(),
2959 format!("invalid depth {depth}; valid values are {valid_depths}")
2960 );
2961 }
2962
2963 #[tokio::test]
2964 async fn test_option_book_subscription_rejects_invalid_depth_before_registration() {
2965 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
2966 set_data_event_sender(tx);
2967
2968 let config = BybitDataClientConfig {
2969 product_types: vec![BybitProductType::Option],
2970 ..Default::default()
2971 };
2972
2973 let mut client = BybitDataClient::new(*BYBIT_CLIENT_ID, config).unwrap();
2974 let instrument_id = InstrumentId::from("ETH-26JUN26-16000-P-OPTION.BYBIT");
2975 client
2976 .instruments
2977 .insert(instrument_id, option_instrument());
2978
2979 let command = SubscribeBookDeltas::new(
2980 instrument_id,
2981 BookType::L2_MBP,
2982 Some(*BYBIT_CLIENT_ID),
2983 None,
2984 UUID4::new(),
2985 UnixNanos::default(),
2986 NonZeroUsize::new(50),
2987 false,
2988 None,
2989 None,
2990 );
2991
2992 let e = client.subscribe_book_deltas(command).unwrap_err();
2993
2994 assert_eq!(
2995 e.to_string(),
2996 "invalid depth 50; valid values are [25, 100]"
2997 );
2998 assert!(!client.book_depths.contains_key(&instrument_id));
2999 }
3000
3001 #[rstest]
3002 #[case::open(false, true, Some(1_672_324_800_000_000_000))]
3003 #[case::close(true, true, Some(1_672_325_100_000_000_000))]
3004 #[case::unconfirmed_open(false, false, None)]
3005 #[case::unconfirmed_close(true, false, None)]
3006 fn test_handle_kline_bar_timestamp(
3007 #[case] on_close: bool,
3008 #[case] confirmed: bool,
3009 #[case] expected: Option<u64>,
3010 ) {
3011 let instrument = linear_instrument();
3012 let instruments = build_instruments(std::slice::from_ref(&instrument));
3013 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3014 empty_subs();
3015 let (book_sync, ws_client, tasks) = book_context();
3016 let bar_type = BarType::from("BTCUSDT-LINEAR.BYBIT-5-MINUTE-LAST-EXTERNAL");
3017 bar_types.insert("kline.5.BTCUSDT".to_string(), bar_type);
3018 let mut msg: BybitWsKlineMsg =
3019 serde_json::from_str(&load_test_json("ws_kline.json")).unwrap();
3020 msg.data[0].confirm = confirmed;
3021
3022 let mut funding_cache = AHashMap::new();
3023 let clock = get_atomic_clock_realtime();
3024 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3025 handle_ws_message(
3026 &BybitWsMessage::Kline(msg),
3027 &tx.into(),
3028 &instruments,
3029 Some(BybitProductType::Linear),
3030 &trade_subs,
3031 &ticker_subs,
3032 "e_subs,
3033 &book_depths,
3034 &book_sync,
3035 &ws_client,
3036 &greeks_subs,
3037 &Arc::new(AtomicMap::new()),
3038 &bar_types,
3039 on_close,
3040 &DashMap::new(),
3041 &mut funding_cache,
3042 Duration::ZERO,
3043 &tasks,
3044 clock,
3045 );
3046
3047 if let Some(expected) = expected {
3048 let DataEvent::Data(Data::Bar(bar)) = rx.try_recv().unwrap() else {
3049 panic!("Expected bar event");
3050 };
3051 assert_eq!(bar.bar_type, bar_type);
3052 assert_eq!(bar.ts_event, UnixNanos::from(expected));
3053 }
3054 assert!(rx.try_recv().is_err());
3055 }
3056
3057 #[rstest]
3058 fn test_handle_trade_message_emits_trade_tick() {
3059 let instrument = linear_instrument();
3060 let instruments = build_instruments(std::slice::from_ref(&instrument));
3061 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3062 empty_subs();
3063 let (book_sync, ws_client, tasks) = book_context();
3064 trade_subs.insert(instrument.id());
3065
3066 let mut funding_cache = AHashMap::new();
3067 let clock = get_atomic_clock_realtime();
3068
3069 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3070
3071 let json = load_test_json("ws_public_trade.json");
3072 let msg: BybitWsTradeMsg = serde_json::from_str(&json).unwrap();
3073 let ws_msg = BybitWsMessage::Trade(msg);
3074
3075 handle_ws_message(
3076 &ws_msg,
3077 &tx.into(),
3078 &instruments,
3079 Some(BybitProductType::Linear),
3080 &trade_subs,
3081 &ticker_subs,
3082 "e_subs,
3083 &book_depths,
3084 &book_sync,
3085 &ws_client,
3086 &greeks_subs,
3087 &Arc::new(AtomicMap::new()),
3088 &bar_types,
3089 true,
3090 &DashMap::new(),
3091 &mut funding_cache,
3092 Duration::ZERO,
3093 &tasks,
3094 clock,
3095 );
3096
3097 let event = rx.try_recv().unwrap();
3098 match event {
3099 DataEvent::Data(Data::Trade(tick)) => {
3100 assert_eq!(tick.instrument_id, instrument.id());
3101 assert_eq!(tick.price, instrument.make_price(27451.00));
3102 assert_eq!(tick.size, instrument.make_qty(0.010, None));
3103 assert_eq!(tick.aggressor_side, AggressorSide::Buy);
3104 }
3105 other => panic!("Expected Trade data event, found {other:?}"),
3106 }
3107 }
3108
3109 #[rstest]
3110 fn test_handle_trade_message_unknown_symbol_no_event() {
3111 let instruments = AHashMap::new();
3112 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3113 empty_subs();
3114 let (book_sync, ws_client, tasks) = book_context();
3115
3116 let mut funding_cache = AHashMap::new();
3117 let clock = get_atomic_clock_realtime();
3118
3119 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3120
3121 let json = load_test_json("ws_public_trade.json");
3122 let msg: BybitWsTradeMsg = serde_json::from_str(&json).unwrap();
3123 let ws_msg = BybitWsMessage::Trade(msg);
3124
3125 handle_ws_message(
3126 &ws_msg,
3127 &tx.into(),
3128 &instruments,
3129 Some(BybitProductType::Linear),
3130 &trade_subs,
3131 &ticker_subs,
3132 "e_subs,
3133 &book_depths,
3134 &book_sync,
3135 &ws_client,
3136 &greeks_subs,
3137 &Arc::new(AtomicMap::new()),
3138 &bar_types,
3139 true,
3140 &DashMap::new(),
3141 &mut funding_cache,
3142 Duration::ZERO,
3143 &tasks,
3144 clock,
3145 );
3146
3147 rx.try_recv().unwrap_err();
3148 }
3149
3150 #[rstest]
3151 #[case::metadata_only(None)]
3152 #[case::with_identifier(Some("bybit-liquidations"))]
3153 fn test_handle_liquidation_message_emits_subscribed_data_type(
3154 #[case] identifier: Option<&str>,
3155 ) {
3156 let instrument = linear_instrument();
3157 let instrument_id = instrument.id();
3158 let instruments = build_instruments(&[instrument]);
3159 let data_type = liquidation_data_type(instrument_id, identifier);
3160 let liquidation_subs = Arc::new(AtomicMap::new());
3161 liquidation_subs.insert(instrument_id, Arc::new(data_type.clone()));
3162 let ts_init = UnixNanos::new(1_739_502_303_500_000_000);
3163 let msg: BybitWsLiquidationMsg =
3164 serde_json::from_str(&load_test_json("ws_all_liquidation.json")).unwrap();
3165
3166 let events = handle_liquidation_message(msg, &instruments, &liquidation_subs, ts_init);
3167
3168 assert_eq!(events.len(), 2);
3169 assert_eq!(events[0].data_type, data_type);
3170 assert_eq!(events[0].data_type.topic(), data_type.topic());
3171 assert_eq!(events[0].data_type.identifier(), identifier);
3172 assert_eq!(events[1].data_type, data_type);
3173 assert_eq!(
3174 as_liquidation(&events[0]),
3175 &BybitLiquidation::new(
3176 instrument_id,
3177 PositionSide::Long,
3178 Price::from("96250.5"),
3179 Quantity::from("0.015"),
3180 UnixNanos::new(1_739_502_302_929_000_000),
3181 ts_init,
3182 ),
3183 );
3184 assert_eq!(
3185 as_liquidation(&events[1]),
3186 &BybitLiquidation::new(
3187 instrument_id,
3188 PositionSide::Short,
3189 Price::from("97410.0"),
3190 Quantity::from("1.250"),
3191 UnixNanos::new(1_739_502_303_011_000_000),
3192 ts_init,
3193 ),
3194 );
3195 }
3196
3197 #[rstest]
3198 #[case::unsubscribed_instrument("ETHUSDT")]
3199 #[case::uncached_symbol("SOLUSDT")]
3200 fn test_handle_liquidation_message_skips_element(#[case] skipped_symbol: &str) {
3201 let instruments = linear_instruments();
3202 let instrument_id = instruments[0].id();
3203 let instruments = build_instruments(&instruments);
3204 let data_type = liquidation_data_type(instrument_id, None);
3205 let liquidation_subs = Arc::new(AtomicMap::new());
3206 liquidation_subs.insert(instrument_id, Arc::new(data_type.clone()));
3207 let ts_init = UnixNanos::new(1_739_502_303_500_000_000);
3208 let mut value: serde_json::Value =
3209 serde_json::from_str(&load_test_json("ws_all_liquidation.json")).unwrap();
3210 value["data"][1]["s"] = serde_json::json!(skipped_symbol);
3211 let msg: BybitWsLiquidationMsg = serde_json::from_value(value).unwrap();
3212
3213 let events = handle_liquidation_message(msg, &instruments, &liquidation_subs, ts_init);
3214
3215 assert_eq!(instruments.len(), 2);
3216 assert_eq!(events.len(), 1);
3217 assert_eq!(events[0].data_type, data_type);
3218 assert_eq!(
3219 as_liquidation(&events[0]),
3220 &BybitLiquidation::new(
3221 instrument_id,
3222 PositionSide::Long,
3223 Price::from("96250.5"),
3224 Quantity::from("0.015"),
3225 UnixNanos::new(1_739_502_302_929_000_000),
3226 ts_init,
3227 ),
3228 );
3229 }
3230
3231 #[rstest]
3232 fn test_handle_liquidation_message_skips_invalid_element() {
3233 let instrument = linear_instrument();
3234 let instrument_id = instrument.id();
3235 let instruments = build_instruments(&[instrument]);
3236 let data_type = liquidation_data_type(instrument_id, None);
3237 let liquidation_subs = Arc::new(AtomicMap::new());
3238 liquidation_subs.insert(instrument_id, Arc::new(data_type.clone()));
3239 let ts_init = UnixNanos::new(1_739_502_303_500_000_000);
3240 let mut value: serde_json::Value =
3241 serde_json::from_str(&load_test_json("ws_all_liquidation.json")).unwrap();
3242 value["data"][0]["S"] = serde_json::json!("");
3243 let msg: BybitWsLiquidationMsg = serde_json::from_value(value).unwrap();
3244
3245 let events = handle_liquidation_message(msg, &instruments, &liquidation_subs, ts_init);
3246
3247 assert_eq!(events.len(), 1);
3248 assert_eq!(events[0].data_type, data_type);
3249 assert_eq!(
3250 as_liquidation(&events[0]),
3251 &BybitLiquidation::new(
3252 instrument_id,
3253 PositionSide::Short,
3254 Price::from("97410.0"),
3255 Quantity::from("1.250"),
3256 UnixNanos::new(1_739_502_303_011_000_000),
3257 ts_init,
3258 ),
3259 );
3260 }
3261
3262 #[rstest]
3263 fn test_handle_liquidation_message_without_subscription_no_event() {
3264 let instrument = linear_instrument();
3265 let instruments = build_instruments(&[instrument]);
3266 let liquidation_subs = Arc::new(AtomicMap::new());
3267 let msg: BybitWsLiquidationMsg =
3268 serde_json::from_str(&load_test_json("ws_all_liquidation.json")).unwrap();
3269
3270 let events = handle_liquidation_message(
3271 msg,
3272 &instruments,
3273 &liquidation_subs,
3274 UnixNanos::new(1_739_502_303_500_000_000),
3275 );
3276
3277 assert!(events.is_empty());
3278 }
3279
3280 #[rstest]
3281 #[case::shared_depth_one("orderbook.1.BTCUSDT", 1, "snapshot", true, true)]
3282 #[case::depth_one_with_deeper_book("orderbook.1.BTCUSDT", 50, "snapshot", false, true)]
3283 #[case::deeper_snapshot("orderbook.50.BTCUSDT", 50, "snapshot", true, false)]
3284 #[case::deeper_deletion("orderbook.50.BTCUSDT", 50, "delta", true, false)]
3285 #[case::unsubscribed_depth("orderbook.200.BTCUSDT", 50, "delta", false, false)]
3286 #[case::invalid_topic("orderbook.invalid.BTCUSDT", 1, "snapshot", false, false)]
3287 #[case::wrong_symbol("orderbook.1.ETHUSDT", 1, "snapshot", false, false)]
3288 fn test_handle_orderbook_routes_by_depth(
3289 #[case] topic: &str,
3290 #[case] book_depth: u32,
3291 #[case] msg_type: &str,
3292 #[case] expect_book: bool,
3293 #[case] expect_quote: bool,
3294 ) {
3295 let instrument = linear_instrument();
3296 let instrument_id = instrument.id();
3297 let instruments = build_instruments(&[instrument]);
3298 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3299 empty_subs();
3300 let (book_sync, ws_client, tasks) = book_context();
3301 book_depths.insert(instrument_id, book_depth);
3302 book_sync.record_subscription(instrument_id, Instant::now(), SnapshotGate::default());
3303
3304 let synced = book_sync.validate(instrument_id, true, 123_456_789, Instant::now());
3306 assert_eq!(synced, BookSequenceOutcome::Accept);
3307 quote_subs.insert(instrument_id);
3308
3309 let mut funding_cache = AHashMap::new();
3310 let ts_init = UnixNanos::new(1_709_891_680_000_000_000);
3311 let clock = AtomicTime::new(false, ts_init);
3312 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3313 let fixture = if msg_type == "snapshot" {
3314 "ws_orderbook_snapshot.json"
3315 } else {
3316 "ws_orderbook_delta.json"
3317 };
3318 let mut msg: BybitWsOrderbookDepthMsg =
3319 serde_json::from_str(&load_test_json(fixture)).unwrap();
3320 msg.topic = topic.into();
3321
3322 handle_ws_message(
3323 &BybitWsMessage::Orderbook(msg),
3324 &tx.into(),
3325 &instruments,
3326 Some(BybitProductType::Linear),
3327 &trade_subs,
3328 &ticker_subs,
3329 "e_subs,
3330 &book_depths,
3331 &book_sync,
3332 &ws_client,
3333 &greeks_subs,
3334 &Arc::new(AtomicMap::new()),
3335 &bar_types,
3336 true,
3337 &DashMap::new(),
3338 &mut funding_cache,
3339 Duration::ZERO,
3340 &tasks,
3341 &clock,
3342 );
3343
3344 if expect_book {
3345 let DataEvent::Data(Data::BookDeltas(deltas)) = rx.try_recv().unwrap() else {
3346 panic!("Expected book deltas");
3347 };
3348 assert_eq!(deltas.instrument_id, instrument_id);
3349 assert_eq!(
3350 deltas.deltas[0].action == BookAction::Clear,
3351 msg_type == "snapshot"
3352 );
3353
3354 if msg_type == "delta" {
3355 assert_eq!(deltas.deltas.len(), 2);
3356 assert_eq!(deltas.deltas[1].action, BookAction::Delete);
3357 assert_eq!(deltas.deltas[1].order.size, Quantity::from("0.000"));
3358 }
3359 }
3360
3361 if expect_quote {
3362 let expected = QuoteTick::new(
3363 instrument_id,
3364 Price::from("27450.00"),
3365 Price::from("27451.00"),
3366 Quantity::from("0.500"),
3367 Quantity::from("0.750"),
3368 UnixNanos::new(1_709_891_679_000_000_000),
3369 ts_init,
3370 );
3371 let DataEvent::Data(Data::Quote(quote)) = rx.try_recv().unwrap() else {
3372 panic!("Expected quote");
3373 };
3374 assert_eq!(quote, expected);
3375 }
3376
3377 rx.try_recv().unwrap_err();
3378 }
3379
3380 fn orderbook_msg(msg_type: &str, update_id: i64, levels: bool) -> BybitWsOrderbookDepthMsg {
3381 let fixture = if msg_type == "snapshot" {
3382 "ws_orderbook_snapshot.json"
3383 } else {
3384 "ws_orderbook_delta.json"
3385 };
3386
3387 let mut msg: BybitWsOrderbookDepthMsg =
3388 serde_json::from_str(&load_test_json(fixture)).unwrap();
3389 msg.topic = "orderbook.50.BTCUSDT".into();
3390 msg.data.u = update_id;
3391
3392 if !levels {
3393 msg.data.b.clear();
3394 msg.data.a.clear();
3395 }
3396
3397 msg
3398 }
3399
3400 #[rstest]
3401 fn test_handle_orderbook_delta_without_levels_advances_book() {
3402 let instrument = linear_instrument();
3403 let instrument_id = instrument.id();
3404 let (_, _, _, book_depths, _, _) = empty_subs();
3405 let (book_sync, ws_client, tasks) = book_context();
3406 book_depths.insert(instrument_id, 50);
3407 book_sync.record_subscription(instrument_id, Instant::now(), SnapshotGate::default());
3408 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3409 let sender = tx.into();
3410 let ts_init = UnixNanos::new(1);
3411
3412 for msg in [
3413 orderbook_msg("snapshot", 100, true),
3414 orderbook_msg("delta", 101, false),
3415 orderbook_msg("delta", 102, true),
3416 ] {
3417 handle_orderbook_deltas(
3418 &msg,
3419 &instrument,
3420 &sender,
3421 &book_depths,
3422 &book_sync,
3423 &ws_client,
3424 Duration::ZERO,
3425 &tasks,
3426 ts_init,
3427 );
3428 }
3429
3430 let mut emitted = Vec::new();
3431
3432 while let Ok(DataEvent::Data(Data::BookDeltas(deltas))) = rx.try_recv() {
3433 emitted.push(deltas.deltas[0].action);
3434 }
3435
3436 assert_eq!(emitted, [BookAction::Clear, BookAction::Update]);
3437 }
3438
3439 #[rstest]
3440 #[case::gap(102, "27450.00")]
3441 #[case::malformed_level(101, "not-a-price")]
3442 #[case::negative_update_id(-1, "27450.00")]
3443 #[tokio::test]
3444 async fn test_handle_orderbook_fault_suppresses_output_and_claims_recovery(
3445 #[case] update_id: i64,
3446 #[case] bid_price: &str,
3447 ) {
3448 let instrument = linear_instrument();
3449 let instrument_id = instrument.id();
3450 let (_, _, _, book_depths, _, _) = empty_subs();
3451 let book_sync = BookSyncTracker::default();
3452 let ws_client = BybitWebSocketClient::new_public(None, 20);
3453 let tasks = TaskGroup::new();
3454 let spawner = tasks.spawner().unwrap();
3455 book_depths.insert(instrument_id, 50);
3456 book_sync.record_subscription(instrument_id, Instant::now(), SnapshotGate::default());
3457 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3458 let sender = tx.into();
3459 let mut fault = orderbook_msg("delta", update_id, true);
3460 fault.data.b[0][0] = bid_price.to_string();
3461
3462 for msg in [orderbook_msg("snapshot", 100, true), fault] {
3463 handle_orderbook_deltas(
3464 &msg,
3465 &instrument,
3466 &sender,
3467 &book_depths,
3468 &book_sync,
3469 &ws_client,
3470 Duration::from_secs(1),
3471 &spawner,
3472 UnixNanos::new(1),
3473 );
3474 }
3475
3476 let DataEvent::Data(Data::BookDeltas(snapshot)) = rx.try_recv().unwrap() else {
3477 panic!("Expected book snapshot");
3478 };
3479
3480 assert_eq!(snapshot.deltas[0].action, BookAction::Clear);
3481 rx.try_recv().unwrap_err();
3482 assert!(book_sync.claim_recovery(instrument_id).is_none());
3483 tasks.begin_shutdown();
3484 }
3485
3486 #[tokio::test]
3487 async fn test_handle_reconnected_resyncs_books_of_reconnected_socket() {
3488 let instrument = linear_instrument();
3489 let instrument_id = instrument.id();
3490 let instruments = build_instruments(std::slice::from_ref(&instrument));
3491 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3492 empty_subs();
3493 let book_sync = BookSyncTracker::default();
3494 let ws_client = BybitWebSocketClient::new_public(None, 20);
3495 let tasks = TaskGroup::new();
3496 let spawner = tasks.spawner().unwrap();
3497 book_depths.insert(instrument_id, 50);
3498 book_sync.record_subscription(instrument_id, Instant::now(), SnapshotGate::default());
3499 let synced = book_sync.validate(instrument_id, true, 100, Instant::now());
3500 assert_eq!(synced, BookSequenceOutcome::Accept);
3501
3502 let mut funding_cache = AHashMap::new();
3503 let clock = get_atomic_clock_realtime();
3504 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3505 let sender = tx.into();
3506 let snapshot_timeout = Duration::from_secs(10);
3507
3508 for message in [
3510 BybitWsMessage::Reconnected,
3511 BybitWsMessage::Orderbook(orderbook_msg("delta", 101, true)),
3512 ] {
3513 handle_ws_message(
3514 &message,
3515 &sender,
3516 &instruments,
3517 Some(BybitProductType::Linear),
3518 &trade_subs,
3519 &ticker_subs,
3520 "e_subs,
3521 &book_depths,
3522 &book_sync,
3523 &ws_client,
3524 &greeks_subs,
3525 &Arc::new(AtomicMap::new()),
3526 &bar_types,
3527 true,
3528 &DashMap::new(),
3529 &mut funding_cache,
3530 snapshot_timeout,
3531 &spawner,
3532 clock,
3533 );
3534 }
3535
3536 let deadline = Instant::now() + snapshot_timeout;
3537 let expired = book_sync.take_expired_snapshots(BybitProductType::Linear, deadline);
3538
3539 rx.try_recv().unwrap_err();
3540 assert_eq!(expired.len(), 1);
3541 assert_eq!(expired[0].instrument_id, instrument_id);
3542 tasks.begin_shutdown();
3543 }
3544
3545 #[rstest]
3546 #[case::unsubscribe(false)]
3547 #[case::reset(true)]
3548 #[tokio::test]
3549 async fn test_book_removal_cancels_running_recovery(#[case] reset: bool) {
3550 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
3551 set_data_event_sender(tx);
3552 let mut client =
3553 BybitDataClient::new(*BYBIT_CLIENT_ID, BybitDataClientConfig::default()).unwrap();
3554 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
3555 client.book_depths.insert(instrument_id, 50);
3556 client.book_sync.record_subscription(
3557 instrument_id,
3558 Instant::now(),
3559 SnapshotGate::default(),
3560 );
3561 let recovery = client.book_sync.claim_recovery(instrument_id).unwrap();
3562
3563 if reset {
3564 client.reset().unwrap();
3565 } else {
3566 let command = UnsubscribeBookDeltas::new(
3567 instrument_id,
3568 Some(*BYBIT_CLIENT_ID),
3569 None,
3570 UUID4::new(),
3571 UnixNanos::default(),
3572 None,
3573 None,
3574 );
3575 client.unsubscribe_book_deltas(&command).unwrap();
3576 }
3577
3578 assert!(recovery.cancellation.is_cancelled());
3579 assert!(!client.book_depths.contains_key(&instrument_id));
3580 }
3581
3582 #[rstest]
3583 fn test_handle_orderbook_message_no_sub_no_event() {
3584 let instrument = linear_instrument();
3585 let instruments = build_instruments(&[instrument]);
3586 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3587 empty_subs();
3588 let (book_sync, ws_client, tasks) = book_context();
3589
3590 let mut funding_cache = AHashMap::new();
3591 let clock = get_atomic_clock_realtime();
3592
3593 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3594
3595 let json = load_test_json("ws_orderbook_snapshot.json");
3596 let msg: BybitWsOrderbookDepthMsg = serde_json::from_str(&json).unwrap();
3597 let ws_msg = BybitWsMessage::Orderbook(msg);
3598
3599 handle_ws_message(
3600 &ws_msg,
3601 &tx.into(),
3602 &instruments,
3603 Some(BybitProductType::Linear),
3604 &trade_subs,
3605 &ticker_subs,
3606 "e_subs,
3607 &book_depths,
3608 &book_sync,
3609 &ws_client,
3610 &greeks_subs,
3611 &Arc::new(AtomicMap::new()),
3612 &bar_types,
3613 true,
3614 &DashMap::new(),
3615 &mut funding_cache,
3616 Duration::ZERO,
3617 &tasks,
3618 clock,
3619 );
3620
3621 rx.try_recv().unwrap_err();
3622 }
3623
3624 #[rstest]
3625 fn test_handle_ticker_linear_does_not_emit_quote() {
3626 let instrument = linear_instrument();
3627 let instrument_id = instrument.id();
3628 let instruments = build_instruments(&[instrument]);
3629 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3630 empty_subs();
3631 let (book_sync, ws_client, tasks) = book_context();
3632
3633 let mut subs = AHashSet::new();
3634 subs.insert("quotes");
3635 ticker_subs.insert(instrument_id, subs);
3636
3637 let mut funding_cache = AHashMap::new();
3638 let clock = get_atomic_clock_realtime();
3639
3640 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3641
3642 let json = load_test_json("ws_ticker_linear.json");
3643 let msg: BybitWsTickerLinearMsg = serde_json::from_str(&json).unwrap();
3644 let ws_msg = BybitWsMessage::TickerLinear(msg);
3645
3646 handle_ws_message(
3647 &ws_msg,
3648 &tx.into(),
3649 &instruments,
3650 Some(BybitProductType::Linear),
3651 &trade_subs,
3652 &ticker_subs,
3653 "e_subs,
3654 &book_depths,
3655 &book_sync,
3656 &ws_client,
3657 &greeks_subs,
3658 &Arc::new(AtomicMap::new()),
3659 &bar_types,
3660 true,
3661 &DashMap::new(),
3662 &mut funding_cache,
3663 Duration::ZERO,
3664 &tasks,
3665 clock,
3666 );
3667
3668 rx.try_recv().unwrap_err();
3669 }
3670
3671 #[rstest]
3672 #[case::crypto_perpetual(linear_instrument())]
3673 #[case::perpetual_contract(tradfi_linear_instrument())]
3674 fn test_handle_ticker_linear_funding_dedup(#[case] instrument: InstrumentAny) {
3675 let instrument_id = instrument.id();
3676 let instruments = build_instruments(&[instrument]);
3677 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3678 empty_subs();
3679 let (book_sync, ws_client, tasks) = book_context();
3680
3681 let mut subs = AHashSet::new();
3682 subs.insert("funding");
3683 ticker_subs.insert(instrument_id, subs);
3684
3685 let mut funding_cache = AHashMap::new();
3686 let clock = get_atomic_clock_realtime();
3687
3688 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3689
3690 let json = load_test_json("ws_ticker_linear.json");
3691 let msg: BybitWsTickerLinearMsg = serde_json::from_str(&json).unwrap();
3692 let ws_msg = BybitWsMessage::TickerLinear(msg.clone());
3693
3694 handle_ws_message(
3695 &ws_msg,
3696 &tx.clone().into(),
3697 &instruments,
3698 Some(BybitProductType::Linear),
3699 &trade_subs,
3700 &ticker_subs,
3701 "e_subs,
3702 &book_depths,
3703 &book_sync,
3704 &ws_client,
3705 &greeks_subs,
3706 &Arc::new(AtomicMap::new()),
3707 &bar_types,
3708 true,
3709 &DashMap::new(),
3710 &mut funding_cache,
3711 Duration::ZERO,
3712 &tasks,
3713 clock,
3714 );
3715
3716 let event = rx.try_recv().unwrap();
3717 assert!(matches!(event, DataEvent::Data(Data::FundingRate(_))));
3718
3719 let ws_msg2 = BybitWsMessage::TickerLinear(msg);
3721 handle_ws_message(
3722 &ws_msg2,
3723 &tx.into(),
3724 &instruments,
3725 Some(BybitProductType::Linear),
3726 &trade_subs,
3727 &ticker_subs,
3728 "e_subs,
3729 &book_depths,
3730 &book_sync,
3731 &ws_client,
3732 &greeks_subs,
3733 &Arc::new(AtomicMap::new()),
3734 &bar_types,
3735 true,
3736 &DashMap::new(),
3737 &mut funding_cache,
3738 Duration::ZERO,
3739 &tasks,
3740 clock,
3741 );
3742
3743 rx.try_recv().unwrap_err();
3744 }
3745
3746 #[rstest]
3747 #[case::crypto_perpetual(linear_instrument())]
3748 #[case::perpetual_contract(tradfi_linear_instrument())]
3749 #[tokio::test]
3750 async fn test_subscribe_funding_rates_accepts_perpetuals(#[case] instrument: InstrumentAny) {
3751 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
3752 set_data_event_sender(tx);
3753 let mut client = BybitDataClient::new(
3754 *BYBIT_CLIENT_ID,
3755 BybitDataClientConfig {
3756 product_types: vec![BybitProductType::Linear],
3757 ..Default::default()
3758 },
3759 )
3760 .unwrap();
3761 let instrument_id = instrument.id();
3762 client.instruments.insert(instrument_id, instrument);
3763
3764 let command = SubscribeFundingRates::new(
3765 instrument_id,
3766 Some(*BYBIT_CLIENT_ID),
3767 None,
3768 UUID4::new(),
3769 UnixNanos::default(),
3770 None,
3771 None,
3772 );
3773 client.subscribe_funding_rates(command).unwrap();
3774
3775 let subs = client.ticker_subs.get_cloned(&instrument_id).unwrap();
3776 assert!(subs.contains("funding"));
3777 }
3778
3779 #[tokio::test]
3780 async fn test_funding_rate_list_continues_after_invalid_instrument() {
3781 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
3782 set_data_event_sender(tx);
3783
3784 let mut client = BybitDataClient::new(
3785 *BYBIT_CLIENT_ID,
3786 BybitDataClientConfig {
3787 product_types: vec![BybitProductType::Linear, BybitProductType::Option],
3788 ..Default::default()
3789 },
3790 )
3791 .unwrap();
3792
3793 let invalid = option_instrument();
3794 let first = linear_instruments().pop().unwrap();
3795 let last = tradfi_linear_instrument();
3796 let ids = [invalid.id(), first.id(), last.id()];
3797 for instrument in [invalid, first, last] {
3798 client.instruments.insert(instrument.id(), instrument);
3799 }
3800
3801 let command = SubscribeFundingRates::new(
3802 ids,
3803 Some(*BYBIT_CLIENT_ID),
3804 None,
3805 UUID4::new(),
3806 UnixNanos::from(17),
3807 Some(UUID4::new()),
3808 None,
3809 );
3810 let error = client.subscribe_funding_rates(command).unwrap_err();
3811 let partial = error.downcast_ref::<SubscriptionError>().unwrap();
3812 assert_eq!(partial.instrument_ids_accepted, vec![ids[1], ids[2]]);
3813 assert_eq!(partial.bar_types_accepted, Vec::<BarType>::new());
3814 assert!(!client.ticker_subs.contains_key(&ids[0]));
3815
3816 for id in &ids[1..] {
3817 assert_eq!(
3818 client.ticker_subs.get_cloned(id).unwrap(),
3819 AHashSet::from_iter(["funding"])
3820 );
3821 }
3822
3823 client
3824 .unsubscribe_funding_rates(&UnsubscribeFundingRates::new(
3825 [ids[1], ids[2]],
3826 Some(*BYBIT_CLIENT_ID),
3827 None,
3828 UUID4::new(),
3829 UnixNanos::from(23),
3830 None,
3831 None,
3832 ))
3833 .unwrap();
3834 assert!(client.ticker_subs.is_empty());
3835 client.disconnect().await.unwrap();
3836 }
3837
3838 #[rstest]
3839 fn test_handle_ticker_linear_mark_and_index_prices() {
3840 let instrument = linear_instrument();
3841 let instrument_id = instrument.id();
3842 let instruments = build_instruments(&[instrument]);
3843 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3844 empty_subs();
3845 let (book_sync, ws_client, tasks) = book_context();
3846
3847 let mut subs = AHashSet::new();
3848 subs.insert("mark_prices");
3849 subs.insert("index_prices");
3850 ticker_subs.insert(instrument_id, subs);
3851
3852 let mut funding_cache = AHashMap::new();
3853 let clock = get_atomic_clock_realtime();
3854
3855 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3856
3857 let json = load_test_json("ws_ticker_linear.json");
3858 let msg: BybitWsTickerLinearMsg = serde_json::from_str(&json).unwrap();
3859 let ws_msg = BybitWsMessage::TickerLinear(msg);
3860
3861 handle_ws_message(
3862 &ws_msg,
3863 &tx.into(),
3864 &instruments,
3865 Some(BybitProductType::Linear),
3866 &trade_subs,
3867 &ticker_subs,
3868 "e_subs,
3869 &book_depths,
3870 &book_sync,
3871 &ws_client,
3872 &greeks_subs,
3873 &Arc::new(AtomicMap::new()),
3874 &bar_types,
3875 true,
3876 &DashMap::new(),
3877 &mut funding_cache,
3878 Duration::ZERO,
3879 &tasks,
3880 clock,
3881 );
3882
3883 let event1 = rx.try_recv().unwrap();
3884 assert!(matches!(event1, DataEvent::Data(Data::MarkPrice(_))));
3885
3886 let event2 = rx.try_recv().unwrap();
3887 assert!(matches!(event2, DataEvent::Data(Data::IndexPrice(_))));
3888 }
3889
3890 #[rstest]
3891 #[case::option(BybitProductType::Option, true)]
3892 #[case::linear(BybitProductType::Linear, false)]
3893 fn test_handle_reconnected_clears_caches(
3894 #[case] product_type: BybitProductType,
3895 #[case] clear_option_quotes: bool,
3896 ) {
3897 let instruments = AHashMap::new();
3898 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3899 empty_subs();
3900 let (book_sync, ws_client, tasks) = book_context();
3901
3902 let mut funding_cache = AHashMap::new();
3903 let clock = get_atomic_clock_realtime();
3904
3905 let instrument_id = InstrumentId::from("BTCUSDT-LINEAR.BYBIT");
3906
3907 let option_quote = QuoteTick::new(
3908 instrument_id,
3909 Price::from("100.00"),
3910 Price::from("101.00"),
3911 Quantity::from("1.0"),
3912 Quantity::from("1.0"),
3913 UnixNanos::default(),
3914 UnixNanos::default(),
3915 );
3916 funding_cache.insert(
3917 Ustr::from("BTCUSDT"),
3918 (
3919 Some("-0.001".to_string()),
3920 Some("1000".to_string()),
3921 Some("8".to_string()),
3922 ),
3923 );
3924
3925 let last_quotes = DashMap::new();
3926 last_quotes.insert(instrument_id, option_quote);
3927
3928 let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
3929
3930 handle_ws_message(
3931 &BybitWsMessage::Reconnected,
3932 &tx.into(),
3933 &instruments,
3934 Some(product_type),
3935 &trade_subs,
3936 &ticker_subs,
3937 "e_subs,
3938 &book_depths,
3939 &book_sync,
3940 &ws_client,
3941 &greeks_subs,
3942 &Arc::new(AtomicMap::new()),
3943 &bar_types,
3944 true,
3945 &last_quotes,
3946 &mut funding_cache,
3947 Duration::ZERO,
3948 &tasks,
3949 clock,
3950 );
3951
3952 assert!(funding_cache.is_empty());
3953 assert_eq!(
3954 last_quotes.get(&instrument_id).map(|quote| *quote),
3955 if clear_option_quotes {
3956 None
3957 } else {
3958 Some(option_quote)
3959 },
3960 );
3961 }
3962
3963 #[rstest]
3964 fn test_handle_ticker_option_greeks() {
3965 let instrument = option_instrument();
3968 let instrument_id = instrument.id();
3969
3970 let ticker_key = Ustr::from("BTC-6JAN23-17500-C-OPTION");
3972 let mut instruments = AHashMap::new();
3973 instruments.insert(ticker_key, instrument);
3974
3975 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
3976 empty_subs();
3977
3978 let (book_sync, ws_client, tasks) = book_context();
3979 greeks_subs.insert(instrument_id);
3980
3981 let mut funding_cache = AHashMap::new();
3982 let clock = get_atomic_clock_realtime();
3983
3984 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
3985
3986 let json = load_test_json("ws_ticker_option.json");
3987 let msg: BybitWsTickerOptionMsg = serde_json::from_str(&json).unwrap();
3988 let ws_msg = BybitWsMessage::TickerOption(msg);
3989
3990 handle_ws_message(
3991 &ws_msg,
3992 &tx.into(),
3993 &instruments,
3994 Some(BybitProductType::Option),
3995 &trade_subs,
3996 &ticker_subs,
3997 "e_subs,
3998 &book_depths,
3999 &book_sync,
4000 &ws_client,
4001 &greeks_subs,
4002 &Arc::new(AtomicMap::new()),
4003 &bar_types,
4004 true,
4005 &DashMap::new(),
4006 &mut funding_cache,
4007 Duration::ZERO,
4008 &tasks,
4009 clock,
4010 );
4011
4012 let event = rx.try_recv().unwrap();
4013 assert!(matches!(event, DataEvent::Data(Data::OptionGreeks(_))));
4014 }
4015
4016 #[rstest]
4017 #[case::bid_price("9.5", "10", "2", "5")]
4018 #[case::ask_price("9", "11", "2", "5")]
4019 #[case::bid_size("9", "10", "3", "5")]
4020 #[case::ask_size("9", "10", "2", "6")]
4021 fn test_handle_ticker_option_quote_dedup(
4022 #[case] bid_price: &str,
4023 #[case] ask_price: &str,
4024 #[case] bid_size: &str,
4025 #[case] ask_size: &str,
4026 ) {
4027 let instrument = option_instrument();
4029 let instrument_id = instrument.id();
4030 let mut instruments = AHashMap::new();
4031 instruments.insert(Ustr::from("BTC-6JAN23-17500-C-OPTION"), instrument.clone());
4032
4033 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
4034 empty_subs();
4035 let (book_sync, ws_client, tasks) = book_context();
4036
4037 let mut subs = AHashSet::new();
4038 subs.insert("quotes");
4039 subs.insert("mark_prices");
4040 ticker_subs.insert(instrument_id, subs);
4041
4042 let last_quotes = DashMap::new();
4043 let mut funding_cache = AHashMap::new();
4044 let ts1 = UnixNanos::new(1_672_917_511_100_000_000);
4045 let ts2 = UnixNanos::new(1_672_917_511_200_000_000);
4046 let ts3 = UnixNanos::new(1_672_917_511_300_000_000);
4047 let ts4 = UnixNanos::new(1_672_917_511_400_000_000);
4048 let clock = AtomicTime::new(false, ts1);
4049 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
4050
4051 let json = load_test_json("ws_ticker_option.json");
4052 let mut msg: BybitWsTickerOptionMsg = serde_json::from_str(&json).unwrap();
4053
4054 msg.data.bid_price = "9".to_string();
4055 msg.data.bid_size = "2".to_string();
4056 msg.data.ask_size = "5".to_string();
4057 msg.data.mark_price = "7.8".to_string();
4058
4059 let mut unchanged = msg.clone();
4061 unchanged.ts += 100;
4062 let mut changed = msg.clone();
4063 changed.ts += 200;
4064 changed.data.bid_price = bid_price.to_string();
4065 changed.data.ask_price = ask_price.to_string();
4066 changed.data.bid_size = bid_size.to_string();
4067 changed.data.ask_size = ask_size.to_string();
4068 changed.data.mark_price = "8.2".to_string();
4069 let mut repeated = changed.clone();
4070 repeated.ts += 100;
4071
4072 for (ts_init, msg) in [
4073 (ts1, msg),
4074 (ts2, unchanged),
4075 (ts3, changed),
4076 (ts4, repeated),
4077 ] {
4078 clock.set_time(ts_init);
4079 handle_ws_message(
4080 &BybitWsMessage::TickerOption(msg),
4081 &tx.clone().into(),
4082 &instruments,
4083 Some(BybitProductType::Option),
4084 &trade_subs,
4085 &ticker_subs,
4086 "e_subs,
4087 &book_depths,
4088 &book_sync,
4089 &ws_client,
4090 &greeks_subs,
4091 &Arc::new(AtomicMap::new()),
4092 &bar_types,
4093 true,
4094 &last_quotes,
4095 &mut funding_cache,
4096 Duration::ZERO,
4097 &tasks,
4098 &clock,
4099 );
4100 }
4101
4102 let mut quotes = Vec::new();
4103 let mut mark_prices = Vec::new();
4104
4105 while let Ok(event) = rx.try_recv() {
4106 match event {
4107 DataEvent::Data(Data::Quote(quote)) => quotes.push(quote),
4108 DataEvent::Data(Data::MarkPrice(mark)) => mark_prices.push(mark),
4109 other => panic!("Unexpected event: {other:?}"),
4110 }
4111 }
4112
4113 let price = |value: &str| {
4114 Price::from_decimal_dp(value.parse().unwrap(), instrument.price_precision()).unwrap()
4115 };
4116
4117 let size = |value: &str| {
4118 Quantity::from_decimal_dp(value.parse().unwrap(), instrument.size_precision()).unwrap()
4119 };
4120
4121 let initial = QuoteTick::new(
4122 instrument_id,
4123 price("9"),
4124 price("10"),
4125 size("2"),
4126 size("5"),
4127 UnixNanos::new(1_672_917_511_074_000_000),
4128 ts1,
4129 );
4130
4131 let changed = QuoteTick::new(
4132 instrument_id,
4133 price(bid_price),
4134 price(ask_price),
4135 size(bid_size),
4136 size(ask_size),
4137 UnixNanos::new(1_672_917_511_274_000_000),
4138 ts3,
4139 );
4140
4141 let expected_marks = [
4142 ("7.8", 1_672_917_511_074_000_000, ts1),
4143 ("7.8", 1_672_917_511_174_000_000, ts2),
4144 ("8.2", 1_672_917_511_274_000_000, ts3),
4145 ("8.2", 1_672_917_511_374_000_000, ts4),
4146 ]
4147 .map(|(value, ts_event, ts_init)| {
4148 MarkPriceUpdate::new(
4149 instrument_id,
4150 price(value),
4151 UnixNanos::new(ts_event),
4152 ts_init,
4153 )
4154 });
4155
4156 assert_eq!(quotes, vec![initial, changed]);
4157 assert_eq!(mark_prices, expected_marks);
4158 }
4159
4160 #[rstest]
4161 #[case::resubscribe(false, false)]
4162 #[case::resubscribe_after_late_quote(false, true)]
4163 #[case::reconnect(true, false)]
4164 #[tokio::test]
4165 async fn test_option_quote_subscription_emits_unchanged_initial_quote(
4166 #[case] reconnect: bool,
4167 #[case] late_quote: bool,
4168 ) {
4169 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
4170 set_data_event_sender(tx);
4171
4172 let mut client = BybitDataClient::new(
4173 *BYBIT_CLIENT_ID,
4174 BybitDataClientConfig {
4175 product_types: vec![BybitProductType::Option],
4176 ..Default::default()
4177 },
4178 )
4179 .unwrap();
4180
4181 let instrument = option_instrument();
4182 let instrument_id = instrument.id();
4183 client.instruments.insert(instrument_id, instrument.clone());
4184 let instruments = build_instruments(std::slice::from_ref(&instrument));
4185 let ts1 = UnixNanos::new(1_700_000_000_000_000_000);
4186 let ts2 = UnixNanos::new(1_700_000_000_100_000_000);
4187 let ts3 = UnixNanos::new(1_700_000_000_200_000_000);
4188 let clock = AtomicTime::new(false, ts1);
4189 let mut msg: BybitWsTickerOptionMsg =
4190 serde_json::from_str(&load_test_json("ws_ticker_option.json")).unwrap();
4191 msg.data.symbol = instrument.raw_symbol().inner();
4192 msg.data.bid_price = "9".to_string();
4193 msg.data.ask_price = "10".to_string();
4194 msg.data.bid_size = "2".to_string();
4195 msg.data.ask_size = "5".to_string();
4196
4197 let initial = QuoteTick::new(
4198 instrument_id,
4199 Price::from("9.0"),
4200 Price::from("10.0"),
4201 Quantity::from("2"),
4202 Quantity::from("5"),
4203 UnixNanos::new(1_672_917_511_074_000_000),
4204 ts1,
4205 );
4206
4207 let subscribe = || {
4208 SubscribeQuotes::new(
4209 instrument_id,
4210 Some(*BYBIT_CLIENT_ID),
4211 None,
4212 UUID4::new(),
4213 UnixNanos::default(),
4214 None,
4215 None,
4216 )
4217 };
4218
4219 let handle = |client: &BybitDataClient, message: &BybitWsMessage| {
4220 let ws = client
4221 .get_ws_client_for_product(BybitProductType::Option)
4222 .unwrap();
4223 handle_ws_message(
4224 message,
4225 &client.data_sender,
4226 &instruments,
4227 Some(BybitProductType::Option),
4228 &client.trade_subs,
4229 &client.ticker_subs,
4230 &client.quote_subs,
4231 &client.book_depths,
4232 &client.book_sync,
4233 ws,
4234 &client.option_greeks_subs,
4235 &client.liquidation_subs,
4236 ws.bar_types_cache(),
4237 true,
4238 &client.last_quotes,
4239 &mut AHashMap::new(),
4240 Duration::ZERO,
4241 &client.command_tasks.spawner().unwrap(),
4242 &clock,
4243 );
4244 };
4245
4246 client.subscribe_quotes(subscribe()).unwrap();
4247 handle(&client, &BybitWsMessage::TickerOption(msg.clone()));
4248 if reconnect {
4249 handle(&client, &BybitWsMessage::Reconnected);
4250 } else {
4251 client
4252 .unsubscribe_quotes(&UnsubscribeQuotes::new(
4253 instrument_id,
4254 Some(*BYBIT_CLIENT_ID),
4255 None,
4256 UUID4::new(),
4257 UnixNanos::default(),
4258 None,
4259 None,
4260 ))
4261 .unwrap();
4262 clock.set_time(ts2);
4263 msg.ts += 100;
4264 handle(&client, &BybitWsMessage::TickerOption(msg.clone()));
4265 if late_quote {
4266 client.last_quotes.insert(instrument_id, initial);
4267 }
4268
4269 client.subscribe_quotes(subscribe()).unwrap();
4270 }
4271
4272 clock.set_time(ts3);
4273 msg.ts = 1_672_917_511_274;
4274 handle(&client, &BybitWsMessage::TickerOption(msg));
4275 let mut quotes = Vec::new();
4276
4277 while let Ok(event) = rx.try_recv() {
4278 match event {
4279 DataEvent::Data(Data::Quote(quote)) => quotes.push(quote),
4280 other => panic!("Unexpected event: {other:?}"),
4281 }
4282 }
4283
4284 assert_eq!(
4285 quotes,
4286 vec![
4287 initial,
4288 QuoteTick {
4289 ts_event: UnixNanos::new(1_672_917_511_274_000_000),
4290 ts_init: ts3,
4291 ..initial
4292 }
4293 ]
4294 );
4295 }
4296
4297 #[rstest]
4298 fn test_handle_execution_message_ignored_by_data() {
4299 let instruments = AHashMap::new();
4300 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
4301 empty_subs();
4302 let (book_sync, ws_client, tasks) = book_context();
4303
4304 let mut funding_cache = AHashMap::new();
4305 let clock = get_atomic_clock_realtime();
4306
4307 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
4308
4309 let json = load_test_json("ws_account_order.json");
4310 let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
4311 serde_json::from_str(&json).unwrap();
4312 let ws_msg = BybitWsMessage::AccountOrder(msg);
4313
4314 handle_ws_message(
4315 &ws_msg,
4316 &tx.into(),
4317 &instruments,
4318 None,
4319 &trade_subs,
4320 &ticker_subs,
4321 "e_subs,
4322 &book_depths,
4323 &book_sync,
4324 &ws_client,
4325 &greeks_subs,
4326 &Arc::new(AtomicMap::new()),
4327 &bar_types,
4328 true,
4329 &DashMap::new(),
4330 &mut funding_cache,
4331 Duration::ZERO,
4332 &tasks,
4333 clock,
4334 );
4335
4336 rx.try_recv().unwrap_err();
4337 }
4338
4339 #[rstest]
4340 fn test_instrument_resolution_with_product_type() {
4341 let instrument = linear_instrument();
4342
4343 let mut map = AHashMap::new();
4344 map.insert(instrument.id().symbol.inner(), instrument.clone());
4345
4346 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
4347 empty_subs();
4348
4349 let (book_sync, ws_client, tasks) = book_context();
4350 trade_subs.insert(instrument.id());
4351
4352 let mut funding_cache = AHashMap::new();
4353 let clock = get_atomic_clock_realtime();
4354 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
4355
4356 let json = load_test_json("ws_public_trade.json");
4357 let msg: BybitWsTradeMsg = serde_json::from_str(&json).unwrap();
4358
4359 handle_ws_message(
4361 &BybitWsMessage::Trade(msg.clone()),
4362 &tx.clone().into(),
4363 &map,
4364 None,
4365 &trade_subs,
4366 &ticker_subs,
4367 "e_subs,
4368 &book_depths,
4369 &book_sync,
4370 &ws_client,
4371 &greeks_subs,
4372 &Arc::new(AtomicMap::new()),
4373 &bar_types,
4374 true,
4375 &DashMap::new(),
4376 &mut funding_cache,
4377 Duration::ZERO,
4378 &tasks,
4379 clock,
4380 );
4381 rx.try_recv().unwrap_err();
4382
4383 handle_ws_message(
4385 &BybitWsMessage::Trade(msg),
4386 &tx.into(),
4387 &map,
4388 Some(BybitProductType::Linear),
4389 &trade_subs,
4390 &ticker_subs,
4391 "e_subs,
4392 &book_depths,
4393 &book_sync,
4394 &ws_client,
4395 &greeks_subs,
4396 &Arc::new(AtomicMap::new()),
4397 &bar_types,
4398 true,
4399 &DashMap::new(),
4400 &mut funding_cache,
4401 Duration::ZERO,
4402 &tasks,
4403 clock,
4404 );
4405
4406 let event = rx.try_recv().unwrap();
4407 assert!(matches!(event, DataEvent::Data(Data::Trade(_))));
4408 }
4409
4410 #[rstest]
4411 fn test_handle_trade_filters_by_subscription() {
4412 let instrument = linear_instrument();
4413 let instruments = build_instruments(std::slice::from_ref(&instrument));
4414 let (trade_subs, ticker_subs, quote_subs, book_depths, greeks_subs, bar_types) =
4415 empty_subs();
4416 let (book_sync, ws_client, tasks) = book_context();
4417
4418 let mut funding_cache = AHashMap::new();
4419 let clock = get_atomic_clock_realtime();
4420 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
4421
4422 let json = load_test_json("ws_public_trade.json");
4423 let msg: BybitWsTradeMsg = serde_json::from_str(&json).unwrap();
4424
4425 handle_ws_message(
4427 &BybitWsMessage::Trade(msg.clone()),
4428 &tx.clone().into(),
4429 &instruments,
4430 Some(BybitProductType::Linear),
4431 &trade_subs,
4432 &ticker_subs,
4433 "e_subs,
4434 &book_depths,
4435 &book_sync,
4436 &ws_client,
4437 &greeks_subs,
4438 &Arc::new(AtomicMap::new()),
4439 &bar_types,
4440 true,
4441 &DashMap::new(),
4442 &mut funding_cache,
4443 Duration::ZERO,
4444 &tasks,
4445 clock,
4446 );
4447 rx.try_recv().unwrap_err();
4448
4449 trade_subs.insert(instrument.id());
4451 handle_ws_message(
4452 &BybitWsMessage::Trade(msg),
4453 &tx.into(),
4454 &instruments,
4455 Some(BybitProductType::Linear),
4456 &trade_subs,
4457 &ticker_subs,
4458 "e_subs,
4459 &book_depths,
4460 &book_sync,
4461 &ws_client,
4462 &greeks_subs,
4463 &Arc::new(AtomicMap::new()),
4464 &bar_types,
4465 true,
4466 &DashMap::new(),
4467 &mut funding_cache,
4468 Duration::ZERO,
4469 &tasks,
4470 clock,
4471 );
4472 let event = rx.try_recv().unwrap();
4473 assert!(matches!(event, DataEvent::Data(Data::Trade(_))));
4474 }
4475
4476 const CREDENTIAL_CASE_VAR: &str = "BYBIT_DATA_CREDENTIAL_CASE";
4477 const CREDENTIAL_CHILD: &str = "data::tests::test_data_client_credentials_child";
4478 const CREDENTIAL_ENV_VARS: [&str; 6] = [
4479 "BYBIT_API_KEY",
4480 "BYBIT_API_SECRET",
4481 "BYBIT_DEMO_API_KEY",
4482 "BYBIT_DEMO_API_SECRET",
4483 "BYBIT_TESTNET_API_KEY",
4484 "BYBIT_TESTNET_API_SECRET",
4485 ];
4486 const MAINNET_KEY: &str = "mainnet-data-key";
4487 const MAINNET_SECRET: &str = "mainnet-data-secret";
4488 const DEMO_KEY: &str = "demo-data-key";
4489 const DEMO_SECRET: &str = "demo-data-secret";
4490 const TESTNET_KEY: &str = "testnet-data-key";
4491 const TESTNET_SECRET: &str = "testnet-data-secret";
4492 const CONFIG_KEY: &str = "config-data-key";
4493 const CONFIG_SECRET: &str = "config-data-secret";
4494
4495 #[rstest]
4496 fn test_data_client_resolves_credentials_from_selected_environment() {
4497 let all_env = [
4499 ("BYBIT_API_KEY", MAINNET_KEY),
4500 ("BYBIT_API_SECRET", MAINNET_SECRET),
4501 ("BYBIT_DEMO_API_KEY", DEMO_KEY),
4502 ("BYBIT_DEMO_API_SECRET", DEMO_SECRET),
4503 ("BYBIT_TESTNET_API_KEY", TESTNET_KEY),
4504 ("BYBIT_TESTNET_API_SECRET", TESTNET_SECRET),
4505 ];
4506 let cases: &[(&str, &[(&str, &str)])] = &[
4507 ("explicit_without_env", &[]),
4508 ("explicit_precedes_env", &all_env),
4509 ("mainnet_env", &all_env),
4510 ("demo_env", &all_env),
4511 ("testnet_env", &all_env),
4512 ("absent", &[]),
4513 ("partial_key_only", &[("BYBIT_API_KEY", MAINNET_KEY)]),
4514 (
4515 "partial_secret_only",
4516 &[("BYBIT_API_SECRET", MAINNET_SECRET)],
4517 ),
4518 (
4519 "demo_ignores_mainnet",
4520 &[
4521 ("BYBIT_API_KEY", MAINNET_KEY),
4522 ("BYBIT_API_SECRET", MAINNET_SECRET),
4523 ],
4524 ),
4525 (
4526 "explicit_key_with_env_secret",
4527 &[("BYBIT_API_SECRET", MAINNET_SECRET)],
4528 ),
4529 ];
4530
4531 for (case, env) in cases {
4532 assert_credential_case(case, env);
4533 }
4534 }
4535
4536 #[rstest]
4537 #[ignore = "runs only in an isolated child process"]
4538 fn test_data_client_credentials_child() {
4539 let Ok(case) = std::env::var(CREDENTIAL_CASE_VAR) else {
4540 return;
4541 };
4542
4543 match case.as_str() {
4544 "explicit_without_env" => assert_credential(
4545 &client(
4546 BybitEnvironment::Mainnet,
4547 Some(CONFIG_KEY),
4548 Some(CONFIG_SECRET),
4549 ),
4550 CONFIG_KEY,
4551 CONFIG_SECRET,
4552 ),
4553 "explicit_precedes_env" => assert_credential(
4554 &client(
4555 BybitEnvironment::Mainnet,
4556 Some(CONFIG_KEY),
4557 Some(CONFIG_SECRET),
4558 ),
4559 CONFIG_KEY,
4560 CONFIG_SECRET,
4561 ),
4562 "mainnet_env" => assert_credential(
4563 &client(BybitEnvironment::Mainnet, None, None),
4564 MAINNET_KEY,
4565 MAINNET_SECRET,
4566 ),
4567 "demo_env" => assert_credential(
4568 &client(BybitEnvironment::Demo, None, None),
4569 DEMO_KEY,
4570 DEMO_SECRET,
4571 ),
4572 "testnet_env" => assert_credential(
4573 &client(BybitEnvironment::Testnet, None, None),
4574 TESTNET_KEY,
4575 TESTNET_SECRET,
4576 ),
4577 "absent" => assert_public(&client(BybitEnvironment::Mainnet, None, None)),
4578 "partial_key_only" | "partial_secret_only" => {
4579 assert_public(&client(BybitEnvironment::Mainnet, None, None));
4580 }
4581 "demo_ignores_mainnet" => assert_public(&client(BybitEnvironment::Demo, None, None)),
4582 "explicit_key_with_env_secret" => assert_credential(
4583 &client(BybitEnvironment::Mainnet, Some(CONFIG_KEY), None),
4584 CONFIG_KEY,
4585 MAINNET_SECRET,
4586 ),
4587 #[cfg(feature = "python")]
4588 "python_explicit" => assert_credential(
4589 &python_client(
4590 BybitEnvironment::Mainnet,
4591 Some(CONFIG_KEY),
4592 Some(CONFIG_SECRET),
4593 ),
4594 CONFIG_KEY,
4595 CONFIG_SECRET,
4596 ),
4597 #[cfg(feature = "python")]
4598 "python_testnet_env" => assert_credential(
4599 &python_client(BybitEnvironment::Testnet, None, None),
4600 TESTNET_KEY,
4601 TESTNET_SECRET,
4602 ),
4603 #[cfg(feature = "python")]
4604 "python_absent" => assert_public(&python_client(BybitEnvironment::Demo, None, None)),
4605 #[cfg(feature = "python")]
4606 "python_demo_ignores_mainnet" => {
4607 assert_public(&python_client(BybitEnvironment::Demo, None, None));
4608 }
4609 other => panic!("unknown credential case {other}"),
4610 }
4611 }
4612
4613 #[cfg(feature = "python")]
4614 #[rstest]
4615 fn test_python_data_config_resolves_credentials_from_selected_environment() {
4616 let mainnet_env = [
4617 ("BYBIT_API_KEY", MAINNET_KEY),
4618 ("BYBIT_API_SECRET", MAINNET_SECRET),
4619 ];
4620 let testnet_env = [
4621 ("BYBIT_TESTNET_API_KEY", TESTNET_KEY),
4622 ("BYBIT_TESTNET_API_SECRET", TESTNET_SECRET),
4623 ];
4624 let cases: &[(&str, &[(&str, &str)])] = &[
4625 ("python_explicit", &mainnet_env),
4626 ("python_testnet_env", &testnet_env),
4627 ("python_absent", &[]),
4628 ("python_demo_ignores_mainnet", &mainnet_env),
4629 ];
4630
4631 for (case, env) in cases {
4632 assert_credential_case(case, env);
4633 }
4634 }
4635
4636 fn assert_credential_case(case: &str, env: &[(&str, &str)]) {
4637 let mut command =
4638 Command::new(std::env::current_exe().expect("test executable must exist"));
4639 command
4640 .arg(CREDENTIAL_CHILD)
4641 .arg("--exact")
4642 .arg("--ignored")
4643 .arg("--test-threads=1")
4644 .env(CREDENTIAL_CASE_VAR, case);
4645
4646 for name in CREDENTIAL_ENV_VARS {
4647 command.env_remove(name);
4648 }
4649
4650 for (name, value) in env {
4651 command.env(name, value);
4652 }
4653
4654 let output = command.output().expect("isolated credential test must run");
4655 let stdout = String::from_utf8_lossy(&output.stdout);
4656 let stderr = String::from_utf8_lossy(&output.stderr);
4657 assert!(
4658 output.status.success(),
4659 "credential case {case} failed\nstdout:\n{stdout}\nstderr:\n{stderr}",
4660 );
4661 assert!(
4662 stdout.contains("1 passed"),
4663 "credential case {case} did not run the child test\nstdout:\n{stdout}\nstderr:\n{stderr}",
4664 );
4665 }
4666
4667 #[cfg(feature = "python")]
4668 fn python_client(
4669 environment: BybitEnvironment,
4670 api_key: Option<&str>,
4671 api_secret: Option<&str>,
4672 ) -> BybitDataClient {
4673 pyo3::Python::initialize();
4674
4675 let config = pyo3::Python::attach(|py| {
4676 let module = pyo3::types::PyModule::new(py, "bybit").expect("Bybit module");
4677 crate::python::bybit(py, &module).expect("Bybit Python module should register");
4678 let cls = module
4679 .getattr("BybitDataClientConfig")
4680 .expect("BybitDataClientConfig");
4681 let kwargs = pyo3::types::PyDict::new(py);
4682 kwargs
4683 .set_item("environment", environment)
4684 .expect("environment");
4685 kwargs
4686 .set_item("product_types", vec![BybitProductType::Linear])
4687 .expect("product types");
4688
4689 if let Some(key) = api_key {
4690 kwargs.set_item("api_key", key).expect("api key");
4691 }
4692
4693 if let Some(secret) = api_secret {
4694 kwargs.set_item("api_secret", secret).expect("api secret");
4695 }
4696
4697 let obj = cls
4698 .call((), Some(&kwargs))
4699 .expect("Python data config should build");
4700 obj.extract().expect("Python data config should extract")
4701 });
4702
4703 build_client(config)
4704 }
4705
4706 fn client(
4707 environment: BybitEnvironment,
4708 api_key: Option<&str>,
4709 api_secret: Option<&str>,
4710 ) -> BybitDataClient {
4711 build_client(BybitDataClientConfig {
4712 environment,
4713 api_key: api_key.map(Into::into),
4714 api_secret: api_secret.map(Into::into),
4715 product_types: vec![BybitProductType::Linear],
4716 ..Default::default()
4717 })
4718 }
4719
4720 fn build_client(config: BybitDataClientConfig) -> BybitDataClient {
4721 let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel();
4722 set_data_event_sender(sender);
4723 BybitDataClient::new(*BYBIT_CLIENT_ID, config).unwrap()
4724 }
4725
4726 fn assert_credential(client: &BybitDataClient, key: &str, secret: &str) {
4727 let credential = client
4728 .http_client
4729 .credential()
4730 .expect("data client should store a complete credential pair");
4731 let expected = Credential::new(key, secret);
4732
4733 assert_eq!(credential.api_key(), key);
4734 assert_eq!(
4735 credential.sign_with_payload("1700000000000", 5_000, Some("category=linear")),
4736 expected.sign_with_payload("1700000000000", 5_000, Some("category=linear")),
4737 );
4738 assert!(client.ws_clients.iter().all(|ws| ws.credential().is_none()));
4739 }
4740
4741 fn assert_public(client: &BybitDataClient) {
4742 assert!(client.http_client.credential().is_none());
4743 assert!(client.ws_clients.iter().all(|ws| ws.credential().is_none()));
4744 }
4745}