1use std::{
19 sync::{
20 Arc,
21 atomic::{AtomicBool, AtomicU64, Ordering},
22 },
23 time::Duration,
24};
25
26use ahash::AHashMap;
27use anyhow::Context;
28use dashmap::{DashMap, DashSet, mapref::entry::Entry};
29use nautilus_common::{
30 cache::InstrumentLookupError,
31 clients::{DataClient, execute_bar_subscriptions, execute_instrument_subscriptions},
32 live::{runner::get_data_event_sender, sender::EventSender},
33 messages::{
34 DataEvent,
35 data::{
36 BarsResponse, BookResponse, DataResponse, FundingRatesResponse, InstrumentResponse,
37 InstrumentsResponse, RequestBars, RequestBookDepth, RequestBookSnapshot,
38 RequestFundingRates, RequestInstrument, RequestInstruments, RequestQuotes,
39 RequestTrades, SubscribeBars, SubscribeBookDeltas, SubscribeBookDepth,
40 SubscribeFundingRates, SubscribeIndexPrices, SubscribeInstrument,
41 SubscribeInstrumentStatus, SubscribeMarkPrices, SubscribeQuotes, SubscribeTrades,
42 TradesResponse, UnsubscribeBars, UnsubscribeBookDeltas, UnsubscribeBookDepth,
43 UnsubscribeFundingRates, UnsubscribeIndexPrices, UnsubscribeInstrument,
44 UnsubscribeInstrumentStatus, UnsubscribeMarkPrices, UnsubscribeQuotes,
45 UnsubscribeTrades,
46 },
47 },
48};
49use nautilus_core::{
50 AtomicMap, UnixNanos,
51 datetime::datetime_to_unix_nanos,
52 time::{AtomicTime, get_atomic_clock_realtime},
53};
54use nautilus_live::{
55 SocketControlFactory,
56 task::{TaskGroup, TaskGroupGuard, TaskJoinOutcome, TaskSlot, finish_task},
57};
58use nautilus_model::{
59 data::{Data, InstrumentStatus, TradeTick},
60 enums::{BookType, MarketStatusAction},
61 identifiers::{ClientId, InstrumentId, Venue},
62 instruments::{Instrument, InstrumentAny},
63};
64use tokio_util::sync::CancellationToken;
65
66use crate::{
67 common::{
68 consts::DISCONNECT_TIMEOUT,
69 enums::{LighterCandleResolution, LighterMarketStatus},
70 rate_limit::resolve_quota,
71 symbol::MarketRegistry,
72 },
73 config::{LighterDataClientConfig, validate_proxy_url},
74 http::{
75 client::{LighterHttpClient, LighterRawHttpClient},
76 parse::parse_l2_order_book_snapshot,
77 query::LighterOrderBookOrdersQuery,
78 },
79 websocket::{
80 DATA_STREAMS_ENDPOINT, LighterWsError,
81 client::{LighterWebSocketClient, RetainedTaskSlot, TaskRetentionGuard},
82 messages::{LighterMarketSelection, LighterWsChannel, NautilusWsMessage},
83 },
84};
85
86mod limits;
87mod market_stats;
88
89use self::{
90 limits::{clamp_book_snapshot_limit, clamp_recent_trades_limit},
91 market_stats::{
92 MarketStatsKind, MarketStatsSubscription, emit_ws_message as emit_market_stats_ws_message,
93 subscribe_channel as subscribe_market_stats_channel,
94 unsubscribe_channel as unsubscribe_market_stats_channel,
95 },
96};
97
98#[derive(Debug)]
99pub struct LighterDataClient {
100 clock: &'static AtomicTime,
101 client_id: ClientId,
102 config: LighterDataClientConfig,
103 http_client: LighterHttpClient,
104 ws_client: LighterWebSocketClient,
105 registry: Arc<MarketRegistry>,
106 socket_factory: SocketControlFactory,
107 is_connected: AtomicBool,
108 cancellation_token: CancellationToken,
109 tasks: TaskGroup,
110 ws_disconnect_handle: TaskSlot<Result<(), LighterWsError>>,
111 ws_handler_retained: Arc<RetainedTaskSlot>,
112 shutdown_errors: Vec<String>,
113 data_sender: EventSender<DataEvent>,
114 instruments: Arc<AtomicMap<InstrumentId, InstrumentAny>>,
115 instrument_statuses: Arc<DashMap<InstrumentId, LighterMarketStatus>>,
116 instrument_status_subscriptions: Arc<DashSet<InstrumentId>>,
117 market_stats_subscriptions: Arc<DashMap<InstrumentId, MarketStatsSubscription>>,
118 market_stats_subscription_generations: Arc<DashMap<InstrumentId, u64>>,
119 next_market_stats_subscription_generation: AtomicU64,
120}
121
122impl LighterDataClient {
123 pub fn new(client_id: ClientId, config: LighterDataClientConfig) -> anyhow::Result<Self> {
129 validate_proxy_url(config.proxy_url.as_ref())?;
130 let clock = get_atomic_clock_realtime();
131 let data_sender = get_data_event_sender();
132 let venue = config.resolved_venue();
133 let settlement_currency = config.settlement_currency();
134 let socket_factory = SocketControlFactory::new(client_id, Some(venue));
135
136 let registry = Arc::new(MarketRegistry::new_with_venue_and_settlement_currency(
137 venue,
138 settlement_currency,
139 ));
140
141 let raw_http = LighterRawHttpClient::new_with_quotas(
142 config.environment,
143 Some(config.http_url()),
144 config.http_timeout_secs,
145 config
146 .proxy_url
147 .as_ref()
148 .map(|value| value.expose_secret().to_owned()),
149 resolve_quota(config.rest_quota_per_min),
150 None,
151 )
152 .context("failed to construct Lighter raw HTTP client")?;
153
154 let http_client =
155 LighterHttpClient::from_raw_with_registry(raw_http, Arc::clone(®istry));
156
157 let ws_client = Self::create_ws_client(&config, Arc::clone(®istry), &socket_factory);
158
159 let tasks = TaskGroup::new();
160
161 Ok(Self {
162 clock,
163 client_id,
164 config,
165 http_client,
166 ws_client,
167 registry,
168 socket_factory,
169 is_connected: AtomicBool::new(false),
170 cancellation_token: tasks.cancellation_token(),
171 tasks,
172 ws_disconnect_handle: TaskSlot::new(),
173 ws_handler_retained: Arc::new(RetainedTaskSlot::new()),
174 shutdown_errors: Vec::new(),
175 data_sender,
176 instruments: Arc::new(AtomicMap::new()),
177 instrument_statuses: Arc::new(DashMap::new()),
178 instrument_status_subscriptions: Arc::new(DashSet::new()),
179 market_stats_subscriptions: Arc::new(DashMap::new()),
180 market_stats_subscription_generations: Arc::new(DashMap::new()),
181 next_market_stats_subscription_generation: AtomicU64::new(1),
182 })
183 }
184
185 fn venue(&self) -> Venue {
186 self.config.resolved_venue()
187 }
188
189 #[must_use]
193 pub fn has_credentials(&self) -> bool {
194 self.config.has_credentials()
195 }
196
197 fn create_ws_client(
198 config: &LighterDataClientConfig,
199 registry: Arc<MarketRegistry>,
200 socket_factory: &SocketControlFactory,
201 ) -> LighterWebSocketClient {
202 let ws_client = LighterWebSocketClient::new(
203 Some(config.ws_url()),
204 config.environment,
205 registry,
206 config.transport_backend,
207 config.ws_timeout_secs,
208 Duration::from_secs(config.book_snapshot_timeout_secs),
209 config
210 .proxy_url
211 .as_ref()
212 .map(|value| value.expose_secret().to_owned()),
213 );
214
215 ws_client.with_socket_control(socket_factory.control(DATA_STREAMS_ENDPOINT))
216 }
217
218 fn take_ws_client(&mut self) -> LighterWebSocketClient {
219 std::mem::replace(
220 &mut self.ws_client,
221 Self::create_ws_client(
222 &self.config,
223 Arc::clone(&self.registry),
224 &self.socket_factory,
225 ),
226 )
227 }
228
229 fn spawn_ws_disconnect(&mut self) {
230 if self.ws_disconnect_handle.is_some() {
231 return;
232 }
233 self.ws_client.begin_shutdown();
234 let ws_client = self.take_ws_client();
235 let retained = Arc::clone(&self.ws_handler_retained);
236
237 if let Err(e) = self
238 .ws_disconnect_handle
239 .spawn(ws_client.disconnect_with_task_retention(retained))
240 {
241 log::error!("Failed to start Lighter WebSocket disconnect task: {e}");
242 }
243 }
244
245 fn spawn_task<F>(&self, fut: F)
247 where
248 F: std::future::Future<Output = ()> + Send + 'static,
249 {
250 let cancellation_token = self.cancellation_token.clone();
251
252 let future = async move {
253 tokio::select! {
254 biased;
255 () = cancellation_token.cancelled() => {}
256 () = fut => {}
257 }
258 };
259
260 if let Err(e) = self.tasks.spawn(future) {
261 log::debug!("Skipping Lighter data task after shutdown began: {e}");
262 }
263 }
264
265 fn abort_tasks(&self) {
266 self.tasks.begin_shutdown();
267 }
268
269 async fn shutdown_tasks(&mut self) -> anyhow::Result<()> {
270 self.tasks.begin_shutdown();
271 if let Err(e) = self
272 .tasks
273 .finish_shutdown(Duration::from_secs(1), DISCONNECT_TIMEOUT)
274 .await
275 {
276 self.shutdown_errors.push(format!("data tasks failed: {e}"));
277 }
278
279 Self::finish_owned_task(
280 &mut self.ws_disconnect_handle,
281 "WebSocket disconnect",
282 &mut self.shutdown_errors,
283 )
284 .await;
285
286 if let Err(e) = self.ws_handler_retained.finish().await {
287 self.shutdown_errors.push(e.to_string());
288 }
289
290 self.take_shutdown_result("Failed to terminate Lighter tasks")
291 }
292
293 fn take_shutdown_result(&mut self, context: &str) -> anyhow::Result<()> {
294 if self.shutdown_errors.is_empty() {
295 Ok(())
296 } else {
297 let errors = std::mem::take(&mut self.shutdown_errors);
298 anyhow::bail!("{context}: {}", errors.join("; "))
299 }
300 }
301
302 async fn finish_owned_task(
303 slot: &mut TaskSlot<Result<(), LighterWsError>>,
304 description: &str,
305 errors: &mut Vec<String>,
306 ) {
307 let Some(outcome) = finish_task(slot, DISCONNECT_TIMEOUT, DISCONNECT_TIMEOUT).await else {
308 return;
309 };
310
311 match outcome {
312 TaskJoinOutcome::Completed(Ok(())) | TaskJoinOutcome::Aborted => {}
313 TaskJoinOutcome::Completed(Err(e)) => {
314 errors.push(format!("{description} failed: {e}"));
315 }
316 TaskJoinOutcome::Failed(e) => {
317 errors.push(format!("{description} task failed: {e}"));
318 }
319 TaskJoinOutcome::Incomplete => {
320 errors.push(format!("{description} task did not stop after abort"));
321 }
322 }
323 }
324
325 async fn bootstrap_instruments(&self) -> anyhow::Result<Vec<InstrumentAny>> {
326 let instruments_with_status = self
327 .http_client
328 .request_instruments_with_status()
329 .await
330 .context("failed to fetch instruments during bootstrap")?;
331 let instruments: Vec<InstrumentAny> = instruments_with_status
332 .iter()
333 .map(|(instrument, _)| instrument.clone())
334 .collect();
335
336 let mut ws_cache: Vec<(i64, InstrumentAny)> = Vec::with_capacity(instruments.len());
337 self.instruments.rcu(|m| {
338 for instrument in &instruments {
339 m.insert(instrument.id(), instrument.clone());
340 }
341 });
342
343 for instrument in &instruments {
344 if let Some(market_index) = self.registry.market_index(&instrument.id()) {
345 ws_cache.push((market_index, instrument.clone()));
346 } else {
347 log::warn!(
348 "No market_index registered for instrument {} during bootstrap",
349 instrument.id(),
350 );
351 }
352 }
353
354 self.instrument_statuses.clear();
355 for (instrument, status) in &instruments_with_status {
356 cache_lighter_instrument_status(&self.instrument_statuses, instrument.id(), *status);
357 }
358
359 self.ws_client.cache_instruments(ws_cache);
360
361 log::debug!(
362 "Bootstrapped {} Lighter instruments ({} registry entries)",
363 self.instruments.len(),
364 self.registry.len(),
365 );
366 Ok(instruments)
367 }
368
369 async fn spawn_ws(&mut self) -> anyhow::Result<()> {
370 let mut ws_guard = TaskRetentionGuard::new(
374 self.ws_client.clone(),
375 Arc::clone(&self.ws_handler_retained),
376 );
377 ws_guard
378 .client_mut()
379 .connect_with_cancellation(self.cancellation_token.clone())
380 .await
381 .context("failed to connect to Lighter WebSocket")?;
382
383 if let Err(e) = ws_guard.client_mut().wait_until_active().await {
384 let ws_client = ws_guard.disarm();
385 let mut rollback_errors = Vec::new();
386
387 if let Err(e) = ws_client
388 .disconnect_with_task_retention(Arc::clone(&self.ws_handler_retained))
389 .await
390 {
391 rollback_errors.push(e.to_string());
392 }
393
394 if let Err(e) = self.ws_handler_retained.finish().await {
395 rollback_errors.push(e.to_string());
396 }
397
398 let readiness_error =
399 anyhow::Error::new(e).context("Lighter WebSocket did not reach active state");
400
401 if rollback_errors.is_empty() {
402 return Err(readiness_error);
403 }
404 return Err(readiness_error.context(format!(
405 "Lighter WebSocket readiness rollback failed: {}",
406 rollback_errors.join("; ")
407 )));
408 }
409
410 let mut ws_client = ws_guard.disarm();
411 self.ws_client.set_task_slot(ws_client.take_task_slot());
412
413 let cancellation_token = self.cancellation_token.clone();
414 let data_sender = self.data_sender.clone();
415 let market_stats_subscriptions = Arc::clone(&self.market_stats_subscriptions);
416
417 let future = async move {
418 log::debug!("Lighter WebSocket consumption loop started");
419
420 loop {
421 tokio::select! {
422 biased;
424 () = cancellation_token.cancelled() => {
425 log::debug!("Lighter WebSocket consumption loop cancelled");
426 break;
427 }
428 msg_opt = ws_client.next_event() => {
429 match msg_opt {
430 Some(NautilusWsMessage::Trades(trades)) => {
431 for trade in trades {
432 if let Err(e) = data_sender
433 .send(DataEvent::Data(Data::Trade(trade)))
434 {
435 log::error!("Failed to send trade tick: {e}");
436 }
437 }
438 }
439 Some(NautilusWsMessage::Quote(quote)) => {
440 if let Err(e) = data_sender
441 .send(DataEvent::Data(Data::Quote(quote)))
442 {
443 log::error!("Failed to send quote tick: {e}");
444 }
445 }
446 Some(NautilusWsMessage::Deltas(deltas)) => {
447 let data = Data::BookDeltas(Box::new(deltas));
448 if let Err(e) = data_sender.send(DataEvent::Data(data)) {
449 log::error!("Failed to send order book deltas: {e}");
450 }
451 }
452 Some(NautilusWsMessage::Depth(depth)) => {
453 if let Err(e) =
454 data_sender.send(DataEvent::Data(Data::BookDepth(depth)))
455 {
456 log::error!("Failed to send order book depth: {e}");
457 }
458 }
459 Some(NautilusWsMessage::Bar(bar)) => {
460 if let Err(e) = data_sender.send(DataEvent::Data(Data::Bar(bar))) {
461 log::error!("Failed to send bar: {e}");
462 }
463 }
464 Some(message @ (NautilusWsMessage::MarkPrice(_)
465 | NautilusWsMessage::IndexPrice(_)
466 | NautilusWsMessage::FundingRate(_))) =>
467 {
468 emit_market_stats_ws_message(
469 &data_sender,
470 &market_stats_subscriptions,
471 &message,
472 );
473 }
474 Some(NautilusWsMessage::Raw(value)) => {
475 log::debug!("Unhandled Lighter raw frame: {value}");
476 }
477 Some(
481 NautilusWsMessage::ExecutionReports(_)
482 | NautilusWsMessage::PositionSnapshot { .. }
483 | NautilusWsMessage::PositionUpdate { .. }
484 | NautilusWsMessage::AccountState(_)
485 | NautilusWsMessage::AccountTx { .. }
486 | NautilusWsMessage::SendTxAck { .. }
487 | NautilusWsMessage::SendTxRejected { .. }
488 | NautilusWsMessage::SendTxBatchResult { .. }
489 | NautilusWsMessage::AccountStreamFirstFrame(_),
490 ) => {}
491 Some(NautilusWsMessage::Reconnected { .. }) => {
492 log::debug!("Lighter WebSocket reconnected");
493 }
494 None => {
495 log::debug!("Lighter WebSocket next_event returned None");
496 tokio::select! {
497 () = cancellation_token.cancelled() => {
498 log::debug!(
499 "Lighter WebSocket consumption loop cancelled"
500 );
501 break;
502 }
503 () = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {}
504 }
505 }
506 }
507 }
508 }
509 }
510
511 log::debug!("Lighter WebSocket consumption loop finished");
512 };
513
514 self.tasks
515 .spawn(future)
516 .context("failed to register Lighter WebSocket consumption task")?;
517 log::debug!("Lighter WebSocket consumption task spawned");
518
519 Ok(())
520 }
521
522 fn spawn_instrument_refresh(&self) -> anyhow::Result<()> {
523 let minutes = self.config.update_instruments_interval_mins;
524 if minutes == 0 {
525 log::debug!("Lighter instrument refresh disabled (interval=0)");
526 return Ok(());
527 }
528
529 let interval = Duration::from_secs(minutes.saturating_mul(60));
530 let cancellation = self.cancellation_token.clone();
531 let http_client = self.http_client.clone();
532 let instruments_cache = Arc::clone(&self.instruments);
533 let statuses = Arc::clone(&self.instrument_statuses);
534 let status_subscriptions = Arc::clone(&self.instrument_status_subscriptions);
535 let registry = Arc::clone(&self.registry);
536 let ws_client = self.ws_client.clone();
537 let data_sender = self.data_sender.clone();
538 let client_id = self.client_id;
539 let clock = self.clock;
540
541 let future = async move {
542 loop {
543 let sleep = tokio::time::sleep(interval);
544 tokio::pin!(sleep);
545 tokio::select! {
546 () = cancellation.cancelled() => {
547 log::debug!("Lighter instrument refresh task cancelled");
548 break;
549 }
550 () = &mut sleep => {
551 let Some(result) = await_instrument_refresh(
552 &cancellation,
553 http_client.request_instruments_with_status(),
554 ).await else {
555 log::debug!("Lighter instrument refresh task cancelled");
556 break;
557 };
558
559 match result {
560 Ok(items) => {
561 instruments_cache.rcu(|m| {
562 for (instrument, _) in &items {
563 m.insert(instrument.id(), instrument.clone());
564 }
565 });
566
567 let ws_cache: Vec<(i64, InstrumentAny)> = items
568 .iter()
569 .filter_map(|(instrument, _)| {
570 registry
571 .market_index(&instrument.id())
572 .map(|idx| (idx, instrument.clone()))
573 })
574 .collect();
575
576 if !ws_cache.is_empty() {
577 ws_client.cache_instruments(ws_cache);
578 }
579
580 statuses.clear();
581 let ts_init = clock.get_time_ns();
582
583 for (instrument, status) in &items {
584 cache_lighter_instrument_status(
585 &statuses,
586 instrument.id(),
587 *status,
588 );
589 emit_lighter_instrument_status_if_subscribed(
590 &data_sender,
591 &status_subscriptions,
592 instrument.id(),
593 *status,
594 ts_init,
595 ts_init,
596 );
597
598 if let Err(e) = data_sender
599 .send(DataEvent::Data(instrument.clone().into()))
600 {
601 log::warn!(
602 "Failed to send refreshed Lighter instrument: {e}"
603 );
604 }
605 }
606
607 log::debug!(
608 "Lighter instruments refreshed: client_id={client_id}, count={}",
609 items.len(),
610 );
611 }
612 Err(e) => {
613 log::warn!(
614 "Failed to refresh Lighter instruments: client_id={client_id}, error={e:?}",
615 );
616 }
617 }
618 }
619 }
620 }
621 };
622
623 self.tasks
624 .spawn(future)
625 .context("failed to register Lighter instrument refresh task")?;
626 Ok(())
627 }
628
629 async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {
630 self.tasks.begin_shutdown();
631 self.ws_client.begin_shutdown();
632
633 if let Err(e) = self.shutdown_tasks().await {
634 self.shutdown_errors.push(e.to_string());
635 }
636 let ws_client = self.take_ws_client();
637 if let Err(e) = ws_client
638 .disconnect_with_task_retention(Arc::clone(&self.ws_handler_retained))
639 .await
640 {
641 self.shutdown_errors.push(e.to_string());
642 }
643
644 if let Err(e) = self.ws_handler_retained.finish().await {
645 self.shutdown_errors.push(e.to_string());
646 }
647 self.is_connected.store(false, Ordering::Release);
648
649 self.take_shutdown_result("Failed to roll back Lighter data startup")
650 }
651
652 fn clear_market_stats_subscriptions(&self) {
653 self.market_stats_subscriptions.clear();
654 self.market_stats_subscription_generations.clear();
655 }
656
657 fn clear_instrument_status_subscriptions(&self) {
658 self.instrument_status_subscriptions.clear();
659 }
660
661 fn emit_cached_instrument_status(&self, instrument_id: InstrumentId) -> bool {
662 let Some(status) = self
663 .instrument_statuses
664 .get(&instrument_id)
665 .map(|status| *status)
666 else {
667 return false;
668 };
669
670 let ts_init = self.clock.get_time_ns();
671 emit_lighter_instrument_status(&self.data_sender, instrument_id, status, ts_init, ts_init);
672 true
673 }
674
675 fn activate_market_stats_subscription(
676 &self,
677 instrument_id: InstrumentId,
678 channel: LighterWsChannel,
679 kind: MarketStatsKind,
680 label: &'static str,
681 ) {
682 let generation_entry = self
683 .market_stats_subscription_generations
684 .entry(instrument_id)
685 .or_insert_with(|| {
686 self.next_market_stats_subscription_generation
687 .fetch_add(1, Ordering::Relaxed)
688 });
689 let generation = *generation_entry;
690
691 let subscribe_channel = match self.market_stats_subscriptions.entry(instrument_id) {
692 Entry::Occupied(mut entry) => {
693 let subscription = entry.get_mut();
694 let should_subscribe = subscription.flags.is_empty();
695 subscription.flags.insert(kind);
696 should_subscribe.then(|| subscription.channel.clone())
697 }
698 Entry::Vacant(entry) => {
699 entry.insert(MarketStatsSubscription::new(channel.clone(), kind));
700 Some(channel)
701 }
702 };
703 drop(generation_entry);
704
705 if let Some(channel) = subscribe_channel {
706 let ws = self.ws_client.clone();
707 let subscriptions = Arc::clone(&self.market_stats_subscriptions);
708 let generations = Arc::clone(&self.market_stats_subscription_generations);
709 self.spawn_task(async move {
710 if let Err(e) = subscribe_market_stats_channel(ws, channel).await {
711 log::error!("Failed to subscribe to Lighter {label}: {e:?}");
712
713 rollback_market_stats_subscription(
716 &subscriptions,
717 &generations,
718 instrument_id,
719 generation,
720 );
721 }
722 });
723 }
724 }
725
726 fn deactivate_market_stats_subscription(
727 &self,
728 instrument_id: InstrumentId,
729 kind: MarketStatsKind,
730 label: &'static str,
731 ) {
732 let generation = self
734 .market_stats_subscription_generations
735 .entry(instrument_id);
736 let unsubscribe_channel = match self.market_stats_subscriptions.entry(instrument_id) {
737 Entry::Occupied(mut entry) => {
738 entry.get_mut().flags.remove(kind);
739 if entry.get().flags.is_empty() {
740 if let Entry::Occupied(generation) = generation {
741 generation.remove();
742 }
743 Some(entry.remove().channel)
744 } else {
745 None
746 }
747 }
748 Entry::Vacant(_) => None,
749 };
750
751 if let Some(channel) = unsubscribe_channel {
752 let ws = self.ws_client.clone();
753 self.spawn_task(async move {
754 if let Err(e) = unsubscribe_market_stats_channel(ws, channel).await {
755 log::error!("Failed to unsubscribe from Lighter {label}: {e:?}");
756 }
757 });
758 }
759 }
760
761 fn perp_market_stats_channel(
762 &self,
763 instrument_id: InstrumentId,
764 label: &str,
765 ) -> anyhow::Result<LighterWsChannel> {
766 let instrument = self
767 .instruments
768 .get_cloned(&instrument_id)
769 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
770
771 anyhow::ensure!(
772 matches!(instrument, InstrumentAny::CryptoPerpetual(_)),
773 "Lighter {label} subscriptions require a perpetual instrument: {instrument_id}",
774 );
775
776 let market_index = self.registry.market_index(&instrument_id).ok_or_else(|| {
777 anyhow::anyhow!("No Lighter market_index registered for {instrument_id}")
778 })?;
779
780 Ok(LighterWsChannel::MarketStats(
781 LighterMarketSelection::Market(market_index),
782 ))
783 }
784
785 fn index_market_stats_channel(
786 &self,
787 instrument_id: InstrumentId,
788 ) -> anyhow::Result<LighterWsChannel> {
789 let instrument = self
790 .instruments
791 .get_cloned(&instrument_id)
792 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
793 let market_index = self.registry.market_index(&instrument_id).ok_or_else(|| {
794 anyhow::anyhow!("No Lighter market_index registered for {instrument_id}")
795 })?;
796
797 match instrument {
798 InstrumentAny::CryptoPerpetual(_) => Ok(LighterWsChannel::MarketStats(
799 LighterMarketSelection::Market(market_index),
800 )),
801 InstrumentAny::CurrencyPair(_) => Ok(LighterWsChannel::SpotMarketStats(
802 LighterMarketSelection::Market(market_index),
803 )),
804 _ => anyhow::bail!(
805 "Lighter index price subscriptions require a perpetual or spot instrument: {instrument_id}",
806 ),
807 }
808 }
809}
810
811async fn await_instrument_refresh<T>(
812 cancellation: &CancellationToken,
813 request: impl std::future::Future<Output = T>,
814) -> Option<T> {
815 tokio::select! {
816 biased;
817 () = cancellation.cancelled() => None,
818 result = request => (!cancellation.is_cancelled()).then_some(result),
819 }
820}
821
822fn cache_lighter_instrument_status(
823 statuses: &DashMap<InstrumentId, LighterMarketStatus>,
824 instrument_id: InstrumentId,
825 status: LighterMarketStatus,
826) {
827 statuses.insert(instrument_id, status);
828}
829
830fn rollback_market_stats_subscription(
831 subscriptions: &DashMap<InstrumentId, MarketStatsSubscription>,
832 generations: &DashMap<InstrumentId, u64>,
833 instrument_id: InstrumentId,
834 failed_generation: u64,
835) {
836 let Entry::Occupied(generation) = generations.entry(instrument_id) else {
837 return;
838 };
839
840 if *generation.get() != failed_generation {
841 return;
842 }
843
844 subscriptions.remove(&instrument_id);
845 generation.remove();
846}
847
848fn emit_lighter_instrument_status_if_subscribed(
849 sender: &EventSender<DataEvent>,
850 subscriptions: &DashSet<InstrumentId>,
851 instrument_id: InstrumentId,
852 status: LighterMarketStatus,
853 ts_event: UnixNanos,
854 ts_init: UnixNanos,
855) {
856 if subscriptions.contains(&instrument_id) {
857 emit_lighter_instrument_status(sender, instrument_id, status, ts_event, ts_init);
858 }
859}
860
861fn emit_lighter_instrument_status(
862 sender: &EventSender<DataEvent>,
863 instrument_id: InstrumentId,
864 status: LighterMarketStatus,
865 ts_event: UnixNanos,
866 ts_init: UnixNanos,
867) {
868 let action = lighter_market_status_action(status);
869 let is_trading = Some(matches!(action, MarketStatusAction::Trading));
870 let status = InstrumentStatus::new(
871 instrument_id,
872 action,
873 ts_event,
874 ts_init,
875 None,
876 None,
877 is_trading,
878 None,
879 None,
880 );
881
882 if let Err(e) = sender.send(DataEvent::Data(Data::InstrumentStatus(status))) {
883 log::error!("Failed to send Lighter instrument status: {e}");
884 }
885}
886
887fn lighter_market_status_action(status: LighterMarketStatus) -> MarketStatusAction {
888 match status {
889 LighterMarketStatus::Active => MarketStatusAction::Trading,
890 LighterMarketStatus::Inactive => MarketStatusAction::NotAvailableForTrading,
891 }
892}
893
894#[async_trait::async_trait(?Send)]
895impl DataClient for LighterDataClient {
896 fn client_id(&self) -> ClientId {
897 self.client_id
898 }
899
900 fn venue(&self) -> Option<Venue> {
901 Some(self.venue())
902 }
903
904 fn start(&mut self) -> anyhow::Result<()> {
905 log::info!(
906 "Starting Lighter data client: client_id={}, environment={:?}, has_credentials={}",
907 self.client_id,
908 self.config.environment,
909 self.has_credentials(),
910 );
911 Ok(())
912 }
913
914 fn stop(&mut self) -> anyhow::Result<()> {
915 log::info!("Stopping Lighter data client {}", self.client_id);
916 self.abort_tasks();
917 self.spawn_ws_disconnect();
918 self.is_connected.store(false, Ordering::Release);
919 self.clear_instrument_status_subscriptions();
920 self.clear_market_stats_subscriptions();
921 Ok(())
922 }
923
924 fn reset(&mut self) -> anyhow::Result<()> {
925 log::debug!("Resetting Lighter data client {}", self.client_id);
926 self.abort_tasks();
927 self.spawn_ws_disconnect();
928 self.is_connected.store(false, Ordering::Release);
929 self.clear_instrument_status_subscriptions();
930 self.clear_market_stats_subscriptions();
931 Ok(())
932 }
933
934 fn dispose(&mut self) -> anyhow::Result<()> {
935 log::debug!("Disposing Lighter data client {}", self.client_id);
936 self.stop()
937 }
938
939 fn is_connected(&self) -> bool {
940 self.is_connected.load(Ordering::Acquire)
941 }
942
943 fn is_disconnected(&self) -> bool {
944 !self.is_connected()
945 }
946
947 async fn connect(&mut self) -> anyhow::Result<()> {
948 if self.is_connected()
949 && self.tasks.is_open()
950 && self.ws_disconnect_handle.is_none()
951 && self.ws_handler_retained.is_empty()
952 {
953 return Ok(());
954 }
955
956 if !self.tasks.is_open()
957 || !self.tasks.is_empty()
958 || self.ws_disconnect_handle.is_some()
959 || !self.ws_handler_retained.is_empty()
960 {
961 self.teardown_partial_connect().await?;
962 }
963
964 if !self.tasks.is_open() {
965 self.tasks.start_generation().map_err(|e| {
966 anyhow::anyhow!("Failed to start Lighter data task generation: {e}")
967 })?;
968 self.cancellation_token = self.tasks.cancellation_token();
969 }
970
971 let ws_client = self.ws_client.clone();
972 let setup_guard = TaskGroupGuard::new(&[&self.tasks], move || {
973 ws_client.begin_shutdown();
974 });
975
976 let instruments = self
977 .bootstrap_instruments()
978 .await
979 .context("failed to bootstrap Lighter instruments")?;
980
981 for instrument in instruments {
982 if let Err(e) = self.data_sender.send(DataEvent::Data(instrument.into())) {
983 log::warn!("Failed to send instrument: {e}");
984 }
985 }
986
987 let session_result = async {
988 self.spawn_ws()
989 .await
990 .context("failed to spawn Lighter WebSocket consumer")?;
991 self.spawn_instrument_refresh()?;
992 Ok::<(), anyhow::Error>(())
993 }
994 .await;
995
996 if let Err(e) = session_result {
997 if let Err(teardown_error) = self.teardown_partial_connect().await {
998 return Err(e.context(format!(
999 "Lighter data startup teardown failed: {teardown_error}"
1000 )));
1001 }
1002 return Err(e);
1003 }
1004
1005 setup_guard.disarm();
1006 self.is_connected.store(true, Ordering::Relaxed);
1007 log::info!("Connected: client_id={}", self.client_id);
1008
1009 Ok(())
1010 }
1011
1012 async fn disconnect(&mut self) -> anyhow::Result<()> {
1013 if !self.is_connected()
1014 && self.tasks.is_empty()
1015 && self.tasks.is_open()
1016 && self.ws_disconnect_handle.is_none()
1017 && self.ws_handler_retained.is_empty()
1018 && self.shutdown_errors.is_empty()
1019 {
1020 return Ok(());
1021 }
1022
1023 self.tasks.begin_shutdown();
1024 self.ws_client.begin_shutdown();
1025 self.clear_instrument_status_subscriptions();
1026 self.clear_market_stats_subscriptions();
1027
1028 if let Err(e) = self.shutdown_tasks().await {
1029 self.shutdown_errors.push(e.to_string());
1030 }
1031
1032 let ws_client = self.take_ws_client();
1033 if let Err(e) = ws_client
1034 .disconnect_with_task_retention(Arc::clone(&self.ws_handler_retained))
1035 .await
1036 {
1037 self.shutdown_errors.push(e.to_string());
1038 }
1039
1040 if let Err(e) = self.ws_handler_retained.finish().await {
1041 self.shutdown_errors.push(e.to_string());
1042 }
1043
1044 self.instruments.store(AHashMap::new());
1045 self.instrument_statuses.clear();
1046 self.registry.clear();
1047
1048 self.is_connected.store(false, Ordering::Relaxed);
1049 log::info!("Disconnected: client_id={}", self.client_id);
1050
1051 self.take_shutdown_result("Failed to disconnect Lighter data client")
1052 }
1053
1054 fn subscribe_instrument(&mut self, cmd: SubscribeInstrument) -> anyhow::Result<()> {
1055 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1056 let instruments = self.instruments.load();
1057 if let Some(instrument) = instruments.get(&instrument_id) {
1058 if let Err(e) = self
1059 .data_sender
1060 .send(DataEvent::Data(instrument.clone().into()))
1061 {
1062 log::error!("Failed to send instrument {instrument_id}: {e}");
1063 }
1064 } else {
1065 log::warn!("Instrument {instrument_id} not found in cache");
1066 }
1067
1068 Ok(())
1069 })
1070 }
1071
1072 fn unsubscribe_instrument(&mut self, cmd: &UnsubscribeInstrument) -> anyhow::Result<()> {
1073 execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
1074 log::debug!("Unsubscribing from instrument: {instrument_id} (cache replay only)",);
1075 Ok(())
1076 })
1077 }
1078
1079 fn subscribe_instrument_status(
1080 &mut self,
1081 subscription: SubscribeInstrumentStatus,
1082 ) -> anyhow::Result<()> {
1083 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1084 self.instrument_status_subscriptions.insert(instrument_id);
1085 if self.emit_cached_instrument_status(instrument_id) {
1086 return Ok(());
1087 }
1088
1089 let http = self.http_client.clone();
1090 let ws = self.ws_client.clone();
1091 let registry = Arc::clone(&self.registry);
1092 let sender = self.data_sender.clone();
1093 let instruments_cache = Arc::clone(&self.instruments);
1094 let statuses = Arc::clone(&self.instrument_statuses);
1095 let subscriptions = Arc::clone(&self.instrument_status_subscriptions);
1096 let clock = self.clock;
1097
1098 self.spawn_task(async move {
1099 match http.request_instrument_with_status(instrument_id).await {
1100 Ok((instrument, status)) => {
1101 instruments_cache.rcu(|map| {
1102 map.insert(instrument.id(), instrument.clone());
1103 });
1104
1105 if let Some(market_index) = registry.market_index(&instrument.id()) {
1106 ws.cache_instrument(market_index, instrument.clone());
1107 }
1108
1109 cache_lighter_instrument_status(&statuses, instrument.id(), status);
1110 let ts_init = clock.get_time_ns();
1111 emit_lighter_instrument_status_if_subscribed(
1112 &sender,
1113 &subscriptions,
1114 instrument.id(),
1115 status,
1116 ts_init,
1117 ts_init,
1118 );
1119 }
1120 Err(e) => {
1121 log::error!(
1122 "Failed to fetch Lighter instrument status for {instrument_id}: {e:?}"
1123 );
1124 }
1125 }
1126 });
1127
1128 Ok(())
1129 })
1130 }
1131
1132 fn subscribe_book_deltas(&mut self, subscription: SubscribeBookDeltas) -> anyhow::Result<()> {
1133 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1134 let subscription = subscription.with_instrument_ids(instrument_id);
1135 validate_book_deltas_subscription(subscription.book_type)?;
1136
1137 let ws = self.ws_client.clone();
1138 self.spawn_task(async move {
1139 if let Err(e) = ws.subscribe_book(instrument_id).await {
1140 log::error!("Failed to subscribe to Lighter book deltas: {e:?}");
1141 }
1142 });
1143
1144 Ok(())
1145 })
1146 }
1147
1148 fn subscribe_book_depth(&mut self, subscription: SubscribeBookDepth) -> anyhow::Result<()> {
1149 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1150 let subscription = subscription.with_instrument_ids(instrument_id);
1151 log::debug!("Subscribing to book depth: {instrument_id}");
1152
1153 validate_book_depth_subscription(subscription.book_type)?;
1154
1155 let ws = self.ws_client.clone();
1156 self.spawn_task(async move {
1157 if let Err(e) = ws.subscribe_book_depth(instrument_id).await {
1158 log::error!("Failed to subscribe to Lighter book depth: {e:?}");
1159 }
1160 });
1161
1162 Ok(())
1163 })
1164 }
1165
1166 fn subscribe_quotes(&mut self, subscription: SubscribeQuotes) -> anyhow::Result<()> {
1167 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1168 let ws = self.ws_client.clone();
1169 self.spawn_task(async move {
1170 if let Err(e) = ws.subscribe_quotes(instrument_id).await {
1171 log::error!("Failed to subscribe to Lighter quotes: {e:?}");
1172 }
1173 });
1174
1175 Ok(())
1176 })
1177 }
1178
1179 fn subscribe_trades(&mut self, subscription: SubscribeTrades) -> anyhow::Result<()> {
1180 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1181 let ws = self.ws_client.clone();
1182 self.spawn_task(async move {
1183 if let Err(e) = ws.subscribe_trades(instrument_id).await {
1184 log::error!("Failed to subscribe to Lighter trades: {e:?}");
1185 }
1186 });
1187
1188 Ok(())
1189 })
1190 }
1191
1192 fn subscribe_mark_prices(&mut self, subscription: SubscribeMarkPrices) -> anyhow::Result<()> {
1193 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1194 let channel = self.perp_market_stats_channel(instrument_id, "mark price")?;
1195 self.activate_market_stats_subscription(
1196 instrument_id,
1197 channel,
1198 MarketStatsKind::MarkPrice,
1199 "mark price",
1200 );
1201
1202 Ok(())
1203 })
1204 }
1205
1206 fn subscribe_index_prices(&mut self, subscription: SubscribeIndexPrices) -> anyhow::Result<()> {
1207 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1208 let channel = self.index_market_stats_channel(instrument_id)?;
1209 self.activate_market_stats_subscription(
1210 instrument_id,
1211 channel,
1212 MarketStatsKind::IndexPrice,
1213 "index price",
1214 );
1215
1216 Ok(())
1217 })
1218 }
1219
1220 fn subscribe_funding_rates(
1221 &mut self,
1222 subscription: SubscribeFundingRates,
1223 ) -> anyhow::Result<()> {
1224 execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
1225 let channel = self.perp_market_stats_channel(instrument_id, "funding rate")?;
1226 self.activate_market_stats_subscription(
1227 instrument_id,
1228 channel,
1229 MarketStatsKind::FundingRate,
1230 "funding rate",
1231 );
1232
1233 Ok(())
1234 })
1235 }
1236
1237 fn subscribe_bars(&mut self, subscription: SubscribeBars) -> anyhow::Result<()> {
1238 execute_bar_subscriptions(&subscription.bar_types, |bar_type| {
1239 let resolution = LighterCandleResolution::try_from(&bar_type)?;
1240 anyhow::ensure!(
1241 resolution.is_ws_streamable(),
1242 "Lighter does not offer {bar_type} on the candle WebSocket stream",
1243 );
1244
1245 let instrument_id = bar_type.instrument_id();
1246 if !self.instruments.contains_key(&instrument_id) {
1247 return Err(InstrumentLookupError::not_found(instrument_id).into());
1248 }
1249
1250 let ws = self.ws_client.clone();
1251 self.spawn_task(async move {
1252 if let Err(e) = ws.subscribe_candles(instrument_id, resolution).await {
1253 log::error!("Failed to subscribe to Lighter candles for {bar_type}: {e:?}");
1254 }
1255 });
1256
1257 Ok(())
1258 })
1259 }
1260
1261 fn unsubscribe_book_deltas(
1262 &mut self,
1263 unsubscription: &UnsubscribeBookDeltas,
1264 ) -> anyhow::Result<()> {
1265 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1266 log::debug!("Unsubscribing from book deltas: {instrument_id}");
1267
1268 let ws = self.ws_client.clone();
1269 self.spawn_task(async move {
1270 if let Err(e) = ws.unsubscribe_book(instrument_id).await {
1271 log::error!("Failed to unsubscribe from Lighter book deltas: {e:?}");
1272 }
1273 });
1274
1275 Ok(())
1276 })
1277 }
1278
1279 fn unsubscribe_book_depth(
1280 &mut self,
1281 unsubscription: &UnsubscribeBookDepth,
1282 ) -> anyhow::Result<()> {
1283 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1284 log::debug!("Unsubscribing from book depth: {instrument_id}");
1285
1286 let ws = self.ws_client.clone();
1287 self.spawn_task(async move {
1288 if let Err(e) = ws.unsubscribe_book_depth(instrument_id).await {
1289 log::error!("Failed to unsubscribe from Lighter book depth: {e:?}");
1290 }
1291 });
1292
1293 Ok(())
1294 })
1295 }
1296
1297 fn unsubscribe_quotes(&mut self, unsubscription: &UnsubscribeQuotes) -> anyhow::Result<()> {
1298 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1299 log::debug!("Unsubscribing from quotes: {instrument_id}");
1300
1301 let ws = self.ws_client.clone();
1302 self.spawn_task(async move {
1303 if let Err(e) = ws.unsubscribe_quotes(instrument_id).await {
1304 log::error!("Failed to unsubscribe from Lighter quotes: {e:?}");
1305 }
1306 });
1307
1308 Ok(())
1309 })
1310 }
1311
1312 fn unsubscribe_trades(&mut self, unsubscription: &UnsubscribeTrades) -> anyhow::Result<()> {
1313 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1314 log::debug!("Unsubscribing from trades: {instrument_id}");
1315
1316 let ws = self.ws_client.clone();
1317 self.spawn_task(async move {
1318 if let Err(e) = ws.unsubscribe_trades(instrument_id).await {
1319 log::error!("Failed to unsubscribe from Lighter trades: {e:?}");
1320 }
1321 });
1322
1323 Ok(())
1324 })
1325 }
1326
1327 fn unsubscribe_instrument_status(
1328 &mut self,
1329 unsubscription: &UnsubscribeInstrumentStatus,
1330 ) -> anyhow::Result<()> {
1331 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1332 self.instrument_status_subscriptions.remove(&instrument_id);
1333
1334 Ok(())
1335 })
1336 }
1337
1338 fn unsubscribe_mark_prices(
1339 &mut self,
1340 unsubscription: &UnsubscribeMarkPrices,
1341 ) -> anyhow::Result<()> {
1342 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1343 self.deactivate_market_stats_subscription(
1344 instrument_id,
1345 MarketStatsKind::MarkPrice,
1346 "mark price",
1347 );
1348
1349 Ok(())
1350 })
1351 }
1352
1353 fn unsubscribe_index_prices(
1354 &mut self,
1355 unsubscription: &UnsubscribeIndexPrices,
1356 ) -> anyhow::Result<()> {
1357 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1358 self.deactivate_market_stats_subscription(
1359 instrument_id,
1360 MarketStatsKind::IndexPrice,
1361 "index price",
1362 );
1363
1364 Ok(())
1365 })
1366 }
1367
1368 fn unsubscribe_funding_rates(
1369 &mut self,
1370 unsubscription: &UnsubscribeFundingRates,
1371 ) -> anyhow::Result<()> {
1372 execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
1373 self.deactivate_market_stats_subscription(
1374 instrument_id,
1375 MarketStatsKind::FundingRate,
1376 "funding rate",
1377 );
1378
1379 Ok(())
1380 })
1381 }
1382
1383 fn unsubscribe_bars(&mut self, unsubscription: &UnsubscribeBars) -> anyhow::Result<()> {
1384 execute_bar_subscriptions(&unsubscription.bar_types, |bar_type| {
1385 let resolution = match LighterCandleResolution::try_from(&bar_type) {
1386 Ok(resolution) => resolution,
1387 Err(e) => {
1388 log::warn!("Skipping Lighter candle unsubscribe for {bar_type}: {e}");
1389 return Ok(());
1390 }
1391 };
1392
1393 let instrument_id = bar_type.instrument_id();
1394 let ws = self.ws_client.clone();
1395 self.spawn_task(async move {
1396 if let Err(e) = ws.unsubscribe_candles(instrument_id, resolution).await {
1397 log::error!("Failed to unsubscribe from Lighter candles for {bar_type}: {e:?}");
1398 }
1399 });
1400
1401 Ok(())
1402 })
1403 }
1404
1405 fn request_instruments(&self, request: RequestInstruments) -> anyhow::Result<()> {
1406 log::debug!("Requesting Lighter instruments");
1407
1408 let http = self.http_client.clone();
1409 let ws = self.ws_client.clone();
1410 let registry = Arc::clone(&self.registry);
1411 let sender = self.data_sender.clone();
1412 let instruments_cache = Arc::clone(&self.instruments);
1413 let status_cache = Arc::clone(&self.instrument_statuses);
1414 let status_subscriptions = Arc::clone(&self.instrument_status_subscriptions);
1415 let request_id = request.request_id;
1416 let client_id = request.client_id.unwrap_or(self.client_id);
1417 let venue = self.venue();
1418 let start_nanos = datetime_to_unix_nanos(request.start);
1419 let end_nanos = datetime_to_unix_nanos(request.end);
1420 let params = request.params;
1421 let clock = self.clock;
1422
1423 self.spawn_task(async move {
1424 match http.request_instruments_with_status().await {
1425 Ok(instruments_with_status) => {
1426 let instruments: Vec<InstrumentAny> = instruments_with_status
1427 .iter()
1428 .map(|(instrument, _)| instrument.clone())
1429 .collect();
1430
1431 instruments_cache.rcu(|map| {
1432 for instrument in &instruments {
1433 map.insert(instrument.id(), instrument.clone());
1434 }
1435 });
1436
1437 let ws_cache: Vec<(i64, InstrumentAny)> = instruments
1438 .iter()
1439 .filter_map(|i| registry.market_index(&i.id()).map(|idx| (idx, i.clone())))
1440 .collect();
1441
1442 if !ws_cache.is_empty() {
1443 ws.cache_instruments(ws_cache);
1444 }
1445
1446 status_cache.clear();
1447 let ts_init = clock.get_time_ns();
1448
1449 for (instrument, status) in &instruments_with_status {
1450 cache_lighter_instrument_status(&status_cache, instrument.id(), *status);
1451 emit_lighter_instrument_status_if_subscribed(
1452 &sender,
1453 &status_subscriptions,
1454 instrument.id(),
1455 *status,
1456 ts_init,
1457 ts_init,
1458 );
1459 }
1460
1461 let response = DataResponse::Instruments(InstrumentsResponse::new(
1462 request_id,
1463 client_id,
1464 venue,
1465 instruments,
1466 start_nanos,
1467 end_nanos,
1468 clock.get_time_ns(),
1469 params,
1470 ));
1471
1472 if let Err(e) = sender.send(DataEvent::Response(response)) {
1473 log::error!("Failed to send instruments response: {e}");
1474 }
1475 }
1476 Err(e) => {
1477 log::error!("Failed to fetch Lighter instruments: {e:?}");
1478 }
1479 }
1480 });
1481
1482 Ok(())
1483 }
1484
1485 fn request_instrument(&self, request: RequestInstrument) -> anyhow::Result<()> {
1486 let instrument_id = request.single_instrument_id()?;
1487
1488 log::debug!("Requesting Lighter instrument: {instrument_id}");
1489
1490 let http = self.http_client.clone();
1491 let ws = self.ws_client.clone();
1492 let registry = Arc::clone(&self.registry);
1493 let sender = self.data_sender.clone();
1494 let instruments_cache = Arc::clone(&self.instruments);
1495 let status_cache = Arc::clone(&self.instrument_statuses);
1496 let status_subscriptions = Arc::clone(&self.instrument_status_subscriptions);
1497 let request_id = request.request_id;
1498 let client_id = request.client_id.unwrap_or(self.client_id);
1499 let start_nanos = datetime_to_unix_nanos(request.start);
1500 let end_nanos = datetime_to_unix_nanos(request.end);
1501 let params = request.params;
1502 let clock = self.clock;
1503
1504 self.spawn_task(async move {
1505 match http.request_instrument_with_status(instrument_id).await {
1506 Ok((instrument, status)) => {
1507 instruments_cache.rcu(|map| {
1508 map.insert(instrument.id(), instrument.clone());
1509 });
1510
1511 if let Some(market_index) = registry.market_index(&instrument.id()) {
1512 ws.cache_instrument(market_index, instrument.clone());
1513 }
1514
1515 cache_lighter_instrument_status(&status_cache, instrument.id(), status);
1516 let ts_init = clock.get_time_ns();
1517 emit_lighter_instrument_status_if_subscribed(
1518 &sender,
1519 &status_subscriptions,
1520 instrument.id(),
1521 status,
1522 ts_init,
1523 ts_init,
1524 );
1525
1526 let response = DataResponse::Instrument(Box::new(InstrumentResponse::new(
1527 request_id,
1528 client_id,
1529 instrument.id(),
1530 instrument,
1531 start_nanos,
1532 end_nanos,
1533 clock.get_time_ns(),
1534 params,
1535 )));
1536
1537 if let Err(e) = sender.send(DataEvent::Response(response)) {
1538 log::error!("Failed to send instrument response: {e}");
1539 }
1540 }
1541 Err(e) => {
1542 log::error!("Failed to fetch Lighter instrument {instrument_id}: {e:?}");
1543 }
1544 }
1545 });
1546
1547 Ok(())
1548 }
1549
1550 fn request_bars(&self, request: RequestBars) -> anyhow::Result<()> {
1551 let bar_type = request.single_bar_type()?;
1552
1553 log::debug!("Requesting Lighter bars for {bar_type}");
1554
1555 LighterCandleResolution::try_from(&bar_type)?;
1556
1557 let instrument_id = bar_type.instrument_id();
1558 let instrument = self
1559 .instruments
1560 .get_cloned(&instrument_id)
1561 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1562
1563 let http = self.http_client.clone();
1564 let sender = self.data_sender.clone();
1565 let start = request.start;
1566 let end = request.end;
1567 let limit = request.limit.map(|n| n.get() as u32);
1568 let request_id = request.request_id;
1569 let client_id = request.client_id.unwrap_or(self.client_id);
1570 let params = request.params;
1571 let clock = self.clock;
1572 let start_nanos = datetime_to_unix_nanos(start);
1573 let end_nanos = datetime_to_unix_nanos(end);
1574
1575 self.spawn_task(async move {
1576 match http
1577 .request_bars(&instrument, bar_type, start, end, limit)
1578 .await
1579 {
1580 Ok(bars) => {
1581 let response = DataResponse::Bars(BarsResponse::new(
1582 request_id,
1583 client_id,
1584 bar_type,
1585 bars,
1586 start_nanos,
1587 end_nanos,
1588 clock.get_time_ns(),
1589 params,
1590 ));
1591
1592 if let Err(e) = sender.send(DataEvent::Response(response)) {
1593 log::error!("Failed to send bars response: {e}");
1594 }
1595 }
1596 Err(e) => {
1597 log::error!("Lighter bars request failed for {instrument_id}: {e:?}");
1598 }
1599 }
1600 });
1601
1602 Ok(())
1603 }
1604
1605 fn request_quotes(&self, request: RequestQuotes) -> anyhow::Result<()> {
1606 let instrument_id = request.single_instrument_id()?;
1607
1608 anyhow::bail!(
1609 "Lighter does not support historical quote requests for {instrument_id}; \
1610 subscribe to quotes via WebSocket for live BBO ticks"
1611 )
1612 }
1613
1614 fn request_trades(&self, request: RequestTrades) -> anyhow::Result<()> {
1615 let instrument_id = request.single_instrument_id()?;
1616
1617 log::debug!("Requesting Lighter trades for {instrument_id}");
1618
1619 let instrument = self
1620 .instruments
1621 .get_cloned(&instrument_id)
1622 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1623
1624 let http = self.http_client.clone();
1625 let sender = self.data_sender.clone();
1626 let request_id = request.request_id;
1627 let client_id = request.client_id.unwrap_or(self.client_id);
1628 let limit = clamp_recent_trades_limit(request.limit);
1629 let start_nanos = datetime_to_unix_nanos(request.start);
1630 let end_nanos = datetime_to_unix_nanos(request.end);
1631 let params = request.params;
1632 let clock = self.clock;
1633
1634 self.spawn_task(async move {
1635 match http.request_recent_trades(&instrument, limit).await {
1636 Ok(mut trades) => {
1637 retain_trade_ticks_in_range(&mut trades, start_nanos, end_nanos);
1638
1639 let response = DataResponse::Trades(TradesResponse::new(
1640 request_id,
1641 client_id,
1642 instrument_id,
1643 trades,
1644 start_nanos,
1645 end_nanos,
1646 clock.get_time_ns(),
1647 params,
1648 ));
1649
1650 if let Err(e) = sender.send(DataEvent::Response(response)) {
1651 log::error!("Failed to send trades response: {e}");
1652 }
1653 }
1654 Err(e) => {
1655 log::error!("Lighter trades request failed for {instrument_id}: {e}");
1656 }
1657 }
1658 });
1659
1660 Ok(())
1661 }
1662
1663 fn request_funding_rates(&self, request: RequestFundingRates) -> anyhow::Result<()> {
1664 let instrument_id = request.single_instrument_id()?;
1665
1666 log::debug!("Requesting Lighter funding rates for {instrument_id}");
1667
1668 let instrument = self
1669 .instruments
1670 .get_cloned(&instrument_id)
1671 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1672
1673 anyhow::ensure!(
1674 matches!(instrument, InstrumentAny::CryptoPerpetual(_)),
1675 "Lighter funding-rate requests require a perpetual instrument: {instrument_id}",
1676 );
1677
1678 let http = self.http_client.clone();
1679 let sender = self.data_sender.clone();
1680 let request_id = request.request_id;
1681 let client_id = request.client_id.unwrap_or(self.client_id);
1682 let start = request.start;
1683 let end = request.end;
1684 let limit = request.limit.map(|n| n.get());
1685 let start_nanos = datetime_to_unix_nanos(start);
1686 let end_nanos = datetime_to_unix_nanos(end);
1687 let params = request.params;
1688 let clock = self.clock;
1689
1690 self.spawn_task(async move {
1691 match http
1692 .request_funding_rates(&instrument, start, end, limit)
1693 .await
1694 {
1695 Ok(funding_rates) => {
1696 let response = DataResponse::FundingRates(FundingRatesResponse::new(
1697 request_id,
1698 client_id,
1699 instrument_id,
1700 funding_rates,
1701 start_nanos,
1702 end_nanos,
1703 clock.get_time_ns(),
1704 params,
1705 ));
1706
1707 if let Err(e) = sender.send(DataEvent::Response(response)) {
1708 log::error!("Failed to send funding rates response: {e}");
1709 }
1710 }
1711 Err(e) => {
1712 log::error!("Lighter funding rates request failed for {instrument_id}: {e:?}");
1713 }
1714 }
1715 });
1716
1717 Ok(())
1718 }
1719
1720 fn request_book_snapshot(&self, request: RequestBookSnapshot) -> anyhow::Result<()> {
1721 let instrument_id = request.single_instrument_id()?;
1722
1723 log::debug!("Requesting Lighter book snapshot for {instrument_id}");
1724
1725 let instrument = self
1726 .instruments
1727 .get_cloned(&instrument_id)
1728 .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1729
1730 let market_index = self.registry.market_index(&instrument_id).ok_or_else(|| {
1731 anyhow::anyhow!("No Lighter market_index registered for {instrument_id}")
1732 })?;
1733
1734 let http = self.http_client.clone();
1735 let sender = self.data_sender.clone();
1736 let request_id = request.request_id;
1737 let client_id = request.client_id.unwrap_or(self.client_id);
1738 let limit = clamp_book_snapshot_limit(request.depth);
1739 let params = request.params;
1740 let clock = self.clock;
1741 let price_precision = instrument.price_precision();
1742 let size_precision = instrument.size_precision();
1743
1744 let query = LighterOrderBookOrdersQuery {
1745 market_id: market_index,
1746 limit,
1747 };
1748
1749 self.spawn_task(async move {
1750 match http.inner.get_order_book_orders(&query).await {
1751 Ok(snapshot) => {
1752 let ts_init = clock.get_time_ns();
1753 let book = parse_l2_order_book_snapshot(
1754 &snapshot,
1755 instrument_id,
1756 price_precision,
1757 size_precision,
1758 );
1759
1760 let response = DataResponse::Book(BookResponse::new(
1761 request_id,
1762 client_id,
1763 instrument_id,
1764 book,
1765 None,
1766 None,
1767 ts_init,
1768 params,
1769 ));
1770
1771 if let Err(e) = sender.send(DataEvent::Response(response)) {
1772 log::error!("Failed to send book snapshot response: {e}");
1773 }
1774 }
1775 Err(e) => {
1776 log::error!("Lighter book snapshot request failed for {instrument_id}: {e:?}");
1777 }
1778 }
1779 });
1780
1781 Ok(())
1782 }
1783
1784 fn request_book_depth(&self, request: RequestBookDepth) -> anyhow::Result<()> {
1785 let instrument_id = request.single_instrument_id()?;
1786
1787 anyhow::bail!(
1788 "Lighter does not support historical order book depth requests for {instrument_id}; \
1789 use request_book_snapshot for an L2 snapshot or subscribe_book_depth for live depth"
1790 )
1791 }
1792}
1793
1794fn retain_trade_ticks_in_range(
1795 trades: &mut Vec<TradeTick>,
1796 start_nanos: Option<UnixNanos>,
1797 end_nanos: Option<UnixNanos>,
1798) {
1799 trades.retain(|trade| trade_tick_in_range(trade.ts_event, start_nanos, end_nanos));
1800 trades.sort_by_key(|trade| trade.ts_event);
1801}
1802
1803fn trade_tick_in_range(
1804 ts_event: UnixNanos,
1805 start_nanos: Option<UnixNanos>,
1806 end_nanos: Option<UnixNanos>,
1807) -> bool {
1808 start_nanos.is_none_or(|start| ts_event >= start) && end_nanos.is_none_or(|end| ts_event <= end)
1809}
1810
1811fn validate_book_deltas_subscription(book_type: BookType) -> anyhow::Result<()> {
1816 validate_l2_mbp_book_type(book_type, "deltas")
1817}
1818
1819fn validate_book_depth_subscription(book_type: BookType) -> anyhow::Result<()> {
1820 validate_l2_mbp_book_type(book_type, "depth")
1821}
1822
1823fn validate_l2_mbp_book_type(book_type: BookType, label: &str) -> anyhow::Result<()> {
1824 anyhow::ensure!(
1825 book_type == BookType::L2_MBP,
1826 "Lighter only supports L2_MBP order book {label}",
1827 );
1828 Ok(())
1829}
1830
1831#[cfg(test)]
1832mod tests {
1833 use std::{num::NonZeroUsize, time::Duration};
1834
1835 use axum::{
1836 Router,
1837 extract::Query,
1838 http::StatusCode,
1839 response::{IntoResponse, Response},
1840 routing::get,
1841 };
1842 use jiff::Timestamp;
1843 use nautilus_common::live::runner::replace_data_event_sender;
1844 use nautilus_core::UUID4;
1845 use nautilus_model::{
1846 data::{
1847 BarSpecification, BarType, FundingRateUpdate, IndexPriceUpdate, MarkPriceUpdate,
1848 TradeTick,
1849 },
1850 enums::{AggregationSource, AggressorSide, BarAggregation, PriceType},
1851 identifiers::{InstrumentId, Symbol, TradeId},
1852 instruments::{CryptoPerpetual, CurrencyPair},
1853 types::{Currency, Price, Quantity},
1854 };
1855 use rstest::rstest;
1856 use rust_decimal::Decimal;
1857
1858 use super::{
1859 limits::{LIGHTER_BOOK_ORDERS_MAX_LIMIT, LIGHTER_RECENT_TRADES_MAX_LIMIT},
1860 market_stats::{MarketStatsFlags, MarketStatsSubscription},
1861 *,
1862 };
1863 use crate::{
1864 common::{
1865 consts::LIGHTER_VENUE,
1866 enums::{LighterFundingResolution, LighterProductType},
1867 },
1868 http::query::{LighterFundingsQuery, LighterRecentTradesQuery},
1869 };
1870
1871 struct DropSignal(Option<tokio::sync::oneshot::Sender<()>>);
1872
1873 impl Drop for DropSignal {
1874 fn drop(&mut self) {
1875 if let Some(sender) = self.0.take() {
1876 let _ = sender.send(());
1877 }
1878 }
1879 }
1880
1881 const HTTP_ORDER_BOOK_DETAILS: &str =
1882 include_str!("../../test_data/http_order_book_details.json");
1883 const HTTP_FUNDINGS: &str = include_str!("../../test_data/http_fundings.json");
1884 const HTTP_RECENT_TRADES: &str = include_str!("../../test_data/http_recent_trades.json");
1885 const HTTP_RECENT_TRADES_NULL: &str =
1886 include_str!("../../test_data/http_recent_trades_null.json");
1887 const HTTP_RECENT_TRADES_UNORDERED: &str =
1888 include_str!("../../test_data/http_recent_trades_unordered.json");
1889 const PRIVATE_KEY_HEX: &str =
1890 "0b8e0f63c24d8baacd9d29ad4e9a4b73c4a8d2bb8b16dc4fa9d7c2e1d3a8b1f0e8d3a4c5b6e7f001";
1891
1892 #[rstest]
1893 #[case::none_defaults_to_cap(None, LIGHTER_BOOK_ORDERS_MAX_LIMIT)]
1894 #[case::below_cap_passes_through(Some(10), 10)]
1895 #[case::at_cap_passes_through(
1896 Some(LIGHTER_BOOK_ORDERS_MAX_LIMIT as usize),
1897 LIGHTER_BOOK_ORDERS_MAX_LIMIT
1898 )]
1899 #[case::above_cap_clamps(Some(500), LIGHTER_BOOK_ORDERS_MAX_LIMIT)]
1900 #[case::usize_max_clamps(Some(usize::MAX), LIGHTER_BOOK_ORDERS_MAX_LIMIT)]
1901 fn test_clamp_book_snapshot_limit(#[case] depth: Option<usize>, #[case] expected: u16) {
1902 let depth = depth.map(|n| NonZeroUsize::new(n).expect("non-zero"));
1903 assert_eq!(clamp_book_snapshot_limit(depth), expected);
1904 }
1905
1906 #[rstest]
1907 #[case::none_defaults_to_cap(None, LIGHTER_RECENT_TRADES_MAX_LIMIT)]
1908 #[case::below_cap_passes_through(Some(10), 10)]
1909 #[case::at_cap_passes_through(
1910 Some(LIGHTER_RECENT_TRADES_MAX_LIMIT as usize),
1911 LIGHTER_RECENT_TRADES_MAX_LIMIT
1912 )]
1913 #[case::above_cap_clamps(Some(500), LIGHTER_RECENT_TRADES_MAX_LIMIT)]
1914 #[case::usize_max_clamps(Some(usize::MAX), LIGHTER_RECENT_TRADES_MAX_LIMIT)]
1915 fn test_clamp_recent_trades_limit(#[case] limit: Option<usize>, #[case] expected: u16) {
1916 let limit = limit.map(|n| NonZeroUsize::new(n).expect("non-zero"));
1917 assert_eq!(clamp_recent_trades_limit(limit), expected);
1918 }
1919
1920 #[rstest]
1921 fn test_new_uses_readonly_websocket_url() {
1922 let client = create_data_client_for_test();
1923
1924 assert_eq!(
1925 client.ws_client.url(),
1926 "wss://mainnet.zklighter.elliot.ai/stream?readonly=true",
1927 );
1928 }
1929
1930 #[rstest]
1931 fn test_validate_book_deltas_accepts_l2_mbp() {
1932 assert!(validate_book_deltas_subscription(BookType::L2_MBP).is_ok());
1933 }
1934
1935 #[rstest]
1936 #[case(BookType::L1_MBP)]
1937 #[case(BookType::L3_MBO)]
1938 fn test_validate_book_deltas_rejects_other_book_types(#[case] book_type: BookType) {
1939 let err = validate_book_deltas_subscription(book_type).unwrap_err();
1940 assert!(
1941 err.to_string().contains("L2_MBP"),
1942 "expected error to cite L2_MBP, was: {err}",
1943 );
1944 }
1945
1946 #[rstest]
1947 fn test_validate_book_depth_accepts_l2_mbp() {
1948 assert!(validate_book_depth_subscription(BookType::L2_MBP).is_ok());
1949 }
1950
1951 #[rstest]
1952 #[case(BookType::L1_MBP)]
1953 #[case(BookType::L3_MBO)]
1954 fn test_validate_book_depth_rejects_other_book_types(#[case] book_type: BookType) {
1955 let err = validate_book_depth_subscription(book_type).unwrap_err();
1956 assert!(
1957 err.to_string().contains("depth"),
1958 "expected error to cite depth, was: {err}",
1959 );
1960 }
1961
1962 #[rstest]
1963 #[case(LighterMarketStatus::Active, MarketStatusAction::Trading)]
1964 #[case(
1965 LighterMarketStatus::Inactive,
1966 MarketStatusAction::NotAvailableForTrading
1967 )]
1968 fn test_lighter_market_status_action(
1969 #[case] status: LighterMarketStatus,
1970 #[case] expected: MarketStatusAction,
1971 ) {
1972 assert_eq!(lighter_market_status_action(status), expected);
1973 }
1974
1975 #[tokio::test]
1976 async fn test_subscribe_instrument_status_replays_cached_status() {
1977 let (mut client, mut receiver) = create_data_client_with_receiver_for_test();
1978 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
1979 cache_lighter_instrument_status(
1980 &client.instrument_statuses,
1981 instrument_id,
1982 LighterMarketStatus::Active,
1983 );
1984
1985 DataClient::subscribe_instrument_status(
1986 &mut client,
1987 SubscribeInstrumentStatus::new(
1988 instrument_id,
1989 Some(ClientId::new("LIGHTER")),
1990 None,
1991 UUID4::new(),
1992 UnixNanos::default(),
1993 None,
1994 None,
1995 ),
1996 )
1997 .unwrap();
1998
1999 let event = receiver.recv().await.expect("instrument status event");
2000 match event {
2001 DataEvent::Data(Data::InstrumentStatus(status)) => {
2002 assert_eq!(status.instrument_id, instrument_id);
2003 assert_eq!(status.action, MarketStatusAction::Trading);
2004 assert_eq!(status.is_trading, Some(true));
2005 }
2006 event => panic!("expected instrument status, was {event:?}"),
2007 }
2008 }
2009
2010 #[tokio::test]
2011 async fn test_subscribe_instrument_status_fetches_when_cache_is_empty() {
2012 let base_url = spawn_order_book_details_server().await;
2013 let config = LighterDataClientConfig {
2014 base_url_http: Some(base_url),
2015 ..Default::default()
2016 };
2017 let (mut client, mut receiver) =
2018 create_data_client_with_receiver_and_config_for_test(config);
2019 let instrument_id = client.registry.insert(0, "ETH", LighterProductType::Perp);
2020
2021 DataClient::subscribe_instrument_status(
2022 &mut client,
2023 SubscribeInstrumentStatus::new(
2024 instrument_id,
2025 Some(ClientId::new("LIGHTER")),
2026 None,
2027 UUID4::new(),
2028 UnixNanos::default(),
2029 None,
2030 None,
2031 ),
2032 )
2033 .unwrap();
2034
2035 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2036 .await
2037 .expect("instrument status response")
2038 .expect("instrument status event");
2039
2040 match event {
2041 DataEvent::Data(Data::InstrumentStatus(status)) => {
2042 assert_eq!(status.instrument_id, instrument_id);
2043 assert_eq!(status.action, MarketStatusAction::Trading);
2044 assert_eq!(status.is_trading, Some(true));
2045 }
2046 event => panic!("expected instrument status, was {event:?}"),
2047 }
2048 assert!(client.instruments.get_cloned(&instrument_id).is_some());
2049 assert_eq!(
2050 client
2051 .instrument_statuses
2052 .get(&instrument_id)
2053 .map(|status| *status),
2054 Some(LighterMarketStatus::Active),
2055 );
2056 }
2057
2058 #[tokio::test]
2059 async fn test_market_stats_subscriptions_share_perp_channel_until_last_unsub() {
2060 let mut client = create_data_client_for_test();
2061 client.cancellation_token.cancel();
2063 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2064
2065 DataClient::subscribe_mark_prices(
2066 &mut client,
2067 SubscribeMarkPrices::new(
2068 instrument_id,
2069 Some(ClientId::new("LIGHTER")),
2070 None,
2071 UUID4::new(),
2072 UnixNanos::default(),
2073 None,
2074 None,
2075 ),
2076 )
2077 .unwrap();
2078 DataClient::subscribe_index_prices(
2079 &mut client,
2080 SubscribeIndexPrices::new(
2081 instrument_id,
2082 Some(ClientId::new("LIGHTER")),
2083 None,
2084 UUID4::new(),
2085 UnixNanos::default(),
2086 None,
2087 None,
2088 ),
2089 )
2090 .unwrap();
2091 DataClient::subscribe_funding_rates(
2092 &mut client,
2093 SubscribeFundingRates::new(
2094 instrument_id,
2095 Some(ClientId::new("LIGHTER")),
2096 None,
2097 UUID4::new(),
2098 UnixNanos::default(),
2099 None,
2100 None,
2101 ),
2102 )
2103 .unwrap();
2104
2105 let subscription = client
2106 .market_stats_subscriptions
2107 .get(&instrument_id)
2108 .expect("market stats subscription");
2109 assert_eq!(
2110 subscription.flags,
2111 MarketStatsFlags {
2112 mark_price: true,
2113 index_price: true,
2114 funding_rate: true,
2115 },
2116 );
2117 assert!(matches!(
2118 subscription.channel,
2119 LighterWsChannel::MarketStats(LighterMarketSelection::Market(0)),
2120 ));
2121 drop(subscription);
2122 assert!(
2123 client
2124 .market_stats_subscription_generations
2125 .contains_key(&instrument_id),
2126 );
2127
2128 DataClient::unsubscribe_mark_prices(
2129 &mut client,
2130 &UnsubscribeMarkPrices::new(
2131 instrument_id,
2132 Some(ClientId::new("LIGHTER")),
2133 None,
2134 UUID4::new(),
2135 UnixNanos::default(),
2136 None,
2137 None,
2138 ),
2139 )
2140 .unwrap();
2141 assert_eq!(
2142 client
2143 .market_stats_subscriptions
2144 .get(&instrument_id)
2145 .expect("index and funding still active")
2146 .flags,
2147 MarketStatsFlags {
2148 index_price: true,
2149 funding_rate: true,
2150 ..Default::default()
2151 },
2152 );
2153
2154 DataClient::unsubscribe_index_prices(
2155 &mut client,
2156 &UnsubscribeIndexPrices::new(
2157 instrument_id,
2158 Some(ClientId::new("LIGHTER")),
2159 None,
2160 UUID4::new(),
2161 UnixNanos::default(),
2162 None,
2163 None,
2164 ),
2165 )
2166 .unwrap();
2167 assert_eq!(
2168 client
2169 .market_stats_subscriptions
2170 .get(&instrument_id)
2171 .expect("funding still active")
2172 .flags,
2173 MarketStatsFlags {
2174 funding_rate: true,
2175 ..Default::default()
2176 },
2177 );
2178
2179 DataClient::unsubscribe_funding_rates(
2180 &mut client,
2181 &UnsubscribeFundingRates::new(
2182 instrument_id,
2183 Some(ClientId::new("LIGHTER")),
2184 None,
2185 UUID4::new(),
2186 UnixNanos::default(),
2187 None,
2188 None,
2189 ),
2190 )
2191 .unwrap();
2192 assert!(
2193 !client
2194 .market_stats_subscriptions
2195 .contains_key(&instrument_id)
2196 );
2197 assert!(
2198 !client
2199 .market_stats_subscription_generations
2200 .contains_key(&instrument_id),
2201 );
2202 }
2203
2204 #[rstest]
2205 fn test_market_stats_ws_forwarding_requires_matching_subscription() {
2206 let subscriptions = DashMap::new();
2207 let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel();
2208 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2209 let other_instrument_id = InstrumentId::new(Symbol::new("BTC-PERP"), *LIGHTER_VENUE);
2210
2211 subscriptions.insert(
2212 instrument_id,
2213 MarketStatsSubscription {
2214 channel: LighterWsChannel::MarketStats(LighterMarketSelection::Market(0)),
2215 flags: MarketStatsFlags {
2216 mark_price: true,
2217 index_price: true,
2218 funding_rate: true,
2219 },
2220 },
2221 );
2222
2223 assert!(emit_market_stats_ws_message(
2224 &sender.clone().into(),
2225 &subscriptions,
2226 &NautilusWsMessage::MarkPrice(MarkPriceUpdate::new(
2227 instrument_id,
2228 Price::from("2000.00"),
2229 UnixNanos::from(10),
2230 UnixNanos::from(1),
2231 )),
2232 ));
2233 assert!(emit_market_stats_ws_message(
2234 &sender.clone().into(),
2235 &subscriptions,
2236 &NautilusWsMessage::IndexPrice(IndexPriceUpdate::new(
2237 instrument_id,
2238 Price::from("1999.50"),
2239 UnixNanos::from(11),
2240 UnixNanos::from(1),
2241 )),
2242 ));
2243 assert!(emit_market_stats_ws_message(
2244 &sender.clone().into(),
2245 &subscriptions,
2246 &NautilusWsMessage::FundingRate(FundingRateUpdate::new(
2247 instrument_id,
2248 Decimal::new(12, 6),
2249 None,
2250 Some(UnixNanos::from(100)),
2251 UnixNanos::from(12),
2252 UnixNanos::from(1),
2253 )),
2254 ));
2255
2256 match receiver.try_recv().unwrap() {
2257 DataEvent::Data(Data::MarkPrice(update)) => {
2258 assert_eq!(update.instrument_id, instrument_id);
2259 assert_eq!(update.value, Price::from("2000.00"));
2260 }
2261 event => panic!("expected mark price update, was {event:?}"),
2262 }
2263
2264 match receiver.try_recv().unwrap() {
2265 DataEvent::Data(Data::IndexPrice(update)) => {
2266 assert_eq!(update.instrument_id, instrument_id);
2267 assert_eq!(update.value, Price::from("1999.50"));
2268 }
2269 event => panic!("expected index price update, was {event:?}"),
2270 }
2271
2272 match receiver.try_recv().unwrap() {
2273 DataEvent::Data(Data::FundingRate(update)) => {
2274 assert_eq!(update.instrument_id, instrument_id);
2275 assert_eq!(update.rate, Decimal::new(12, 6));
2276 }
2277 event => panic!("expected funding rate update, was {event:?}"),
2278 }
2279
2280 assert!(!emit_market_stats_ws_message(
2281 &sender.into(),
2282 &subscriptions,
2283 &NautilusWsMessage::MarkPrice(MarkPriceUpdate::new(
2284 other_instrument_id,
2285 Price::from("1.00"),
2286 UnixNanos::from(13),
2287 UnixNanos::from(1),
2288 )),
2289 ));
2290 assert!(receiver.try_recv().is_err());
2291 }
2292
2293 #[rstest]
2294 fn test_index_market_stats_channel_uses_spot_stream_for_spot_instrument() {
2295 let client = create_data_client_for_test();
2296 let instrument_id = cache_test_instrument(&client, 2048, "ETH", LighterProductType::Spot);
2297
2298 let channel = client.index_market_stats_channel(instrument_id).unwrap();
2299
2300 assert!(matches!(
2301 channel,
2302 LighterWsChannel::SpotMarketStats(LighterMarketSelection::Market(2048)),
2303 ));
2304 }
2305
2306 #[rstest]
2307 fn test_index_market_stats_channel_routes_widened_ids_by_instrument_type() {
2308 let client = create_data_client_for_test();
2309 let perp_id = cache_test_instrument(&client, 40_000, "ETH", LighterProductType::Perp);
2310 let spot_id = cache_test_instrument(&client, 50_000, "ETH", LighterProductType::Spot);
2311
2312 let perp_channel = client.index_market_stats_channel(perp_id).unwrap();
2313 let spot_channel = client.index_market_stats_channel(spot_id).unwrap();
2314
2315 assert!(matches!(
2316 perp_channel,
2317 LighterWsChannel::MarketStats(LighterMarketSelection::Market(40_000)),
2318 ));
2319 assert!(matches!(
2320 spot_channel,
2321 LighterWsChannel::SpotMarketStats(LighterMarketSelection::Market(50_000)),
2322 ));
2323 }
2324
2325 #[rstest]
2326 fn test_mark_price_channel_rejects_spot_instrument() {
2327 let client = create_data_client_for_test();
2328 let instrument_id = cache_test_instrument(&client, 2048, "ETH", LighterProductType::Spot);
2329
2330 let err = client
2331 .perp_market_stats_channel(instrument_id, "mark price")
2332 .unwrap_err();
2333
2334 assert!(
2335 err.to_string()
2336 .contains("mark price subscriptions require a perpetual instrument"),
2337 );
2338 }
2339
2340 #[rstest]
2341 fn test_request_bars_rejects_unsupported_bar_type() {
2342 let client = create_data_client_for_test();
2343 let request = RequestBars::new(
2344 unsupported_three_minute_bar_type(),
2345 None,
2346 None,
2347 None,
2348 Some(ClientId::new("LIGHTER")),
2349 UUID4::new(),
2350 UnixNanos::default(),
2351 None,
2352 );
2353
2354 let err = DataClient::request_bars(&client, request).unwrap_err();
2355
2356 assert_eq!(err.to_string(), "unsupported Lighter candle minute step: 3");
2357 }
2358
2359 #[rstest]
2360 fn test_subscribe_bars_rejects_unsupported_bar_type() {
2361 let mut client = create_data_client_for_test();
2362 let subscription = SubscribeBars::new(
2363 unsupported_three_minute_bar_type(),
2364 Some(ClientId::new("LIGHTER")),
2365 None,
2366 UUID4::new(),
2367 UnixNanos::default(),
2368 None,
2369 None,
2370 );
2371
2372 let err = DataClient::subscribe_bars(&mut client, subscription).unwrap_err();
2373
2374 assert_eq!(err.to_string(), "unsupported Lighter candle minute step: 3");
2375 }
2376
2377 #[rstest]
2378 fn test_subscribe_bars_accepts_ws_streamable_resolution() {
2379 let mut client = create_data_client_for_test();
2380 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2381 let bar_type = BarType::new(
2382 instrument_id,
2383 BarSpecification::new(1, BarAggregation::Minute, PriceType::Last),
2384 AggregationSource::External,
2385 );
2386 let subscription = SubscribeBars::new(
2387 bar_type,
2388 Some(ClientId::new("LIGHTER")),
2389 None,
2390 UUID4::new(),
2391 UnixNanos::default(),
2392 None,
2393 None,
2394 );
2395
2396 DataClient::subscribe_bars(&mut client, subscription).unwrap();
2397 }
2398
2399 #[rstest]
2400 fn test_subscribe_bars_missing_cached_instrument_returns_lookup_error() {
2401 let mut client = create_data_client_for_test();
2402 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2403 let bar_type = BarType::new(
2404 instrument_id,
2405 BarSpecification::new(1, BarAggregation::Minute, PriceType::Last),
2406 AggregationSource::External,
2407 );
2408 let subscription = SubscribeBars::new(
2409 bar_type,
2410 Some(ClientId::new("LIGHTER")),
2411 None,
2412 UUID4::new(),
2413 UnixNanos::default(),
2414 None,
2415 None,
2416 );
2417
2418 let err = DataClient::subscribe_bars(&mut client, subscription).unwrap_err();
2419
2420 assert_eq!(
2421 err.to_string(),
2422 InstrumentLookupError::not_found(instrument_id).to_string()
2423 );
2424 }
2425
2426 #[rstest]
2427 fn test_subscribe_bars_rejects_one_week_with_ws_message() {
2428 let mut client = create_data_client_for_test();
2429 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2430 let bar_type = BarType::new(
2431 instrument_id,
2432 BarSpecification::new(1, BarAggregation::Week, PriceType::Last),
2433 AggregationSource::External,
2434 );
2435 let subscription = SubscribeBars::new(
2436 bar_type,
2437 Some(ClientId::new("LIGHTER")),
2438 None,
2439 UUID4::new(),
2440 UnixNanos::default(),
2441 None,
2442 None,
2443 );
2444
2445 let err = DataClient::subscribe_bars(&mut client, subscription).unwrap_err();
2446
2447 assert!(
2448 err.to_string().contains("does not offer")
2449 && err.to_string().contains("candle WebSocket stream"),
2450 "expected WS-streamable rejection, was: {err}",
2451 );
2452 }
2453
2454 #[rstest]
2455 fn test_unsubscribe_bars_returns_ok_for_unsupported_bar_type() {
2456 let mut client = create_data_client_for_test();
2457 let unsubscription = UnsubscribeBars::new(
2458 unsupported_three_minute_bar_type(),
2459 Some(ClientId::new("LIGHTER")),
2460 None,
2461 UUID4::new(),
2462 UnixNanos::default(),
2463 None,
2464 None,
2465 );
2466
2467 DataClient::unsubscribe_bars(&mut client, &unsubscription).unwrap();
2468 }
2469
2470 #[rstest]
2471 fn test_subscribe_book_depth_rejects_unsupported_book_type() {
2472 let mut client = create_data_client_for_test();
2473 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2474 let subscription = SubscribeBookDepth::new(
2475 instrument_id,
2476 BookType::L1_MBP,
2477 Some(ClientId::new("LIGHTER")),
2478 None,
2479 UUID4::new(),
2480 UnixNanos::default(),
2481 None,
2482 false,
2483 None,
2484 None,
2485 );
2486
2487 let err = DataClient::subscribe_book_depth(&mut client, subscription).unwrap_err();
2488
2489 assert!(err.to_string().contains("L2_MBP"));
2490 }
2491
2492 #[rstest]
2493 fn test_request_quotes_rejects_unsupported_rest_quotes() {
2494 let client = create_data_client_for_test();
2495 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2496 let request = RequestQuotes::new(
2497 instrument_id,
2498 None,
2499 None,
2500 None,
2501 Some(ClientId::new("LIGHTER")),
2502 UUID4::new(),
2503 UnixNanos::default(),
2504 None,
2505 );
2506
2507 let err = DataClient::request_quotes(&client, request).unwrap_err();
2508
2509 assert!(
2510 err.to_string()
2511 .contains("does not support historical quote requests"),
2512 );
2513 }
2514
2515 #[rstest]
2516 fn test_request_book_depth_rejects_unsupported_rest_depth() {
2517 let client = create_data_client_for_test();
2518 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2519 let request = RequestBookDepth::new(
2520 instrument_id,
2521 None,
2522 None,
2523 None,
2524 NonZeroUsize::new(10),
2525 Some(ClientId::new("LIGHTER")),
2526 UUID4::new(),
2527 UnixNanos::default(),
2528 None,
2529 );
2530
2531 let err = DataClient::request_book_depth(&client, request).unwrap_err();
2532
2533 assert!(
2534 err.to_string()
2535 .contains("does not support historical order book depth requests"),
2536 );
2537 }
2538
2539 #[rstest]
2540 fn test_request_funding_rates_rejects_spot_instrument() {
2541 let client = create_data_client_for_test();
2542 let instrument_id = cache_test_instrument(&client, 2048, "ETH", LighterProductType::Spot);
2543 let request = RequestFundingRates::new(
2544 instrument_id,
2545 None,
2546 None,
2547 None,
2548 Some(ClientId::new("LIGHTER")),
2549 UUID4::new(),
2550 UnixNanos::default(),
2551 None,
2552 );
2553
2554 let err = DataClient::request_funding_rates(&client, request).unwrap_err();
2555
2556 assert!(
2557 err.to_string()
2558 .contains("funding-rate requests require a perpetual instrument"),
2559 );
2560 }
2561
2562 #[tokio::test]
2563 async fn test_request_funding_rates_emits_response() {
2564 let base_url = spawn_fundings_server().await;
2565 let config = LighterDataClientConfig {
2566 base_url_http: Some(base_url),
2567 ..Default::default()
2568 };
2569 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2570 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2571 let start = Timestamp::from_second(1_778_702_400).unwrap();
2572 let end = Timestamp::from_second(1_778_706_000).unwrap();
2573 let request = RequestFundingRates::new(
2574 instrument_id,
2575 Some(start),
2576 Some(end),
2577 NonZeroUsize::new(2),
2578 Some(ClientId::new("LIGHTER")),
2579 UUID4::new(),
2580 UnixNanos::default(),
2581 None,
2582 );
2583
2584 DataClient::request_funding_rates(&client, request).unwrap();
2585
2586 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2587 .await
2588 .expect("funding rates response")
2589 .expect("funding rates event");
2590
2591 match event {
2592 DataEvent::Response(DataResponse::FundingRates(response)) => {
2593 assert_eq!(response.instrument_id, instrument_id);
2594 assert_eq!(response.data.len(), 2);
2595 assert_eq!(response.data[0].rate, Decimal::new(12, 6));
2596 assert_eq!(response.data[0].interval, Some(60));
2597 assert_eq!(
2598 response.data[0].ts_event,
2599 UnixNanos::from(1_778_702_400_000_000_000)
2600 );
2601 assert_eq!(response.data[1].rate, Decimal::new(-2, 6));
2602 assert_eq!(response.data[1].interval, Some(60));
2603 }
2604 event => panic!("expected funding rates response, was {event:?}"),
2605 }
2606 }
2607
2608 #[tokio::test]
2609 async fn test_request_trades_uses_recent_trades_endpoint() {
2610 let base_url = spawn_trades_server().await;
2611 let config = LighterDataClientConfig {
2612 base_url_http: Some(base_url),
2613 ..Default::default()
2614 };
2615 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2616 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2617 let start = Timestamp::from_second(1_700_000_000).unwrap();
2618 let request = RequestTrades::new(
2619 instrument_id,
2620 Some(start),
2621 None,
2622 NonZeroUsize::new(50),
2623 Some(ClientId::new("LIGHTER")),
2624 UUID4::new(),
2625 UnixNanos::default(),
2626 None,
2627 );
2628
2629 DataClient::request_trades(&client, request).unwrap();
2630
2631 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2632 .await
2633 .expect("trades response")
2634 .expect("trades event");
2635
2636 match event {
2637 DataEvent::Response(DataResponse::Trades(response)) => {
2638 assert_eq!(response.instrument_ids, vec![instrument_id]);
2639 assert_eq!(response.data.len(), 1);
2640 let tick = &response.data[0];
2641 assert_eq!(tick.instrument_id, instrument_id);
2642 assert_eq!(tick.price, Price::from("2361.31"));
2643 assert_eq!(tick.size, Quantity::from("0.0005"));
2644 assert_eq!(tick.aggressor_side, AggressorSide::Sell);
2645 assert_eq!(tick.trade_id.to_string(), "19211490282");
2646 }
2647 event => panic!("expected trades response, was {event:?}"),
2648 }
2649 }
2650
2651 #[tokio::test]
2652 async fn test_request_trades_clamps_limit_to_venue_cap() {
2653 let base_url = spawn_trades_server_with_response_and_limit(
2654 HTTP_RECENT_TRADES,
2655 LIGHTER_RECENT_TRADES_MAX_LIMIT,
2656 )
2657 .await;
2658 let config = LighterDataClientConfig {
2659 base_url_http: Some(base_url),
2660 ..Default::default()
2661 };
2662 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2663 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2664 let request = RequestTrades::new(
2665 instrument_id,
2666 None,
2667 None,
2668 NonZeroUsize::new(usize::from(LIGHTER_RECENT_TRADES_MAX_LIMIT) + 1),
2669 Some(ClientId::new("LIGHTER")),
2670 UUID4::new(),
2671 UnixNanos::default(),
2672 None,
2673 );
2674
2675 DataClient::request_trades(&client, request).unwrap();
2676
2677 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2678 .await
2679 .expect("trades response")
2680 .expect("trades event");
2681
2682 assert!(
2683 matches!(event, DataEvent::Response(DataResponse::Trades(_))),
2684 "expected trades response, was {event:?}",
2685 );
2686 }
2687
2688 #[tokio::test]
2689 async fn test_request_trades_emits_empty_response_for_null_recent_trades() {
2690 let base_url = spawn_trades_server_with_response(HTTP_RECENT_TRADES_NULL).await;
2691 let config = LighterDataClientConfig {
2692 base_url_http: Some(base_url),
2693 ..Default::default()
2694 };
2695 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2696 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2697 let start = Timestamp::from_second(1_700_000_000).unwrap();
2698 let request = RequestTrades::new(
2699 instrument_id,
2700 Some(start),
2701 None,
2702 NonZeroUsize::new(50),
2703 Some(ClientId::new("LIGHTER")),
2704 UUID4::new(),
2705 UnixNanos::default(),
2706 None,
2707 );
2708
2709 DataClient::request_trades(&client, request).unwrap();
2710
2711 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2712 .await
2713 .expect("trades response")
2714 .expect("trades event");
2715
2716 match event {
2717 DataEvent::Response(DataResponse::Trades(response)) => {
2718 assert_eq!(response.instrument_ids, vec![instrument_id]);
2719 assert!(response.data.is_empty());
2720 }
2721 event => panic!("expected trades response, was {event:?}"),
2722 }
2723 }
2724
2725 #[tokio::test]
2726 async fn test_request_trades_filters_recent_trades_to_requested_range() {
2727 let base_url = spawn_trades_server().await;
2728 let config = LighterDataClientConfig {
2729 base_url_http: Some(base_url),
2730 ..Default::default()
2731 };
2732 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2733 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2734 let end = Timestamp::from_second(1_700_000_000).unwrap();
2735 let request = RequestTrades::new(
2736 instrument_id,
2737 None,
2738 Some(end),
2739 NonZeroUsize::new(50),
2740 Some(ClientId::new("LIGHTER")),
2741 UUID4::new(),
2742 UnixNanos::default(),
2743 None,
2744 );
2745
2746 DataClient::request_trades(&client, request).unwrap();
2747
2748 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2749 .await
2750 .expect("trades response")
2751 .expect("trades event");
2752
2753 match event {
2754 DataEvent::Response(DataResponse::Trades(response)) => {
2755 assert_eq!(response.instrument_ids, vec![instrument_id]);
2756 assert!(response.data.is_empty());
2757 }
2758 event => panic!("expected trades response, was {event:?}"),
2759 }
2760 }
2761
2762 #[tokio::test]
2763 async fn test_request_trades_returns_recent_trades_in_timestamp_order() {
2764 let base_url = spawn_trades_server_with_response(HTTP_RECENT_TRADES_UNORDERED).await;
2765 let config = LighterDataClientConfig {
2766 base_url_http: Some(base_url),
2767 ..Default::default()
2768 };
2769 let (client, mut receiver) = create_data_client_with_receiver_and_config_for_test(config);
2770 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2771 let start = Timestamp::from_millisecond(1_777_945_103_092).unwrap();
2772 let end = Timestamp::from_millisecond(1_777_945_103_094).unwrap();
2773 let request = RequestTrades::new(
2774 instrument_id,
2775 Some(start),
2776 Some(end),
2777 NonZeroUsize::new(50),
2778 Some(ClientId::new("LIGHTER")),
2779 UUID4::new(),
2780 UnixNanos::default(),
2781 None,
2782 );
2783
2784 DataClient::request_trades(&client, request).unwrap();
2785
2786 let event = tokio::time::timeout(Duration::from_secs(2), receiver.recv())
2787 .await
2788 .expect("trades response")
2789 .expect("trades event");
2790
2791 match event {
2792 DataEvent::Response(DataResponse::Trades(response)) => {
2793 assert_eq!(response.instrument_ids, vec![instrument_id]);
2794 assert_eq!(
2795 response
2796 .data
2797 .iter()
2798 .map(|trade| trade.trade_id.to_string())
2799 .collect::<Vec<_>>(),
2800 vec!["19211490282", "19211490283", "19211490284"],
2801 );
2802 assert_eq!(
2803 response
2804 .data
2805 .iter()
2806 .map(|trade| trade.ts_event.as_u64())
2807 .collect::<Vec<_>>(),
2808 vec![
2809 1_777_945_103_092_000_000,
2810 1_777_945_103_093_000_000,
2811 1_777_945_103_094_000_000,
2812 ],
2813 );
2814 }
2815 event => panic!("expected trades response, was {event:?}"),
2816 }
2817 }
2818
2819 #[rstest]
2820 fn test_retain_trade_ticks_in_range_sorts_ascending() {
2821 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
2822 let tick = |ts_event, trade_id| {
2823 TradeTick::new(
2824 instrument_id,
2825 Price::from("1.0"),
2826 Quantity::from("1.0"),
2827 AggressorSide::Buy,
2828 TradeId::new(trade_id),
2829 UnixNanos::from(ts_event),
2830 UnixNanos::from(ts_event + 1),
2831 )
2832 };
2833 let mut trades = vec![tick(4, "4"), tick(1, "1"), tick(3, "3"), tick(2, "2")];
2834
2835 retain_trade_ticks_in_range(
2836 &mut trades,
2837 Some(UnixNanos::from(2)),
2838 Some(UnixNanos::from(4)),
2839 );
2840
2841 assert_eq!(
2842 trades
2843 .iter()
2844 .map(|trade| trade.ts_event.as_u64())
2845 .collect::<Vec<_>>(),
2846 vec![2, 3, 4],
2847 );
2848 }
2849
2850 #[tokio::test]
2851 async fn test_spawn_instrument_refresh_skipped_when_interval_zero() {
2852 let config = LighterDataClientConfig {
2853 update_instruments_interval_mins: 0,
2854 ..Default::default()
2855 };
2856 let (client, _receiver) = create_data_client_with_receiver_and_config_for_test(config);
2857
2858 assert!(client.tasks.is_empty());
2859 client
2860 .spawn_instrument_refresh()
2861 .expect("instrument refresh remains disabled");
2862 assert!(client.tasks.is_empty());
2863 }
2864
2865 #[tokio::test]
2866 async fn test_spawn_instrument_refresh_registers_task() {
2867 let config = LighterDataClientConfig {
2868 update_instruments_interval_mins: 60,
2869 ..Default::default()
2870 };
2871 let (mut client, _receiver) = create_data_client_with_receiver_and_config_for_test(config);
2872
2873 assert!(client.tasks.is_empty());
2874 client
2875 .spawn_instrument_refresh()
2876 .expect("instrument refresh task registration");
2877 assert_eq!(client.tasks.len(), 1);
2878
2879 client.tasks.begin_shutdown();
2880 client.shutdown_tasks().await.expect("task shutdown");
2881 }
2882
2883 #[tokio::test]
2884 async fn test_await_instrument_refresh_drops_result_when_request_cancels() {
2885 let cancellation = CancellationToken::new();
2886 let request_cancellation = cancellation.clone();
2887
2888 let result = await_instrument_refresh(&cancellation, async move {
2889 request_cancellation.cancel();
2890 42
2891 })
2892 .await;
2893
2894 assert_eq!(result, None);
2895 }
2896
2897 #[tokio::test]
2898 async fn test_reset_closes_registered_task_generation_until_drain() {
2899 let (mut client, _receiver) = create_data_client_with_receiver_for_test();
2900 let old_token = client.cancellation_token.clone();
2901 let (started_tx, started_rx) = tokio::sync::oneshot::channel();
2902 let (dropped_tx, dropped_rx) = tokio::sync::oneshot::channel();
2903
2904 client
2905 .tasks
2906 .spawn(async move {
2907 let _drop_signal = DropSignal(Some(dropped_tx));
2908 let _ = started_tx.send(());
2909 std::future::pending::<()>().await;
2910 })
2911 .expect("registered task spawn");
2912 started_rx.await.expect("registered task started");
2913
2914 client.reset().expect("reset");
2915
2916 assert!(old_token.is_cancelled());
2917 assert_eq!(client.tasks.len(), 1);
2918 assert!(!client.tasks.is_open());
2919 assert!(client.cancellation_token.is_cancelled());
2920 client.shutdown_tasks().await.expect("reset task shutdown");
2921 assert!(client.tasks.is_empty());
2922 tokio::time::timeout(Duration::from_secs(2), dropped_rx)
2923 .await
2924 .expect("registered task was not aborted")
2925 .expect("drop signal sender dropped");
2926 }
2927
2928 #[tokio::test]
2929 async fn test_spawn_task_suppresses_output_after_cancellation() {
2930 let (client, mut receiver) = create_data_client_with_receiver_for_test();
2931 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2932 let instrument = client
2933 .instruments
2934 .get_cloned(&instrument_id)
2935 .expect("cached instrument");
2936
2937 client.cancellation_token.cancel();
2939
2940 let sender = client.data_sender.clone();
2941 client.spawn_task(async move {
2942 let _ = sender.send(DataEvent::Data(instrument.into()));
2943 });
2944
2945 let result = tokio::time::timeout(Duration::from_millis(200), receiver.recv()).await;
2946 assert!(
2947 result.is_err(),
2948 "expected no DataEvent after cancellation, was {result:?}",
2949 );
2950 }
2951
2952 #[tokio::test]
2953 async fn test_connect_is_idempotent_when_already_connected() {
2954 let (mut client, _receiver) = create_data_client_with_receiver_for_test();
2955 client.is_connected.store(true, Ordering::Release);
2956
2957 client
2958 .connect()
2959 .await
2960 .expect("connect returns Ok when already connected");
2961
2962 assert!(
2963 client.tasks.is_empty(),
2964 "an already-connected client must not spawn duplicate tasks",
2965 );
2966 assert!(client.is_connected());
2967 }
2968
2969 #[tokio::test]
2970 async fn test_disconnect_drains_in_flight_task_and_suppresses_late_event() {
2971 let (mut client, mut receiver) = create_data_client_with_receiver_for_test();
2972 let instrument_id = cache_test_instrument(&client, 0, "ETH", LighterProductType::Perp);
2973 let instrument = client
2974 .instruments
2975 .get_cloned(&instrument_id)
2976 .expect("cached instrument");
2977 client.is_connected.store(true, Ordering::Release);
2978
2979 let sender = client.data_sender.clone();
2981 let (hold_tx, hold_rx) = tokio::sync::oneshot::channel::<()>();
2982 client.spawn_task(async move {
2983 let _ = hold_rx.await;
2984 let _ = sender.send(DataEvent::Data(instrument.into()));
2985 });
2986 assert_eq!(client.tasks.len(), 1);
2987
2988 client.disconnect().await.expect("disconnect");
2989
2990 assert!(
2991 client.tasks.is_empty(),
2992 "disconnect must drain tracked tasks",
2993 );
2994 assert!(!client.is_connected());
2995 let result = tokio::time::timeout(Duration::from_millis(200), receiver.recv()).await;
2996 assert!(
2997 result.is_err(),
2998 "expected no DataEvent after disconnect, was {result:?}",
2999 );
3000
3001 drop(hold_tx);
3002 }
3003
3004 #[tokio::test]
3005 async fn test_disconnect_aborts_task_that_ignores_cancellation() {
3006 let (mut client, _receiver) = create_data_client_with_receiver_for_test();
3007 client.is_connected.store(true, Ordering::Release);
3008
3009 let (started_tx, started_rx) = tokio::sync::oneshot::channel();
3010 let (dropped_tx, dropped_rx) = tokio::sync::oneshot::channel();
3011
3012 client
3014 .tasks
3015 .spawn(async move {
3016 let _drop_signal = DropSignal(Some(dropped_tx));
3017 let _ = started_tx.send(());
3018 std::future::pending::<()>().await;
3019 })
3020 .expect("uncancellable task spawn");
3021 started_rx.await.expect("task started");
3022
3023 client.disconnect().await.expect("disconnect");
3024
3025 assert!(client.tasks.is_empty());
3026 assert!(!client.is_connected());
3027 tokio::time::timeout(Duration::from_secs(5), dropped_rx)
3028 .await
3029 .expect("task aborted after timeout")
3030 .expect("drop signal sender dropped");
3031 }
3032
3033 #[rstest]
3034 fn test_rollback_market_stats_subscription_clears_piggybacked_flags() {
3035 let subscriptions = DashMap::new();
3036 let generations = DashMap::new();
3037 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
3038 subscriptions.insert(
3039 instrument_id,
3040 MarketStatsSubscription {
3041 channel: LighterWsChannel::MarketStats(LighterMarketSelection::Market(0)),
3042 flags: MarketStatsFlags {
3043 mark_price: true,
3044 index_price: true,
3045 ..Default::default()
3046 },
3047 },
3048 );
3049 generations.insert(instrument_id, 7);
3050
3051 rollback_market_stats_subscription(&subscriptions, &generations, instrument_id, 7);
3052
3053 assert!(
3054 !subscriptions.contains_key(&instrument_id),
3055 "all flags share the failed underlying channel",
3056 );
3057 assert!(!generations.contains_key(&instrument_id));
3058 }
3059
3060 #[rstest]
3061 fn test_rollback_market_stats_subscription_keeps_replacement_generation() {
3062 let subscriptions = DashMap::new();
3063 let generations = DashMap::new();
3064 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
3065 let replacement = MarketStatsSubscription {
3066 channel: LighterWsChannel::MarketStats(LighterMarketSelection::Market(0)),
3067 flags: MarketStatsFlags {
3068 funding_rate: true,
3069 ..Default::default()
3070 },
3071 };
3072 subscriptions.insert(instrument_id, replacement.clone());
3073 generations.insert(instrument_id, 8);
3074
3075 rollback_market_stats_subscription(&subscriptions, &generations, instrument_id, 7);
3076
3077 assert_eq!(
3078 subscriptions
3079 .get(&instrument_id)
3080 .expect("replacement retained")
3081 .flags,
3082 replacement.flags,
3083 );
3084 assert_eq!(generations.get(&instrument_id).map(|value| *value), Some(8));
3085 }
3086
3087 #[rstest]
3088 fn data_constructor_rejects_proxy_without_scheme() {
3089 let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel();
3090 replace_data_event_sender(sender);
3091
3092 let config = LighterDataClientConfig {
3093 proxy_url: Some("127.0.0.1:8080".into()),
3094 ..Default::default()
3095 };
3096
3097 assert_eq!(
3098 LighterDataClient::new(ClientId::from("LIGHTER"), config)
3099 .unwrap_err()
3100 .to_string(),
3101 "Lighter proxy_url must be an absolute http:// or https:// URL with a host"
3102 );
3103 }
3104
3105 fn create_data_client_for_test() -> LighterDataClient {
3106 create_data_client_with_receiver_for_test().0
3107 }
3108
3109 fn create_data_client_with_receiver_for_test() -> (
3110 LighterDataClient,
3111 tokio::sync::mpsc::UnboundedReceiver<DataEvent>,
3112 ) {
3113 create_data_client_with_receiver_and_config_for_test(LighterDataClientConfig::default())
3114 }
3115
3116 fn create_data_client_with_receiver_and_config_for_test(
3117 mut config: LighterDataClientConfig,
3118 ) -> (
3119 LighterDataClient,
3120 tokio::sync::mpsc::UnboundedReceiver<DataEvent>,
3121 ) {
3122 config.api_key_index = Some(5);
3123 config.account_index = Some(12_345);
3124 config.private_key = Some(PRIVATE_KEY_HEX.into());
3125 let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
3126 replace_data_event_sender(sender);
3127 let client = LighterDataClient::new(ClientId::new("LIGHTER"), config).unwrap();
3128 (client, receiver)
3129 }
3130
3131 async fn spawn_order_book_details_server() -> String {
3132 let app = Router::new().route("/api/v1/orderBookDetails", get(order_book_details));
3133 let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
3134 let addr = listener.local_addr().unwrap();
3135 tokio::spawn(async move {
3136 axum::serve(listener, app).await.unwrap();
3137 });
3138
3139 format!("http://{addr}")
3140 }
3141
3142 async fn spawn_fundings_server() -> String {
3143 let app = Router::new().route("/api/v1/fundings", get(fundings));
3144 let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
3145 let addr = listener.local_addr().unwrap();
3146 tokio::spawn(async move {
3147 axum::serve(listener, app).await.unwrap();
3148 });
3149
3150 format!("http://{addr}")
3151 }
3152
3153 async fn spawn_trades_server() -> String {
3154 spawn_trades_server_with_response(HTTP_RECENT_TRADES).await
3155 }
3156
3157 async fn spawn_trades_server_with_response(response_body: &'static str) -> String {
3158 spawn_trades_server_with_response_and_limit(response_body, 50).await
3159 }
3160
3161 async fn spawn_trades_server_with_response_and_limit(
3162 response_body: &'static str,
3163 expected_limit: u16,
3164 ) -> String {
3165 let app = Router::new().route(
3166 "/api/v1/recentTrades",
3167 get(
3168 move |Query(query): Query<LighterRecentTradesQuery>| async move {
3169 recent_trades_response(&query, response_body, expected_limit)
3170 },
3171 ),
3172 );
3173 let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
3174 let addr = listener.local_addr().unwrap();
3175 tokio::spawn(async move {
3176 axum::serve(listener, app).await.unwrap();
3177 });
3178
3179 format!("http://{addr}")
3180 }
3181
3182 async fn order_book_details() -> Response {
3183 (StatusCode::OK, HTTP_ORDER_BOOK_DETAILS).into_response()
3184 }
3185
3186 async fn fundings(Query(query): Query<LighterFundingsQuery>) -> Response {
3187 assert_eq!(query.market_id, 0);
3188 assert_eq!(query.resolution, LighterFundingResolution::OneHour);
3189 assert_eq!(query.start_timestamp, 1_778_702_400_000);
3190 assert_eq!(query.end_timestamp, 1_778_706_000_000);
3191 assert_eq!(
3192 query.count_back,
3193 i64::from(crate::http::client::LIGHTER_FUNDINGS_MAX_LIMIT)
3194 );
3195 (StatusCode::OK, HTTP_FUNDINGS).into_response()
3196 }
3197
3198 fn recent_trades_response(
3199 query: &LighterRecentTradesQuery,
3200 response_body: &'static str,
3201 expected_limit: u16,
3202 ) -> Response {
3203 assert_eq!(query.market_id, 0);
3204 assert_eq!(query.limit, expected_limit);
3205 (StatusCode::OK, response_body).into_response()
3206 }
3207
3208 fn cache_test_instrument(
3209 client: &LighterDataClient,
3210 market_index: i64,
3211 venue_symbol: &str,
3212 product_type: LighterProductType,
3213 ) -> InstrumentId {
3214 let instrument_id = client
3215 .registry
3216 .insert(market_index, venue_symbol, product_type);
3217 let instrument = match product_type {
3218 LighterProductType::Perp => test_perp_instrument(instrument_id, venue_symbol),
3219 LighterProductType::Spot => test_spot_instrument(instrument_id, venue_symbol),
3220 };
3221
3222 client.instruments.rcu(|m| {
3223 m.insert(instrument_id, instrument.clone());
3224 });
3225
3226 instrument_id
3227 }
3228
3229 fn test_perp_instrument(instrument_id: InstrumentId, venue_symbol: &str) -> InstrumentAny {
3230 InstrumentAny::CryptoPerpetual(
3231 CryptoPerpetual::builder()
3232 .instrument_id(instrument_id)
3233 .raw_symbol(Symbol::new(format!("{venue_symbol}-PERP")))
3234 .base_currency(Currency::from(venue_symbol))
3235 .quote_currency(Currency::from("USDC"))
3236 .settlement_currency(Currency::from("USDC"))
3237 .is_inverse(false)
3238 .price_precision(2)
3239 .size_precision(4)
3240 .price_increment(Price::from("0.01"))
3241 .size_increment(Quantity::from("0.0001"))
3242 .ts_event(UnixNanos::default())
3243 .ts_init(UnixNanos::default())
3244 .build()
3245 .unwrap(),
3246 )
3247 }
3248
3249 fn test_spot_instrument(instrument_id: InstrumentId, venue_symbol: &str) -> InstrumentAny {
3250 InstrumentAny::CurrencyPair(
3251 CurrencyPair::builder()
3252 .instrument_id(instrument_id)
3253 .raw_symbol(Symbol::new(format!("{venue_symbol}-SPOT")))
3254 .base_currency(Currency::from(venue_symbol))
3255 .quote_currency(Currency::from("USDC"))
3256 .price_precision(2)
3257 .size_precision(4)
3258 .price_increment(Price::from("0.01"))
3259 .size_increment(Quantity::from("0.0001"))
3260 .ts_event(UnixNanos::default())
3261 .ts_init(UnixNanos::default())
3262 .build()
3263 .unwrap(),
3264 )
3265 }
3266
3267 fn unsupported_three_minute_bar_type() -> BarType {
3268 let instrument_id = InstrumentId::new(Symbol::new("ETH-PERP"), *LIGHTER_VENUE);
3269 BarType::new(
3270 instrument_id,
3271 BarSpecification::new(3, BarAggregation::Minute, PriceType::Last),
3272 AggregationSource::External,
3273 )
3274 }
3275}