1use std::{
19 sync::{
20 Arc,
21 atomic::{AtomicBool, Ordering},
22 },
23 time::Duration,
24};
25
26use ahash::{AHashMap, AHashSet};
27use anyhow::Context;
28use async_trait::async_trait;
29use futures_util::StreamExt;
30use indexmap::IndexMap;
31use nautilus_common::{
32 clients::{DataClient, execute_bar_subscriptions, execute_instrument_subscriptions},
33 live::{runner::get_data_event_sender, sender::EventSender},
34 log_debug, log_info,
35 messages::{
36 DataEvent, DataResponse,
37 data::{
38 BarsResponse, BookResponse, CustomDataResponse, InstrumentResponse,
39 InstrumentsResponse, OptionChainReferencePriceResponse, RequestBars,
40 RequestBookSnapshot, RequestCustomData, RequestInstrument, RequestInstruments,
41 RequestOptionChainReferencePrice, RequestTrades, SubscribeBars, SubscribeBookDeltas,
42 SubscribeBookDepth, SubscribeCustomData, SubscribeFundingRates, SubscribeIndexPrices,
43 SubscribeInstrument, SubscribeInstrumentStatus, SubscribeInstruments,
44 SubscribeMarkPrices, SubscribeOptionGreeks, SubscribeQuotes, SubscribeTrades,
45 TradesResponse, UnsubscribeBars, UnsubscribeBookDeltas, UnsubscribeBookDepth,
46 UnsubscribeCustomData, UnsubscribeFundingRates, UnsubscribeIndexPrices,
47 UnsubscribeInstrument, UnsubscribeInstrumentStatus, UnsubscribeInstruments,
48 UnsubscribeMarkPrices, UnsubscribeOptionGreeks, UnsubscribeQuotes, UnsubscribeTrades,
49 },
50 },
51};
52use nautilus_core::{
53 AtomicMap, AtomicSet, Params,
54 datetime::datetime_to_unix_nanos,
55 time::{AtomicTime, get_atomic_clock_realtime},
56};
57use nautilus_live::{
58 SocketControl,
59 task::{TaskGroup, TaskGroupGuard},
60};
61use nautilus_model::{
62 data::{CustomData, Data, DataType},
63 enums::BookType,
64 identifiers::{ClientId, InstrumentId, Venue},
65 instruments::{Instrument, InstrumentAny},
66 types::Price,
67};
68use parking_lot::Mutex;
69use rust_decimal::Decimal;
70use tokio_util::sync::CancellationToken;
71
72use crate::{
73 common::{
74 capabilities,
75 consts::{
76 DERIBIT_BOOK_DEFAULT_DEPTH, DERIBIT_BOOK_DEFAULT_GROUP, DERIBIT_BOOK_VALID_DEPTHS,
77 DERIBIT_VENUE,
78 },
79 parse::{bar_spec_to_resolution, parse_instrument_kind_currency},
80 },
81 config::DeribitDataClientConfig,
82 data_types::{DeribitBookSummary, register_deribit_custom_data},
83 http::{
84 client::DeribitHttpClient,
85 models::{DeribitCurrency, DeribitProductType},
86 },
87 websocket::{
88 auth::DERIBIT_DATA_SESSION_NAME, client::DeribitWebSocketClient,
89 enums::DeribitUpdateInterval, messages::NautilusWsMessage,
90 },
91};
92
93#[derive(Debug)]
95pub struct DeribitDataClient {
96 client_id: ClientId,
97 config: DeribitDataClientConfig,
98 http_client: DeribitHttpClient,
99 ws_client: Option<DeribitWebSocketClient>,
100 is_connected: AtomicBool,
101 cancellation_token: CancellationToken,
102 session_tasks: TaskGroup,
103 command_tasks: TaskGroup,
104 data_sender: EventSender<DataEvent>,
105 instruments: Arc<AtomicMap<InstrumentId, InstrumentAny>>,
106 instruments_lock: Arc<Mutex<()>>,
107 mark_price_subs: Arc<AtomicSet<InstrumentId>>,
108 index_price_subs: Arc<AtomicSet<InstrumentId>>,
109 option_greeks_subs: Arc<AtomicSet<InstrumentId>>,
110 combo_leg_trade_subs: Arc<AtomicMap<InstrumentId, AHashMap<InstrumentId, usize>>>,
111 clock: &'static AtomicTime,
112}
113
114impl DeribitDataClient {
115 const BOOK_SUMMARY_TYPE_NAME: &'static str = "DeribitBookSummary";
116
117 pub fn new(client_id: ClientId, config: DeribitDataClientConfig) -> anyhow::Result<Self> {
123 config.validate()?;
124
125 let clock = get_atomic_clock_realtime();
126 let data_sender = get_data_event_sender();
127 let api_key = config
128 .api_key
129 .as_ref()
130 .map(|value| value.expose_secret().to_owned());
131 let api_secret = config
132 .api_secret
133 .as_ref()
134 .map(|value| value.expose_secret().to_owned());
135 let proxy_url = config
136 .proxy_url
137 .as_ref()
138 .map(|value| value.expose_secret().to_owned());
139
140 let http_client = if config.has_api_credentials() {
141 DeribitHttpClient::new_with_env(
142 api_key.clone(),
143 api_secret.clone(),
144 config.base_url_http.clone(),
145 config.environment,
146 config.http_timeout_secs,
147 config.max_retries,
148 config.retry_delay_initial_ms,
149 config.retry_delay_max_ms,
150 proxy_url.clone(),
151 )?
152 } else {
153 DeribitHttpClient::new(
154 config.base_url_http.clone(),
155 config.environment,
156 config.http_timeout_secs,
157 config.max_retries,
158 config.retry_delay_initial_ms,
159 config.retry_delay_max_ms,
160 proxy_url.clone(),
161 )?
162 };
163
164 let ws_client = DeribitWebSocketClient::new(
165 Some(config.ws_url()),
166 api_key,
167 api_secret,
168 config.heartbeat_interval_secs,
169 config.auth_timeout_secs,
170 config.environment,
171 config.transport_backend,
172 proxy_url,
173 )?
174 .with_socket_control(SocketControl::new(
175 client_id,
176 Some(*DERIBIT_VENUE),
177 "deribit-data-streams",
178 ))
179 .with_book_timeouts(
180 config.book_snapshot_timeout_secs,
181 config.book_stale_threshold_secs,
182 );
183
184 let session_tasks = TaskGroup::new();
185 let command_tasks = TaskGroup::new();
186
187 Ok(Self {
188 client_id,
189 config,
190 http_client,
191 ws_client: Some(ws_client),
192 is_connected: AtomicBool::new(false),
193 cancellation_token: session_tasks.cancellation_token(),
194 session_tasks,
195 command_tasks,
196 data_sender,
197 instruments: Arc::new(AtomicMap::new()),
198 instruments_lock: Arc::new(Mutex::new(())),
199 mark_price_subs: Arc::new(AtomicSet::new()),
200 index_price_subs: Arc::new(AtomicSet::new()),
201 option_greeks_subs: Arc::new(AtomicSet::new()),
202 combo_leg_trade_subs: Arc::new(AtomicMap::new()),
203 clock,
204 })
205 }
206
207 fn ws_client_mut(&mut self) -> anyhow::Result<&mut DeribitWebSocketClient> {
209 self.ws_client
210 .as_mut()
211 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))
212 }
213
214 fn spawn_command<F>(&self, future: F)
215 where
216 F: std::future::Future<Output = ()> + Send + 'static,
217 {
218 if let Err(e) = self.command_tasks.spawn(future) {
219 log::warn!("Skipping Deribit data command after shutdown began: {e}");
220 }
221 }
222
223 async fn finish_tasks(&self) -> anyhow::Result<()> {
224 let (session_result, command_result) = tokio::join!(
225 self.session_tasks
226 .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
227 self.command_tasks
228 .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
229 );
230 session_result.context("failed to finish Deribit data session tasks")?;
231 command_result.context("failed to finish Deribit data command tasks")?;
232 Ok(())
233 }
234
235 async fn prepare_task_groups(&mut self) -> anyhow::Result<()> {
236 if !self.session_tasks.is_open() || !self.command_tasks.is_open() {
237 self.session_tasks.begin_shutdown();
238 self.command_tasks.begin_shutdown();
239 self.finish_tasks().await?;
240 self.session_tasks
241 .start_generation()
242 .context("failed to start Deribit data session task generation")?;
243 self.command_tasks
244 .start_generation()
245 .context("failed to start Deribit data command task generation")?;
246 self.cancellation_token = self.session_tasks.cancellation_token();
247 }
248 Ok(())
249 }
250
251 async fn teardown_partial_connect(&self) -> anyhow::Result<()> {
252 self.session_tasks.begin_shutdown();
253 self.command_tasks.begin_shutdown();
254 if let Some(ws) = self.ws_client.as_ref() {
255 ws.begin_shutdown();
256 }
257
258 let mut errors = Vec::new();
259
260 if let Some(ws) = self.ws_client.as_ref()
261 && let Err(e) = ws.close().await
262 {
263 errors.push(format!("WebSocket shutdown failed: {e}"));
264 }
265
266 if let Err(e) = self.finish_tasks().await {
267 errors.push(e.to_string());
268 }
269 self.is_connected.store(false, Ordering::Release);
270
271 if errors.is_empty() {
272 Ok(())
273 } else {
274 anyhow::bail!(errors.join("; "))
275 }
276 }
277
278 fn get_interval(&self, params: &Option<Params>) -> Option<DeribitUpdateInterval> {
283 if let Some(interval) = params
284 .as_ref()
285 .and_then(|p| p.get_str("interval"))
286 .and_then(|s| s.parse::<DeribitUpdateInterval>().ok())
287 {
288 return Some(interval);
289 }
290
291 if let Some(ws) = self.ws_client.as_ref()
293 && ws.is_authenticated()
294 {
295 return Some(DeribitUpdateInterval::Raw);
296 }
297 None
298 }
299
300 fn spawn_stream_task(
302 &self,
303 stream: impl futures_util::Stream<Item = NautilusWsMessage> + Send + 'static,
304 ) -> anyhow::Result<()> {
305 let data_sender = self.data_sender.clone();
306 let instruments = Arc::clone(&self.instruments);
307 let cancellation = self.cancellation_token.clone();
308
309 let future = async move {
310 tokio::pin!(stream);
311
312 loop {
313 tokio::select! {
314 maybe_msg = stream.next() => {
315 match maybe_msg {
316 Some(msg) => Self::handle_ws_message(msg, &data_sender, &instruments),
317 None => {
318 log::debug!("WebSocket stream ended");
319 break;
320 }
321 }
322 }
323 () = cancellation.cancelled() => {
324 log::debug!("WebSocket stream task cancelled");
325 break;
326 }
327 }
328 }
329 };
330
331 self.session_tasks
332 .spawn(future)
333 .context("failed to register Deribit WebSocket stream task")?;
334 Ok(())
335 }
336
337 fn spawn_instrument_refresh(&self) -> anyhow::Result<()> {
339 if let Some(future) = self.instrument_refresh() {
340 self.session_tasks
341 .spawn(future)
342 .context("failed to register Deribit instrument refresh task")?;
343 }
344
345 Ok(())
346 }
347
348 fn instrument_refresh(&self) -> Option<impl Future<Output = ()> + Send + 'static> {
349 let minutes = self.config.update_instruments_interval_mins;
350
351 if minutes == 0 {
352 return None;
353 }
354
355 let product_types = if self.config.product_types.is_empty() {
356 vec![DeribitProductType::Future]
357 } else {
358 self.config.product_types.clone()
359 };
360 let interval = Duration::from_secs(minutes.saturating_mul(60));
361 let cancellation = self.cancellation_token.clone();
362 let http_client = self.http_client.clone();
363 let ws_client = self.ws_client.clone();
364 let instruments = Arc::clone(&self.instruments);
365 let instruments_lock = Arc::clone(&self.instruments_lock);
366 let sender = self.data_sender.clone();
367
368 Some(async move {
369 loop {
370 tokio::select! {
371 biased;
372 () = cancellation.cancelled() => break,
373 () = tokio::time::sleep(interval) => {}
374 }
375
376 for product_type in &product_types {
377 let result = tokio::select! {
378 biased;
379 () = cancellation.cancelled() => return,
380 result = http_client.request_instruments(DeribitCurrency::ANY, Some(*product_type)) => result,
381 };
382
383 match result {
384 Ok(fetched) => cache_instruments(
385 fetched,
386 &instruments,
387 &http_client,
388 ws_client.as_ref(),
389 &sender,
390 &cancellation,
391 &instruments_lock,
392 ),
393 Err(e) => {
394 log::warn!("Failed to refresh instruments for {product_type:?}: {e}");
395 }
396 }
397 }
398 }
399 })
400 }
401
402 fn handle_ws_message(
404 message: NautilusWsMessage,
405 sender: &EventSender<DataEvent>,
406 instruments: &Arc<AtomicMap<InstrumentId, InstrumentAny>>,
407 ) {
408 match message {
409 NautilusWsMessage::Data(payloads) => {
410 for data in payloads {
411 Self::send_data(sender, data);
412 }
413 }
414 NautilusWsMessage::Deltas(deltas) => {
415 Self::send_data(sender, Data::BookDeltas(Box::new(deltas)));
416 }
417 NautilusWsMessage::Instrument(instrument) => {
418 let instrument_any = *instrument;
419 instruments.insert(instrument_any.id(), instrument_any.clone());
420
421 if let Err(e) = sender.send(DataEvent::Data(instrument_any.into())) {
422 log::warn!("Failed to send instrument update: {e}");
423 }
424 }
425 NautilusWsMessage::OptionGreeks(greeks) => {
426 if let Err(e) = sender.send(DataEvent::Data(Data::OptionGreeks(greeks))) {
427 log::error!("Failed to send option greeks: {e}");
428 }
429 }
430 NautilusWsMessage::Error(e) => {
431 log::warn!("WebSocket error: {e:?}");
432 }
433 NautilusWsMessage::Raw(value) => {
434 log::debug!("Unhandled raw message: {value}");
435 }
436 NautilusWsMessage::Reconnected => {
437 log::info!("WebSocket reconnected");
438 }
439 NautilusWsMessage::Authenticated(auth) => {
440 log::debug!("WebSocket authenticated: expires_in={}s", auth.expires_in);
441 }
442 NautilusWsMessage::FundingRates(funding_rates) => {
443 for funding_rate in funding_rates {
444 if let Err(e) = sender.send(DataEvent::Data(Data::FundingRate(funding_rate))) {
445 log::error!("Failed to send funding rate: {e}");
446 }
447 }
448 }
449 NautilusWsMessage::InstrumentStatus(status) => {
450 if let Err(e) = sender.send(DataEvent::Data(Data::InstrumentStatus(status))) {
451 log::error!("Failed to send instrument status event: {e}");
452 }
453 }
454 NautilusWsMessage::OrderStatusReports(reports) => {
455 log::warn!(
456 "Data client received OrderStatusReports message (should be handled by execution client): {} reports",
457 reports.len()
458 );
459 }
460 NautilusWsMessage::FillReports(reports) => {
461 log::warn!(
462 "Data client received FillReports message (should be handled by execution client): {} reports",
463 reports.len()
464 );
465 }
466 NautilusWsMessage::OrderFilled(order) => {
467 log::warn!(
468 "Data client received OrderFilled message (should be handled by execution client): {order:?}"
469 );
470 }
471 NautilusWsMessage::OrderRejected(order) => {
472 log::warn!(
473 "Data client received OrderRejected message (should be handled by execution client): {order:?}"
474 );
475 }
476 NautilusWsMessage::OrderAccepted(order) => {
477 log::warn!(
478 "Data client received OrderAccepted message (should be handled by execution client): {order:?}"
479 );
480 }
481 NautilusWsMessage::OrderCanceled(order) => {
482 log::warn!(
483 "Data client received OrderCanceled message (should be handled by execution client): {order:?}"
484 );
485 }
486 NautilusWsMessage::OrderExpired(order) => {
487 log::warn!(
488 "Data client received OrderExpired message (should be handled by execution client): {order:?}"
489 );
490 }
491 NautilusWsMessage::OrderUpdated(order) => {
492 log::warn!(
493 "Data client received OrderUpdated message (should be handled by execution client): {order:?}"
494 );
495 }
496 NautilusWsMessage::OrderModifyDenied(order) => {
497 log::warn!("Data client received order modify denial: {order:?}");
498 }
499 NautilusWsMessage::OrderCancelDenied(order) => {
500 log::warn!("Data client received order cancel denial: {order:?}");
501 }
502 NautilusWsMessage::OrderCancelRejected(order) => {
503 log::warn!(
504 "Data client received OrderCancelRejected message (should be handled by execution client): {order:?}"
505 );
506 }
507 NautilusWsMessage::OrderModifyRejected(order) => {
508 log::warn!(
509 "Data client received OrderModifyRejected message (should be handled by execution client): {order:?}"
510 );
511 }
512 NautilusWsMessage::AccountState(state) => {
513 log::warn!(
514 "Data client received AccountState message (should be handled by execution client): {state:?}"
515 );
516 }
517 NautilusWsMessage::AuthenticationFailed(reason) => {
518 log::error!("Authentication failed in data client: {reason}");
519 }
520 }
521 }
522
523 fn send_data(sender: &EventSender<DataEvent>, data: Data) {
525 if let Err(e) = sender.send(DataEvent::Data(data)) {
526 log::error!("Failed to send data: {e}");
527 }
528 }
529
530 fn prepare_subscribe(&self, instrument_id: InstrumentId) -> anyhow::Result<bool> {
534 if self.instruments.contains_key(&instrument_id) {
535 return Ok(false);
536 }
537
538 if !self.config.auto_load_missing_instruments {
539 anyhow::bail!(
540 "Instrument {instrument_id} not found and `auto_load_missing_instruments` is disabled"
541 );
542 }
543 Ok(true)
544 }
545
546 fn validate_public_trades(&self, instrument_id: InstrumentId) -> anyhow::Result<()> {
547 if let Some(instrument) = self.instruments.get_cloned(&instrument_id) {
548 capabilities::validate_public_trades(&instrument)?;
549 }
550
551 Ok(())
552 }
553
554 async fn lazy_load_instrument(
556 http_client: &DeribitHttpClient,
557 ws: &DeribitWebSocketClient,
558 instruments: &AtomicMap<InstrumentId, InstrumentAny>,
559 instrument_id: InstrumentId,
560 ) -> anyhow::Result<()> {
561 let instrument = http_client
562 .request_instrument(instrument_id)
563 .await
564 .with_context(|| format!("failed to lazy-load instrument {instrument_id}"))?;
565 instruments.insert(instrument.id(), instrument.clone());
566 http_client.cache_instruments(std::slice::from_ref(&instrument));
567 ws.cache_instruments(std::slice::from_ref(&instrument));
568 Ok(())
569 }
570
571 fn subscribe_combo_legs(params: &Option<Params>) -> bool {
572 params
573 .as_ref()
574 .and_then(|params| params.get_bool("subscribe_combo_legs"))
575 .unwrap_or(false)
576 }
577
578 fn book_summary_metadata_currency(data_type: &DataType) -> anyhow::Result<String> {
579 data_type
580 .metadata()
581 .and_then(|m| m.get("currency"))
582 .and_then(|v| v.as_str())
583 .map(str::trim)
584 .filter(|value| !value.is_empty())
585 .map(str::to_ascii_uppercase)
586 .ok_or_else(|| {
587 anyhow::anyhow!("DeribitBookSummary requests require metadata['currency']")
588 })
589 }
590
591 fn book_summary_metadata_kind(data_type: &DataType) -> Option<String> {
592 data_type
593 .metadata()
594 .and_then(|m| m.get("kind"))
595 .and_then(|v| v.as_str())
596 .map(str::trim)
597 .filter(|value| !value.is_empty())
598 .map(str::to_ascii_lowercase)
599 }
600
601 fn book_summary_data_type(currency: &str, kind: &str) -> DataType {
602 let metadata = Params::new(
603 [
604 (
605 "currency".to_string(),
606 serde_json::Value::String(currency.to_string()),
607 ),
608 (
609 "kind".to_string(),
610 serde_json::Value::String(kind.to_string()),
611 ),
612 ]
613 .into(),
614 );
615 DataType::new(
616 Self::BOOK_SUMMARY_TYPE_NAME,
617 Some(metadata),
618 Some(format!("{currency}:{kind}")),
619 )
620 }
621
622 fn combo_leg_trade_ids(
623 instruments: &AtomicMap<InstrumentId, InstrumentAny>,
624 instrument_id: InstrumentId,
625 ) -> Vec<InstrumentId> {
626 let Some(instrument) = instruments.get_cloned(&instrument_id) else {
627 log::warn!("Cannot expand Deribit combo legs for missing instrument {instrument_id}");
628 return Vec::new();
629 };
630
631 let info = match instrument {
632 InstrumentAny::CryptoOptionSpread(spread) => spread.info,
633 InstrumentAny::CryptoFuturesSpread(spread) => spread.info,
634 _ => return Vec::new(),
635 };
636 let Some(info) = info else {
637 return Vec::new();
638 };
639 let Some(legs) = info
640 .get("deribit_combo_legs")
641 .and_then(serde_json::Value::as_array)
642 else {
643 return Vec::new();
644 };
645
646 let mut leg_ids = Vec::new();
647 let mut seen = AHashSet::new();
648
649 for leg in legs {
650 let Some(leg_id_str) = leg.get("instrument_id").and_then(serde_json::Value::as_str)
651 else {
652 continue;
653 };
654
655 match InstrumentId::from_as_ref(leg_id_str) {
656 Ok(leg_id) if leg_id != instrument_id && seen.insert(leg_id) => {
657 leg_ids.push(leg_id);
658 }
659 Ok(_) => {}
660 Err(e) => {
661 log::warn!(
662 "Skipping invalid Deribit combo leg instrument ID {leg_id_str}: {e}"
663 );
664 }
665 }
666 }
667
668 leg_ids
669 }
670
671 fn track_combo_leg_trade_subs(
672 subscriptions: &AtomicMap<InstrumentId, AHashMap<InstrumentId, usize>>,
673 instrument_id: InstrumentId,
674 leg_ids: &[InstrumentId],
675 ) {
676 if leg_ids.is_empty() {
677 return;
678 }
679
680 subscriptions.rcu(|subscriptions| {
681 let counts = subscriptions.entry(instrument_id).or_default();
682
683 for leg_id in leg_ids {
684 counts
685 .entry(*leg_id)
686 .and_modify(|count| *count += 1)
687 .or_insert(1);
688 }
689 });
690 }
691
692 fn combo_leg_trade_unsubs(
693 subscriptions: &AtomicMap<InstrumentId, AHashMap<InstrumentId, usize>>,
694 instrument_id: InstrumentId,
695 ) -> Vec<InstrumentId> {
696 let mut leg_ids = Vec::new();
697
698 subscriptions.rcu(|subscriptions| {
699 let remove_instrument = if let Some(counts) = subscriptions.get_mut(&instrument_id) {
700 leg_ids = counts.keys().copied().collect();
701 counts.retain(|_, count| {
702 if *count > 1 {
703 *count -= 1;
704 true
705 } else {
706 false
707 }
708 });
709 counts.is_empty()
710 } else {
711 leg_ids = Vec::new();
712 false
713 };
714
715 if remove_instrument {
716 subscriptions.remove(&instrument_id);
717 }
718 });
719
720 leg_ids
721 }
722
723 async fn load_data_instruments(&self) -> anyhow::Result<Vec<InstrumentAny>> {
724 let product_types = if self.config.product_types.is_empty() {
725 vec![DeribitProductType::Future]
726 } else {
727 self.config.product_types.clone()
728 };
729
730 let mut all_instruments = Vec::new();
731
732 for product_type in &product_types {
733 let fetched = self
734 .http_client
735 .request_instruments(DeribitCurrency::ANY, Some(*product_type))
736 .await
737 .with_context(|| format!("failed to request instruments for {product_type:?}"))?;
738 self.cache_loaded_instruments(&fetched);
739 all_instruments.extend(fetched);
740 }
741
742 let required = self
743 .http_client
744 .request_uncached_instruments(self.config.load_ids.as_deref().unwrap_or(&[]))
745 .await
746 .context("failed to load reconciliation instruments")?;
747 self.cache_loaded_instruments(&required);
748 all_instruments.extend(required);
749 self.append_cached_load_ids(&mut all_instruments);
750 log::debug!(
751 "Cached instruments: client_id={}, total={}",
752 self.client_id,
753 all_instruments.len()
754 );
755 Ok(all_instruments)
756 }
757
758 fn cache_loaded_instruments(&self, instruments: &[InstrumentAny]) {
759 if instruments.is_empty() {
760 return;
761 }
762
763 self.http_client.cache_instruments(instruments);
764 self.instruments.rcu(|cached| {
765 for instrument in instruments {
766 cached.insert(instrument.id(), instrument.clone());
767 }
768 });
769 }
770
771 fn append_cached_load_ids(&self, instruments: &mut Vec<InstrumentAny>) {
772 let Some(load_ids) = self
773 .config
774 .load_ids
775 .as_deref()
776 .filter(|ids| !ids.is_empty())
777 else {
778 return;
779 };
780
781 let mut cached = Vec::new();
782
783 for instrument_id in load_ids {
784 if instruments
785 .iter()
786 .any(|instrument| instrument.id() == *instrument_id)
787 {
788 continue;
789 }
790
791 let Some(instrument) = self
792 .http_client
793 .get_instrument(&instrument_id.symbol.inner())
794 else {
795 continue;
796 };
797
798 if instrument.id() == *instrument_id {
799 cached.push(instrument);
800 }
801 }
802
803 self.cache_loaded_instruments(&cached);
804 instruments.extend(cached);
805 }
806}
807
808#[async_trait(?Send)]
809impl DataClient for DeribitDataClient {
810 fn client_id(&self) -> ClientId {
811 self.client_id
812 }
813
814 fn venue(&self) -> Option<Venue> {
815 Some(*DERIBIT_VENUE)
816 }
817
818 fn start(&mut self) -> anyhow::Result<()> {
819 log::info!(
820 "Starting data client: client_id={}, environment={}",
821 self.client_id,
822 self.config.environment
823 );
824 Ok(())
825 }
826
827 fn stop(&mut self) -> anyhow::Result<()> {
828 log::info!("Stopping data client: {}", self.client_id);
829 self.session_tasks.begin_shutdown();
830 let _guard = self.instruments_lock.lock();
831 self.command_tasks.begin_shutdown();
832 if let Some(ws) = self.ws_client.as_ref() {
833 ws.begin_shutdown();
834 }
835 self.is_connected.store(false, Ordering::Relaxed);
836 Ok(())
837 }
838
839 fn reset(&mut self) -> anyhow::Result<()> {
840 log::info!("Resetting data client: {}", self.client_id);
841 self.session_tasks.begin_shutdown();
842 self.command_tasks.begin_shutdown();
843 if let Some(ws) = self.ws_client.as_ref() {
844 ws.begin_shutdown();
845 }
846 self.is_connected.store(false, Ordering::Relaxed);
847
848 {
849 let _guard = self.instruments_lock.lock();
850 self.instruments.store(AHashMap::new());
851 }
852 self.combo_leg_trade_subs.store(AHashMap::new());
853 Ok(())
854 }
855
856 fn dispose(&mut self) -> anyhow::Result<()> {
857 log::debug!("Disposing data client: {}", self.client_id);
858 self.stop()
859 }
860
861 fn is_connected(&self) -> bool {
862 self.is_connected.load(Ordering::SeqCst)
863 }
864
865 fn is_disconnected(&self) -> bool {
866 !self.is_connected()
867 }
868
869 async fn connect(&mut self) -> anyhow::Result<()> {
870 if self.is_connected() && self.session_tasks.is_open() && self.command_tasks.is_open() {
871 return Ok(());
872 }
873
874 self.prepare_task_groups().await?;
875 let cancellation_token = self.cancellation_token.clone();
876 let ws_client = self.ws_client.clone();
877 let setup_guard =
878 TaskGroupGuard::new(&[&self.session_tasks, &self.command_tasks], move || {
879 cancellation_token.cancel();
880
881 if let Some(ws) = ws_client {
882 ws.begin_shutdown();
883 }
884 });
885
886 register_deribit_custom_data();
887
888 let all_instruments = self.load_data_instruments().await?;
889 for instrument in &all_instruments {
890 if let Err(e) = self
891 .data_sender
892 .send(DataEvent::Data(instrument.clone().into()))
893 {
894 log::warn!("Failed to send instrument: {e}");
895 }
896 }
897
898 let mark_price_subs = self.mark_price_subs.clone();
900 let index_price_subs = self.index_price_subs.clone();
901 let option_greeks_subs = self.option_greeks_subs.clone();
902 let ws = self.ws_client_mut()?;
903 ws.cache_instruments(&all_instruments);
904 ws.set_mark_price_subs(mark_price_subs);
905 ws.set_index_price_subs(index_price_subs);
906 ws.set_option_greeks_subs(option_greeks_subs);
907
908 ws.connect().await.context("failed to connect WebSocket")?;
910 let activation_result = async {
911 ws.wait_until_active(10.0)
912 .await
913 .context("WebSocket failed to become active")?;
914
915 if ws.has_credentials() {
917 ws.authenticate_session(DERIBIT_DATA_SESSION_NAME)
918 .await
919 .context("failed to authenticate WebSocket")?;
920 log_debug!("WebSocket authenticated");
921 }
922 Ok::<(), anyhow::Error>(())
923 }
924 .await;
925
926 if let Err(e) = activation_result {
927 if let Err(teardown_error) = self.teardown_partial_connect().await {
928 return Err(e.context(format!(
929 "Deribit data startup teardown failed: {teardown_error}"
930 )));
931 }
932 return Err(e);
933 }
934
935 let stream_result = self.ws_client_mut().and_then(|ws| Ok(ws.stream()?));
937 let stream = match stream_result {
938 Ok(stream) => stream,
939 Err(e) => {
940 if let Err(teardown_error) = self.teardown_partial_connect().await {
941 return Err(e.context(format!(
942 "Deribit data startup teardown failed: {teardown_error}"
943 )));
944 }
945 return Err(e);
946 }
947 };
948
949 if let Err(e) = self
950 .spawn_stream_task(stream)
951 .and_then(|()| self.spawn_instrument_refresh())
952 {
953 if let Err(teardown_error) = self.teardown_partial_connect().await {
954 return Err(e.context(format!(
955 "Deribit data startup teardown failed: {teardown_error}"
956 )));
957 }
958 return Err(e);
959 }
960
961 self.is_connected.store(true, Ordering::Release);
962 setup_guard.disarm();
963 log_info!("Connected ({})", self.config.environment);
964 Ok(())
965 }
966
967 async fn disconnect(&mut self) -> anyhow::Result<()> {
968 self.teardown_partial_connect().await?;
969
970 log_info!("Disconnected");
971 Ok(())
972 }
973
974 fn subscribe_instruments(&mut self, cmd: SubscribeInstruments) -> anyhow::Result<()> {
975 let kind = cmd
977 .params
978 .as_ref()
979 .and_then(|p| p.get_str("kind"))
980 .unwrap_or("any")
981 .to_string();
982 let currency = cmd
983 .params
984 .as_ref()
985 .and_then(|p| p.get_str("currency"))
986 .unwrap_or("any")
987 .to_string();
988
989 let ws = self
990 .ws_client
991 .as_ref()
992 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
993 .clone();
994
995 log::debug!("Subscribing to instrument state changes for {kind}.{currency}");
996
997 self.spawn_command(async move {
998 if let Err(e) = ws.subscribe_instrument_status(&kind, ¤cy).await {
999 log::error!("Failed to subscribe to instrument status for {kind}.{currency}: {e}");
1000 }
1001 });
1002
1003 Ok(())
1004 }
1005
1006 fn subscribe_instrument(&mut self, cmd: SubscribeInstrument) -> anyhow::Result<()> {
1007 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1008 if !self.instruments.contains_key(&instrument_id) {
1010 log::warn!(
1011 "Instrument {instrument_id} not in cache - it may have been created after connect()"
1012 );
1013 }
1014
1015 let (kind, currency) = parse_instrument_kind_currency(&instrument_id);
1017
1018 let ws = self
1019 .ws_client
1020 .as_ref()
1021 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1022 .clone();
1023
1024 log::debug!(
1025 "Subscribing to instrument state for {instrument_id} (channel: {kind}.{currency})"
1026 );
1027
1028 self.spawn_command(async move {
1030 if let Err(e) = ws.subscribe_instrument_status(&kind, ¤cy).await {
1031 log::error!(
1032 "Failed to subscribe to instrument status for {instrument_id}: {e}"
1033 );
1034 }
1035 });
1036
1037 Ok(())
1038 })
1039 }
1040
1041 fn subscribe_book_deltas(&mut self, cmd: SubscribeBookDeltas) -> anyhow::Result<()> {
1042 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1043 let cmd = cmd.with_instrument_ids(instrument_id);
1044 if cmd.book_type != BookType::L2_MBP {
1045 anyhow::bail!("Deribit only supports L2_MBP order book deltas");
1046 }
1047
1048 let needs_load = self.prepare_subscribe(instrument_id)?;
1049
1050 let ws = self
1051 .ws_client
1052 .as_ref()
1053 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1054 .clone();
1055 let http_client = self.http_client.clone();
1056 let instruments = Arc::clone(&self.instruments);
1057 let interval = self.get_interval(&cmd.params);
1058
1059 let depth = cmd
1060 .depth
1061 .map(|d| d.get() as u32)
1062 .or_else(|| {
1063 cmd.params
1064 .as_ref()
1065 .and_then(|p| p.get_u64("depth"))
1066 .map(|n| n as u32)
1067 })
1068 .unwrap_or(DERIBIT_BOOK_DEFAULT_DEPTH);
1069
1070 if !DERIBIT_BOOK_VALID_DEPTHS.contains(&depth) {
1071 anyhow::bail!(
1072 "invalid depth {depth}; supported depths: {DERIBIT_BOOK_VALID_DEPTHS:?}"
1073 );
1074 }
1075
1076 let group = cmd
1077 .params
1078 .as_ref()
1079 .and_then(|p| p.get_str("group"))
1080 .unwrap_or(DERIBIT_BOOK_DEFAULT_GROUP)
1081 .to_string();
1082
1083 log::debug!(
1084 "Subscribing to book deltas for {} (group: {}, depth: {}, interval: {}, book_type: {:?})",
1085 instrument_id,
1086 group,
1087 depth,
1088 interval.map_or("100ms (default)".to_string(), |i| i.to_string()),
1089 cmd.book_type
1090 );
1091
1092 self.spawn_command(async move {
1093 if needs_load
1094 && let Err(e) =
1095 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1096 .await
1097 {
1098 log::error!("Lazy-load failed for {instrument_id} (book deltas): {e}");
1099 return;
1100 }
1101
1102 let result = if interval == Some(DeribitUpdateInterval::Raw) {
1103 ws.subscribe_book(instrument_id, interval).await
1104 } else {
1105 ws.subscribe_book_grouped(instrument_id, &group, depth, interval)
1106 .await
1107 };
1108
1109 if let Err(e) = result {
1110 log::error!("Failed to subscribe to book deltas for {instrument_id}: {e}");
1111 }
1112 });
1113
1114 Ok(())
1115 })
1116 }
1117
1118 fn subscribe_book_depth(&mut self, cmd: SubscribeBookDepth) -> anyhow::Result<()> {
1119 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1120 let cmd = cmd.with_instrument_ids(instrument_id);
1121 if cmd.book_type != BookType::L2_MBP {
1122 anyhow::bail!("Deribit only supports L2_MBP order book depth");
1123 }
1124
1125 let needs_load = self.prepare_subscribe(instrument_id)?;
1126
1127 let ws = self
1128 .ws_client
1129 .as_ref()
1130 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1131 .clone();
1132 let http_client = self.http_client.clone();
1133 let instruments = Arc::clone(&self.instruments);
1134 let interval = self.get_interval(&cmd.params);
1135 let group = cmd
1136 .params
1137 .as_ref()
1138 .and_then(|p| p.get_str("group"))
1139 .unwrap_or(DERIBIT_BOOK_DEFAULT_GROUP)
1140 .to_string();
1141
1142 log::debug!(
1143 "Subscribing to book depth for {} (group: {}, interval: {}, book_type: {:?})",
1144 instrument_id,
1145 group,
1146 interval.map_or("100ms (default)".to_string(), |i| i.to_string()),
1147 cmd.book_type
1148 );
1149
1150 self.spawn_command(async move {
1151 if needs_load
1152 && let Err(e) =
1153 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1154 .await
1155 {
1156 log::error!("Lazy-load failed for {instrument_id} (book depth): {e}");
1157 return;
1158 }
1159
1160 if let Err(e) = ws
1161 .subscribe_book_depth(instrument_id, &group, interval)
1162 .await
1163 {
1164 log::error!("Failed to subscribe to book depth for {instrument_id}: {e}");
1165 }
1166 });
1167
1168 Ok(())
1169 })
1170 }
1171
1172 fn subscribe_quotes(&mut self, cmd: SubscribeQuotes) -> anyhow::Result<()> {
1173 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1174 let cmd = cmd.with_instrument_ids(instrument_id);
1175 let command_id = cmd.command_id;
1176 let needs_load = self.prepare_subscribe(instrument_id)?;
1177
1178 let ws = self
1179 .ws_client
1180 .as_ref()
1181 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1182 .clone();
1183 let http_client = self.http_client.clone();
1184 let instruments = Arc::clone(&self.instruments);
1185
1186 self.spawn_command(async move {
1187 if needs_load
1188 && let Err(e) =
1189 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id).await
1190 {
1191 log::error!(
1192 "Lazy-load failed for {instrument_id} (quotes, command_id={command_id}): {e}"
1193 );
1194 return;
1195 }
1196
1197 if let Err(e) = ws.subscribe_quotes(instrument_id).await {
1198 log::error!("Failed to subscribe to quotes for {instrument_id}: {e}");
1199 }
1200 });
1201
1202 Ok(())
1203 })
1204 }
1205
1206 fn subscribe_trades(&mut self, cmd: SubscribeTrades) -> anyhow::Result<()> {
1207 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1208 self.subscribe_trades_member(instrument_id, &cmd)
1209 })
1210 }
1211
1212 fn subscribe_mark_prices(&mut self, cmd: SubscribeMarkPrices) -> anyhow::Result<()> {
1213 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1214 let cmd = cmd.with_instrument_ids(instrument_id);
1215 let needs_load = self.prepare_subscribe(instrument_id)?;
1216
1217 let ws = self
1218 .ws_client
1219 .as_ref()
1220 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1221 .clone();
1222 let http_client = self.http_client.clone();
1223 let instruments = Arc::clone(&self.instruments);
1224 let interval = self.get_interval(&cmd.params);
1225
1226 self.mark_price_subs.insert(instrument_id);
1228
1229 log::debug!(
1230 "Subscribing to mark prices for {} (via ticker channel, interval: {})",
1231 instrument_id,
1232 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1233 );
1234
1235 self.spawn_command(async move {
1236 if needs_load
1237 && let Err(e) =
1238 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1239 .await
1240 {
1241 log::error!("Lazy-load failed for {instrument_id} (mark prices): {e}");
1242 return;
1243 }
1244
1245 if let Err(e) = ws.subscribe_ticker(instrument_id, interval).await {
1246 log::error!("Failed to subscribe to mark prices for {instrument_id}: {e}");
1247 }
1248 });
1249
1250 Ok(())
1251 })
1252 }
1253
1254 fn subscribe_index_prices(&mut self, cmd: SubscribeIndexPrices) -> anyhow::Result<()> {
1255 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1256 let cmd = cmd.with_instrument_ids(instrument_id);
1257 let needs_load = self.prepare_subscribe(instrument_id)?;
1258
1259 let ws = self
1260 .ws_client
1261 .as_ref()
1262 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1263 .clone();
1264 let http_client = self.http_client.clone();
1265 let instruments = Arc::clone(&self.instruments);
1266 let interval = self.get_interval(&cmd.params);
1267
1268 self.index_price_subs.insert(instrument_id);
1270
1271 log::debug!(
1272 "Subscribing to index prices for {} (via ticker channel, interval: {})",
1273 instrument_id,
1274 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1275 );
1276
1277 self.spawn_command(async move {
1278 if needs_load
1279 && let Err(e) =
1280 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1281 .await
1282 {
1283 log::error!("Lazy-load failed for {instrument_id} (index prices): {e}");
1284 return;
1285 }
1286
1287 if let Err(e) = ws.subscribe_ticker(instrument_id, interval).await {
1288 log::error!("Failed to subscribe to index prices for {instrument_id}: {e}");
1289 }
1290 });
1291
1292 Ok(())
1293 })
1294 }
1295
1296 fn subscribe_bars(&mut self, cmd: SubscribeBars) -> anyhow::Result<()> {
1297 execute_bar_subscriptions(&cmd.bar_types, |bar_type| {
1298 let instrument_id = bar_type.instrument_id();
1299 self.validate_public_trades(instrument_id)?;
1300 let needs_load = self.prepare_subscribe(instrument_id)?;
1301
1302 let ws = self
1303 .ws_client
1304 .as_ref()
1305 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1306 .clone();
1307 let http_client = self.http_client.clone();
1308 let instruments = Arc::clone(&self.instruments);
1309 let resolution = bar_spec_to_resolution(&bar_type);
1310
1311 self.spawn_command(async move {
1312 if needs_load
1313 && let Err(e) =
1314 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1315 .await
1316 {
1317 log::error!("Lazy-load failed for {instrument_id} (bars): {e}");
1318 return;
1319 }
1320
1321 if let Err(e) = ws.subscribe_chart(instrument_id, &resolution).await {
1322 log::error!("Failed to subscribe to bars for {instrument_id}: {e}");
1323 }
1324 });
1325
1326 Ok(())
1327 })
1328 }
1329
1330 fn subscribe_funding_rates(&mut self, cmd: SubscribeFundingRates) -> anyhow::Result<()> {
1331 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1332 let cmd = cmd.with_instrument_ids(instrument_id);
1333 let command_id = cmd.command_id;
1334 let needs_load = self.prepare_subscribe(instrument_id)?;
1335
1336 let ws = self
1337 .ws_client
1338 .as_ref()
1339 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1340 .clone();
1341 let http_client = self.http_client.clone();
1342 let instruments = Arc::clone(&self.instruments);
1343 let interval = self.get_interval(&cmd.params);
1344
1345 log::debug!(
1346 "Subscribing to funding rates for {} (perpetual channel, interval: {})",
1347 instrument_id,
1348 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1349 );
1350
1351 self.spawn_command(async move {
1352 if needs_load
1353 && let Err(e) =
1354 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id).await
1355 {
1356 log::error!("Lazy-load failed for {instrument_id} (funding rates): {e}");
1357 return;
1358 }
1359
1360 let is_perpetual = instruments
1362 .load()
1363 .get(&instrument_id)
1364 .is_some_and(|inst| matches!(inst, InstrumentAny::CryptoPerpetual(_)));
1365
1366 if !is_perpetual {
1367 log::warn!(
1368 "Funding rates subscription rejected for {instrument_id} (command_id={command_id}): only available for perpetual instruments"
1369 );
1370 return;
1371 }
1372
1373 if let Err(e) = ws
1374 .subscribe_perpetual_interests_rates_updates(instrument_id, interval)
1375 .await
1376 {
1377 log::error!("Failed to subscribe to funding rates for {instrument_id}: {e}");
1378 }
1379 });
1380
1381 Ok(())
1382 })
1383 }
1384
1385 fn subscribe_instrument_status(
1386 &mut self,
1387 cmd: SubscribeInstrumentStatus,
1388 ) -> anyhow::Result<()> {
1389 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1390 let (kind, currency) = parse_instrument_kind_currency(&instrument_id);
1391
1392 let ws = self
1393 .ws_client
1394 .as_ref()
1395 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1396 .clone();
1397
1398 log::debug!("Subscribing to instrument status for {instrument_id} ({kind}.{currency})");
1399
1400 self.spawn_command(async move {
1401 if let Err(e) = ws.subscribe_instrument_status(&kind, ¤cy).await {
1402 log::error!(
1403 "Failed to subscribe to instrument status for {instrument_id}: {e}"
1404 );
1405 }
1406 });
1407
1408 Ok(())
1409 })
1410 }
1411
1412 fn subscribe_option_greeks(&mut self, cmd: SubscribeOptionGreeks) -> anyhow::Result<()> {
1413 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1414 let cmd = cmd.with_instrument_ids(instrument_id);
1415 let needs_load = self.prepare_subscribe(instrument_id)?;
1416
1417 let ws = self
1418 .ws_client
1419 .as_ref()
1420 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1421 .clone();
1422 let http_client = self.http_client.clone();
1423 let instruments = Arc::clone(&self.instruments);
1424 let interval = self.get_interval(&cmd.params);
1425
1426 self.option_greeks_subs.insert(instrument_id);
1428
1429 log::debug!(
1430 "Subscribing to option greeks for {} (via ticker channel, interval: {})",
1431 instrument_id,
1432 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1433 );
1434
1435 self.spawn_command(async move {
1436 if needs_load
1437 && let Err(e) =
1438 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id)
1439 .await
1440 {
1441 log::error!("Lazy-load failed for {instrument_id} (option greeks): {e}");
1442 return;
1443 }
1444
1445 if let Err(e) = ws.subscribe_ticker(instrument_id, interval).await {
1446 log::error!("Failed to subscribe to option greeks for {instrument_id}: {e}");
1447 }
1448 });
1449
1450 Ok(())
1451 })
1452 }
1453
1454 fn subscribe(&mut self, cmd: SubscribeCustomData) -> anyhow::Result<()> {
1455 let data_type = cmd.data_type.type_name();
1456 if data_type != "DeribitVolatilityIndex" {
1457 log::warn!("Unsupported custom data subscription: {data_type}");
1458 return Ok(());
1459 }
1460
1461 let Some(index_name) = cmd
1462 .data_type
1463 .metadata()
1464 .as_ref()
1465 .and_then(|m| m.get("index_name"))
1466 .and_then(|v| v.as_str())
1467 .map(str::trim)
1468 .filter(|value| !value.is_empty())
1469 .map(ToString::to_string)
1470 else {
1471 log::warn!(
1472 "Rejected Deribit volatility index subscription: missing required metadata `index_name`"
1473 );
1474 return Ok(());
1475 };
1476
1477 log::debug!("Subscribing to Deribit volatility index: {index_name}");
1478
1479 let ws = self
1480 .ws_client
1481 .as_ref()
1482 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1483 .clone();
1484
1485 self.spawn_command(async move {
1486 if let Err(e) = ws.subscribe_volatility_index(&index_name).await {
1487 log::error!("Failed to subscribe to volatility index {index_name}: {e}");
1488 }
1489 });
1490
1491 Ok(())
1492 }
1493
1494 fn unsubscribe_instrument_status(
1495 &mut self,
1496 cmd: &UnsubscribeInstrumentStatus,
1497 ) -> anyhow::Result<()> {
1498 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1499 let (kind, currency) = parse_instrument_kind_currency(&instrument_id);
1500
1501 let ws = self
1502 .ws_client
1503 .as_ref()
1504 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1505 .clone();
1506
1507 log::debug!(
1508 "Unsubscribing from instrument status for {instrument_id} ({kind}.{currency})"
1509 );
1510
1511 self.spawn_command(async move {
1512 if let Err(e) = ws.unsubscribe_instrument_status(&kind, ¤cy).await {
1513 log::error!(
1514 "Failed to unsubscribe from instrument status for {instrument_id}: {e}"
1515 );
1516 }
1517 });
1518
1519 Ok(())
1520 })
1521 }
1522
1523 fn unsubscribe_instruments(&mut self, cmd: &UnsubscribeInstruments) -> anyhow::Result<()> {
1524 let kind = cmd
1525 .params
1526 .as_ref()
1527 .and_then(|p| p.get_str("kind"))
1528 .unwrap_or("any")
1529 .to_string();
1530 let currency = cmd
1531 .params
1532 .as_ref()
1533 .and_then(|p| p.get_str("currency"))
1534 .unwrap_or("any")
1535 .to_string();
1536
1537 let ws = self
1538 .ws_client
1539 .as_ref()
1540 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1541 .clone();
1542
1543 log::debug!("Unsubscribing from instrument state changes for {kind}.{currency}");
1544
1545 self.spawn_command(async move {
1546 if let Err(e) = ws.unsubscribe_instrument_status(&kind, ¤cy).await {
1547 log::error!(
1548 "Failed to unsubscribe from instrument status for {kind}.{currency}: {e}"
1549 );
1550 }
1551 });
1552
1553 Ok(())
1554 }
1555
1556 fn unsubscribe_instrument(&mut self, cmd: &UnsubscribeInstrument) -> anyhow::Result<()> {
1557 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1558 let (kind, currency) = parse_instrument_kind_currency(&instrument_id);
1560
1561 let ws = self
1562 .ws_client
1563 .as_ref()
1564 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1565 .clone();
1566
1567 log::debug!(
1568 "Unsubscribing from instrument state for {instrument_id} (channel: {kind}.{currency})"
1569 );
1570
1571 self.spawn_command(async move {
1572 if let Err(e) = ws.unsubscribe_instrument_status(&kind, ¤cy).await {
1573 log::error!(
1574 "Failed to unsubscribe from instrument status for {instrument_id}: {e}"
1575 );
1576 }
1577 });
1578
1579 Ok(())
1580 })
1581 }
1582
1583 fn unsubscribe_book_deltas(&mut self, cmd: &UnsubscribeBookDeltas) -> anyhow::Result<()> {
1584 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1585 let cmd = &cmd.with_instrument_ids(instrument_id);
1586 let ws = self
1587 .ws_client
1588 .as_ref()
1589 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1590 .clone();
1591 let interval = self.get_interval(&cmd.params);
1592
1593 let depth = cmd
1594 .params
1595 .as_ref()
1596 .and_then(|p| p.get_u64("depth"))
1597 .map_or(DERIBIT_BOOK_DEFAULT_DEPTH, |n| n as u32);
1598
1599 if !DERIBIT_BOOK_VALID_DEPTHS.contains(&depth) {
1600 anyhow::bail!(
1601 "invalid depth {depth}; supported depths: {DERIBIT_BOOK_VALID_DEPTHS:?}"
1602 );
1603 }
1604
1605 let group = cmd
1606 .params
1607 .as_ref()
1608 .and_then(|p| p.get_str("group"))
1609 .unwrap_or(DERIBIT_BOOK_DEFAULT_GROUP)
1610 .to_string();
1611
1612 log::debug!(
1613 "Unsubscribing from book deltas for {} (group: {}, depth: {}, interval: {})",
1614 instrument_id,
1615 group,
1616 depth,
1617 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1618 );
1619
1620 self.spawn_command(async move {
1621 let result = if interval == Some(DeribitUpdateInterval::Raw) {
1622 ws.unsubscribe_book(instrument_id, interval).await
1623 } else {
1624 ws.unsubscribe_book_grouped(instrument_id, &group, depth, interval)
1625 .await
1626 };
1627
1628 if let Err(e) = result {
1629 log::error!("Failed to unsubscribe from book deltas for {instrument_id}: {e}");
1630 }
1631 });
1632
1633 Ok(())
1634 })
1635 }
1636
1637 fn unsubscribe_book_depth(&mut self, cmd: &UnsubscribeBookDepth) -> anyhow::Result<()> {
1638 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1639 let cmd = &cmd.with_instrument_ids(instrument_id);
1640 let ws = self
1641 .ws_client
1642 .as_ref()
1643 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1644 .clone();
1645 let interval = self.get_interval(&cmd.params);
1646 let group = cmd
1647 .params
1648 .as_ref()
1649 .and_then(|p| p.get_str("group"))
1650 .unwrap_or(DERIBIT_BOOK_DEFAULT_GROUP)
1651 .to_string();
1652
1653 log::debug!(
1654 "Unsubscribing from book depth for {} (group: {}, interval: {})",
1655 instrument_id,
1656 group,
1657 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1658 );
1659
1660 self.spawn_command(async move {
1661 if let Err(e) = ws
1662 .unsubscribe_book_depth(instrument_id, &group, interval)
1663 .await
1664 {
1665 log::error!("Failed to unsubscribe from book depth for {instrument_id}: {e}");
1666 }
1667 });
1668
1669 Ok(())
1670 })
1671 }
1672
1673 fn unsubscribe_quotes(&mut self, cmd: &UnsubscribeQuotes) -> anyhow::Result<()> {
1674 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1675 let ws = self
1676 .ws_client
1677 .as_ref()
1678 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1679 .clone();
1680
1681 self.spawn_command(async move {
1682 if let Err(e) = ws.unsubscribe_quotes(instrument_id).await {
1683 log::error!("Failed to unsubscribe from quotes for {instrument_id}: {e}");
1684 }
1685 });
1686
1687 Ok(())
1688 })
1689 }
1690
1691 fn unsubscribe_trades(&mut self, cmd: &UnsubscribeTrades) -> anyhow::Result<()> {
1692 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1693 let cmd = &cmd.with_instrument_ids(instrument_id);
1694 let ws = self
1695 .ws_client
1696 .as_ref()
1697 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1698 .clone();
1699 let command_id = cmd.command_id;
1700 let interval = self.get_interval(&cmd.params);
1701 let mut subscription_ids = vec![instrument_id];
1702 subscription_ids.extend(Self::combo_leg_trade_unsubs(
1703 &self.combo_leg_trade_subs,
1704 instrument_id,
1705 ));
1706 let subscription_count = subscription_ids.len();
1707
1708 log::debug!(
1709 "Unsubscribing from trades for {} instruments from {} (interval: {})",
1710 subscription_ids.len(),
1711 instrument_id,
1712 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1713 );
1714
1715 self.spawn_command(async move {
1716 for subscription_id in subscription_ids {
1717 if let Err(e) = ws.unsubscribe_trades(subscription_id, interval).await {
1718 log::error!("Failed to unsubscribe from trades for {subscription_id}: {e}");
1719 }
1720 }
1721
1722 log::debug!(
1723 "Processed trade unsubscription batch: command_id={command_id}, requests={subscription_count}, instrument={instrument_id}"
1724 );
1725 });
1726
1727 Ok(())
1728 })
1729 }
1730
1731 fn unsubscribe_mark_prices(&mut self, cmd: &UnsubscribeMarkPrices) -> anyhow::Result<()> {
1732 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1733 let cmd = &cmd.with_instrument_ids(instrument_id);
1734 let ws = self
1735 .ws_client
1736 .as_ref()
1737 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1738 .clone();
1739 let interval = self.get_interval(&cmd.params);
1740
1741 self.mark_price_subs.remove(&instrument_id);
1743
1744 log::debug!(
1745 "Unsubscribing from mark prices for {} (via ticker channel, interval: {})",
1746 instrument_id,
1747 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1748 );
1749
1750 self.spawn_command(async move {
1751 if let Err(e) = ws.unsubscribe_ticker(instrument_id, interval).await {
1752 log::error!("Failed to unsubscribe from mark prices for {instrument_id}: {e}");
1753 }
1754 });
1755
1756 Ok(())
1757 })
1758 }
1759
1760 fn unsubscribe_index_prices(&mut self, cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> {
1761 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1762 let cmd = &cmd.with_instrument_ids(instrument_id);
1763 let ws = self
1764 .ws_client
1765 .as_ref()
1766 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1767 .clone();
1768 let interval = self.get_interval(&cmd.params);
1769
1770 self.index_price_subs.remove(&instrument_id);
1772
1773 log::debug!(
1774 "Unsubscribing from index prices for {} (via ticker channel, interval: {})",
1775 instrument_id,
1776 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1777 );
1778
1779 self.spawn_command(async move {
1780 if let Err(e) = ws.unsubscribe_ticker(instrument_id, interval).await {
1781 log::error!("Failed to unsubscribe from index prices for {instrument_id}: {e}");
1782 }
1783 });
1784
1785 Ok(())
1786 })
1787 }
1788
1789 fn unsubscribe_bars(&mut self, cmd: &UnsubscribeBars) -> anyhow::Result<()> {
1790 execute_bar_subscriptions(&cmd.bar_types, |bar_type| {
1791 let ws = self
1792 .ws_client
1793 .as_ref()
1794 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1795 .clone();
1796 let instrument_id = bar_type.instrument_id();
1797 let resolution = bar_spec_to_resolution(&bar_type);
1798 self.spawn_command(async move {
1799 if let Err(e) = ws.unsubscribe_chart(instrument_id, &resolution).await {
1800 log::error!("Failed to unsubscribe from bars for {instrument_id}: {e}");
1801 }
1802 });
1803
1804 Ok(())
1805 })
1806 }
1807
1808 fn unsubscribe_funding_rates(&mut self, cmd: &UnsubscribeFundingRates) -> anyhow::Result<()> {
1809 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1810 let cmd = &cmd.with_instrument_ids(instrument_id);
1811 let is_perpetual = self
1813 .instruments
1814 .load()
1815 .get(&instrument_id)
1816 .is_some_and(|inst| matches!(inst, InstrumentAny::CryptoPerpetual(_)));
1817
1818 if !is_perpetual {
1819 log::warn!(
1820 "Funding rates unsubscription rejected for {instrument_id}: only available for perpetual instruments"
1821 );
1822 return Ok(());
1823 }
1824
1825 let ws = self
1826 .ws_client
1827 .as_ref()
1828 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1829 .clone();
1830 let interval = self.get_interval(&cmd.params);
1831
1832 log::debug!(
1833 "Unsubscribing from funding rates for {} (perpetual channel, interval: {})",
1834 instrument_id,
1835 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1836 );
1837
1838 self.spawn_command(async move {
1839 if let Err(e) = ws
1840 .unsubscribe_perpetual_interest_rates_updates(instrument_id, interval)
1841 .await
1842 {
1843 log::error!(
1844 "Failed to unsubscribe from funding rates for {instrument_id}: {e}"
1845 );
1846 }
1847 });
1848
1849 Ok(())
1850 })
1851 }
1852
1853 fn unsubscribe_option_greeks(&mut self, cmd: &UnsubscribeOptionGreeks) -> anyhow::Result<()> {
1854 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1855 let cmd = &cmd.with_instrument_ids(instrument_id);
1856 let ws = self
1857 .ws_client
1858 .as_ref()
1859 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1860 .clone();
1861 let interval = self.get_interval(&cmd.params);
1862
1863 self.option_greeks_subs.remove(&instrument_id);
1865
1866 log::debug!(
1867 "Unsubscribing from option greeks for {} (via ticker channel, interval: {})",
1868 instrument_id,
1869 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
1870 );
1871
1872 self.spawn_command(async move {
1873 if let Err(e) = ws.unsubscribe_ticker(instrument_id, interval).await {
1874 log::error!(
1875 "Failed to unsubscribe from option greeks for {instrument_id}: {e}"
1876 );
1877 }
1878 });
1879
1880 Ok(())
1881 })
1882 }
1883
1884 fn unsubscribe(&mut self, cmd: &UnsubscribeCustomData) -> anyhow::Result<()> {
1885 let data_type = cmd.data_type.type_name();
1886 if data_type != "DeribitVolatilityIndex" {
1887 log::warn!("Unsupported custom data unsubscription: {data_type}");
1888 return Ok(());
1889 }
1890
1891 let Some(index_name) = cmd
1892 .data_type
1893 .metadata()
1894 .as_ref()
1895 .and_then(|m| m.get("index_name"))
1896 .and_then(|v| v.as_str())
1897 .map(str::trim)
1898 .filter(|value| !value.is_empty())
1899 .map(ToString::to_string)
1900 else {
1901 log::warn!(
1902 "Rejected Deribit volatility index unsubscription: missing required metadata `index_name`"
1903 );
1904 return Ok(());
1905 };
1906
1907 log::debug!("Unsubscribing from Deribit volatility index: {index_name}");
1908
1909 let ws = self
1910 .ws_client
1911 .as_ref()
1912 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
1913 .clone();
1914
1915 self.spawn_command(async move {
1916 if let Err(e) = ws.unsubscribe_volatility_index(&index_name).await {
1917 log::error!("Failed to unsubscribe from volatility index {index_name}: {e}");
1918 }
1919 });
1920
1921 Ok(())
1922 }
1923
1924 fn request_instruments(&self, request: RequestInstruments) -> anyhow::Result<()> {
1925 if request.start.is_some() {
1926 log::warn!(
1927 "Requesting instruments for {:?} with specified `start` which has no effect",
1928 request.venue
1929 );
1930 }
1931
1932 if request.end.is_some() {
1933 log::warn!(
1934 "Requesting instruments for {:?} with specified `end` which has no effect",
1935 request.venue
1936 );
1937 }
1938
1939 let http_client = self.http_client.clone();
1940 let ws_client = self.ws_client.clone();
1941 let instruments_cache = Arc::clone(&self.instruments);
1942 let sender = self.data_sender.clone();
1943 let request_id = request.request_id;
1944 let client_id = request.client_id.unwrap_or(self.client_id);
1945 let start_nanos = datetime_to_unix_nanos(request.start);
1946 let end_nanos = datetime_to_unix_nanos(request.end);
1947 let params = request.params;
1948 let clock = self.clock;
1949 let venue = *DERIBIT_VENUE;
1950
1951 let product_types = if self.config.product_types.is_empty() {
1953 vec![crate::http::models::DeribitProductType::Future]
1954 } else {
1955 self.config.product_types.clone()
1956 };
1957
1958 self.spawn_command(async move {
1959 let mut all_instruments = Vec::new();
1960
1961 for product_type in &product_types {
1962 log::debug!(
1963 "Requesting instruments for currency=ANY, product_type={product_type:?}"
1964 );
1965
1966 match http_client
1967 .request_instruments(DeribitCurrency::ANY, Some(*product_type))
1968 .await
1969 {
1970 Ok(instruments) => {
1971 log::debug!(
1972 "Fetched {} instruments for ANY/{:?}",
1973 instruments.len(),
1974 product_type
1975 );
1976
1977 instruments_cache.rcu(|m| {
1978 for instrument in &instruments {
1979 m.insert(instrument.id(), instrument.clone());
1980 }
1981 });
1982 all_instruments.extend(instruments);
1983 }
1984 Err(e) => {
1985 log::error!("Failed to fetch instruments for ANY/{product_type:?}: {e:?}");
1986 }
1987 }
1988 }
1989
1990 if !all_instruments.is_empty() {
1993 http_client.cache_instruments(&all_instruments);
1994
1995 if let Some(ws) = &ws_client {
1996 ws.cache_instruments(&all_instruments);
1997 }
1998 }
1999
2000 let response = DataResponse::Instruments(InstrumentsResponse::new(
2002 request_id,
2003 client_id,
2004 venue,
2005 all_instruments,
2006 start_nanos,
2007 end_nanos,
2008 clock.get_time_ns(),
2009 params,
2010 ));
2011
2012 if let Err(e) = sender.send(DataEvent::Response(response)) {
2013 log::error!("Failed to send instruments response: {e}");
2014 }
2015 });
2016
2017 Ok(())
2018 }
2019
2020 fn request_instrument(&self, request: RequestInstrument) -> anyhow::Result<()> {
2021 let instrument_id = request.single_instrument_id()?;
2022
2023 if request.start.is_some() {
2024 log::warn!(
2025 "Requesting instrument {instrument_id} with specified `start` which has no effect"
2026 );
2027 }
2028
2029 if request.end.is_some() {
2030 log::warn!(
2031 "Requesting instrument {instrument_id} with specified `end` which has no effect"
2032 );
2033 }
2034
2035 log::debug!("Fetching instrument {instrument_id} from API");
2036
2037 let http_client = self.http_client.clone();
2038 let ws_client = self.ws_client.clone();
2039 let instruments_cache = Arc::clone(&self.instruments);
2040 let sender = self.data_sender.clone();
2041 let request_id = request.request_id;
2042 let client_id = request.client_id.unwrap_or(self.client_id);
2043 let start_nanos = datetime_to_unix_nanos(request.start);
2044 let end_nanos = datetime_to_unix_nanos(request.end);
2045 let params = request.params;
2046 let clock = self.clock;
2047
2048 self.spawn_command(async move {
2049 match http_client
2050 .request_instrument(instrument_id)
2051 .await
2052 .context("failed to request instrument from Deribit")
2053 {
2054 Ok(instrument) => {
2055 log::debug!("Successfully fetched instrument: {instrument_id}");
2056
2057 instruments_cache.insert(instrument.id(), instrument.clone());
2058 http_client.cache_instruments(std::slice::from_ref(&instrument));
2059
2060 if let Some(ws) = &ws_client {
2061 ws.cache_instruments(std::slice::from_ref(&instrument));
2062 }
2063
2064 let response = DataResponse::Instrument(Box::new(InstrumentResponse::new(
2066 request_id,
2067 client_id,
2068 instrument.id(),
2069 instrument,
2070 start_nanos,
2071 end_nanos,
2072 clock.get_time_ns(),
2073 params,
2074 )));
2075
2076 if let Err(e) = sender.send(DataEvent::Response(response)) {
2077 log::error!("Failed to send instrument response: {e}");
2078 }
2079 }
2080 Err(e) => {
2081 log::error!("Instrument request failed for {instrument_id}: {e:?}");
2082 }
2083 }
2084 });
2085
2086 Ok(())
2087 }
2088
2089 fn request_trades(&self, request: RequestTrades) -> anyhow::Result<()> {
2090 let instrument_id = request.single_instrument_id()?;
2091 self.validate_public_trades(instrument_id)?;
2092
2093 let http_client = self.http_client.clone();
2094 let sender = self.data_sender.clone();
2095 let start = request.start;
2096 let end = request.end;
2097 let limit = request.limit.map(|n| n.get() as u32);
2098 let request_id = request.request_id;
2099 let client_id = request.client_id.unwrap_or(self.client_id);
2100 let params = request.params;
2101 let clock = self.clock;
2102 let start_nanos = datetime_to_unix_nanos(start);
2103 let end_nanos = datetime_to_unix_nanos(end);
2104
2105 self.spawn_command(async move {
2106 match http_client
2107 .request_trades(instrument_id, start, end, limit)
2108 .await
2109 .context("failed to request trades from Deribit")
2110 {
2111 Ok(trades) => {
2112 let response = DataResponse::Trades(TradesResponse::new(
2113 request_id,
2114 client_id,
2115 instrument_id,
2116 trades,
2117 start_nanos,
2118 end_nanos,
2119 clock.get_time_ns(),
2120 params,
2121 ));
2122
2123 if let Err(e) = sender.send(DataEvent::Response(response)) {
2124 log::error!("Failed to send trades response: {e}");
2125 }
2126 }
2127 Err(e) => log::error!("Trades request failed for {instrument_id}: {e:?}"),
2128 }
2129 });
2130
2131 Ok(())
2132 }
2133
2134 fn request_bars(&self, request: RequestBars) -> anyhow::Result<()> {
2135 let bar_type = request.single_bar_type()?;
2136 self.validate_public_trades(bar_type.instrument_id())?;
2137
2138 let http_client = self.http_client.clone();
2139 let sender = self.data_sender.clone();
2140 let start = request.start;
2141 let end = request.end;
2142 let limit = request.limit.map(|n| n.get() as u32);
2143 let request_id = request.request_id;
2144 let client_id = request.client_id.unwrap_or(self.client_id);
2145 let params = request.params;
2146 let clock = self.clock;
2147 let start_nanos = datetime_to_unix_nanos(start);
2148 let end_nanos = datetime_to_unix_nanos(end);
2149
2150 self.spawn_command(async move {
2151 match http_client
2152 .request_bars(bar_type, start, end, limit)
2153 .await
2154 .context("failed to request bars from Deribit")
2155 {
2156 Ok(bars) => {
2157 let response = DataResponse::Bars(BarsResponse::new(
2158 request_id,
2159 client_id,
2160 bar_type,
2161 bars,
2162 start_nanos,
2163 end_nanos,
2164 clock.get_time_ns(),
2165 params,
2166 ));
2167
2168 if let Err(e) = sender.send(DataEvent::Response(response)) {
2169 log::error!("Failed to send bars response: {e}");
2170 }
2171 }
2172 Err(e) => log::error!("Bars request failed for {bar_type}: {e:?}"),
2173 }
2174 });
2175
2176 Ok(())
2177 }
2178
2179 fn request_book_snapshot(&self, request: RequestBookSnapshot) -> anyhow::Result<()> {
2180 let instrument_id = request.single_instrument_id()?;
2181
2182 let http_client = self.http_client.clone();
2183 let sender = self.data_sender.clone();
2184 let depth = request.depth.map(|n| n.get() as u32);
2185 let request_id = request.request_id;
2186 let client_id = request.client_id.unwrap_or(self.client_id);
2187 let params = request.params;
2188 let clock = self.clock;
2189
2190 self.spawn_command(async move {
2191 match http_client
2192 .request_book_snapshot(instrument_id, depth)
2193 .await
2194 .context("failed to request book snapshot from Deribit")
2195 {
2196 Ok(book) => {
2197 let response = DataResponse::Book(BookResponse::new(
2198 request_id,
2199 client_id,
2200 instrument_id,
2201 book,
2202 None,
2203 None,
2204 clock.get_time_ns(),
2205 params,
2206 ));
2207
2208 if let Err(e) = sender.send(DataEvent::Response(response)) {
2209 log::error!("Failed to send book snapshot response: {e}");
2210 }
2211 }
2212 Err(e) => {
2213 log::error!("Book snapshot request failed for {instrument_id}: {e:?}");
2214 }
2215 }
2216 });
2217
2218 Ok(())
2219 }
2220
2221 fn request_data(&self, request: RequestCustomData) -> anyhow::Result<()> {
2222 if request.data_type.type_name() != Self::BOOK_SUMMARY_TYPE_NAME {
2223 log::warn!(
2224 "Unsupported custom data request: {}",
2225 request.data_type.type_name()
2226 );
2227 return Ok(());
2228 }
2229
2230 let currency = Self::book_summary_metadata_currency(&request.data_type)?;
2231 let kind = Self::book_summary_metadata_kind(&request.data_type);
2232 let kind_str = kind.as_deref().unwrap_or("option").to_string();
2233 let data_type = Self::book_summary_data_type(¤cy, &kind_str);
2234 let http_client = self.http_client.clone();
2235 let sender = self.data_sender.clone();
2236 let request_id = request.request_id;
2237 let client_id = request.client_id;
2238 let params = request.params;
2239 let clock = self.clock;
2240 let venue = *DERIBIT_VENUE;
2241 let start = request.start;
2242 let end = request.end;
2243 let start_nanos = datetime_to_unix_nanos(start);
2244 let end_nanos = datetime_to_unix_nanos(end);
2245
2246 self.spawn_command(async move {
2247 log::debug!(
2248 "Requesting Deribit book summaries for currency={currency} kind={kind_str}"
2249 );
2250
2251 match http_client
2252 .request_book_summaries_kind(¤cy, Some(&kind_str))
2253 .await
2254 {
2255 Ok(summaries) => {
2256 let ts = clock.get_time_ns();
2257 let data: Vec<CustomData> = summaries
2258 .into_iter()
2259 .map(|raw| {
2260 CustomData::new(
2261 Arc::new(DeribitBookSummary::from_raw(raw, ts)),
2262 data_type.clone(),
2263 )
2264 })
2265 .collect();
2266
2267 let response = DataResponse::Data(CustomDataResponse::new(
2268 request_id,
2269 client_id,
2270 Some(venue),
2271 data_type,
2272 data,
2273 start_nanos,
2274 end_nanos,
2275 ts,
2276 params,
2277 ));
2278
2279 if let Err(e) = sender.send(DataEvent::Response(response)) {
2280 log::error!("Failed to send book summary response: {e}");
2281 }
2282 }
2283 Err(e) => {
2284 log::error!(
2286 "Book summary request failed for currency={currency} kind={kind_str}: {e:?}"
2287 );
2288 let ts = clock.get_time_ns();
2289 let response = DataResponse::Data(CustomDataResponse::new(
2290 request_id,
2291 client_id,
2292 Some(venue),
2293 data_type,
2294 Vec::<CustomData>::new(),
2295 start_nanos,
2296 end_nanos,
2297 ts,
2298 params,
2299 ));
2300
2301 if let Err(send_err) = sender.send(DataEvent::Response(response)) {
2302 log::error!("Failed to send empty book summary response: {send_err}");
2303 }
2304 }
2305 }
2306 });
2307
2308 Ok(())
2309 }
2310
2311 fn request_option_chain_reference_price(
2312 &self,
2313 request: RequestOptionChainReferencePrice,
2314 ) -> anyhow::Result<()> {
2315 let series_id = request.series_id;
2316 let instrument_id = request.instrument_id;
2317 let http_client = self.http_client.clone();
2318 let sender = self.data_sender.clone();
2319 let request_id = request.request_id;
2320 let client_id = request.client_id.unwrap_or(self.client_id());
2321 let params = request.params;
2322 let clock = self.clock;
2323
2324 self.spawn_command(async move {
2325 let instrument_name = instrument_id.symbol.to_string();
2326 let price = match http_client.request_ticker(&instrument_name).await {
2327 Ok(ticker) => ticker.underlying_price.and_then(|decimal| {
2328 if decimal <= Decimal::ZERO {
2329 return None;
2330 }
2331
2332 match Price::from_decimal(decimal) {
2333 Ok(price) => Some(price),
2334 Err(e) => {
2335 log::warn!(
2336 "Invalid Deribit option-chain reference price for {instrument_id}: {e}"
2337 );
2338 None
2339 }
2340 }
2341 }),
2342 Err(e) => {
2343 log::error!(
2344 "Option-chain reference price request failed for {series_id}: {e:?}"
2345 );
2346 None
2347 }
2348 };
2349 let response = DataResponse::OptionChainReferencePrice(
2350 OptionChainReferencePriceResponse::new(
2351 request_id,
2352 client_id,
2353 series_id,
2354 price,
2355 clock.get_time_ns(),
2356 params,
2357 ),
2358 );
2359
2360 if let Err(e) = sender.send(DataEvent::Response(response)) {
2361 log::error!("Failed to send option-chain reference price response: {e}");
2362 }
2363 });
2364
2365 Ok(())
2366 }
2367}
2368
2369impl DeribitDataClient {
2370 fn subscribe_trades_member(
2371 &self,
2372 instrument_id: InstrumentId,
2373 cmd: &SubscribeTrades,
2374 ) -> anyhow::Result<()> {
2375 self.validate_public_trades(instrument_id)?;
2376 let command_id = cmd.command_id;
2377 let needs_load = self.prepare_subscribe(instrument_id)?;
2378 let subscribe_combo_legs = Self::subscribe_combo_legs(&cmd.params);
2379
2380 let ws = self
2381 .ws_client
2382 .as_ref()
2383 .ok_or_else(|| anyhow::anyhow!("WebSocket client not initialized"))?
2384 .clone();
2385 let http_client = self.http_client.clone();
2386 let instruments = Arc::clone(&self.instruments);
2387 let combo_leg_trade_subs = Arc::clone(&self.combo_leg_trade_subs);
2388 let auto_load_missing_instruments = self.config.auto_load_missing_instruments;
2389 let interval = self.get_interval(&cmd.params);
2390
2391 log::debug!(
2392 "Subscribing to trades for {} (interval: {})",
2393 instrument_id,
2394 interval.map_or("100ms (default)".to_string(), |i| i.to_string())
2395 );
2396
2397 self.spawn_command(async move {
2398 if needs_load
2399 && let Err(e) =
2400 Self::lazy_load_instrument(&http_client, &ws, &instruments, instrument_id).await
2401 {
2402 log::error!("Lazy-load failed for {instrument_id} (trades): {e}");
2403 return;
2404 }
2405
2406 let mut subscription_ids = vec![instrument_id];
2407 if subscribe_combo_legs {
2408 subscription_ids.extend(Self::load_combo_trade_legs(
2409 &http_client, &ws, &instruments, instrument_id, auto_load_missing_instruments,
2410 ).await);
2411 }
2412
2413 let subscription_count = subscription_ids.len();
2414 let mut opened_leg_ids = Vec::new();
2415
2416 for subscription_id in subscription_ids {
2417 if let Err(e) = ws.subscribe_trades(subscription_id, interval).await {
2418 log::error!("Failed to subscribe to trades for {subscription_id}: {e}");
2419 continue;
2420 }
2421
2422 if subscription_id != instrument_id {
2423 opened_leg_ids.push(subscription_id);
2424 }
2425 }
2426
2427 Self::track_combo_leg_trade_subs(
2428 &combo_leg_trade_subs,
2429 instrument_id,
2430 &opened_leg_ids,
2431 );
2432
2433 log::debug!(
2434 "Processed trade subscription batch: command_id={command_id}, requests={subscription_count}, instrument={instrument_id}"
2435 );
2436 });
2437
2438 Ok(())
2439 }
2440
2441 async fn load_combo_trade_legs(
2442 http_client: &DeribitHttpClient,
2443 ws_client: &DeribitWebSocketClient,
2444 instruments: &Arc<AtomicMap<InstrumentId, InstrumentAny>>,
2445 instrument_id: InstrumentId,
2446 auto_load_missing_instruments: bool,
2447 ) -> Vec<InstrumentId> {
2448 let mut subscription_ids = Vec::new();
2449 let leg_ids = Self::combo_leg_trade_ids(instruments, instrument_id);
2450 if leg_ids.is_empty() {
2451 log::warn!(
2452 "No Deribit combo legs found for trade subscription opt-in on {instrument_id}"
2453 );
2454 }
2455
2456 for leg_id in leg_ids {
2457 if !instruments.contains_key(&leg_id) {
2458 if !auto_load_missing_instruments {
2459 log::error!(
2460 "Instrument {leg_id} not found and `auto_load_missing_instruments` is disabled"
2461 );
2462 continue;
2463 }
2464
2465 if let Err(e) =
2466 Self::lazy_load_instrument(http_client, ws_client, instruments, leg_id).await
2467 {
2468 log::error!("Lazy-load failed for {leg_id} (combo leg trades): {e}");
2469 continue;
2470 }
2471 }
2472
2473 subscription_ids.push(leg_id);
2474 }
2475
2476 subscription_ids
2477 }
2478}
2479
2480fn cache_instruments(
2484 mut fetched: Vec<InstrumentAny>,
2485 instruments: &AtomicMap<InstrumentId, InstrumentAny>,
2486 http_client: &DeribitHttpClient,
2487 ws_client: Option<&DeribitWebSocketClient>,
2488 sender: &EventSender<DataEvent>,
2489 cancellation: &CancellationToken,
2490 instruments_lock: &Mutex<()>,
2491) {
2492 fetched.sort_by_key(|instrument| instrument.id());
2493 let _guard = instruments_lock.lock();
2494
2495 if cancellation.is_cancelled() {
2496 return;
2497 }
2498
2499 let cached = instruments.load();
2500 for instrument in &mut fetched {
2501 if let Some(cached) = cached.get(&instrument.id()) {
2502 preserve_combo_leg_info(instrument, cached);
2503 }
2504 }
2505 drop(cached);
2506
2507 instruments.rcu(|m| {
2508 for instrument in &fetched {
2509 m.insert(instrument.id(), instrument.clone());
2510 }
2511 });
2512 http_client.cache_instruments(&fetched);
2513
2514 if let Some(ws) = ws_client {
2515 ws.cache_instruments(&fetched);
2516 }
2517
2518 for instrument in fetched {
2519 if let Err(e) = sender.send(DataEvent::Data(instrument.into())) {
2520 log::warn!("Failed to send instrument: {e}");
2521 }
2522 }
2523}
2524
2525fn preserve_combo_leg_info(instrument: &mut InstrumentAny, cached: &InstrumentAny) {
2529 let (info, cached_info) = match (instrument, cached) {
2530 (InstrumentAny::CryptoOptionSpread(spread), InstrumentAny::CryptoOptionSpread(cached)) => {
2531 (&mut spread.info, &cached.info)
2532 }
2533 (
2534 InstrumentAny::CryptoFuturesSpread(spread),
2535 InstrumentAny::CryptoFuturesSpread(cached),
2536 ) => (&mut spread.info, &cached.info),
2537 _ => return,
2538 };
2539
2540 if info
2541 .as_ref()
2542 .is_some_and(|info| info.contains_key("deribit_combo_legs"))
2543 {
2544 return;
2545 }
2546
2547 let Some(cached_info) = cached_info
2548 .as_ref()
2549 .filter(|cached_info| cached_info.contains_key("deribit_combo_legs"))
2550 else {
2551 return;
2552 };
2553
2554 let mut merged = info
2555 .as_ref()
2556 .map(|info| IndexMap::clone(info))
2557 .unwrap_or_default();
2558
2559 for key in [
2560 "deribit_combo_id",
2561 "deribit_combo_state",
2562 "deribit_combo_legs",
2563 ] {
2564 if let Some(value) = cached_info.get(key) {
2565 merged.insert(key.to_string(), value.clone());
2566 }
2567 }
2568 *info = Some(Params::new(merged));
2569}
2570
2571#[cfg(test)]
2572#[path = "../tests/integration/data_client.rs"]
2573mod data_client_tests;
2574
2575#[cfg(test)]
2576mod tests {
2577 use nautilus_common::live::runner::replace_data_event_sender;
2578 use nautilus_core::UnixNanos;
2579 use rstest::rstest;
2580
2581 use super::*;
2582 use crate::{
2583 common::{
2584 consts::DERIBIT_CLIENT_ID, parse::parse_deribit_instrument_any,
2585 rpc::DeribitJsonRpcResponse, testing::load_test_json,
2586 },
2587 http::models::DeribitInstrument,
2588 };
2589
2590 fn perpetual(ts_init: u64) -> InstrumentAny {
2591 let response: DeribitJsonRpcResponse<Vec<DeribitInstrument>> =
2592 serde_json::from_str(&load_test_json("http_get_instruments.json")).unwrap();
2593 let definition = response
2594 .result
2595 .unwrap()
2596 .into_iter()
2597 .find(|instrument| instrument.instrument_name == "BTC-PERPETUAL")
2598 .unwrap();
2599 let ts_init = UnixNanos::from(ts_init);
2600 parse_deribit_instrument_any(&definition, ts_init, ts_init)
2601 .unwrap()
2602 .unwrap()
2603 }
2604
2605 #[rstest]
2606 #[case::stop("stop")]
2607 #[case::reset("reset")]
2608 #[case::dispose("dispose")]
2609 #[tokio::test]
2610 async fn test_cache_instruments_rejects_completion_after_shutdown(#[case] shutdown: &str) {
2611 let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel();
2612 replace_data_event_sender(sender);
2613 let mut client =
2614 DeribitDataClient::new(*DERIBIT_CLIENT_ID, DeribitDataClientConfig::default()).unwrap();
2615 let original = perpetual(1);
2616 let instrument_id = original.id();
2617 client.instruments.insert(instrument_id, original);
2618 let cancellation = client.cancellation_token.clone();
2619
2620 match shutdown {
2621 "stop" => client.stop().unwrap(),
2622 "reset" => client.reset().unwrap(),
2623 "dispose" => client.dispose().unwrap(),
2624 _ => unreachable!(),
2625 }
2626
2627 cache_instruments(
2628 vec![perpetual(2)],
2629 &client.instruments,
2630 &client.http_client,
2631 client.ws_client.as_ref(),
2632 &client.data_sender,
2633 &cancellation,
2634 &client.instruments_lock,
2635 );
2636
2637 let expected = (shutdown != "reset").then_some(UnixNanos::from(1));
2638 assert_eq!(
2639 client
2640 .instruments
2641 .get_cloned(&instrument_id)
2642 .map(|instrument| instrument.ts_init()),
2643 expected
2644 );
2645 assert!(
2646 client
2647 .http_client
2648 .get_instrument(&instrument_id.symbol.inner())
2649 .is_none()
2650 );
2651 assert!(receiver.try_recv().is_err());
2652 }
2653
2654 fn combo(fixture: &str, legs: Option<&str>) -> InstrumentAny {
2655 let response: DeribitJsonRpcResponse<Vec<DeribitInstrument>> =
2656 serde_json::from_str(&load_test_json(fixture)).unwrap();
2657 let definition = response.result.unwrap().remove(0);
2658 let ts_init = UnixNanos::default();
2659 let mut instrument = parse_deribit_instrument_any(&definition, ts_init, ts_init)
2660 .unwrap()
2661 .unwrap();
2662 let info = legs.map(|leg| {
2663 Params::new(IndexMap::from([(
2664 "deribit_combo_legs".to_string(),
2665 serde_json::json!([{ "instrument_name": leg }]),
2666 )]))
2667 });
2668
2669 match &mut instrument {
2670 InstrumentAny::CryptoOptionSpread(spread) => spread.info = info,
2671 InstrumentAny::CryptoFuturesSpread(spread) => spread.info = info,
2672 _ => panic!("expected spread, was {instrument:?}"),
2673 }
2674 instrument
2675 }
2676
2677 fn combo_leg_name(instrument: &InstrumentAny) -> Option<&str> {
2678 let info = match instrument {
2679 InstrumentAny::CryptoOptionSpread(spread) => spread.info.as_ref(),
2680 InstrumentAny::CryptoFuturesSpread(spread) => spread.info.as_ref(),
2681 _ => None,
2682 }?;
2683 info.get("deribit_combo_legs")?[0]["instrument_name"].as_str()
2684 }
2685
2686 #[rstest]
2687 #[case::option_preserved(
2688 "http_get_instruments_option_combo.json",
2689 None,
2690 Some("cached"),
2691 Some("cached")
2692 )]
2693 #[case::futures_preserved(
2694 "http_get_instruments_future_combo.json",
2695 None,
2696 Some("cached"),
2697 Some("cached")
2698 )]
2699 #[case::fresh_wins(
2700 "http_get_instruments_future_combo.json",
2701 Some("fresh"),
2702 Some("cached"),
2703 Some("fresh")
2704 )]
2705 #[case::nothing_cached("http_get_instruments_future_combo.json", None, None, None)]
2706 fn test_preserve_combo_leg_info(
2707 #[case] fixture: &str,
2708 #[case] fetched_legs: Option<&str>,
2709 #[case] cached_legs: Option<&str>,
2710 #[case] expected: Option<&str>,
2711 ) {
2712 let mut fetched = combo(fixture, fetched_legs);
2713 let cached = combo(fixture, cached_legs);
2714
2715 preserve_combo_leg_info(&mut fetched, &cached);
2716
2717 assert_eq!(combo_leg_name(&fetched), expected);
2718 }
2719}