Skip to main content

nautilus_coinbase/data/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Coinbase Advanced Trade data client for NautilusTrader.
17//!
18//! Implements the [`DataClient`] trait, providing market data subscriptions and
19//! historical data requests through the Coinbase Advanced Trade API.
20
21use std::{
22    sync::{
23        Arc,
24        atomic::{AtomicBool, Ordering},
25    },
26    time::Duration,
27};
28
29use ahash::AHashSet;
30use anyhow::Context;
31use nautilus_common::{
32    cache::InstrumentLookupError,
33    clients::{DataClient, execute_bar_subscriptions, execute_instrument_subscriptions},
34    live::{dst::time::Instant, runner::get_data_event_sender, sender::EventSender},
35    messages::{
36        DataEvent,
37        data::{
38            BarsResponse, BookResponse, DataResponse, InstrumentResponse, InstrumentsResponse,
39            RequestBars, RequestBookSnapshot, RequestInstrument, RequestInstruments, RequestTrades,
40            SubscribeBars, SubscribeBookDeltas, SubscribeFundingRates, SubscribeIndexPrices,
41            SubscribeInstrument, SubscribeInstrumentStatus, SubscribeMarkPrices, SubscribeQuotes,
42            SubscribeTrades, TradesResponse, UnsubscribeBars, UnsubscribeBookDeltas,
43            UnsubscribeFundingRates, UnsubscribeIndexPrices, UnsubscribeInstrument,
44            UnsubscribeInstrumentStatus, UnsubscribeMarkPrices, UnsubscribeQuotes,
45            UnsubscribeTrades,
46        },
47    },
48};
49use nautilus_core::{
50    AtomicMap,
51    datetime::datetime_to_unix_nanos,
52    time::{AtomicTime, get_atomic_clock_realtime},
53};
54use nautilus_live::{
55    SocketControl,
56    book::snapshot::{SnapshotGate, snapshot_expired},
57    task::{TaskGroup, TaskGroupGuard, TaskSpawner},
58};
59use nautilus_model::{
60    data::{Data, OrderBookDeltas},
61    enums::{BarAggregation, BookAction, BookType},
62    identifiers::{ClientId, InstrumentId, Venue},
63    instruments::{Instrument, InstrumentAny},
64    orderbook::OrderBook,
65};
66use parking_lot::Mutex;
67use tokio_util::sync::CancellationToken;
68use ustr::Ustr;
69
70pub(crate) mod poll;
71
72use crate::{
73    book::{
74        BookSequenceOutcome,
75        recovery::{spawn_recovery_monitor, spawn_recovery_task, spawn_task, start_recovery},
76        sync::BookSyncTracker,
77    },
78    common::{
79        consts::COINBASE_VENUE, credential::CoinbaseCredential, enums::CoinbaseWsChannel,
80        parse::bar_type_to_granularity,
81    },
82    config::CoinbaseDataClientConfig,
83    data::poll::DerivPollManager,
84    http::{
85        client::{CoinbaseHttpClient, data_client_retry_config},
86        models::{CandlesResponse, PriceBook, TickerResponse},
87        parse::{parse_bar, parse_product_book_snapshot, parse_trade_tick},
88    },
89    provider::CoinbaseInstrumentProvider,
90    websocket::{client::CoinbaseWebSocketClient, handler::NautilusWsMessage},
91};
92
93/// Data client for Coinbase Advanced Trade.
94///
95/// Owns an HTTP client, WebSocket client, and instrument provider. Bootstraps
96/// instruments on connect, subscribes to WS channels for live data, and handles
97/// historical data requests through the REST API.
98#[derive(Debug)]
99pub struct CoinbaseDataClient {
100    client_id: ClientId,
101    config: CoinbaseDataClientConfig,
102    http_client: CoinbaseHttpClient,
103    ws_client: CoinbaseWebSocketClient,
104    provider: CoinbaseInstrumentProvider,
105    is_connected: AtomicBool,
106    cancellation_token: CancellationToken,
107    session_tasks: TaskGroup,
108    command_tasks: TaskGroup,
109    shutdown_errors: Vec<String>,
110    data_sender: EventSender<DataEvent>,
111    instruments: Arc<AtomicMap<InstrumentId, InstrumentAny>>,
112    deriv_polls: DerivPollManager,
113    clock: &'static AtomicTime,
114    instrument_status_subs: Arc<Mutex<AHashSet<InstrumentId>>>,
115    book_sync: BookSyncTracker,
116    /// Maps each subscribed book to the wire `product_id` its `level2` subscription uses.
117    book_products: Arc<AtomicMap<InstrumentId, Ustr>>,
118}
119
120impl CoinbaseDataClient {
121    /// Creates a new [`CoinbaseDataClient`] instance.
122    ///
123    /// # Errors
124    ///
125    /// Returns an error if the HTTP client fails to initialize.
126    pub fn new(client_id: ClientId, config: CoinbaseDataClientConfig) -> anyhow::Result<Self> {
127        let clock = get_atomic_clock_realtime();
128        let data_sender = get_data_event_sender();
129
130        let retry_config = data_client_retry_config();
131        let proxy_url = config
132            .proxy_url
133            .as_ref()
134            .map(|value| value.expose_secret().to_owned());
135
136        let http_client = match CoinbaseCredential::resolve(
137            config.api_key.as_ref().map(|value| value.expose_secret()),
138            config
139                .api_secret
140                .as_ref()
141                .map(|value| value.expose_secret()),
142        ) {
143            Some(credential) => CoinbaseHttpClient::with_credentials(
144                credential,
145                config.environment,
146                config.http_timeout_secs,
147                proxy_url.clone(),
148                Some(retry_config),
149            )
150            .map_err(|e| anyhow::anyhow!("Failed to create HTTP client: {e}"))?,
151            None => CoinbaseHttpClient::new(
152                config.environment,
153                config.http_timeout_secs,
154                proxy_url.clone(),
155                Some(retry_config),
156            )
157            .map_err(|e| anyhow::anyhow!("Failed to create HTTP client: {e}"))?,
158        };
159
160        if let Some(url) = &config.base_url_rest {
161            http_client.set_base_url(url.clone());
162        }
163
164        let ws_url = config.ws_url();
165        let ws_client = CoinbaseWebSocketClient::new(&ws_url, config.transport_backend, proxy_url)
166            .with_socket_control(SocketControl::new(
167                client_id,
168                Some(*COINBASE_VENUE),
169                "coinbase-data-streams",
170            ));
171        let provider = CoinbaseInstrumentProvider::new(http_client.clone());
172
173        let deriv_polls = DerivPollManager::new(
174            http_client.clone(),
175            data_sender.clone(),
176            clock,
177            config.derivatives_poll_interval_secs,
178        );
179
180        let session_tasks = TaskGroup::new();
181        let command_tasks = TaskGroup::new();
182
183        Ok(Self {
184            client_id,
185            config,
186            http_client,
187            ws_client,
188            provider,
189            is_connected: AtomicBool::new(false),
190            cancellation_token: session_tasks.cancellation_token(),
191            session_tasks,
192            command_tasks,
193            shutdown_errors: Vec::new(),
194            data_sender,
195            instruments: Arc::new(AtomicMap::new()),
196            deriv_polls,
197            clock,
198            instrument_status_subs: Arc::new(Mutex::new(AHashSet::new())),
199            book_sync: BookSyncTracker::default(),
200            book_products: Arc::new(AtomicMap::new()),
201        })
202    }
203
204    fn venue(&self) -> Venue {
205        *COINBASE_VENUE
206    }
207
208    fn spawn_command<F>(&self, future: F)
209    where
210        F: std::future::Future<Output = ()> + Send + 'static,
211    {
212        if let Err(e) = self.command_tasks.spawn(future) {
213            log::warn!("Skipping Coinbase data command after shutdown began: {e}");
214        }
215    }
216
217    async fn finish_tasks(&self) -> anyhow::Result<()> {
218        let (session_result, command_result) = tokio::join!(
219            self.session_tasks
220                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
221            self.command_tasks
222                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
223        );
224        session_result.context("failed to finish Coinbase data session tasks")?;
225        command_result.context("failed to finish Coinbase data command tasks")?;
226        Ok(())
227    }
228
229    async fn prepare_task_groups(&mut self) -> anyhow::Result<()> {
230        if !self.session_tasks.is_open() || !self.command_tasks.is_open() {
231            self.teardown_partial_connect().await?;
232            self.session_tasks
233                .start_generation()
234                .context("failed to start Coinbase data session task generation")?;
235            self.command_tasks
236                .start_generation()
237                .context("failed to start Coinbase data command task generation")?;
238            self.cancellation_token = self.session_tasks.cancellation_token();
239        }
240        self.deriv_polls.prepare().await
241    }
242
243    async fn bootstrap_instruments(&self) -> anyhow::Result<Vec<InstrumentAny>> {
244        let instruments = self
245            .provider
246            .load_all()
247            .await
248            .context("failed to fetch instruments during bootstrap")?;
249
250        self.instruments.rcu(|m| {
251            for instrument in &instruments {
252                m.insert(instrument.id(), instrument.clone());
253            }
254        });
255
256        self.ws_client.update_instruments(&instruments).await;
257
258        log::debug!("Bootstrapped {} instruments", instruments.len());
259        Ok(instruments)
260    }
261
262    async fn spawn_ws(&mut self) -> anyhow::Result<()> {
263        self.ws_client
264            .connect()
265            .await
266            .context("failed to connect to Coinbase WebSocket")?;
267
268        let Some(mut out_rx) = self.ws_client.take_out_rx() else {
269            self.ws_client
270                .disconnect()
271                .await
272                .context("failed to roll back Coinbase WebSocket without output receiver")?;
273            anyhow::bail!("WebSocket output receiver not available");
274        };
275
276        let data_sender = self.data_sender.clone();
277        let cancellation_token = self.cancellation_token.clone();
278        let status_subs = Arc::clone(&self.instrument_status_subs);
279        let books = match self.book_router() {
280            Ok(books) => books,
281            Err(e) => {
282                self.ws_client
283                    .disconnect()
284                    .await
285                    .context("failed to roll back Coinbase WebSocket after shutdown began")?;
286                return Err(e);
287            }
288        };
289
290        // Books retained from a previous session resync from the replayed subscriptions
291        books.resync();
292
293        let future = async move {
294            log::debug!("Coinbase WebSocket consumption loop started");
295
296            loop {
297                tokio::select! {
298                    () = cancellation_token.cancelled() => {
299                        log::debug!("WebSocket consumption loop cancelled");
300                        break;
301                    }
302                    msg_opt = out_rx.recv() => {
303                        match msg_opt {
304                            Some(msg) => dispatch_ws_message(msg, &data_sender, &status_subs, &books),
305                            None => {
306                                log::debug!("WebSocket output channel closed");
307                                break;
308                            }
309                        }
310                    }
311                }
312            }
313
314            log::debug!("Coinbase WebSocket consumption loop finished");
315        };
316
317        if let Err(e) = self.session_tasks.spawn(future) {
318            if let Err(shutdown_error) = self.ws_client.disconnect().await {
319                return Err(anyhow::Error::new(e).context(format!(
320                    "failed to register Coinbase WebSocket consumption task; startup rollback \
321                     failed: {shutdown_error}"
322                )));
323            }
324            return Err(anyhow::Error::new(e)
325                .context("failed to register Coinbase WebSocket consumption task"));
326        }
327        log::debug!("WebSocket consumption task registered");
328        Ok(())
329    }
330
331    fn book_router(&self) -> anyhow::Result<BookRouter> {
332        let tasks = self
333            .session_tasks
334            .spawner()
335            .context("Coinbase data session task admission is closed")?;
336
337        Ok(BookRouter {
338            sync: self.book_sync.clone(),
339            products: Arc::clone(&self.book_products),
340            ws: self.ws_client.clone(),
341            snapshot_timeout: Duration::from_secs(self.config.book_snapshot_timeout_secs),
342            tasks,
343        })
344    }
345
346    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {
347        self.book_sync.clear();
348        self.cancellation_token.cancel();
349        self.session_tasks.begin_shutdown();
350        self.command_tasks.begin_shutdown();
351        self.deriv_polls.shutdown();
352        self.ws_client.begin_shutdown();
353
354        let (tasks_result, polls_result) =
355            tokio::join!(self.finish_tasks(), self.deriv_polls.finish_shutdown());
356
357        if let Err(e) = tasks_result {
358            self.shutdown_errors.push(e.to_string());
359        }
360
361        if let Err(e) = polls_result {
362            self.shutdown_errors.push(e.to_string());
363        }
364
365        if let Err(e) = self.ws_client.disconnect().await {
366            self.shutdown_errors.push(e.to_string());
367        }
368        self.is_connected.store(false, Ordering::Relaxed);
369
370        if !self.shutdown_errors.is_empty() {
371            anyhow::bail!(std::mem::take(&mut self.shutdown_errors).join("; "));
372        }
373        Ok(())
374    }
375
376    fn product_id(instrument_id: InstrumentId) -> Ustr {
377        instrument_id.symbol.inner()
378    }
379
380    // Resolves a caller-supplied product id to Coinbase's canonical alias (if
381    // any). Coinbase consolidates aliased pairs into a single book server-side
382    // and rewrites WS subscription confirmations and inbound messages to use
383    // the canonical id (e.g. BTC-USDC -> BTC-USD), so we must subscribe with
384    // the canonical id and remember the mapping so inbound messages can be
385    // re-keyed to what the strategy actually subscribed to.
386    fn resolve_wire_product_id(&self, subscribed: Ustr) -> Ustr {
387        self.http_client
388            .product_aliases()
389            .get_cloned(&subscribed)
390            .filter(|alias| !alias.is_empty())
391            .unwrap_or(subscribed)
392    }
393}
394
395/// Routes `level2` events through book synchronization and starts recovery.
396#[derive(Debug)]
397struct BookRouter {
398    sync: BookSyncTracker,
399    products: Arc<AtomicMap<InstrumentId, Ustr>>,
400    ws: CoinbaseWebSocketClient,
401    snapshot_timeout: Duration,
402    tasks: TaskSpawner,
403}
404
405impl BookRouter {
406    /// Returns whether the deltas may be emitted.
407    fn accept(&self, deltas: &OrderBookDeltas) -> bool {
408        let instrument_id = deltas.instrument_id;
409
410        // An unsubscribed book emits nothing, including frames still in flight
411        if !self.products.contains_key(&instrument_id) {
412            return false;
413        }
414
415        let is_snapshot = deltas
416            .deltas
417            .first()
418            .is_some_and(|delta| delta.action == BookAction::Clear);
419
420        if is_snapshot {
421            return self.sync.record_snapshot(instrument_id, Instant::now());
422        }
423
424        match self.sync.validate_update(instrument_id, Instant::now()) {
425            BookSequenceOutcome::Accept => true,
426            BookSequenceOutcome::Suppress => false,
427            BookSequenceOutcome::Recover => {
428                log::warn!("Book update for unsynced {instrument_id}; requesting a fresh snapshot");
429                self.start_recovery(instrument_id);
430                false
431            }
432        }
433    }
434
435    fn handle_sequence_gap(&self, expected: u64, received: u64) {
436        log::warn!("WebSocket sequence gap: expected {expected}, was {received}");
437
438        for instrument_id in self
439            .sync
440            .mark_connection_gap(&self.products, Instant::now())
441        {
442            log::warn!(
443                "Book for {instrument_id} out of sync after sequence gap; requesting a fresh snapshot"
444            );
445            self.start_recovery(instrument_id);
446        }
447    }
448
449    /// Restarts synchronization for every subscribed book after the connection changes.
450    fn resync(&self) {
451        self.sync.reset_on_reconnect(&self.products);
452
453        if self.snapshot_timeout.is_zero() {
454            return;
455        }
456
457        let pending =
458            self.sync
459                .seed_pending_snapshots(&self.products, self.snapshot_timeout, Instant::now());
460
461        if pending > 0 {
462            spawn_recovery_monitor(
463                self.sync.clone(),
464                self.ws.clone(),
465                Arc::clone(&self.products),
466                self.snapshot_timeout,
467                &self.tasks,
468            );
469        }
470    }
471
472    fn start_recovery(&self, instrument_id: InstrumentId) {
473        start_recovery(
474            instrument_id,
475            &self.products,
476            &self.sync,
477            &self.ws,
478            self.snapshot_timeout,
479            &self.tasks,
480        );
481    }
482}
483
484fn dispatch_ws_message(
485    msg: NautilusWsMessage,
486    data_sender: &EventSender<DataEvent>,
487    status_subs: &Arc<Mutex<AHashSet<InstrumentId>>>,
488    books: &BookRouter,
489) {
490    match msg {
491        NautilusWsMessage::Trade(trade) => {
492            if let Err(e) = data_sender.send(DataEvent::Data(Data::Trade(trade))) {
493                log::error!("Failed to send trade tick: {e}");
494            }
495        }
496        NautilusWsMessage::Quote(quote) => {
497            if let Err(e) = data_sender.send(DataEvent::Data(Data::Quote(quote))) {
498                log::error!("Failed to send quote tick: {e}");
499            }
500        }
501        NautilusWsMessage::Deltas(deltas) => {
502            if !books.accept(&deltas) {
503                return;
504            }
505
506            if let Err(e) = data_sender.send(DataEvent::Data(Data::BookDeltas(Box::new(deltas)))) {
507                log::error!("Failed to send order book deltas: {e}");
508            }
509        }
510        NautilusWsMessage::Bar(bar) => {
511            if let Err(e) = data_sender.send(DataEvent::Data(Data::Bar(bar))) {
512                log::error!("Failed to send bar: {e}");
513            }
514        }
515        NautilusWsMessage::InstrumentStatus(status) => {
516            // Coinbase publishes status for every product on a single feed,
517            // so filter to currently-subscribed instruments before emitting.
518            let subscribed = status_subs.lock().contains(&status.instrument_id);
519            if subscribed
520                && let Err(e) = data_sender.send(DataEvent::Data(Data::InstrumentStatus(*status)))
521            {
522                log::error!("Failed to send instrument status: {e}");
523            }
524        }
525        NautilusWsMessage::Reconnected => {
526            log::info!("WebSocket reconnected");
527            books.resync();
528        }
529        NautilusWsMessage::SequenceGap { expected, received } => {
530            books.handle_sequence_gap(expected, received);
531        }
532        NautilusWsMessage::Error(e) => {
533            log::warn!("WebSocket error: {e}");
534        }
535        NautilusWsMessage::UserOrder(_) => {
536            // User-channel execution reports are consumed by the execution client
537            log::debug!("Dropping user-channel update received on the data client");
538        }
539        NautilusWsMessage::FuturesBalanceSummary(_) => {
540            // Futures balance summary events are consumed by the execution client
541            log::debug!("Dropping futures_balance_summary event received on the data client");
542        }
543    }
544}
545
546#[async_trait::async_trait(?Send)]
547impl DataClient for CoinbaseDataClient {
548    fn client_id(&self) -> ClientId {
549        self.client_id
550    }
551
552    fn venue(&self) -> Option<Venue> {
553        Some(Self::venue(self))
554    }
555
556    fn start(&mut self) -> anyhow::Result<()> {
557        log::info!(
558            "Starting Coinbase data client: client_id={}, environment={:?}",
559            self.client_id,
560            self.config.environment,
561        );
562        Ok(())
563    }
564
565    fn stop(&mut self) -> anyhow::Result<()> {
566        log::info!("Stopping Coinbase data client {}", self.client_id);
567        self.session_tasks.begin_shutdown();
568        self.command_tasks.begin_shutdown();
569        self.deriv_polls.shutdown();
570        self.ws_client.begin_shutdown();
571        self.is_connected.store(false, Ordering::Relaxed);
572        Ok(())
573    }
574
575    fn reset(&mut self) -> anyhow::Result<()> {
576        log::debug!("Resetting Coinbase data client {}", self.client_id);
577        self.session_tasks.begin_shutdown();
578        self.command_tasks.begin_shutdown();
579        self.deriv_polls.shutdown();
580        self.ws_client.begin_shutdown();
581        self.is_connected.store(false, Ordering::Relaxed);
582        self.instrument_status_subs.lock().clear();
583        self.book_sync.clear();
584        Ok(())
585    }
586
587    fn dispose(&mut self) -> anyhow::Result<()> {
588        log::debug!("Disposing Coinbase data client {}", self.client_id);
589        self.stop()
590    }
591
592    fn is_connected(&self) -> bool {
593        self.is_connected.load(Ordering::Acquire)
594    }
595
596    fn is_disconnected(&self) -> bool {
597        !self.is_connected()
598    }
599
600    async fn connect(&mut self) -> anyhow::Result<()> {
601        if self.is_connected() && self.session_tasks.is_open() && self.command_tasks.is_open() {
602            return Ok(());
603        }
604
605        self.prepare_task_groups().await?;
606        let cancellation_token = self.cancellation_token.clone();
607        let ws_client = self.ws_client.clone();
608        let setup_guard =
609            TaskGroupGuard::new(&[&self.session_tasks, &self.command_tasks], move || {
610                cancellation_token.cancel();
611                ws_client.begin_shutdown();
612            });
613
614        let connect_result = async {
615            let instruments = self
616                .bootstrap_instruments()
617                .await
618                .context("failed to bootstrap instruments")?;
619
620            for instrument in instruments {
621                if let Err(e) = self.data_sender.send(DataEvent::Data(instrument.into())) {
622                    log::warn!("Failed to send instrument: {e}");
623                }
624            }
625
626            self.spawn_ws()
627                .await
628                .context("failed to spawn WebSocket client")?;
629
630            // Re-spawn polling tasks for any derivatives subscriptions that
631            // survived a previous disconnect. The data engine's client adapter
632            // remembers the subscription set and suppresses duplicate subscribe
633            // commands, so without this resume step index-price and
634            // funding-rate streams would stay dark after a reconnect.
635            self.deriv_polls.resume();
636            Ok::<(), anyhow::Error>(())
637        }
638        .await;
639
640        if let Err(e) = connect_result {
641            if let Err(teardown_error) = self.teardown_partial_connect().await {
642                return Err(e.context(format!(
643                    "Coinbase data startup teardown failed: {teardown_error}"
644                )));
645            }
646            return Err(e);
647        }
648
649        self.is_connected.store(true, Ordering::Relaxed);
650        setup_guard.disarm();
651        log::info!("Connected: client_id={}", self.client_id);
652
653        Ok(())
654    }
655
656    async fn disconnect(&mut self) -> anyhow::Result<()> {
657        let shutdown_result = self.teardown_partial_connect().await;
658        self.instruments.store(ahash::AHashMap::new());
659        log::info!("Disconnected: client_id={}", self.client_id);
660
661        shutdown_result
662    }
663
664    fn subscribe_instrument(&mut self, cmd: SubscribeInstrument) -> anyhow::Result<()> {
665        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
666            let instruments = self.instruments.load();
667
668            if let Some(instrument) = instruments.get(&instrument_id) {
669                if let Err(e) = self
670                    .data_sender
671                    .send(DataEvent::Data(instrument.clone().into()))
672                {
673                    log::error!("Failed to send instrument {instrument_id}: {e}");
674                }
675            } else {
676                log::warn!("Instrument {instrument_id} not found in cache");
677            }
678
679            Ok(())
680        })
681    }
682
683    fn subscribe_book_deltas(&mut self, subscription: SubscribeBookDeltas) -> anyhow::Result<()> {
684        execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
685            let subscription = subscription.with_instrument_ids(instrument_id);
686            if subscription.book_type != BookType::L2_MBP {
687                anyhow::bail!("Coinbase only supports L2_MBP order book deltas");
688            }
689
690            if self.book_products.contains_key(&instrument_id) {
691                log::debug!("Already subscribed to book deltas: {instrument_id}");
692                return Ok(());
693            }
694
695            let ws = self.ws_client.clone();
696            let subscribed_id = Self::product_id(instrument_id);
697            let wire_id = self.resolve_wire_product_id(subscribed_id);
698            if wire_id != subscribed_id {
699                ws.register_subscription_alias(wire_id, subscribed_id);
700            }
701
702            let spawner = self
703                .session_tasks
704                .spawner()
705                .context("Coinbase data session task admission is closed")?;
706
707            // The gate holds back frames from any earlier subscription until this write completes
708            let gate = SnapshotGate::default();
709            gate.lock().close();
710            self.book_products.insert(instrument_id, wire_id);
711            let cancel =
712                self.book_sync
713                    .record_subscription(instrument_id, Instant::now(), gate.clone());
714            let guard = cancel.clone().drop_guard();
715            let tracker = self.book_sync.clone();
716            let timeout = Duration::from_secs(self.config.book_snapshot_timeout_secs);
717
718            spawn_task(&spawner.clone(), async move {
719                let _guard = guard;
720                let result = ws.subscribe_book(wire_id, cancel.clone(), gate).await;
721
722                if cancel.is_cancelled() {
723                    return;
724                }
725
726                match result {
727                    Ok(()) => {
728                        if !snapshot_expired(&cancel, timeout).await {
729                            return;
730                        }
731
732                        log::warn!(
733                            "Initial book snapshot missing for {instrument_id}; requesting a fresh snapshot"
734                        );
735                    }
736                    Err(e) => {
737                        log::warn!(
738                            "Initial book subscription send failed for {instrument_id}: {e}"
739                        );
740                    }
741                }
742
743                if let Some(recovery) = tracker.claim_subscription_recovery(instrument_id, &cancel)
744                {
745                    spawn_recovery_task(instrument_id, wire_id, recovery, ws, timeout, &spawner);
746                }
747            });
748
749            Ok(())
750        })
751    }
752
753    fn subscribe_quotes(&mut self, subscription: SubscribeQuotes) -> anyhow::Result<()> {
754        execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
755            let ws = self.ws_client.clone();
756            let subscribed_id = Self::product_id(instrument_id);
757            let wire_id = self.resolve_wire_product_id(subscribed_id);
758            if wire_id != subscribed_id {
759                ws.register_subscription_alias(wire_id, subscribed_id);
760            }
761
762            self.spawn_command(async move {
763                if let Err(e) = ws.subscribe(CoinbaseWsChannel::Ticker, &[wire_id]).await {
764                    log::error!("Failed to subscribe to quotes: {e:?}");
765                }
766            });
767
768            Ok(())
769        })
770    }
771
772    fn subscribe_trades(&mut self, subscription: SubscribeTrades) -> anyhow::Result<()> {
773        execute_instrument_subscriptions(&subscription.instrument_ids, |instrument_id| {
774            let ws = self.ws_client.clone();
775            let subscribed_id = Self::product_id(instrument_id);
776            let wire_id = self.resolve_wire_product_id(subscribed_id);
777            if wire_id != subscribed_id {
778                ws.register_subscription_alias(wire_id, subscribed_id);
779            }
780
781            self.spawn_command(async move {
782                if let Err(e) = ws
783                    .subscribe(CoinbaseWsChannel::MarketTrades, &[wire_id])
784                    .await
785                {
786                    log::error!("Failed to subscribe to trades: {e:?}");
787                }
788            });
789
790            Ok(())
791        })
792    }
793
794    fn subscribe_mark_prices(&mut self, cmd: SubscribeMarkPrices) -> anyhow::Result<()> {
795        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
796            // Coinbase Advanced Trade does not publish a live mark price for its
797            // perpetuals on either WS or REST. `settlement_price` is the prior
798            // daily settlement and drifts from the live index, so synthesizing a
799            // mark from it would be misleading. Reject explicitly so callers
800            // failing this subscription know why.
801            anyhow::bail!(
802                "Coinbase Advanced Trade does not publish mark prices; \
803                 cannot subscribe for {instrument_id}"
804            )
805        })
806    }
807
808    fn subscribe_index_prices(&mut self, cmd: SubscribeIndexPrices) -> anyhow::Result<()> {
809        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
810            self.deriv_polls.subscribe_index(instrument_id);
811            Ok(())
812        })
813    }
814
815    fn subscribe_funding_rates(&mut self, cmd: SubscribeFundingRates) -> anyhow::Result<()> {
816        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
817            self.deriv_polls.subscribe_funding(instrument_id);
818            Ok(())
819        })
820    }
821
822    fn subscribe_instrument_status(
823        &mut self,
824        cmd: SubscribeInstrumentStatus,
825    ) -> anyhow::Result<()> {
826        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
827            // Register the canonical-to-subscribed alias so the handler re-keys
828            // inbound status events for aliased products (e.g. caller subscribed
829            // to `BTC-USDC` but the venue reports the canonical `BTC-USD`).
830            // Without this the filter below would drop alias-only subscriptions.
831            let subscribed_id = Self::product_id(instrument_id);
832            let wire_id = self.resolve_wire_product_id(subscribed_id);
833            if wire_id != subscribed_id {
834                self.ws_client
835                    .register_subscription_alias(wire_id, subscribed_id);
836            }
837
838            // Coinbase publishes a single product-wide status feed. Only subscribe
839            // to the WS channel once; subsequent calls just record the instrument.
840            let was_empty = {
841                let mut subs = self.instrument_status_subs.lock();
842                let was_empty = subs.is_empty();
843                subs.insert(instrument_id);
844                was_empty
845            };
846
847            if was_empty {
848                let ws = self.ws_client.clone();
849                self.spawn_command(async move {
850                    if let Err(e) = ws.subscribe(CoinbaseWsChannel::Status, &[]).await {
851                        log::error!("Failed to subscribe to status channel: {e:?}");
852                    }
853                });
854            }
855
856            Ok(())
857        })
858    }
859
860    fn subscribe_bars(&mut self, subscription: SubscribeBars) -> anyhow::Result<()> {
861        execute_bar_subscriptions(&subscription.bar_types, |bar_type| {
862            let instrument_id = bar_type.instrument_id();
863
864            if !self.instruments.contains_key(&instrument_id) {
865                anyhow::bail!(InstrumentLookupError::not_found(instrument_id));
866            }
867
868            let subscribed_id = Self::product_id(instrument_id);
869            let wire_id = self.resolve_wire_product_id(subscribed_id);
870            if wire_id != subscribed_id {
871                self.ws_client
872                    .register_subscription_alias(wire_id, subscribed_id);
873            }
874
875            let key = wire_id.to_string();
876
877            // Register on the original client so the bar type persists across clones
878            self.ws_client.register_bar_type(key.clone(), bar_type);
879
880            let mut ws = self.ws_client.clone();
881            self.spawn_command(async move {
882                ws.add_bar_type(key, bar_type).await;
883
884                if let Err(e) = ws.subscribe(CoinbaseWsChannel::Candles, &[wire_id]).await {
885                    log::error!("Failed to subscribe to bars: {e:?}");
886                }
887            });
888
889            Ok(())
890        })
891    }
892
893    // Unsubscribe paths intentionally do NOT call
894    // `unregister_subscription_alias`. The same canonical wire id is shared
895    // across multiple data channels (ticker, market_trades, level2,
896    // candles), so dropping the entry on the first unsubscribe would cause
897    // every still-active channel for the same alias to mistag inbound
898    // messages. The mapping is stable per product for the process lifetime
899    // and the venue does not deliver messages for products that aren't
900    // subscribed to, so leaving it in place is safe.
901
902    fn unsubscribe_instrument(
903        &mut self,
904        _unsubscription: &UnsubscribeInstrument,
905    ) -> anyhow::Result<()> {
906        // `subscribe_instrument` only replays cached state; no venue subscription to tear down.
907        Ok(())
908    }
909
910    fn unsubscribe_book_deltas(
911        &mut self,
912        unsubscription: &UnsubscribeBookDeltas,
913    ) -> anyhow::Result<()> {
914        execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
915            log::debug!("Unsubscribing from book deltas: {instrument_id}");
916
917            let ws = self.ws_client.clone();
918            let subscribed_id = Self::product_id(instrument_id);
919            let wire_id = self.resolve_wire_product_id(subscribed_id);
920
921            // Removing the book cancels its recovery and pending snapshot wait
922            self.book_products.remove(&instrument_id);
923            self.book_sync.remove(instrument_id);
924
925            self.spawn_command(async move {
926                if let Err(e) = ws.unsubscribe(CoinbaseWsChannel::Level2, &[wire_id]).await {
927                    log::error!("Failed to unsubscribe from book deltas: {e:?}");
928                }
929            });
930
931            Ok(())
932        })
933    }
934
935    fn unsubscribe_quotes(&mut self, unsubscription: &UnsubscribeQuotes) -> anyhow::Result<()> {
936        execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
937            log::debug!("Unsubscribing from quotes: {instrument_id}");
938
939            let ws = self.ws_client.clone();
940            let subscribed_id = Self::product_id(instrument_id);
941            let wire_id = self.resolve_wire_product_id(subscribed_id);
942            self.spawn_command(async move {
943                if let Err(e) = ws.unsubscribe(CoinbaseWsChannel::Ticker, &[wire_id]).await {
944                    log::error!("Failed to unsubscribe from quotes: {e:?}");
945                }
946            });
947
948            Ok(())
949        })
950    }
951
952    fn unsubscribe_trades(&mut self, unsubscription: &UnsubscribeTrades) -> anyhow::Result<()> {
953        execute_instrument_subscriptions(&unsubscription.instrument_ids, |instrument_id| {
954            log::debug!("Unsubscribing from trades: {instrument_id}");
955
956            let ws = self.ws_client.clone();
957            let subscribed_id = Self::product_id(instrument_id);
958            let wire_id = self.resolve_wire_product_id(subscribed_id);
959
960            self.spawn_command(async move {
961                if let Err(e) = ws
962                    .unsubscribe(CoinbaseWsChannel::MarketTrades, &[wire_id])
963                    .await
964                {
965                    log::error!("Failed to unsubscribe from trades: {e:?}");
966                }
967            });
968
969            Ok(())
970        })
971    }
972
973    fn unsubscribe_mark_prices(&mut self, _cmd: &UnsubscribeMarkPrices) -> anyhow::Result<()> {
974        Ok(())
975    }
976
977    fn unsubscribe_index_prices(&mut self, cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> {
978        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
979            self.deriv_polls.unsubscribe_index(instrument_id);
980            Ok(())
981        })
982    }
983
984    fn unsubscribe_funding_rates(&mut self, cmd: &UnsubscribeFundingRates) -> anyhow::Result<()> {
985        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
986            self.deriv_polls.unsubscribe_funding(instrument_id);
987            Ok(())
988        })
989    }
990
991    fn unsubscribe_instrument_status(
992        &mut self,
993        cmd: &UnsubscribeInstrumentStatus,
994    ) -> anyhow::Result<()> {
995        execute_instrument_subscriptions(&cmd.instrument_ids, |instrument_id| {
996            log::debug!("Unsubscribing from instrument status: {instrument_id}");
997
998            let now_empty = {
999                let mut subs = self.instrument_status_subs.lock();
1000                subs.remove(&instrument_id);
1001                subs.is_empty()
1002            };
1003
1004            if now_empty {
1005                let ws = self.ws_client.clone();
1006                self.spawn_command(async move {
1007                    if let Err(e) = ws.unsubscribe(CoinbaseWsChannel::Status, &[]).await {
1008                        log::error!("Failed to unsubscribe from status channel: {e:?}");
1009                    }
1010                });
1011            }
1012
1013            Ok(())
1014        })
1015    }
1016
1017    fn unsubscribe_bars(&mut self, unsubscription: &UnsubscribeBars) -> anyhow::Result<()> {
1018        execute_bar_subscriptions(&unsubscription.bar_types, |bar_type| {
1019            let instrument_id = bar_type.instrument_id();
1020            let subscribed_id = Self::product_id(instrument_id);
1021            let wire_id = self.resolve_wire_product_id(subscribed_id);
1022            let ws = self.ws_client.clone();
1023            self.spawn_command(async move {
1024                if let Err(e) = ws.unsubscribe(CoinbaseWsChannel::Candles, &[wire_id]).await {
1025                    log::error!("Failed to unsubscribe from bars: {e:?}");
1026                }
1027            });
1028
1029            Ok(())
1030        })
1031    }
1032
1033    fn request_instruments(&self, request: RequestInstruments) -> anyhow::Result<()> {
1034        log::debug!("Requesting all instruments");
1035
1036        let provider = self.provider.clone();
1037        let sender = self.data_sender.clone();
1038        let instruments_cache = self.instruments.clone();
1039        let ws = self.ws_client.clone();
1040        let request_id = request.request_id;
1041        let client_id = request.client_id.unwrap_or(self.client_id);
1042        let venue = Self::venue(self);
1043        let start_nanos = datetime_to_unix_nanos(request.start);
1044        let end_nanos = datetime_to_unix_nanos(request.end);
1045        let params = request.params;
1046        let clock = self.clock;
1047
1048        self.spawn_command(async move {
1049            match provider.load_all().await {
1050                Ok(instruments) => {
1051                    instruments_cache.rcu(|m| {
1052                        for instrument in &instruments {
1053                            m.insert(instrument.id(), instrument.clone());
1054                        }
1055                    });
1056
1057                    ws.update_instruments(&instruments).await;
1058
1059                    let response = DataResponse::Instruments(InstrumentsResponse::new(
1060                        request_id,
1061                        client_id,
1062                        venue,
1063                        instruments,
1064                        start_nanos,
1065                        end_nanos,
1066                        clock.get_time_ns(),
1067                        params,
1068                    ));
1069
1070                    if let Err(e) = sender.send(DataEvent::Response(response)) {
1071                        log::error!("Failed to send instruments response: {e}");
1072                    }
1073                }
1074                Err(e) => {
1075                    log::error!("Failed to fetch instruments: {e:?}");
1076                }
1077            }
1078        });
1079
1080        Ok(())
1081    }
1082
1083    fn request_instrument(&self, request: RequestInstrument) -> anyhow::Result<()> {
1084        let instrument_id = request.single_instrument_id()?;
1085
1086        log::debug!("Requesting instrument: {instrument_id}");
1087
1088        let provider = self.provider.clone();
1089        let sender = self.data_sender.clone();
1090        let instruments_cache = self.instruments.clone();
1091        let ws = self.ws_client.clone();
1092        let product_id = instrument_id.symbol.to_string();
1093        let request_id = request.request_id;
1094        let client_id = request.client_id.unwrap_or(self.client_id);
1095        let start_nanos = datetime_to_unix_nanos(request.start);
1096        let end_nanos = datetime_to_unix_nanos(request.end);
1097        let params = request.params;
1098        let clock = self.clock;
1099
1100        self.spawn_command(async move {
1101            match provider.load(&product_id).await {
1102                Ok(instrument) => {
1103                    instruments_cache.rcu(|m| {
1104                        m.insert(instrument.id(), instrument.clone());
1105                    });
1106                    ws.update_instrument(instrument.clone()).await;
1107
1108                    let response = DataResponse::Instrument(Box::new(InstrumentResponse::new(
1109                        request_id,
1110                        client_id,
1111                        instrument.id(),
1112                        instrument,
1113                        start_nanos,
1114                        end_nanos,
1115                        clock.get_time_ns(),
1116                        params,
1117                    )));
1118
1119                    if let Err(e) = sender.send(DataEvent::Response(response)) {
1120                        log::error!("Failed to send instrument response: {e}");
1121                    }
1122                }
1123                Err(e) => {
1124                    log::error!("Failed to fetch instrument {instrument_id}: {e:?}");
1125                }
1126            }
1127        });
1128
1129        Ok(())
1130    }
1131
1132    fn request_book_snapshot(&self, request: RequestBookSnapshot) -> anyhow::Result<()> {
1133        let instrument_id = request.single_instrument_id()?;
1134
1135        let product_id = instrument_id.symbol.to_string();
1136
1137        let instruments = self.instruments.load();
1138        let instrument = instruments
1139            .get(&instrument_id)
1140            .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1141        let price_precision = instrument.price_precision();
1142        let size_precision = instrument.size_precision();
1143        let depth = request.depth.map(|d| d.get() as u32);
1144
1145        let http = self.http_client.clone();
1146        let sender = self.data_sender.clone();
1147        let client_id = request.client_id.unwrap_or(self.client_id);
1148        let request_id = request.request_id;
1149        let params = request.params;
1150        let clock = self.clock;
1151
1152        self.spawn_command(async move {
1153            match http.get_product_book(&product_id, depth).await {
1154                Ok(json) => {
1155                    let pricebook_value = json.get("pricebook").cloned().unwrap_or(json);
1156
1157                    let pricebook: PriceBook = match serde_json::from_value(pricebook_value) {
1158                        Ok(b) => b,
1159                        Err(e) => {
1160                            log::error!("Failed to parse product book: {e}");
1161                            return;
1162                        }
1163                    };
1164
1165                    let ts_init = clock.get_time_ns();
1166
1167                    match parse_product_book_snapshot(
1168                        &pricebook,
1169                        instrument_id,
1170                        price_precision,
1171                        size_precision,
1172                        ts_init,
1173                    ) {
1174                        Ok(deltas) => {
1175                            let mut book = OrderBook::new(instrument_id, BookType::L2_MBP);
1176
1177                            for delta in &deltas.deltas {
1178                                if delta.order.side.is_some() {
1179                                    book.add(
1180                                        delta.order,
1181                                        delta.flags,
1182                                        delta.sequence,
1183                                        delta.ts_event,
1184                                    );
1185                                }
1186                            }
1187
1188                            let response = DataResponse::Book(BookResponse::new(
1189                                request_id,
1190                                client_id,
1191                                instrument_id,
1192                                book,
1193                                None,
1194                                None,
1195                                clock.get_time_ns(),
1196                                params,
1197                            ));
1198
1199                            if let Err(e) = sender.send(DataEvent::Response(response)) {
1200                                log::error!("Failed to send book snapshot response: {e}");
1201                            }
1202                        }
1203                        Err(e) => {
1204                            log::error!("Failed to parse book snapshot for {instrument_id}: {e}");
1205                        }
1206                    }
1207                }
1208                Err(e) => {
1209                    log::error!("Book snapshot request failed for {instrument_id}: {e:?}");
1210                }
1211            }
1212        });
1213
1214        Ok(())
1215    }
1216
1217    fn request_trades(&self, request: RequestTrades) -> anyhow::Result<()> {
1218        let instrument_id = request.single_instrument_id()?;
1219
1220        log::debug!("Requesting trades for {instrument_id}");
1221        let product_id = instrument_id.symbol.to_string();
1222
1223        let instruments = self.instruments.load();
1224        let instrument = instruments
1225            .get(&instrument_id)
1226            .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1227        let price_precision = instrument.price_precision();
1228        let size_precision = instrument.size_precision();
1229
1230        let http = self.http_client.clone();
1231        let sender = self.data_sender.clone();
1232        let request_id = request.request_id;
1233        let client_id = request.client_id.unwrap_or(self.client_id);
1234        let limit = request.limit.map_or(100, |n| n.get() as u32);
1235        let start_nanos = datetime_to_unix_nanos(request.start);
1236        let end_nanos = datetime_to_unix_nanos(request.end);
1237        let params = request.params;
1238        let clock = self.clock;
1239
1240        self.spawn_command(async move {
1241            match http.get_market_trades(&product_id, limit).await {
1242                Ok(json) => {
1243                    let ticker: TickerResponse = match serde_json::from_value(json) {
1244                        Ok(r) => r,
1245                        Err(e) => {
1246                            log::error!("Failed to parse trades response: {e}");
1247                            return;
1248                        }
1249                    };
1250
1251                    let ts_init = clock.get_time_ns();
1252                    let mut trades: Vec<_> = ticker
1253                        .trades
1254                        .iter()
1255                        .filter_map(|trade| {
1256                            parse_trade_tick(
1257                                trade,
1258                                instrument_id,
1259                                price_precision,
1260                                size_precision,
1261                                ts_init,
1262                            )
1263                            .map_err(|e| log::warn!("Failed to parse trade: {e}"))
1264                            .ok()
1265                        })
1266                        .collect();
1267
1268                    // Coinbase returns newest-first; sort ascending
1269                    trades.sort_by_key(|t| t.ts_event);
1270
1271                    let response = DataResponse::Trades(TradesResponse::new(
1272                        request_id,
1273                        client_id,
1274                        instrument_id,
1275                        trades,
1276                        start_nanos,
1277                        end_nanos,
1278                        clock.get_time_ns(),
1279                        params,
1280                    ));
1281
1282                    if let Err(e) = sender.send(DataEvent::Response(response)) {
1283                        log::error!("Failed to send trades response: {e}");
1284                    }
1285                }
1286                Err(e) => log::error!("Trades request failed for {instrument_id}: {e:?}"),
1287            }
1288        });
1289
1290        Ok(())
1291    }
1292
1293    fn request_bars(&self, request: RequestBars) -> anyhow::Result<()> {
1294        let bar_type = request.single_bar_type()?;
1295
1296        log::debug!("Requesting bars for {bar_type}");
1297        let granularity = bar_type_to_granularity(&bar_type)?;
1298        let instrument_id = bar_type.instrument_id();
1299        let product_id = instrument_id.symbol.to_string();
1300
1301        let instruments = self.instruments.load();
1302        let instrument = instruments
1303            .get(&instrument_id)
1304            .ok_or_else(|| InstrumentLookupError::not_found(instrument_id))?;
1305        let price_precision = instrument.price_precision();
1306        let size_precision = instrument.size_precision();
1307
1308        let http = self.http_client.clone();
1309        let sender = self.data_sender.clone();
1310        let request_id = request.request_id;
1311        let client_id = request.client_id.unwrap_or(self.client_id);
1312        let start = request.start;
1313        let end = request.end;
1314        let limit = request.limit.map(|n| n.get());
1315        let start_nanos = datetime_to_unix_nanos(start);
1316        let end_nanos = datetime_to_unix_nanos(end);
1317        let params = request.params;
1318        let clock = self.clock;
1319
1320        self.spawn_command(async move {
1321            let now = jiff::Timestamp::now();
1322            let end_secs = end.unwrap_or(now).as_second().to_string();
1323            let start_secs = if let Some(s) = start {
1324                s.as_second().to_string()
1325            } else {
1326                let spec = bar_type.spec();
1327                let step_secs = match spec.aggregation {
1328                    BarAggregation::Minute => spec.step.get() as i64 * 60,
1329                    BarAggregation::Hour => spec.step.get() as i64 * 3600,
1330                    BarAggregation::Day => spec.step.get() as i64 * 86400,
1331                    _ => 60,
1332                };
1333                let count = limit.unwrap_or(300) as i64;
1334                let end_ts = end.unwrap_or(now).as_second();
1335                (end_ts - count * step_secs).to_string()
1336            };
1337
1338            let granularity_str = granularity.to_string();
1339
1340            match http
1341                .get_candles(&product_id, &start_secs, &end_secs, &granularity_str)
1342                .await
1343            {
1344                Ok(json) => {
1345                    let candles_response: CandlesResponse = match serde_json::from_value(json) {
1346                        Ok(r) => r,
1347                        Err(e) => {
1348                            log::error!("Failed to parse candles response: {e}");
1349                            return;
1350                        }
1351                    };
1352
1353                    let ts_init = clock.get_time_ns();
1354                    let mut bars: Vec<_> = candles_response
1355                        .candles
1356                        .iter()
1357                        .filter_map(|candle| {
1358                            parse_bar(candle, bar_type, price_precision, size_precision, ts_init)
1359                                .map_err(|e| log::warn!("Failed to parse bar: {e}"))
1360                                .ok()
1361                        })
1362                        .collect();
1363
1364                    bars.sort_by_key(|b| b.ts_event);
1365
1366                    if let Some(limit) = limit
1367                        && bars.len() > limit
1368                    {
1369                        bars.drain(..bars.len() - limit);
1370                    }
1371
1372                    let response = DataResponse::Bars(BarsResponse::new(
1373                        request_id,
1374                        client_id,
1375                        bar_type,
1376                        bars,
1377                        start_nanos,
1378                        end_nanos,
1379                        clock.get_time_ns(),
1380                        params,
1381                    ));
1382
1383                    if let Err(e) = sender.send(DataEvent::Response(response)) {
1384                        log::error!("Failed to send bars response: {e}");
1385                    }
1386                }
1387                Err(e) => log::error!("Bar request failed: {e:?}"),
1388            }
1389        });
1390
1391        Ok(())
1392    }
1393}
1394
1395#[cfg(test)]
1396mod tests {
1397    use nautilus_common::{
1398        live::runner::set_data_event_sender, messages::data::SubscribeMarkPrices,
1399    };
1400    use nautilus_core::{UUID4, UnixNanos};
1401    use nautilus_model::identifiers::InstrumentId;
1402    use nautilus_network::websocket::TransportBackend;
1403    use rstest::rstest;
1404
1405    use super::*;
1406    use crate::common::consts::COINBASE_CLIENT_ID;
1407
1408    // Coinbase Advanced Trade does not publish live mark prices for its
1409    // perpetuals, so `subscribe_mark_prices` must return an explicit error
1410    // naming the instrument and mentioning mark prices. A regression that
1411    // silently `Ok(())`s the call would mask the unsupported feature.
1412    #[rstest]
1413    #[tokio::test]
1414    async fn test_subscribe_mark_prices_rejects_with_explicit_error() {
1415        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
1416        set_data_event_sender(tx);
1417
1418        let config = CoinbaseDataClientConfig::default();
1419        let mut client =
1420            CoinbaseDataClient::new(*COINBASE_CLIENT_ID, config).expect("client construction");
1421
1422        let instrument_id = InstrumentId::from("BIP-20DEC30-CDE.COINBASE");
1423        let cmd = SubscribeMarkPrices::new(
1424            instrument_id,
1425            Some(*COINBASE_CLIENT_ID),
1426            None,
1427            UUID4::new(),
1428            UnixNanos::default(),
1429            None,
1430            None,
1431        );
1432
1433        let err = client
1434            .subscribe_mark_prices(cmd)
1435            .expect_err("must reject mark-price subscriptions");
1436        let msg = err.to_string();
1437        assert!(
1438            msg.contains("mark prices"),
1439            "error must mention mark prices, was: {msg}"
1440        );
1441        assert!(
1442            msg.contains("BIP-20DEC30-CDE.COINBASE"),
1443            "error must name the instrument, was: {msg}"
1444        );
1445    }
1446
1447    fn make_status_event(instrument_id: InstrumentId) -> NautilusWsMessage {
1448        use nautilus_model::{data::InstrumentStatus, enums::MarketStatusAction};
1449
1450        let status = InstrumentStatus::new(
1451            instrument_id,
1452            MarketStatusAction::Trading,
1453            UnixNanos::from(1),
1454            UnixNanos::from(2),
1455            None,
1456            None,
1457            Some(true),
1458            None,
1459            None,
1460        );
1461        NautilusWsMessage::InstrumentStatus(Box::new(status))
1462    }
1463
1464    fn test_book_router(tasks: &TaskGroup) -> BookRouter {
1465        BookRouter {
1466            sync: BookSyncTracker::default(),
1467            products: Arc::new(AtomicMap::new()),
1468            ws: CoinbaseWebSocketClient::new("ws://127.0.0.1:1", TransportBackend::default(), None),
1469            snapshot_timeout: Duration::from_secs(10),
1470            tasks: tasks.spawner().unwrap(),
1471        }
1472    }
1473
1474    // The dispatch filter is what keeps the venue-wide `status` feed from
1475    // leaking every product's events to a subscriber that only cares about
1476    // one. A regression that drops the `subs.contains(...)` guard would let
1477    // every product's status reach `data_sender`.
1478    #[rstest]
1479    fn test_dispatch_ws_message_status_filter_forwards_subscribed() {
1480        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<DataEvent>();
1481        let instrument_id = InstrumentId::from("BTC-USD.COINBASE");
1482        let mut set = AHashSet::new();
1483        set.insert(instrument_id);
1484        let subs = Arc::new(Mutex::new(set));
1485        let tasks = TaskGroup::new();
1486
1487        dispatch_ws_message(
1488            make_status_event(instrument_id),
1489            &tx.into(),
1490            &subs,
1491            &test_book_router(&tasks),
1492        );
1493
1494        match rx.try_recv() {
1495            Ok(DataEvent::Data(Data::InstrumentStatus(status))) => {
1496                assert_eq!(status.instrument_id, instrument_id);
1497            }
1498            other => panic!("expected DataEvent::Data(Data::InstrumentStatus(...)), was {other:?}"),
1499        }
1500    }
1501
1502    #[rstest]
1503    fn test_dispatch_ws_message_status_filter_drops_unsubscribed() {
1504        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<DataEvent>();
1505        let subscribed = InstrumentId::from("BTC-USD.COINBASE");
1506        let unsubscribed = InstrumentId::from("ETH-USD.COINBASE");
1507        let mut set = AHashSet::new();
1508        set.insert(subscribed);
1509        let subs = Arc::new(Mutex::new(set));
1510        let tasks = TaskGroup::new();
1511
1512        dispatch_ws_message(
1513            make_status_event(unsubscribed),
1514            &tx.into(),
1515            &subs,
1516            &test_book_router(&tasks),
1517        );
1518
1519        assert!(
1520            rx.try_recv().is_err(),
1521            "unsubscribed status must be dropped"
1522        );
1523    }
1524
1525    // First subscribe on an empty set must populate `instrument_status_subs`;
1526    // a second subscribe for the same instrument must not add a duplicate or
1527    // re-spawn the channel-level WS subscribe. Reset must clear the field.
1528    #[rstest]
1529    #[tokio::test]
1530    async fn test_subscribe_instrument_status_records_and_idempotent() {
1531        use nautilus_common::messages::data::SubscribeInstrumentStatus;
1532
1533        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
1534        set_data_event_sender(tx);
1535
1536        let config = CoinbaseDataClientConfig::default();
1537        let mut client =
1538            CoinbaseDataClient::new(*COINBASE_CLIENT_ID, config).expect("client construction");
1539
1540        let instrument_id = InstrumentId::from("BTC-USD.COINBASE");
1541        let cmd = SubscribeInstrumentStatus::new(
1542            instrument_id,
1543            Some(*COINBASE_CLIENT_ID),
1544            None,
1545            UUID4::new(),
1546            UnixNanos::default(),
1547            None,
1548            None,
1549        );
1550
1551        client.subscribe_instrument_status(cmd.clone()).unwrap();
1552        assert!(
1553            client
1554                .instrument_status_subs
1555                .lock()
1556                .contains(&instrument_id)
1557        );
1558
1559        // Duplicate subscribe keeps the set at size 1.
1560        client.subscribe_instrument_status(cmd).unwrap();
1561        assert_eq!(client.instrument_status_subs.lock().len(), 1);
1562
1563        // Reset clears the set so a subsequent connect starts clean.
1564        client.reset().unwrap();
1565        assert!(client.instrument_status_subs.lock().is_empty());
1566    }
1567
1568    // Unsubscribing the last instrument empties the set; intermediate
1569    // unsubscribes leave other entries in place.
1570    #[rstest]
1571    #[tokio::test]
1572    async fn test_unsubscribe_instrument_status_emptying_set() {
1573        use nautilus_common::messages::data::{
1574            SubscribeInstrumentStatus, UnsubscribeInstrumentStatus,
1575        };
1576
1577        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
1578        set_data_event_sender(tx);
1579
1580        let mut client =
1581            CoinbaseDataClient::new(*COINBASE_CLIENT_ID, CoinbaseDataClientConfig::default())
1582                .expect("client construction");
1583
1584        let a = InstrumentId::from("BTC-USD.COINBASE");
1585        let b = InstrumentId::from("ETH-USD.COINBASE");
1586
1587        for id in [a, b] {
1588            client
1589                .subscribe_instrument_status(SubscribeInstrumentStatus::new(
1590                    id,
1591                    Some(*COINBASE_CLIENT_ID),
1592                    None,
1593                    UUID4::new(),
1594                    UnixNanos::default(),
1595                    None,
1596                    None,
1597                ))
1598                .unwrap();
1599        }
1600        assert_eq!(client.instrument_status_subs.lock().len(), 2);
1601
1602        let unsub = |id| {
1603            UnsubscribeInstrumentStatus::new(
1604                id,
1605                Some(*COINBASE_CLIENT_ID),
1606                None,
1607                UUID4::new(),
1608                UnixNanos::default(),
1609                None,
1610                None,
1611            )
1612        };
1613
1614        // Intermediate unsubscribe: `a` leaves, `b` retained.
1615        client.unsubscribe_instrument_status(&unsub(a)).unwrap();
1616        {
1617            let subs = client.instrument_status_subs.lock();
1618            assert!(!subs.contains(&a), "a removed");
1619            assert!(subs.contains(&b), "b retained");
1620            assert_eq!(subs.len(), 1);
1621        }
1622
1623        // Last unsubscribe: set must end up empty so a future first-subscribe
1624        // re-arms the channel-level WS subscription.
1625        client.unsubscribe_instrument_status(&unsub(b)).unwrap();
1626        assert!(
1627            client.instrument_status_subs.lock().is_empty(),
1628            "last unsubscribe must empty the set",
1629        );
1630    }
1631}