kraken-types 0.1.0

Shared types for Kraken WebSocket API v2
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
//! Request and response message types for Kraken WebSocket API v2

use crate::{Channel, Depth, Level, OhlcInterval, Side, SystemStatus, TickerTrigger};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

// ============================================================================
// Request Types
// ============================================================================

/// Subscribe request message
#[derive(Debug, Clone, Serialize)]
pub struct SubscribeRequest {
    /// Always "subscribe"
    pub method: &'static str,
    /// Subscription parameters
    pub params: SubscribeParams,
    /// Optional request ID (echoed in response)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

impl SubscribeRequest {
    /// Create a new subscribe request
    pub fn new(params: SubscribeParams) -> Self {
        Self {
            method: "subscribe",
            params,
            req_id: None,
        }
    }

    /// Add a request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Subscription parameters
#[derive(Debug, Clone, Serialize)]
pub struct SubscribeParams {
    /// Channel to subscribe to
    pub channel: Channel,
    /// Symbols to subscribe to
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub symbol: Vec<String>,
    /// Orderbook depth (book channel only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub depth: Option<u32>,
    /// Whether to receive initial snapshot
    #[serde(skip_serializing_if = "Option::is_none")]
    pub snapshot: Option<bool>,
    /// OHLC interval (ohlc channel only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interval: Option<u32>,
    /// Ticker event trigger
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_trigger: Option<TickerTrigger>,
    /// Authentication token (private channels)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,
}

impl SubscribeParams {
    /// Create book subscription params
    pub fn book(symbols: Vec<String>, depth: Depth) -> Self {
        Self {
            channel: Channel::Book,
            symbol: symbols,
            depth: Some(depth.as_u32()),
            snapshot: Some(true),
            interval: None,
            event_trigger: None,
            token: None,
        }
    }

    /// Create ticker subscription params
    pub fn ticker(symbols: Vec<String>) -> Self {
        Self {
            channel: Channel::Ticker,
            symbol: symbols,
            depth: None,
            snapshot: None,
            interval: None,
            event_trigger: Some(TickerTrigger::Trades),
            token: None,
        }
    }

    /// Create trade subscription params
    pub fn trade(symbols: Vec<String>) -> Self {
        Self {
            channel: Channel::Trade,
            symbol: symbols,
            depth: None,
            snapshot: Some(true),
            interval: None,
            event_trigger: None,
            token: None,
        }
    }

    /// Create OHLC subscription params
    pub fn ohlc(symbols: Vec<String>, interval: OhlcInterval) -> Self {
        Self {
            channel: Channel::Ohlc,
            symbol: symbols,
            depth: None,
            snapshot: None,
            interval: Some(interval as u32),
            event_trigger: None,
            token: None,
        }
    }

    /// Create executions subscription params (private channel)
    ///
    /// Requires a valid WebSocket token from the TokenManager.
    pub fn executions(token: String) -> Self {
        Self {
            channel: Channel::Executions,
            symbol: vec![],
            depth: None,
            snapshot: Some(true),
            interval: None,
            event_trigger: None,
            token: Some(token),
        }
    }

    /// Create balances subscription params (private channel)
    ///
    /// Requires a valid WebSocket token from the TokenManager.
    pub fn balances(token: String) -> Self {
        Self {
            channel: Channel::Balances,
            symbol: vec![],
            depth: None,
            snapshot: Some(true),
            interval: None,
            event_trigger: None,
            token: Some(token),
        }
    }

    /// Set the authentication token for private channels
    pub fn with_token(mut self, token: String) -> Self {
        self.token = Some(token);
        self
    }
}

/// Unsubscribe request message
#[derive(Debug, Clone, Serialize)]
pub struct UnsubscribeRequest {
    /// Always "unsubscribe"
    pub method: &'static str,
    /// Unsubscription parameters
    pub params: SubscribeParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

impl UnsubscribeRequest {
    /// Create a new unsubscribe request
    pub fn new(params: SubscribeParams) -> Self {
        Self {
            method: "unsubscribe",
            params,
            req_id: None,
        }
    }
}

/// Ping request for keepalive
#[derive(Debug, Clone, Serialize)]
pub struct PingRequest {
    /// Always "ping"
    pub method: &'static str,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

impl PingRequest {
    /// Create a new ping request
    pub fn new() -> Self {
        Self {
            method: "ping",
            req_id: None,
        }
    }

    /// Add a request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

impl Default for PingRequest {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Response Types
// ============================================================================

/// Subscribe/Unsubscribe response
#[derive(Debug, Clone, Deserialize)]
pub struct MethodResponse {
    /// Method name (subscribe, unsubscribe, pong)
    pub method: String,
    /// Result details
    pub result: Option<SubscribeResult>,
    /// Whether the operation succeeded
    pub success: bool,
    /// Request processing start time
    pub time_in: String,
    /// Request processing end time
    pub time_out: String,
    /// Echoed request ID
    #[serde(default)]
    pub req_id: Option<u64>,
    /// Error message if failed
    #[serde(default)]
    pub error: Option<String>,
}

/// Subscription result details
#[derive(Debug, Clone, Deserialize)]
pub struct SubscribeResult {
    /// Channel name
    pub channel: String,
    /// Symbol (for symbol-specific subscriptions)
    #[serde(default)]
    pub symbol: Option<String>,
    /// Depth (for book subscriptions)
    #[serde(default)]
    pub depth: Option<u32>,
    /// Snapshot flag
    #[serde(default)]
    pub snapshot: Option<bool>,
}

// ============================================================================
// Channel Data Types
// ============================================================================

/// Generic channel message wrapper
#[derive(Debug, Clone, Deserialize)]
pub struct ChannelMessage<T> {
    /// Channel name
    pub channel: String,
    /// Message type: "snapshot" or "update"
    #[serde(rename = "type")]
    pub msg_type: String,
    /// Channel-specific data
    pub data: Vec<T>,
}

/// Status channel data (sent on connection)
#[derive(Debug, Clone, Deserialize)]
pub struct StatusData {
    /// API version
    pub api_version: String,
    /// Unique connection ID
    pub connection_id: u64,
    /// System status
    pub system: SystemStatus,
    /// Server version
    pub version: String,
}

/// Book snapshot/update data
#[derive(Debug, Clone, Deserialize)]
pub struct BookData {
    /// Trading pair symbol
    pub symbol: String,
    /// Bid levels
    pub bids: Vec<Level>,
    /// Ask levels
    pub asks: Vec<Level>,
    /// CRC32 checksum
    pub checksum: u32,
    /// Update timestamp (updates only)
    #[serde(default)]
    pub timestamp: Option<String>,
}

/// Ticker data
#[derive(Debug, Clone, Deserialize)]
pub struct TickerData {
    /// Trading pair symbol
    pub symbol: String,
    /// Best bid price
    pub bid: Decimal,
    /// Best bid quantity
    pub bid_qty: Decimal,
    /// Best ask price
    pub ask: Decimal,
    /// Best ask quantity
    pub ask_qty: Decimal,
    /// Last trade price
    pub last: Decimal,
    /// 24h volume
    pub volume: Decimal,
    /// 24h VWAP
    pub vwap: Decimal,
    /// 24h low
    pub low: Decimal,
    /// 24h high
    pub high: Decimal,
    /// 24h price change
    pub change: Decimal,
    /// 24h price change percentage
    pub change_pct: Decimal,
}

/// Trade data
#[derive(Debug, Clone, Deserialize)]
pub struct TradeData {
    /// Trading pair symbol
    pub symbol: String,
    /// Trade side
    pub side: Side,
    /// Trade price
    pub price: Decimal,
    /// Trade quantity
    pub qty: Decimal,
    /// Order type
    pub ord_type: String,
    /// Unique trade ID
    pub trade_id: u64,
    /// Trade timestamp
    pub timestamp: String,
}

/// OHLC candle data
#[derive(Debug, Clone, Deserialize)]
pub struct OhlcData {
    /// Trading pair symbol
    pub symbol: String,
    /// Open price
    pub open: Decimal,
    /// High price
    pub high: Decimal,
    /// Low price
    pub low: Decimal,
    /// Close price
    pub close: Decimal,
    /// VWAP
    pub vwap: Decimal,
    /// Volume
    pub volume: Decimal,
    /// Number of trades
    pub trades: u64,
    /// Interval start time
    pub interval_begin: String,
    /// Interval in minutes
    pub interval: u32,
}

/// Asset data from the instrument channel
#[derive(Debug, Clone, Deserialize)]
pub struct InstrumentAsset {
    /// Asset identifier (e.g., "BTC", "USD")
    pub id: String,
    /// Asset status
    #[serde(default)]
    pub status: Option<String>,
    /// Precision (number of decimal places)
    #[serde(default)]
    pub precision: Option<u8>,
    /// Display precision
    #[serde(default)]
    pub precision_display: Option<u8>,
    /// Whether asset is borrowable
    #[serde(default)]
    pub borrowable: Option<bool>,
    /// Collateral value
    #[serde(default)]
    pub collateral_value: Option<Decimal>,
}

/// Trading pair data from the instrument channel
/// Used to get price and quantity precision for checksum calculation
#[derive(Debug, Clone, Deserialize)]
pub struct InstrumentPair {
    /// Trading pair symbol (e.g., "BTC/USD")
    pub symbol: String,
    /// Price precision (number of decimal places)
    pub price_precision: u8,
    /// Quantity precision (number of decimal places)
    pub qty_precision: u8,
    /// Price increment (minimum price step)
    #[serde(default)]
    pub price_increment: Option<Decimal>,
    /// Quantity increment (minimum qty step)
    #[serde(default)]
    pub qty_increment: Option<Decimal>,
    /// Minimum order quantity
    #[serde(default)]
    pub qty_min: Option<Decimal>,
    /// Base asset
    #[serde(default)]
    pub base: Option<String>,
    /// Quote currency
    #[serde(default)]
    pub quote: Option<String>,
    /// Trading status
    #[serde(default)]
    pub status: Option<String>,
}

/// Instrument channel data structure
/// Contains both assets and trading pairs information
#[derive(Debug, Clone, Deserialize)]
pub struct InstrumentChannelData {
    /// List of assets
    #[serde(default)]
    pub assets: Vec<InstrumentAsset>,
    /// List of trading pairs
    #[serde(default)]
    pub pairs: Vec<InstrumentPair>,
}

/// Instrument channel message (has different structure than other channels)
#[derive(Debug, Clone, Deserialize)]
pub struct InstrumentMessage {
    /// Channel name
    pub channel: String,
    /// Message type: "snapshot" or "update"
    #[serde(rename = "type")]
    pub msg_type: String,
    /// Instrument data containing assets and pairs
    pub data: InstrumentChannelData,
}

/// Alias for backwards compatibility
pub type InstrumentData = InstrumentPair;

// ============================================================================
// Private Channel Data Types
// ============================================================================

/// Execution/trade data from the executions channel (private)
#[derive(Debug, Clone, Deserialize)]
pub struct ExecutionData {
    /// Execution type (e.g., "trade", "settled")
    #[serde(rename = "exec_type")]
    pub exec_type: String,
    /// Order ID
    pub order_id: String,
    /// Execution ID
    #[serde(default)]
    pub exec_id: Option<String>,
    /// Trade ID (if applicable)
    #[serde(default)]
    pub trade_id: Option<u64>,
    /// Trading pair symbol
    pub symbol: String,
    /// Order side
    pub side: Side,
    /// Order type
    pub order_type: String,
    /// Order quantity
    #[serde(default)]
    pub order_qty: Option<Decimal>,
    /// Limit price
    #[serde(default)]
    pub limit_price: Option<Decimal>,
    /// Last executed quantity
    #[serde(default)]
    pub last_qty: Option<Decimal>,
    /// Last executed price
    #[serde(default)]
    pub last_price: Option<Decimal>,
    /// Cumulative quantity filled
    #[serde(default)]
    pub cum_qty: Option<Decimal>,
    /// Average fill price
    #[serde(default)]
    pub avg_price: Option<Decimal>,
    /// Fee paid
    #[serde(default)]
    pub fee_paid: Option<Decimal>,
    /// Fee currency
    #[serde(default)]
    pub fee_currency: Option<String>,
    /// Order status
    #[serde(default)]
    pub order_status: Option<String>,
    /// Timestamp
    pub timestamp: String,
}

/// Balance data from the balances channel (private)
#[derive(Debug, Clone, Deserialize)]
pub struct BalanceData {
    /// Asset identifier (e.g., "BTC", "USD")
    pub asset: String,
    /// Available balance (for trading/withdrawal)
    pub balance: Decimal,
    /// Balance on hold (in open orders)
    #[serde(default)]
    pub hold_trade: Option<Decimal>,
}

/// Wallet balance snapshot
#[derive(Debug, Clone, Deserialize)]
pub struct WalletData {
    /// List of asset balances
    pub balances: Vec<BalanceData>,
    /// Wallet type
    #[serde(default)]
    pub wallet_type: Option<String>,
    /// Wallet ID
    #[serde(default)]
    pub wallet_id: Option<String>,
}

// ============================================================================
// Level 3 (L3) Orders Channel Data Types
// ============================================================================

/// L3 order event type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum L3EventType {
    /// New order added to book
    Add,
    /// Order modified
    Modify,
    /// Order deleted from book
    Delete,
}

/// Individual L3 order entry
#[derive(Debug, Clone, Deserialize)]
pub struct L3Order {
    /// Unique order ID
    pub order_id: String,
    /// Limit price
    pub limit_price: Decimal,
    /// Order quantity
    pub order_qty: Decimal,
    /// Timestamp
    pub timestamp: String,
    /// Event type (add, modify, delete)
    #[serde(default)]
    pub event: Option<L3EventType>,
}

/// L3 channel data for a symbol
#[derive(Debug, Clone, Deserialize)]
pub struct L3Data {
    /// Trading pair symbol
    pub symbol: String,
    /// Bid orders
    #[serde(default)]
    pub bids: Vec<L3Order>,
    /// Ask orders
    #[serde(default)]
    pub asks: Vec<L3Order>,
    /// CRC32 checksum (for verification)
    #[serde(default)]
    pub checksum: Option<u32>,
}

/// L3 message type
pub type L3Message = ChannelMessage<L3Data>;

// ============================================================================
// WebSocket Trading Request Types
// ============================================================================

/// Time-in-force for orders
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[allow(clippy::upper_case_acronyms)]
pub enum TimeInForce {
    /// Good til canceled
    #[serde(rename = "gtc")]
    GTC,
    /// Immediate or cancel
    #[serde(rename = "ioc")]
    IOC,
    /// Good til date
    #[serde(rename = "gtd")]
    GTD,
}

/// Add order request via WebSocket
#[derive(Debug, Clone, Serialize)]
pub struct AddOrderRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: AddOrderParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Add order parameters
#[derive(Debug, Clone, Serialize)]
pub struct AddOrderParams {
    /// Order type (market, limit, etc.)
    pub order_type: String,
    /// Order side (buy/sell)
    pub side: Side,
    /// Trading pair symbol
    pub symbol: String,
    /// Order quantity
    #[serde(serialize_with = "serialize_decimal")]
    pub order_qty: Decimal,
    /// Limit price (required for limit orders)
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub limit_price: Option<Decimal>,
    /// Time in force
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_in_force: Option<TimeInForce>,
    /// Stop/trigger price
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub trigger_price: Option<Decimal>,
    /// Client order ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
    /// Post-only flag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post_only: Option<bool>,
    /// Reduce-only flag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reduce_only: Option<bool>,
    /// WebSocket authentication token
    pub token: String,
}

impl AddOrderRequest {
    /// Create a new add order request
    pub fn new(params: AddOrderParams) -> Self {
        Self {
            method: "add_order",
            params,
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Amend order request
#[derive(Debug, Clone, Serialize)]
pub struct AmendOrderRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: AmendOrderParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Amend order parameters
#[derive(Debug, Clone, Serialize)]
pub struct AmendOrderParams {
    /// Order ID to amend
    pub order_id: String,
    /// New limit price
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub limit_price: Option<Decimal>,
    /// New trigger price
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub trigger_price: Option<Decimal>,
    /// New order quantity
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub order_qty: Option<Decimal>,
    /// Post-only flag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub post_only: Option<bool>,
    /// WebSocket authentication token
    pub token: String,
}

impl AmendOrderRequest {
    /// Create a new amend order request
    pub fn new(params: AmendOrderParams) -> Self {
        Self {
            method: "amend_order",
            params,
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Cancel order request
#[derive(Debug, Clone, Serialize)]
pub struct CancelOrderRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: CancelOrderParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Cancel order parameters
#[derive(Debug, Clone, Serialize)]
pub struct CancelOrderParams {
    /// Order IDs to cancel
    pub order_id: Vec<String>,
    /// Client order IDs to cancel
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<Vec<String>>,
    /// WebSocket authentication token
    pub token: String,
}

impl CancelOrderRequest {
    /// Create a new cancel order request
    pub fn new(params: CancelOrderParams) -> Self {
        Self {
            method: "cancel_order",
            params,
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Cancel all orders request
#[derive(Debug, Clone, Serialize)]
pub struct CancelAllRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: CancelAllParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Cancel all orders parameters
#[derive(Debug, Clone, Serialize)]
pub struct CancelAllParams {
    /// WebSocket authentication token
    pub token: String,
}

impl CancelAllRequest {
    /// Create a new cancel all request
    pub fn new(token: String) -> Self {
        Self {
            method: "cancel_all",
            params: CancelAllParams { token },
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Cancel all orders on disconnect request
#[derive(Debug, Clone, Serialize)]
pub struct CancelOnDisconnectRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: CancelOnDisconnectParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Cancel on disconnect parameters
#[derive(Debug, Clone, Serialize)]
pub struct CancelOnDisconnectParams {
    /// Timeout in seconds (0 to disable)
    pub timeout: u32,
    /// WebSocket authentication token
    pub token: String,
}

impl CancelOnDisconnectRequest {
    /// Create a new cancel on disconnect request
    pub fn new(timeout: u32, token: String) -> Self {
        Self {
            method: "cancel_all_orders_after",
            params: CancelOnDisconnectParams { timeout, token },
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Batch add orders request
#[derive(Debug, Clone, Serialize)]
pub struct BatchAddRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: BatchAddParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Batch add parameters
#[derive(Debug, Clone, Serialize)]
pub struct BatchAddParams {
    /// List of orders to add
    pub orders: Vec<BatchOrder>,
    /// WebSocket authentication token
    pub token: String,
    /// Deadline for execution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deadline: Option<String>,
    /// Validate only, don't execute
    #[serde(skip_serializing_if = "Option::is_none")]
    pub validate: Option<bool>,
}

/// Individual order in batch
#[derive(Debug, Clone, Serialize)]
pub struct BatchOrder {
    /// Order type
    pub order_type: String,
    /// Side
    pub side: Side,
    /// Symbol
    pub symbol: String,
    /// Quantity
    #[serde(serialize_with = "serialize_decimal")]
    pub order_qty: Decimal,
    /// Limit price
    #[serde(skip_serializing_if = "Option::is_none", serialize_with = "serialize_option_decimal")]
    pub limit_price: Option<Decimal>,
    /// Client order ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<String>,
}

impl BatchAddRequest {
    /// Create a new batch add request
    pub fn new(params: BatchAddParams) -> Self {
        Self {
            method: "batch_add",
            params,
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

/// Batch cancel orders request
#[derive(Debug, Clone, Serialize)]
pub struct BatchCancelRequest {
    /// Method name
    pub method: &'static str,
    /// Request parameters
    pub params: BatchCancelParams,
    /// Optional request ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub req_id: Option<u64>,
}

/// Batch cancel parameters
#[derive(Debug, Clone, Serialize)]
pub struct BatchCancelParams {
    /// Order IDs to cancel
    pub orders: Vec<String>,
    /// Client order IDs to cancel
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cl_ord_id: Option<Vec<String>>,
    /// WebSocket authentication token
    pub token: String,
}

impl BatchCancelRequest {
    /// Create a new batch cancel request
    pub fn new(params: BatchCancelParams) -> Self {
        Self {
            method: "batch_cancel",
            params,
            req_id: None,
        }
    }

    /// Set request ID
    pub fn with_req_id(mut self, id: u64) -> Self {
        self.req_id = Some(id);
        self
    }
}

// ============================================================================
// Trading Response Types
// ============================================================================

/// Add order response result
#[derive(Debug, Clone, Deserialize)]
pub struct AddOrderResult {
    /// Assigned order ID
    pub order_id: String,
    /// Client order ID (if provided)
    #[serde(default)]
    pub cl_ord_id: Option<String>,
    /// Order status
    #[serde(default)]
    pub order_status: Option<String>,
    /// Order creation timestamp
    #[serde(default)]
    pub timestamp: Option<String>,
}

/// Cancel order response result
#[derive(Debug, Clone, Deserialize)]
pub struct CancelOrderResult {
    /// Order ID that was canceled
    pub order_id: String,
    /// Client order ID
    #[serde(default)]
    pub cl_ord_id: Option<String>,
}

/// Cancel all response result
#[derive(Debug, Clone, Deserialize)]
pub struct CancelAllResult {
    /// Number of orders canceled
    pub count: u32,
}

/// Cancel on disconnect response result
#[derive(Debug, Clone, Deserialize)]
pub struct CancelOnDisconnectResult {
    /// Current timeout setting
    #[serde(default)]
    pub current_time: Option<String>,
    /// Trigger time (when orders will be canceled)
    #[serde(default)]
    pub trigger_time: Option<String>,
}

/// Batch order response
#[derive(Debug, Clone, Deserialize)]
pub struct BatchOrderResult {
    /// Results for each order in the batch
    #[serde(default)]
    pub orders: Vec<BatchOrderResultItem>,
}

/// Individual batch order result
#[derive(Debug, Clone, Deserialize)]
pub struct BatchOrderResultItem {
    /// Order ID
    #[serde(default)]
    pub order_id: Option<String>,
    /// Client order ID
    #[serde(default)]
    pub cl_ord_id: Option<String>,
    /// Error message if failed
    #[serde(default)]
    pub error: Option<String>,
}

/// Helper function to serialize Decimal as string
fn serialize_decimal<S>(value: &Decimal, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_str(&value.to_string())
}

/// Helper function to serialize Option<Decimal> as string
fn serialize_option_decimal<S>(value: &Option<Decimal>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match value {
        Some(d) => serializer.serialize_str(&d.to_string()),
        None => serializer.serialize_none(),
    }
}

// ============================================================================
// Convenience Type Aliases
// ============================================================================

/// Status message type
pub type StatusMessage = ChannelMessage<StatusData>;

/// Book message type
pub type BookMessage = ChannelMessage<BookData>;

/// Ticker message type
pub type TickerMessage = ChannelMessage<TickerData>;

/// Trade message type
pub type TradeMessage = ChannelMessage<TradeData>;

/// OHLC message type
pub type OhlcMessage = ChannelMessage<OhlcData>;

/// Executions message type (private)
pub type ExecutionsMessage = ChannelMessage<ExecutionData>;

/// Balances message type (private)
pub type BalancesMessage = ChannelMessage<WalletData>;

// Note: InstrumentMessage is defined as a separate struct above
// because its data structure differs from other channel messages

// ============================================================================
// Raw Message Parsing
// ============================================================================

/// Parsed message from WebSocket
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum WsMessage {
    /// Method response (subscribe, unsubscribe, pong, trading responses)
    Method(MethodResponse),
    /// Status channel update
    Status(StatusMessage),
    /// Book channel update
    Book(BookMessage),
    /// Ticker channel update
    Ticker(TickerMessage),
    /// Trade channel update
    Trade(TradeMessage),
    /// OHLC channel update
    Ohlc(OhlcMessage),
    /// Instrument channel update (provides precision info)
    Instrument(InstrumentMessage),
    /// Executions channel update (private - requires auth)
    Executions(ExecutionsMessage),
    /// Balances channel update (private - requires auth)
    Balances(BalancesMessage),
    /// Level 3 orders channel update (requires special access)
    Level3(L3Message),
    /// Heartbeat message
    Heartbeat,
    /// Unknown/unsupported message
    Unknown(serde_json::Value),
}

/// Type alias for backwards compatibility and clarity
pub type SubscribeResponse = MethodResponse;

impl WsMessage {
    /// Parse a raw JSON message
    pub fn parse(json: &str) -> Result<Self, serde_json::Error> {
        let value: serde_json::Value = serde_json::from_str(json)?;

        // Check if it's a method response
        if value.get("method").is_some() {
            let response: MethodResponse = serde_json::from_value(value)?;
            return Ok(Self::Method(response));
        }

        // Check channel type
        let channel = value.get("channel").and_then(|v| v.as_str());

        match channel {
            Some("status") => {
                let msg: StatusMessage = serde_json::from_value(value)?;
                Ok(Self::Status(msg))
            }
            Some("book") => {
                let msg: BookMessage = serde_json::from_value(value)?;
                Ok(Self::Book(msg))
            }
            Some("ticker") => {
                let msg: TickerMessage = serde_json::from_value(value)?;
                Ok(Self::Ticker(msg))
            }
            Some("trade") => {
                let msg: TradeMessage = serde_json::from_value(value)?;
                Ok(Self::Trade(msg))
            }
            Some("ohlc") => {
                let msg: OhlcMessage = serde_json::from_value(value)?;
                Ok(Self::Ohlc(msg))
            }
            Some("instrument") => {
                let msg: InstrumentMessage = serde_json::from_value(value)?;
                Ok(Self::Instrument(msg))
            }
            Some("executions") => {
                let msg: ExecutionsMessage = serde_json::from_value(value)?;
                Ok(Self::Executions(msg))
            }
            Some("balances") => {
                let msg: BalancesMessage = serde_json::from_value(value)?;
                Ok(Self::Balances(msg))
            }
            Some("level3") => {
                let msg: L3Message = serde_json::from_value(value)?;
                Ok(Self::Level3(msg))
            }
            Some("heartbeat") => Ok(Self::Heartbeat),
            _ => Ok(Self::Unknown(value)),
        }
    }

    /// Check if this is a book snapshot
    pub fn is_book_snapshot(&self) -> bool {
        matches!(self, Self::Book(msg) if msg.msg_type == "snapshot")
    }

    /// Check if this is a book update
    pub fn is_book_update(&self) -> bool {
        matches!(self, Self::Book(msg) if msg.msg_type == "update")
    }

    /// Check if this is an L3 snapshot
    pub fn is_l3_snapshot(&self) -> bool {
        matches!(self, Self::Level3(msg) if msg.msg_type == "snapshot")
    }

    /// Check if this is an L3 update
    pub fn is_l3_update(&self) -> bool {
        matches!(self, Self::Level3(msg) if msg.msg_type == "update")
    }

    /// Check if this is a trading method response (add_order, cancel_order, etc.)
    pub fn is_trading_response(&self) -> bool {
        matches!(self, Self::Method(resp) if matches!(
            resp.method.as_str(),
            "add_order" | "amend_order" | "edit_order" | "cancel_order" |
            "cancel_all" | "cancel_all_orders_after" | "batch_add" | "batch_cancel"
        ))
    }
}

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

    #[test]
    fn test_parse_status_message() {
        let json = r#"{
            "channel": "status",
            "type": "update",
            "data": [{
                "api_version": "v2",
                "connection_id": 5688663770896937000,
                "system": "online",
                "version": "2.0.10"
            }]
        }"#;

        let msg = WsMessage::parse(json).unwrap();
        match msg {
            WsMessage::Status(status) => {
                assert_eq!(status.data[0].api_version, "v2");
                assert_eq!(status.data[0].system, SystemStatus::Online);
            }
            _ => panic!("Expected Status message"),
        }
    }

    #[test]
    fn test_parse_book_snapshot() {
        let json = r#"{
            "channel": "book",
            "type": "snapshot",
            "data": [{
                "symbol": "BTC/USD",
                "bids": [{"price": 88813.5, "qty": 0.00460208}],
                "asks": [{"price": 88813.6, "qty": 2.85806499}],
                "checksum": 2919786898,
                "timestamp": "2025-12-21T12:28:24.113018Z"
            }]
        }"#;

        let msg = WsMessage::parse(json).unwrap();
        assert!(msg.is_book_snapshot());

        match msg {
            WsMessage::Book(book) => {
                assert_eq!(book.data[0].symbol, "BTC/USD");
                assert_eq!(book.data[0].checksum, 2919786898);
                assert!(!book.data[0].bids.is_empty());
                assert!(!book.data[0].asks.is_empty());
            }
            _ => panic!("Expected Book message"),
        }
    }

    #[test]
    fn test_parse_subscribe_response() {
        let json = r#"{
            "method": "subscribe",
            "result": {
                "channel": "book",
                "depth": 10,
                "snapshot": true,
                "symbol": "BTC/USD"
            },
            "success": true,
            "time_in": "2025-12-21T12:28:05.362191Z",
            "time_out": "2025-12-21T12:28:05.362232Z"
        }"#;

        let msg = WsMessage::parse(json).unwrap();
        match msg {
            WsMessage::Method(resp) => {
                assert!(resp.success);
                assert_eq!(resp.method, "subscribe");
                assert!(resp.result.is_some());
            }
            _ => panic!("Expected Method response"),
        }
    }

    #[test]
    fn test_subscribe_request_serialization() {
        let params = SubscribeParams::book(vec!["BTC/USD".to_string()], Depth::D10);
        let req = SubscribeRequest::new(params).with_req_id(1);

        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"method\":\"subscribe\""));
        assert!(json.contains("\"channel\":\"book\""));
        assert!(json.contains("\"depth\":10"));
    }
}