kraken-ws 0.1.0

Native WebSocket client for Kraken 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
//! Connection and subscription events
//!
//! This module provides event types for both public market data and
//! private account data (executions, balances).

use kraken_book::OrderbookSnapshot;
use kraken_types::{BalanceData, Decimal, ExecutionData, L3Data, L3Order, Side};
use std::collections::HashMap;
use std::time::Duration;

/// Reason for disconnection
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DisconnectReason {
    /// Server closed the connection
    ServerClosed,
    /// Network error occurred
    NetworkError(String),
    /// Connection timed out
    Timeout,
    /// Client requested shutdown
    Shutdown,
    /// Authentication failed
    AuthFailed,
    /// No heartbeat/message received within timeout period
    HeartbeatTimeout,
}

/// Connection lifecycle events
#[derive(Debug, Clone)]
pub enum ConnectionEvent {
    /// Successfully connected to the endpoint
    Connected {
        /// API version reported by server
        api_version: String,
        /// Connection ID from server
        connection_id: u64,
    },
    /// Connection was lost
    Disconnected {
        /// Reason for disconnection
        reason: DisconnectReason,
    },
    /// Attempting to reconnect
    Reconnecting {
        /// Current attempt number (1-indexed)
        attempt: u32,
        /// Delay before this attempt
        delay: Duration,
    },
    /// Reconnection attempts exhausted
    ReconnectFailed {
        /// Final error
        error: String,
    },
    /// Subscriptions restored after reconnect
    SubscriptionsRestored {
        /// Number of subscriptions restored
        count: usize,
    },
    /// Circuit breaker is open (blocking reconnection attempts)
    CircuitBreakerOpen {
        /// Number of times circuit has been tripped
        trips: u64,
    },
}

/// Subscription-specific events
#[derive(Debug, Clone)]
pub enum SubscriptionEvent {
    /// Subscription confirmed by server
    Subscribed {
        /// Channel name
        channel: String,
        /// Symbol(s)
        symbols: Vec<String>,
    },
    /// Subscription rejected
    Rejected {
        /// Channel name
        channel: String,
        /// Rejection reason
        reason: String,
    },
    /// Unsubscribed from channel
    Unsubscribed {
        /// Channel name
        channel: String,
        /// Symbol(s)
        symbols: Vec<String>,
    },
}

/// Market data events
#[derive(Debug, Clone)]
pub enum MarketEvent {
    /// Orderbook snapshot received
    OrderbookSnapshot {
        /// Trading pair symbol
        symbol: String,
        /// Full orderbook state
        snapshot: OrderbookSnapshot,
    },
    /// Orderbook updated
    OrderbookUpdate {
        /// Trading pair symbol
        symbol: String,
        /// Updated orderbook state
        snapshot: OrderbookSnapshot,
    },
    /// Checksum validation failed
    ChecksumMismatch {
        /// Symbol that failed
        symbol: String,
        /// Expected checksum
        expected: u32,
        /// Computed checksum
        computed: u32,
    },
    /// Status message from server
    Status {
        /// System status (online, maintenance, etc.)
        system: String,
        /// API version
        version: String,
    },
    /// Heartbeat received
    Heartbeat,
}

// ============================================================================
// Private Channel Events
// ============================================================================

/// Order status in the lifecycle
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OrderStatus {
    /// Order is pending (not yet acknowledged)
    Pending,
    /// Order is new (acknowledged, waiting in book)
    New,
    /// Order is partially filled
    PartiallyFilled,
    /// Order is completely filled
    Filled,
    /// Order was canceled
    Canceled,
    /// Order expired
    Expired,
    /// Order was rejected
    Rejected,
}

impl OrderStatus {
    /// Parse from status string
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "pending" => Self::Pending,
            "new" | "open" => Self::New,
            "partially_filled" | "partiallyfilled" => Self::PartiallyFilled,
            "filled" | "closed" => Self::Filled,
            "canceled" | "cancelled" => Self::Canceled,
            "expired" => Self::Expired,
            _ => Self::Rejected,
        }
    }

    /// Check if order is still active (can be filled)
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Pending | Self::New | Self::PartiallyFilled)
    }

    /// Check if order is terminal (no more changes)
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Filled | Self::Canceled | Self::Expired | Self::Rejected)
    }
}

/// Tracked order with full state
#[derive(Debug, Clone)]
pub struct TrackedOrder {
    /// Order ID
    pub order_id: String,
    /// Trading pair symbol
    pub symbol: String,
    /// Order side
    pub side: Side,
    /// Order type (limit, market, etc.)
    pub order_type: String,
    /// Original order quantity
    pub order_qty: Decimal,
    /// Limit price (if limit order)
    pub limit_price: Option<Decimal>,
    /// Cumulative filled quantity
    pub filled_qty: Decimal,
    /// Average fill price
    pub avg_price: Option<Decimal>,
    /// Current order status
    pub status: OrderStatus,
    /// Total fees paid
    pub total_fees: Decimal,
    /// Fee currency
    pub fee_currency: Option<String>,
    /// List of fills for this order
    pub fills: Vec<OrderFill>,
    /// Timestamp of last update
    pub last_update: String,
}

impl TrackedOrder {
    /// Create a new tracked order from execution data
    pub fn from_execution(exec: &ExecutionData) -> Self {
        Self {
            order_id: exec.order_id.clone(),
            symbol: exec.symbol.clone(),
            side: exec.side,
            order_type: exec.order_type.clone(),
            order_qty: exec.order_qty.unwrap_or(Decimal::ZERO),
            limit_price: exec.limit_price,
            filled_qty: exec.cum_qty.unwrap_or(Decimal::ZERO),
            avg_price: exec.avg_price,
            status: exec.order_status.as_ref()
                .map(|s| OrderStatus::parse(s))
                .unwrap_or(OrderStatus::Pending),
            total_fees: exec.fee_paid.unwrap_or(Decimal::ZERO),
            fee_currency: exec.fee_currency.clone(),
            fills: Vec::new(),
            last_update: exec.timestamp.clone(),
        }
    }

    /// Update order from new execution data
    pub fn update(&mut self, exec: &ExecutionData) {
        if let Some(cum_qty) = exec.cum_qty {
            self.filled_qty = cum_qty;
        }
        if let Some(avg_price) = exec.avg_price {
            self.avg_price = Some(avg_price);
        }
        if let Some(ref status) = exec.order_status {
            self.status = OrderStatus::parse(status);
        }
        if let Some(fee) = exec.fee_paid {
            self.total_fees = fee;
        }
        if exec.fee_currency.is_some() {
            self.fee_currency = exec.fee_currency.clone();
        }
        self.last_update = exec.timestamp.clone();
    }

    /// Add a fill to this order
    pub fn add_fill(&mut self, fill: OrderFill) {
        self.fills.push(fill);
    }

    /// Remaining quantity to be filled
    pub fn remaining_qty(&self) -> Decimal {
        self.order_qty - self.filled_qty
    }

    /// Fill percentage (0.0 to 1.0)
    pub fn fill_percentage(&self) -> f64 {
        if self.order_qty.is_zero() {
            return 0.0;
        }
        (self.filled_qty / self.order_qty)
            .to_string()
            .parse()
            .unwrap_or(0.0)
    }
}

/// Individual fill (partial execution)
#[derive(Debug, Clone)]
pub struct OrderFill {
    /// Execution ID
    pub exec_id: Option<String>,
    /// Trade ID
    pub trade_id: Option<u64>,
    /// Fill quantity
    pub qty: Decimal,
    /// Fill price
    pub price: Decimal,
    /// Fee for this fill
    pub fee: Decimal,
    /// Fee currency
    pub fee_currency: Option<String>,
    /// Timestamp
    pub timestamp: String,
}

impl OrderFill {
    /// Create from execution data (if it's a trade)
    pub fn from_execution(exec: &ExecutionData) -> Option<Self> {
        // Only create fill if there's a last_qty (indicates actual trade)
        let qty = exec.last_qty?;
        let price = exec.last_price?;

        Some(Self {
            exec_id: exec.exec_id.clone(),
            trade_id: exec.trade_id,
            qty,
            price,
            fee: exec.fee_paid.unwrap_or(Decimal::ZERO),
            fee_currency: exec.fee_currency.clone(),
            timestamp: exec.timestamp.clone(),
        })
    }
}

/// Private channel events (requires authentication)
#[derive(Debug, Clone)]
pub enum PrivateEvent {
    /// Order execution event
    Execution {
        /// Raw execution data from Kraken
        data: ExecutionData,
        /// Parsed execution type
        exec_type: ExecutionType,
    },
    /// Order state changed
    OrderUpdate {
        /// Updated order state
        order: TrackedOrder,
        /// What changed
        change: OrderChange,
    },
    /// Balance update event
    BalanceUpdate {
        /// Updated balances
        balances: Vec<BalanceData>,
        /// Whether this is a snapshot
        is_snapshot: bool,
    },
    /// Full balance snapshot
    BalanceSnapshot {
        /// All balances keyed by asset
        balances: HashMap<String, BalanceInfo>,
    },
}

/// Type of execution event
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutionType {
    /// New order created
    New,
    /// Order was filled (partially or fully)
    Trade,
    /// Order was canceled
    Canceled,
    /// Order expired
    Expired,
    /// Order was amended
    Amended,
    /// Order pending
    Pending,
    /// Unknown type
    Unknown,
}

impl ExecutionType {
    /// Parse from exec_type string
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "new" => Self::New,
            "trade" | "filled" => Self::Trade,
            "canceled" | "cancelled" => Self::Canceled,
            "expired" => Self::Expired,
            "amended" | "modified" => Self::Amended,
            "pending" => Self::Pending,
            _ => Self::Unknown,
        }
    }
}

/// What changed in an order update
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderChange {
    /// Order was created
    Created,
    /// Order was partially filled
    PartialFill,
    /// Order was completely filled
    FullFill,
    /// Order was canceled
    Canceled,
    /// Order expired
    Expired,
    /// Order quantity or price was modified
    Modified,
}

/// Enhanced balance information
#[derive(Debug, Clone)]
pub struct BalanceInfo {
    /// Asset identifier
    pub asset: String,
    /// Available balance (for trading/withdrawal)
    pub available: Decimal,
    /// Balance on hold (in open orders)
    pub hold: Decimal,
    /// Total balance (available + hold)
    pub total: Decimal,
}

impl BalanceInfo {
    /// Create from balance data
    pub fn from_data(data: &BalanceData) -> Self {
        let hold = data.hold_trade.unwrap_or(Decimal::ZERO);
        Self {
            asset: data.asset.clone(),
            available: data.balance,
            hold,
            total: data.balance + hold,
        }
    }
}

// ============================================================================
// Level 3 (L3) Events
// ============================================================================

/// Level 3 orderbook events
///
/// L3 provides individual order visibility, unlike L2 which shows aggregated levels.
#[derive(Debug, Clone)]
pub enum L3Event {
    /// Full L3 orderbook snapshot
    Snapshot {
        /// Trading pair symbol
        symbol: String,
        /// Bid orders
        bids: Vec<L3Order>,
        /// Ask orders
        asks: Vec<L3Order>,
        /// Checksum for validation
        checksum: Option<u32>,
    },
    /// L3 orderbook update
    Update {
        /// Trading pair symbol
        symbol: String,
        /// Bid order changes
        bids: Vec<L3Order>,
        /// Ask order changes
        asks: Vec<L3Order>,
    },
}

impl L3Event {
    /// Create from L3Data
    pub fn from_data(data: &L3Data, is_snapshot: bool) -> Self {
        if is_snapshot {
            Self::Snapshot {
                symbol: data.symbol.clone(),
                bids: data.bids.clone(),
                asks: data.asks.clone(),
                checksum: data.checksum,
            }
        } else {
            Self::Update {
                symbol: data.symbol.clone(),
                bids: data.bids.clone(),
                asks: data.asks.clone(),
            }
        }
    }

    /// Get the symbol for this event
    pub fn symbol(&self) -> &str {
        match self {
            Self::Snapshot { symbol, .. } | Self::Update { symbol, .. } => symbol,
        }
    }

    /// Check if this is a snapshot
    pub fn is_snapshot(&self) -> bool {
        matches!(self, Self::Snapshot { .. })
    }
}

/// Combined event type for event streams
#[derive(Debug, Clone)]
pub enum Event {
    /// Connection-related event
    Connection(ConnectionEvent),
    /// Subscription-related event
    Subscription(SubscriptionEvent),
    /// Market data event
    Market(MarketEvent),
    /// Private channel event (executions, balances)
    Private(Box<PrivateEvent>),
    /// Level 3 orderbook event
    L3(L3Event),
}

impl From<ConnectionEvent> for Event {
    fn from(event: ConnectionEvent) -> Self {
        Event::Connection(event)
    }
}

impl From<SubscriptionEvent> for Event {
    fn from(event: SubscriptionEvent) -> Self {
        Event::Subscription(event)
    }
}

impl From<MarketEvent> for Event {
    fn from(event: MarketEvent) -> Self {
        Event::Market(event)
    }
}

impl From<PrivateEvent> for Event {
    fn from(event: PrivateEvent) -> Self {
        Event::Private(Box::new(event))
    }
}

impl From<L3Event> for Event {
    fn from(event: L3Event) -> Self {
        Event::L3(event)
    }
}

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

    #[test]
    fn test_order_status_parsing() {
        assert_eq!(OrderStatus::parse("pending"), OrderStatus::Pending);
        assert_eq!(OrderStatus::parse("new"), OrderStatus::New);
        assert_eq!(OrderStatus::parse("open"), OrderStatus::New);
        assert_eq!(OrderStatus::parse("filled"), OrderStatus::Filled);
        assert_eq!(OrderStatus::parse("closed"), OrderStatus::Filled);
        assert_eq!(OrderStatus::parse("canceled"), OrderStatus::Canceled);
        assert_eq!(OrderStatus::parse("cancelled"), OrderStatus::Canceled);
    }

    #[test]
    fn test_order_status_states() {
        assert!(OrderStatus::Pending.is_active());
        assert!(OrderStatus::New.is_active());
        assert!(OrderStatus::PartiallyFilled.is_active());
        assert!(!OrderStatus::Filled.is_active());
        assert!(!OrderStatus::Canceled.is_active());

        assert!(OrderStatus::Filled.is_terminal());
        assert!(OrderStatus::Canceled.is_terminal());
        assert!(OrderStatus::Expired.is_terminal());
        assert!(!OrderStatus::New.is_terminal());
    }

    #[test]
    fn test_execution_type_parsing() {
        assert_eq!(ExecutionType::parse("new"), ExecutionType::New);
        assert_eq!(ExecutionType::parse("trade"), ExecutionType::Trade);
        assert_eq!(ExecutionType::parse("filled"), ExecutionType::Trade);
        assert_eq!(ExecutionType::parse("canceled"), ExecutionType::Canceled);
        assert_eq!(ExecutionType::parse("cancelled"), ExecutionType::Canceled);
    }

    #[test]
    fn test_balance_info_creation() {
        let data = BalanceData {
            asset: "BTC".to_string(),
            balance: Decimal::new(100, 2), // 1.00
            hold_trade: Some(Decimal::new(25, 2)), // 0.25
        };

        let info = BalanceInfo::from_data(&data);
        assert_eq!(info.asset, "BTC");
        assert_eq!(info.available, Decimal::new(100, 2));
        assert_eq!(info.hold, Decimal::new(25, 2));
        assert_eq!(info.total, Decimal::new(125, 2));
    }
}