coinbase-advanced 1.0.0

A Rust client library for the Coinbase Advanced Trade API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! WebSocket client implementation.

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use futures::stream::{SplitSink, SplitStream};
use futures::{SinkExt, Stream, StreamExt};
use tokio::net::TcpStream;
use tokio::sync::Mutex;
use tokio_tungstenite::tungstenite::Message as WsMessage;
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};

use super::channels::{Channel, ChannelName, EndpointType};
use super::messages::Message;
use crate::credentials::Credentials;
use crate::error::{Error, Result};
use crate::jwt::generate_ws_jwt;

/// WebSocket endpoints.
const PUBLIC_ENDPOINT: &str = "wss://advanced-trade-ws.coinbase.com";
const USER_ENDPOINT: &str = "wss://advanced-trade-ws-user.coinbase.com";

type Socket = WebSocketStream<MaybeTlsStream<TcpStream>>;
type WsSink = SplitSink<Socket, WsMessage>;
type WsStream = SplitStream<Socket>;

/// Subscription message sent to the WebSocket.
#[derive(Debug, serde::Serialize)]
struct SubscriptionMessage {
    r#type: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    product_ids: Vec<String>,
    channel: ChannelName,
    #[serde(skip_serializing_if = "Option::is_none")]
    jwt: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    timestamp: Option<String>,
}

/// Builder for creating a WebSocket client.
#[derive(Default)]
pub struct WebSocketClientBuilder {
    credentials: Option<Credentials>,
    auto_reconnect: bool,
    max_retries: u32,
}

impl WebSocketClientBuilder {
    /// Create a new WebSocket client builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set credentials for authenticated channels.
    pub fn credentials(mut self, credentials: Credentials) -> Self {
        self.credentials = Some(credentials);
        self
    }

    /// Enable auto-reconnect on connection loss.
    pub fn auto_reconnect(mut self, enable: bool) -> Self {
        self.auto_reconnect = enable;
        if enable && self.max_retries == 0 {
            self.max_retries = 10;
        }
        self
    }

    /// Set maximum number of reconnection attempts.
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Build the WebSocket client.
    pub fn build(self) -> Result<WebSocketClient> {
        Ok(WebSocketClient {
            credentials: self.credentials,
            auto_reconnect: self.auto_reconnect,
            max_retries: self.max_retries,
            public_sink: Arc::new(Mutex::new(None)),
            user_sink: Arc::new(Mutex::new(None)),
            subscriptions: Arc::new(Mutex::new(Subscriptions::new())),
        })
    }
}

/// Tracks current subscriptions for reconnection.
#[derive(Debug, Default)]
struct Subscriptions {
    public: HashMap<ChannelName, Vec<String>>,
    user: HashMap<ChannelName, Vec<String>>,
}

impl Subscriptions {
    fn new() -> Self {
        Self::default()
    }

    fn add(&mut self, channel: &Channel) {
        let name = ChannelName::from(channel);
        let product_ids = channel.product_ids().to_vec();

        let map = match channel.endpoint_type() {
            EndpointType::Public => &mut self.public,
            EndpointType::User => &mut self.user,
        };

        map.entry(name)
            .or_default()
            .extend(product_ids);
    }

    fn remove(&mut self, channel: &Channel) {
        let name = ChannelName::from(channel);
        let product_ids = channel.product_ids();

        let map = match channel.endpoint_type() {
            EndpointType::Public => &mut self.public,
            EndpointType::User => &mut self.user,
        };

        if let Some(ids) = map.get_mut(&name) {
            ids.retain(|id| !product_ids.contains(id));
            if ids.is_empty() {
                map.remove(&name);
            }
        }
    }
}

/// WebSocket client for Coinbase Advanced Trade API.
pub struct WebSocketClient {
    credentials: Option<Credentials>,
    auto_reconnect: bool,
    max_retries: u32,
    public_sink: Arc<Mutex<Option<WsSink>>>,
    user_sink: Arc<Mutex<Option<WsSink>>>,
    subscriptions: Arc<Mutex<Subscriptions>>,
}

impl WebSocketClient {
    /// Create a new WebSocket client builder.
    pub fn builder() -> WebSocketClientBuilder {
        WebSocketClientBuilder::new()
    }

    /// Connect to the WebSocket endpoints.
    ///
    /// Returns a stream of messages from all connected endpoints.
    pub async fn connect(&self) -> Result<MessageStream> {
        let (public_socket, _) = connect_async(PUBLIC_ENDPOINT).await.map_err(|e| {
            Error::websocket(format!("Failed to connect to public WebSocket: {}", e))
        })?;

        let (public_sink, public_stream) = public_socket.split();
        {
            let mut sink = self.public_sink.lock().await;
            *sink = Some(public_sink);
        }

        // If we have credentials, also connect to the user endpoint.
        let user_stream = if self.credentials.is_some() {
            let (user_socket, _) = connect_async(USER_ENDPOINT).await.map_err(|e| {
                Error::websocket(format!("Failed to connect to user WebSocket: {}", e))
            })?;

            let (user_sink, user_stream) = user_socket.split();
            {
                let mut sink = self.user_sink.lock().await;
                *sink = Some(user_sink);
            }
            Some(user_stream)
        } else {
            None
        };

        Ok(MessageStream {
            public_stream: Some(public_stream),
            user_stream,
            client: self.clone_internal(),
        })
    }

    /// Subscribe to one or more channels.
    pub async fn subscribe(&self, channels: &[Channel]) -> Result<()> {
        for channel in channels {
            self.subscribe_one(channel).await?;
        }
        Ok(())
    }

    /// Subscribe to a single channel.
    async fn subscribe_one(&self, channel: &Channel) -> Result<()> {
        let endpoint = channel.endpoint_type();

        // Check if we can subscribe to this channel.
        if channel.requires_auth() && self.credentials.is_none() {
            return Err(Error::websocket(format!(
                "Channel {:?} requires authentication",
                channel.name()
            )));
        }

        let msg = self.build_subscription_message(channel, "subscribe")?;
        self.send_message(&endpoint, msg).await?;

        // Track subscription.
        {
            let mut subs = self.subscriptions.lock().await;
            subs.add(channel);
        }

        Ok(())
    }

    /// Unsubscribe from one or more channels.
    pub async fn unsubscribe(&self, channels: &[Channel]) -> Result<()> {
        for channel in channels {
            self.unsubscribe_one(channel).await?;
        }
        Ok(())
    }

    /// Unsubscribe from a single channel.
    async fn unsubscribe_one(&self, channel: &Channel) -> Result<()> {
        let endpoint = channel.endpoint_type();
        let msg = self.build_subscription_message(channel, "unsubscribe")?;
        self.send_message(&endpoint, msg).await?;

        // Update subscription tracking.
        {
            let mut subs = self.subscriptions.lock().await;
            subs.remove(channel);
        }

        Ok(())
    }

    /// Build a subscription/unsubscription message.
    fn build_subscription_message(&self, channel: &Channel, action: &str) -> Result<WsMessage> {
        let channel_name = ChannelName::from(channel);
        let product_ids = channel.product_ids().to_vec();

        let msg = if channel.requires_auth() {
            let jwt = self.generate_jwt()?;
            SubscriptionMessage {
                r#type: action.to_string(),
                product_ids,
                channel: channel_name,
                jwt: Some(jwt),
                timestamp: None,
            }
        } else {
            let timestamp = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map_err(|e| Error::websocket(format!("Failed to get timestamp: {}", e)))?
                .as_secs()
                .to_string();

            SubscriptionMessage {
                r#type: action.to_string(),
                product_ids,
                channel: channel_name,
                jwt: None,
                timestamp: Some(timestamp),
            }
        };

        let json = serde_json::to_string(&msg)
            .map_err(|e| Error::websocket(format!("Failed to serialize message: {}", e)))?;

        Ok(WsMessage::Text(json.into()))
    }

    /// Generate a JWT for WebSocket authentication.
    fn generate_jwt(&self) -> Result<String> {
        let credentials = self.credentials.as_ref().ok_or_else(|| {
            Error::websocket("Credentials required for authenticated channels")
        })?;
        generate_ws_jwt(credentials)
    }

    /// Send a message to the appropriate endpoint.
    async fn send_message(&self, endpoint: &EndpointType, msg: WsMessage) -> Result<()> {
        let sink = match endpoint {
            EndpointType::Public => &self.public_sink,
            EndpointType::User => &self.user_sink,
        };

        let mut guard = sink.lock().await;
        let sink = guard.as_mut().ok_or_else(|| {
            Error::websocket(format!(
                "{:?} WebSocket not connected. Call connect() first.",
                endpoint
            ))
        })?;

        sink.send(msg).await.map_err(|e| {
            Error::websocket(format!("Failed to send message: {}", e))
        })
    }

    /// Attempt to reconnect after a connection loss.
    #[allow(dead_code)]
    async fn reconnect(&self) -> Result<(Option<WsStream>, Option<WsStream>)> {
        if !self.auto_reconnect {
            return Err(Error::websocket("Auto-reconnect is disabled"));
        }

        let mut retry_count = 0;
        let mut delay = Duration::from_secs(1);

        while retry_count < self.max_retries {
            tokio::time::sleep(delay).await;

            match self.attempt_reconnect().await {
                Ok(streams) => {
                    // Resubscribe to previous channels.
                    self.resubscribe().await?;
                    return Ok(streams);
                }
                Err(e) => {
                    tracing::warn!("Reconnect attempt {} failed: {}", retry_count + 1, e);
                    retry_count += 1;
                    delay = std::cmp::min(delay * 2, Duration::from_secs(60));
                }
            }
        }

        Err(Error::websocket(format!(
            "Failed to reconnect after {} attempts",
            self.max_retries
        )))
    }

    /// Attempt a single reconnection.
    #[allow(dead_code)]
    async fn attempt_reconnect(&self) -> Result<(Option<WsStream>, Option<WsStream>)> {
        // Reconnect to public endpoint.
        let (public_socket, _) = connect_async(PUBLIC_ENDPOINT).await.map_err(|e| {
            Error::websocket(format!("Failed to reconnect to public WebSocket: {}", e))
        })?;

        let (public_sink, public_stream) = public_socket.split();
        {
            let mut sink = self.public_sink.lock().await;
            *sink = Some(public_sink);
        }

        // Reconnect to user endpoint if we have credentials.
        let user_stream = if self.credentials.is_some() {
            let (user_socket, _) = connect_async(USER_ENDPOINT).await.map_err(|e| {
                Error::websocket(format!("Failed to reconnect to user WebSocket: {}", e))
            })?;

            let (user_sink, user_stream) = user_socket.split();
            {
                let mut sink = self.user_sink.lock().await;
                *sink = Some(user_sink);
            }
            Some(user_stream)
        } else {
            None
        };

        Ok((Some(public_stream), user_stream))
    }

    /// Resubscribe to all previously subscribed channels.
    #[allow(dead_code)]
    async fn resubscribe(&self) -> Result<()> {
        // Collect channels to resubscribe to.
        let channels_to_resubscribe: Vec<Channel> = {
            let subs = self.subscriptions.lock().await;
            let mut channels = Vec::new();

            // Collect public channels.
            for (channel_name, product_ids) in &subs.public {
                if let Some(ch) = self.channel_from_name(channel_name.clone(), product_ids.clone()) {
                    channels.push(ch);
                }
            }

            // Collect user channels.
            for (channel_name, product_ids) in &subs.user {
                if let Some(ch) = self.channel_from_name(channel_name.clone(), product_ids.clone()) {
                    channels.push(ch);
                }
            }

            channels
        };

        // Now resubscribe without holding the lock.
        for channel in channels_to_resubscribe {
            self.subscribe_one(&channel).await?;
        }

        Ok(())
    }

    /// Convert a channel name and product IDs back to a Channel enum.
    #[allow(dead_code)]
    fn channel_from_name(&self, name: ChannelName, product_ids: Vec<String>) -> Option<Channel> {
        match name {
            ChannelName::Heartbeats => Some(Channel::Heartbeats),
            ChannelName::Status => Some(Channel::Status),
            ChannelName::Ticker => Some(Channel::Ticker { product_ids }),
            ChannelName::TickerBatch => Some(Channel::TickerBatch { product_ids }),
            ChannelName::Level2 => Some(Channel::Level2 { product_ids }),
            ChannelName::Candles => Some(Channel::Candles { product_ids }),
            ChannelName::MarketTrades => Some(Channel::MarketTrades { product_ids }),
            ChannelName::User => Some(Channel::User),
            ChannelName::FuturesBalanceSummary => Some(Channel::FuturesBalanceSummary),
            ChannelName::Subscriptions => None,
        }
    }

    /// Clone internal state for the message stream.
    fn clone_internal(&self) -> WebSocketClientInternal {
        WebSocketClientInternal {
            credentials: self.credentials.clone(),
            auto_reconnect: self.auto_reconnect,
            max_retries: self.max_retries,
            public_sink: self.public_sink.clone(),
            user_sink: self.user_sink.clone(),
            subscriptions: self.subscriptions.clone(),
        }
    }
}

/// Internal client state that can be cloned for the message stream.
#[derive(Clone)]
#[allow(dead_code)]
struct WebSocketClientInternal {
    credentials: Option<Credentials>,
    auto_reconnect: bool,
    max_retries: u32,
    public_sink: Arc<Mutex<Option<WsSink>>>,
    user_sink: Arc<Mutex<Option<WsSink>>>,
    subscriptions: Arc<Mutex<Subscriptions>>,
}

/// A stream of WebSocket messages.
pub struct MessageStream {
    public_stream: Option<WsStream>,
    user_stream: Option<WsStream>,
    #[allow(dead_code)]
    client: WebSocketClientInternal,
}

impl Stream for MessageStream {
    type Item = Result<Message>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // Try to get a message from the public stream.
        if let Some(ref mut stream) = self.public_stream {
            match Pin::new(stream).poll_next(cx) {
                Poll::Ready(Some(Ok(ws_msg))) => {
                    if let Some(msg) = process_ws_message(ws_msg) {
                        return Poll::Ready(Some(msg));
                    }
                }
                Poll::Ready(Some(Err(e))) => {
                    return Poll::Ready(Some(Err(Error::websocket(format!(
                        "WebSocket error: {}",
                        e
                    )))));
                }
                Poll::Ready(None) => {
                    // Stream ended.
                    self.public_stream = None;
                }
                Poll::Pending => {}
            }
        }

        // Try to get a message from the user stream.
        if let Some(ref mut stream) = self.user_stream {
            match Pin::new(stream).poll_next(cx) {
                Poll::Ready(Some(Ok(ws_msg))) => {
                    if let Some(msg) = process_ws_message(ws_msg) {
                        return Poll::Ready(Some(msg));
                    }
                }
                Poll::Ready(Some(Err(e))) => {
                    return Poll::Ready(Some(Err(Error::websocket(format!(
                        "WebSocket error: {}",
                        e
                    )))));
                }
                Poll::Ready(None) => {
                    self.user_stream = None;
                }
                Poll::Pending => {}
            }
        }

        // If both streams are gone, we're done.
        if self.public_stream.is_none() && self.user_stream.is_none() {
            return Poll::Ready(None);
        }

        Poll::Pending
    }
}

/// Process a raw WebSocket message into a typed Message.
fn process_ws_message(msg: WsMessage) -> Option<Result<Message>> {
    match msg {
        WsMessage::Text(text) => {
            let result = serde_json::from_str::<Message>(&text).map_err(|e| {
                Error::websocket(format!("Failed to parse message: {}. Raw: {}", e, text))
            });
            Some(result)
        }
        WsMessage::Close(frame) => {
            Some(Err(Error::websocket(format!(
                "WebSocket closed: {:?}",
                frame
            ))))
        }
        // Ignore ping/pong/binary frames.
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_default() {
        let client = WebSocketClient::builder().build().unwrap();
        assert!(client.credentials.is_none());
        assert!(!client.auto_reconnect);
        assert_eq!(client.max_retries, 0);
    }

    #[test]
    fn test_builder_with_auto_reconnect() {
        let client = WebSocketClient::builder()
            .auto_reconnect(true)
            .build()
            .unwrap();
        assert!(client.auto_reconnect);
        assert_eq!(client.max_retries, 10);
    }

    #[test]
    fn test_subscription_message_serialize() {
        let msg = SubscriptionMessage {
            r#type: "subscribe".to_string(),
            product_ids: vec!["BTC-USD".to_string()],
            channel: ChannelName::Ticker,
            jwt: None,
            timestamp: Some("1234567890".to_string()),
        };

        let json = serde_json::to_string(&msg).unwrap();
        assert!(json.contains("subscribe"));
        assert!(json.contains("BTC-USD"));
        assert!(json.contains("ticker"));
    }
}