nautilus-coinbase 0.56.0

Coinbase Advanced Trade integration adapter for the Nautilus trading engine
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! WebSocket message types for the Coinbase Advanced Trade API.
//!
//! All incoming messages share an envelope with `channel`, `timestamp`,
//! `sequence_num`, and a channel-specific `events` array. Outgoing
//! subscription messages use a flat format with `type`, `product_ids`,
//! `channel`, and `jwt`.

use std::collections::HashMap;

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use ustr::Ustr;

use crate::common::{
    enums::{
        CoinbaseContractExpiryType, CoinbaseMarginLevel, CoinbaseMarginWindowType,
        CoinbaseOrderSide, CoinbaseOrderStatus, CoinbaseOrderType, CoinbaseProductStatus,
        CoinbaseProductType, CoinbaseRiskManagedBy, CoinbaseTimeInForce, CoinbaseTriggerStatus,
        CoinbaseWsChannel,
    },
    parse::deserialize_decimal_from_str,
};

/// Subscribe or unsubscribe request sent to the WebSocket.
///
/// Public channels (`level2`, `market_trades`, `ticker`, etc.) do not require
/// a JWT. Set `jwt` to `None` for unauthenticated subscriptions; the field
/// is omitted from the serialized JSON.
#[derive(Debug, Clone, Serialize)]
pub struct CoinbaseWsSubscription {
    /// `"subscribe"` or `"unsubscribe"`.
    #[serde(rename = "type")]
    pub msg_type: CoinbaseWsAction,
    /// Product IDs to subscribe to (omitted for channel-level subscriptions).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub product_ids: Vec<Ustr>,
    /// Channel name (subscription-side, e.g. `level2`).
    pub channel: CoinbaseWsChannel,
    /// JWT for authentication (required for `user` and `futures_balance_summary`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jwt: Option<String>,
}

/// WebSocket subscription action type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CoinbaseWsAction {
    Subscribe,
    Unsubscribe,
}

/// Top-level WebSocket message dispatched by channel.
///
/// Uses serde internally-tagged enum on the `channel` field so each variant
/// deserializes only the events relevant to that channel.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "channel")]
pub enum CoinbaseWsMessage {
    /// Order book snapshot or incremental update.
    #[serde(rename = "l2_data")]
    L2Data {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsL2DataEvent>,
    },

    /// Market trade executions.
    #[serde(rename = "market_trades")]
    MarketTrades {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsMarketTradesEvent>,
    },

    /// Price ticker for a single product.
    #[serde(rename = "ticker")]
    Ticker {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsTickerEvent>,
    },

    /// Batched ticker updates for multiple products.
    #[serde(rename = "ticker_batch")]
    TickerBatch {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsTickerEvent>,
    },

    /// OHLC candle updates.
    #[serde(rename = "candles")]
    Candles {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsCandlesEvent>,
    },

    /// User order status updates.
    ///
    /// The feed handler deserializes this channel but ignores it until the
    /// execution client is wired.
    #[serde(rename = "user")]
    User {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsUserEvent>,
    },

    /// Connection heartbeat.
    #[serde(rename = "heartbeats")]
    Heartbeats {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsHeartbeatEvent>,
    },

    /// Futures balance summary (requires auth).
    ///
    /// The feed handler deserializes this channel but ignores it until account
    /// state handling is added.
    #[serde(rename = "futures_balance_summary")]
    FuturesBalanceSummary {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsFuturesBalanceSummaryEvent>,
    },

    /// System status updates.
    ///
    /// The feed handler deserializes this channel but ignores it until venue
    /// status handling is added.
    #[serde(rename = "status")]
    Status {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsStatusEvent>,
    },

    /// Subscription confirmation.
    #[serde(rename = "subscriptions")]
    Subscriptions {
        timestamp: String,
        sequence_num: u64,
        events: Vec<WsSubscriptionsEvent>,
    },
}

/// L2 data event containing book updates.
#[derive(Debug, Clone, Deserialize)]
pub struct WsL2DataEvent {
    /// `"snapshot"` for initial state, `"update"` for incremental.
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub product_id: Ustr,
    pub updates: Vec<WsL2Update>,
}

/// A single order book level update.
#[derive(Debug, Clone, Deserialize)]
pub struct WsL2Update {
    pub side: WsBookSide,
    pub event_time: String,
    pub price_level: String,
    pub new_quantity: String,
}

/// Book side in L2 data messages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WsBookSide {
    Bid,
    Offer,
}

/// Market trades event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsMarketTradesEvent {
    /// `"snapshot"` or `"update"`.
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub trades: Vec<WsTrade>,
}

/// A single trade from the market_trades channel.
#[derive(Debug, Clone, Deserialize)]
pub struct WsTrade {
    pub trade_id: String,
    pub product_id: Ustr,
    pub price: String,
    pub size: String,
    pub side: CoinbaseOrderSide,
    pub time: String,
}

/// Ticker event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsTickerEvent {
    /// `"snapshot"` or `"update"`.
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub tickers: Vec<WsTicker>,
}

/// Ticker data for a single product.
#[derive(Debug, Clone, Deserialize)]
pub struct WsTicker {
    pub product_id: Ustr,
    pub price: String,
    pub volume_24_h: String,
    pub low_24_h: String,
    pub high_24_h: String,
    #[serde(default)]
    pub low_52_w: String,
    #[serde(default)]
    pub high_52_w: String,
    pub price_percent_chg_24_h: String,
    pub best_bid: String,
    pub best_bid_quantity: String,
    pub best_ask: String,
    pub best_ask_quantity: String,
}

/// Candles event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsCandlesEvent {
    /// `"snapshot"` or `"update"`.
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub candles: Vec<WsCandle>,
}

/// A single candle from the candles channel.
#[derive(Debug, Clone, Deserialize)]
pub struct WsCandle {
    pub start: String,
    pub high: String,
    pub low: String,
    pub open: String,
    pub close: String,
    pub volume: String,
    pub product_id: Ustr,
}

/// User event containing order status updates.
#[derive(Debug, Clone, Deserialize)]
pub struct WsUserEvent {
    /// `"snapshot"` or `"update"`.
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub orders: Vec<WsOrderUpdate>,
}

/// Order status update from the user channel.
#[derive(Debug, Clone, Deserialize)]
pub struct WsOrderUpdate {
    pub order_id: String,
    pub client_order_id: String,
    pub contract_expiry_type: CoinbaseContractExpiryType,
    pub cumulative_quantity: String,
    pub leaves_quantity: String,
    pub avg_price: String,
    pub total_fees: String,
    pub status: CoinbaseOrderStatus,
    pub product_id: Ustr,
    pub product_type: CoinbaseProductType,
    pub creation_time: String,
    pub order_side: CoinbaseOrderSide,
    pub order_type: CoinbaseOrderType,
    pub risk_managed_by: CoinbaseRiskManagedBy,
    pub time_in_force: CoinbaseTimeInForce,
    pub trigger_status: CoinbaseTriggerStatus,
    #[serde(default)]
    pub cancel_reason: String,
    #[serde(default)]
    pub reject_reason: String,
    #[serde(default)]
    pub total_value_after_fees: String,
}

/// Heartbeat event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsHeartbeatEvent {
    pub current_time: String,
    pub heartbeat_counter: u64,
}

/// Futures balance summary event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsFuturesBalanceSummaryEvent {
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    pub fcm_balance_summary: WsFcmBalanceSummary,
}

/// Futures balance summary snapshot.
#[derive(Debug, Clone, Deserialize)]
pub struct WsFcmBalanceSummary {
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub futures_buying_power: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub total_usd_balance: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub cbi_usd_balance: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub cfm_usd_balance: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub total_open_orders_hold_amount: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub unrealized_pnl: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub daily_realized_pnl: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub initial_margin: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub available_margin: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub liquidation_threshold: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub liquidation_buffer_amount: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub liquidation_buffer_percentage: Decimal,
    pub intraday_margin_window_measure: WsMarginWindowMeasure,
    pub overnight_margin_window_measure: WsMarginWindowMeasure,
}

/// Margin window summary inside a futures balance snapshot.
#[derive(Debug, Clone, Deserialize)]
pub struct WsMarginWindowMeasure {
    pub margin_window_type: CoinbaseMarginWindowType,
    pub margin_level: CoinbaseMarginLevel,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub initial_margin: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub maintenance_margin: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub liquidation_buffer_percentage: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub total_hold: Decimal,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub futures_buying_power: Decimal,
}

/// Status channel event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsStatusEvent {
    #[serde(rename = "type")]
    pub event_type: WsEventType,
    #[serde(default)]
    pub products: Vec<WsStatusProduct>,
}

/// Status channel product snapshot.
#[derive(Debug, Clone, Deserialize)]
pub struct WsStatusProduct {
    pub product_type: CoinbaseProductType,
    pub id: Ustr,
    pub base_currency: Ustr,
    pub quote_currency: Ustr,
    pub base_increment: String,
    pub quote_increment: String,
    pub display_name: String,
    pub status: CoinbaseProductStatus,
    pub status_message: String,
    #[serde(deserialize_with = "deserialize_decimal_from_str")]
    pub min_market_funds: Decimal,
}

/// Subscription confirmation event.
#[derive(Debug, Clone, Deserialize)]
pub struct WsSubscriptionsEvent {
    pub subscriptions: HashMap<CoinbaseWsChannel, Vec<Ustr>>,
}

/// Event type discriminator for snapshot vs incremental update.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WsEventType {
    Snapshot,
    Update,
}

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

    use super::*;
    use crate::common::testing::load_test_fixture;

    #[rstest]
    fn test_deserialize_l2_snapshot() {
        let json = load_test_fixture("ws_l2_data_snapshot.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::L2Data {
                timestamp,
                sequence_num,
                events,
            } => {
                assert!(!timestamp.is_empty());
                assert_eq!(sequence_num, 0);
                assert_eq!(events.len(), 1);

                let event = &events[0];
                assert_eq!(event.event_type, WsEventType::Snapshot);
                assert_eq!(event.product_id, "BTC-USD");
                assert!(!event.updates.is_empty());

                let bid = event
                    .updates
                    .iter()
                    .find(|u| u.side == WsBookSide::Bid)
                    .expect("should have a bid update");
                assert!(!bid.price_level.is_empty());
                assert!(!bid.new_quantity.is_empty());
            }
            other => panic!("Expected L2Data, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_l2_update() {
        let json = load_test_fixture("ws_l2_data_update.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::L2Data {
                sequence_num,
                events,
                ..
            } => {
                assert!(sequence_num > 0);
                assert_eq!(events[0].event_type, WsEventType::Update);
            }
            other => panic!("Expected L2Data, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_market_trades() {
        let json = load_test_fixture("ws_market_trades.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::MarketTrades { events, .. } => {
                assert_eq!(events.len(), 1);
                assert!(!events[0].trades.is_empty());

                let trade = &events[0].trades[0];
                assert_eq!(trade.product_id, "BTC-USD");
                assert!(!trade.price.is_empty());
                assert!(!trade.size.is_empty());
                assert!(!trade.trade_id.is_empty());
            }
            other => panic!("Expected MarketTrades, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_ticker() {
        let json = load_test_fixture("ws_ticker.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::Ticker { events, .. } => {
                assert_eq!(events.len(), 1);
                assert!(!events[0].tickers.is_empty());

                let ticker = &events[0].tickers[0];
                assert_eq!(ticker.product_id, "BTC-USD");
                assert!(!ticker.best_bid.is_empty());
                assert!(!ticker.best_ask.is_empty());
                assert!(!ticker.best_bid_quantity.is_empty());
                assert!(!ticker.best_ask_quantity.is_empty());
            }
            other => panic!("Expected Ticker, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_candles() {
        let json = load_test_fixture("ws_candles.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::Candles { events, .. } => {
                assert_eq!(events.len(), 1);
                assert!(!events[0].candles.is_empty());

                let candle = &events[0].candles[0];
                assert_eq!(candle.product_id, "BTC-USD");
                assert!(!candle.open.is_empty());
                assert!(!candle.high.is_empty());
                assert!(!candle.low.is_empty());
                assert!(!candle.close.is_empty());
                assert!(!candle.volume.is_empty());
            }
            other => panic!("Expected Candles, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_user_order_update() {
        let json = load_test_fixture("ws_user.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::User { events, .. } => {
                assert_eq!(events.len(), 1);
                assert!(!events[0].orders.is_empty());

                let order = &events[0].orders[0];
                assert!(!order.order_id.is_empty());
                assert_eq!(order.product_id, "BTC-USD");
                assert_eq!(order.status, CoinbaseOrderStatus::Open);
                assert_eq!(order.order_side, CoinbaseOrderSide::Buy);
                assert_eq!(order.order_type, CoinbaseOrderType::Limit);
                assert_eq!(
                    order.contract_expiry_type,
                    CoinbaseContractExpiryType::Unknown
                );
                assert_eq!(order.product_type, CoinbaseProductType::Spot);
                assert_eq!(order.risk_managed_by, CoinbaseRiskManagedBy::Unknown);
                assert_eq!(order.time_in_force, CoinbaseTimeInForce::GoodUntilCancelled);
                assert_eq!(
                    order.trigger_status,
                    CoinbaseTriggerStatus::InvalidOrderType
                );
            }
            other => panic!("Expected User, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_heartbeat() {
        let json = load_test_fixture("ws_heartbeats.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::Heartbeats { events, .. } => {
                assert_eq!(events.len(), 1);
                assert!(!events[0].current_time.is_empty());
                assert!(events[0].heartbeat_counter > 0);
            }
            other => panic!("Expected Heartbeats, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_status_channel() {
        let json = r#"{
          "channel": "status",
          "client_id": "",
          "timestamp": "2023-02-09T20:29:49.753424311Z",
          "sequence_num": 0,
          "events": [
            {
              "type": "snapshot",
              "products": [
                {
                  "product_type": "SPOT",
                  "id": "BTC-USD",
                  "base_currency": "BTC",
                  "quote_currency": "USD",
                  "base_increment": "0.00000001",
                  "quote_increment": "0.01",
                  "display_name": "BTC/USD",
                  "status": "online",
                  "status_message": "",
                  "min_market_funds": "1"
                }
              ]
            }
          ]
        }"#;
        let msg: CoinbaseWsMessage = serde_json::from_str(json).unwrap();

        match msg {
            CoinbaseWsMessage::Status { events, .. } => {
                assert_eq!(events.len(), 1);
                assert_eq!(events[0].event_type, WsEventType::Snapshot);
                assert_eq!(events[0].products.len(), 1);
                let product = &events[0].products[0];
                assert_eq!(product.id, "BTC-USD");
                assert_eq!(product.product_type, CoinbaseProductType::Spot);
                assert_eq!(product.status, CoinbaseProductStatus::Online);
                assert_eq!(product.min_market_funds, Decimal::ONE);
            }
            other => panic!("Expected Status, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_futures_balance_summary_channel() {
        let json = r#"{
          "channel": "futures_balance_summary",
          "client_id": "",
          "timestamp": "2023-02-09T20:33:57.609931463Z",
          "sequence_num": 0,
          "events": [
            {
              "type": "snapshot",
              "fcm_balance_summary": {
                "futures_buying_power": "100.00",
                "total_usd_balance": "200.00",
                "cbi_usd_balance": "300.00",
                "cfm_usd_balance": "400.00",
                "total_open_orders_hold_amount": "500.00",
                "unrealized_pnl": "600.00",
                "daily_realized_pnl": "0",
                "initial_margin": "700.00",
                "available_margin": "800.00",
                "liquidation_threshold": "900.00",
                "liquidation_buffer_amount": "1000.00",
                "liquidation_buffer_percentage": "1000",
                "intraday_margin_window_measure": {
                  "margin_window_type": "FCM_MARGIN_WINDOW_TYPE_INTRADAY",
                  "margin_level": "MARGIN_LEVEL_TYPE_BASE",
                  "initial_margin": "100.00",
                  "maintenance_margin": "200.00",
                  "liquidation_buffer_percentage": "1000",
                  "total_hold": "100.00",
                  "futures_buying_power": "400.00"
                },
                "overnight_margin_window_measure": {
                  "margin_window_type": "FCM_MARGIN_WINDOW_TYPE_OVERNIGHT",
                  "margin_level": "MARGIN_LEVEL_TYPE_BASE",
                  "initial_margin": "300.00",
                  "maintenance_margin": "200.00",
                  "liquidation_buffer_percentage": "1000",
                  "total_hold": "-30.00",
                  "futures_buying_power": "2000.00"
                }
              }
            }
          ]
        }"#;
        let msg: CoinbaseWsMessage = serde_json::from_str(json).unwrap();

        match msg {
            CoinbaseWsMessage::FuturesBalanceSummary { events, .. } => {
                assert_eq!(events.len(), 1);
                assert_eq!(events[0].event_type, WsEventType::Snapshot);
                let summary = &events[0].fcm_balance_summary;
                assert_eq!(summary.futures_buying_power, Decimal::from(100));
                assert_eq!(summary.daily_realized_pnl, Decimal::ZERO);
                assert_eq!(
                    summary.intraday_margin_window_measure.margin_window_type,
                    CoinbaseMarginWindowType::Intraday
                );
                assert_eq!(
                    summary.overnight_margin_window_measure.margin_level,
                    CoinbaseMarginLevel::Base
                );
                assert_eq!(
                    summary.overnight_margin_window_measure.total_hold,
                    "-30.00".parse::<Decimal>().unwrap()
                );
            }
            other => panic!("Expected FuturesBalanceSummary, was {other:?}"),
        }
    }

    #[rstest]
    fn test_deserialize_subscriptions() {
        let json = load_test_fixture("ws_subscriptions.json");
        let msg: CoinbaseWsMessage = serde_json::from_str(&json).unwrap();

        match msg {
            CoinbaseWsMessage::Subscriptions { events, .. } => {
                assert_eq!(events.len(), 1);
                assert_eq!(
                    events[0].subscriptions.get(&CoinbaseWsChannel::Level2),
                    Some(&vec![Ustr::from("BTC-USD")])
                );
                assert_eq!(
                    events[0]
                        .subscriptions
                        .get(&CoinbaseWsChannel::MarketTrades),
                    Some(&vec![Ustr::from("BTC-USD"), Ustr::from("ETH-USD")])
                );
            }
            other => panic!("Expected Subscriptions, was {other:?}"),
        }
    }

    #[rstest]
    fn test_serialize_subscribe_request_with_jwt() {
        let sub = CoinbaseWsSubscription {
            msg_type: CoinbaseWsAction::Subscribe,
            product_ids: vec![Ustr::from("BTC-USD")],
            channel: CoinbaseWsChannel::User,
            jwt: Some("test-jwt-token".to_string()),
        };

        let json = serde_json::to_value(&sub).unwrap();
        assert_eq!(json["type"], "subscribe");
        assert_eq!(json["channel"], "user");
        assert_eq!(json["product_ids"][0], "BTC-USD");
        assert_eq!(json["jwt"], "test-jwt-token");
    }

    #[rstest]
    fn test_serialize_subscribe_request_public_omits_jwt() {
        let sub = CoinbaseWsSubscription {
            msg_type: CoinbaseWsAction::Subscribe,
            product_ids: vec![Ustr::from("BTC-USD")],
            channel: CoinbaseWsChannel::Level2,
            jwt: None,
        };

        let json = serde_json::to_value(&sub).unwrap();
        assert_eq!(json["type"], "subscribe");
        assert_eq!(json["channel"], "level2");
        assert!(json.get("jwt").is_none());
    }

    #[rstest]
    fn test_serialize_unsubscribe_request() {
        let sub = CoinbaseWsSubscription {
            msg_type: CoinbaseWsAction::Unsubscribe,
            product_ids: vec![Ustr::from("ETH-USD")],
            channel: CoinbaseWsChannel::MarketTrades,
            jwt: None,
        };

        let json = serde_json::to_value(&sub).unwrap();
        assert_eq!(json["type"], "unsubscribe");
        assert_eq!(json["channel"], "market_trades");
        assert!(json.get("jwt").is_none());
    }

    #[rstest]
    fn test_serialize_channel_level_subscription_omits_product_ids() {
        let sub = CoinbaseWsSubscription {
            msg_type: CoinbaseWsAction::Subscribe,
            product_ids: vec![],
            channel: CoinbaseWsChannel::Heartbeats,
            jwt: None,
        };

        let json = serde_json::to_value(&sub).unwrap();
        assert_eq!(json["type"], "subscribe");
        assert_eq!(json["channel"], "heartbeats");
        assert!(json.get("product_ids").is_none());
        assert!(json.get("jwt").is_none());
    }

    #[rstest]
    fn test_ws_event_type_values() {
        let snapshot: WsEventType = serde_json::from_str("\"snapshot\"").unwrap();
        assert_eq!(snapshot, WsEventType::Snapshot);

        let update: WsEventType = serde_json::from_str("\"update\"").unwrap();
        assert_eq!(update, WsEventType::Update);
    }

    #[rstest]
    fn test_ws_book_side_values() {
        let bid: WsBookSide = serde_json::from_str("\"bid\"").unwrap();
        assert_eq!(bid, WsBookSide::Bid);

        let offer: WsBookSide = serde_json::from_str("\"offer\"").unwrap();
        assert_eq!(offer, WsBookSide::Offer);
    }
}