kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
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
//! Market depth analytics module
//!
//! This module provides tools for analyzing order book depth, including
//! skewness calculation, bid-ask spread dynamics, depth imbalance indicators,
//! and liquidity heat maps.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use uuid::Uuid;

/// Order book depth level
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceLevel {
    /// Price of this level in the order book.
    pub price: Decimal,
    /// Aggregate volume resting at this price level.
    pub volume: Decimal,
    /// Number of individual orders at this price level.
    pub order_count: u32,
}

/// Order book skewness metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderBookSkewness {
    /// Bid-side liquidity
    pub bid_liquidity: Decimal,
    /// Ask-side liquidity
    pub ask_liquidity: Decimal,
    /// Skewness ratio: (bid - ask) / (bid + ask)
    pub skewness_ratio: Decimal,
    /// Imbalance direction
    pub imbalance_direction: SkewnessDirection,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

/// Direction of order book skewness
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SkewnessDirection {
    /// More liquidity on bid side (buying pressure)
    BidHeavy,
    /// More liquidity on ask side (selling pressure)
    AskHeavy,
    /// Balanced liquidity
    Balanced,
}

impl OrderBookSkewness {
    /// Check if skewness is significant
    pub fn is_significant(&self) -> bool {
        self.skewness_ratio.abs() > dec!(0.2)
    }

    /// Get strength of skewness (0-100)
    pub fn strength(&self) -> u8 {
        let score = (self.skewness_ratio.abs() * dec!(100)).min(dec!(100));
        score.round().to_u8().unwrap_or(0)
    }
}

/// Bid-ask spread dynamics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpreadDynamics {
    /// Best bid price
    pub best_bid: Decimal,
    /// Best ask price
    pub best_ask: Decimal,
    /// Absolute spread
    pub absolute_spread: Decimal,
    /// Relative spread (percentage)
    pub relative_spread: Decimal,
    /// Mid price
    pub mid_price: Decimal,
    /// Spread volatility (rolling std dev)
    pub spread_volatility: Decimal,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

impl SpreadDynamics {
    /// Check if spread is tight
    pub fn is_tight(&self) -> bool {
        self.relative_spread < dec!(0.01) // < 1%
    }

    /// Check if spread is wide
    pub fn is_wide(&self) -> bool {
        self.relative_spread > dec!(0.05) // > 5%
    }

    /// Get spread quality score (0-100, higher is better)
    pub fn quality_score(&self) -> u8 {
        let tightness = (dec!(1) - (self.relative_spread * dec!(20))).max(dec!(0));
        let score = (tightness * dec!(100)).min(dec!(100));
        score.round().to_u8().unwrap_or(0)
    }
}

/// Depth imbalance at various levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DepthImbalance {
    /// Levels analyzed (e.g., top 5 levels)
    pub levels: u32,
    /// Total bid volume
    pub total_bid_volume: Decimal,
    /// Total ask volume
    pub total_ask_volume: Decimal,
    /// Imbalance ratio
    pub imbalance_ratio: Decimal,
    /// Bid order count
    pub bid_order_count: u32,
    /// Ask order count
    pub ask_order_count: u32,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

impl DepthImbalance {
    /// Get pressure direction
    pub fn pressure_direction(&self) -> SkewnessDirection {
        if self.imbalance_ratio > dec!(0.15) {
            SkewnessDirection::BidHeavy
        } else if self.imbalance_ratio < dec!(-0.15) {
            SkewnessDirection::AskHeavy
        } else {
            SkewnessDirection::Balanced
        }
    }

    /// Check if imbalance is extreme
    pub fn is_extreme(&self) -> bool {
        self.imbalance_ratio.abs() > dec!(0.5)
    }
}

/// Liquidity heat map (volume distribution across price levels)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiquidityHeatMap {
    /// Price levels with volume
    pub bid_levels: Vec<PriceLevel>,
    /// Ask levels with volume
    pub ask_levels: Vec<PriceLevel>,
    /// Mid price reference
    pub mid_price: Decimal,
    /// Total bid liquidity
    pub total_bid_liquidity: Decimal,
    /// Total ask liquidity
    pub total_ask_liquidity: Decimal,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

impl LiquidityHeatMap {
    /// Find the price level with maximum liquidity on bid side
    pub fn max_bid_liquidity_level(&self) -> Option<&PriceLevel> {
        self.bid_levels
            .iter()
            .max_by(|a, b| a.volume.cmp(&b.volume))
    }

    /// Find the price level with maximum liquidity on ask side
    pub fn max_ask_liquidity_level(&self) -> Option<&PriceLevel> {
        self.ask_levels
            .iter()
            .max_by(|a, b| a.volume.cmp(&b.volume))
    }

    /// Calculate concentration score (0-100, higher means more concentrated)
    pub fn concentration_score(&self) -> u8 {
        let max_bid_vol = self
            .max_bid_liquidity_level()
            .map(|l| l.volume)
            .unwrap_or(dec!(0));
        let max_ask_vol = self
            .max_ask_liquidity_level()
            .map(|l| l.volume)
            .unwrap_or(dec!(0));

        let total_vol = self.total_bid_liquidity + self.total_ask_liquidity;
        if total_vol == dec!(0) {
            return 0;
        }

        let concentration = ((max_bid_vol + max_ask_vol) / total_vol) * dec!(100);
        concentration.round().to_u8().unwrap_or(0)
    }
}

/// Market depth analyzer
#[derive(Debug, Clone)]
pub struct MarketDepthAnalyzer {
    #[allow(dead_code)]
    token_id: Uuid,
    bid_levels: BTreeMap<Decimal, PriceLevel>,
    ask_levels: BTreeMap<Decimal, PriceLevel>,
    spread_history: Vec<Decimal>,
    max_history: usize,
}

impl MarketDepthAnalyzer {
    /// Create a new market depth analyzer
    pub fn new(token_id: Uuid) -> Self {
        Self {
            token_id,
            bid_levels: BTreeMap::new(),
            ask_levels: BTreeMap::new(),
            spread_history: Vec::new(),
            max_history: 100,
        }
    }

    /// Update bid levels
    pub fn update_bid_level(&mut self, price: Decimal, volume: Decimal, order_count: u32) {
        if volume > dec!(0) {
            self.bid_levels.insert(
                price,
                PriceLevel {
                    price,
                    volume,
                    order_count,
                },
            );
        } else {
            self.bid_levels.remove(&price);
        }
    }

    /// Update ask levels
    pub fn update_ask_level(&mut self, price: Decimal, volume: Decimal, order_count: u32) {
        if volume > dec!(0) {
            self.ask_levels.insert(
                price,
                PriceLevel {
                    price,
                    volume,
                    order_count,
                },
            );
        } else {
            self.ask_levels.remove(&price);
        }
    }

    /// Get best bid price
    pub fn best_bid(&self) -> Option<Decimal> {
        self.bid_levels.keys().next_back().copied()
    }

    /// Get best ask price
    pub fn best_ask(&self) -> Option<Decimal> {
        self.ask_levels.keys().next().copied()
    }

    /// Get mid price
    pub fn mid_price(&self) -> Option<Decimal> {
        let bid = self.best_bid()?;
        let ask = self.best_ask()?;
        Some((bid + ask) / dec!(2))
    }

    /// Calculate order book skewness
    pub fn calculate_skewness(&self, depth_levels: usize) -> OrderBookSkewness {
        let bid_liquidity: Decimal = self
            .bid_levels
            .values()
            .rev()
            .take(depth_levels)
            .map(|l| l.volume)
            .sum();

        let ask_liquidity: Decimal = self
            .ask_levels
            .values()
            .take(depth_levels)
            .map(|l| l.volume)
            .sum();

        let total_liquidity = bid_liquidity + ask_liquidity;
        let skewness_ratio = if total_liquidity > dec!(0) {
            (bid_liquidity - ask_liquidity) / total_liquidity
        } else {
            dec!(0)
        };

        let imbalance_direction = if skewness_ratio > dec!(0.15) {
            SkewnessDirection::BidHeavy
        } else if skewness_ratio < dec!(-0.15) {
            SkewnessDirection::AskHeavy
        } else {
            SkewnessDirection::Balanced
        };

        OrderBookSkewness {
            bid_liquidity,
            ask_liquidity,
            skewness_ratio,
            imbalance_direction,
            timestamp: Utc::now(),
        }
    }

    /// Calculate spread dynamics
    pub fn calculate_spread_dynamics(&mut self) -> Option<SpreadDynamics> {
        let best_bid = self.best_bid()?;
        let best_ask = self.best_ask()?;
        let absolute_spread = best_ask - best_bid;
        let mid_price = (best_bid + best_ask) / dec!(2);
        let relative_spread = if mid_price > dec!(0) {
            absolute_spread / mid_price
        } else {
            dec!(0)
        };

        // Track spread history
        self.spread_history.push(relative_spread);
        if self.spread_history.len() > self.max_history {
            self.spread_history.remove(0);
        }

        // Calculate spread volatility (standard deviation)
        let spread_volatility = if self.spread_history.len() > 1 {
            let mean: Decimal = self.spread_history.iter().sum::<Decimal>()
                / Decimal::from(self.spread_history.len());
            let variance: Decimal = self
                .spread_history
                .iter()
                .map(|&s| {
                    let diff = s - mean;
                    diff * diff
                })
                .sum::<Decimal>()
                / Decimal::from(self.spread_history.len());
            // Approximate square root
            variance.sqrt().unwrap_or(dec!(0))
        } else {
            dec!(0)
        };

        Some(SpreadDynamics {
            best_bid,
            best_ask,
            absolute_spread,
            relative_spread,
            mid_price,
            spread_volatility,
            timestamp: Utc::now(),
        })
    }

    /// Calculate depth imbalance
    pub fn calculate_depth_imbalance(&self, levels: u32) -> DepthImbalance {
        let bid_data: Vec<_> = self
            .bid_levels
            .values()
            .rev()
            .take(levels as usize)
            .collect();
        let ask_data: Vec<_> = self.ask_levels.values().take(levels as usize).collect();

        let total_bid_volume: Decimal = bid_data.iter().map(|l| l.volume).sum();
        let total_ask_volume: Decimal = ask_data.iter().map(|l| l.volume).sum();
        let bid_order_count: u32 = bid_data.iter().map(|l| l.order_count).sum();
        let ask_order_count: u32 = ask_data.iter().map(|l| l.order_count).sum();

        let total_volume = total_bid_volume + total_ask_volume;
        let imbalance_ratio = if total_volume > dec!(0) {
            (total_bid_volume - total_ask_volume) / total_volume
        } else {
            dec!(0)
        };

        DepthImbalance {
            levels,
            total_bid_volume,
            total_ask_volume,
            imbalance_ratio,
            bid_order_count,
            ask_order_count,
            timestamp: Utc::now(),
        }
    }

    /// Generate liquidity heat map
    pub fn generate_heat_map(&self, levels: usize) -> LiquidityHeatMap {
        let bid_levels: Vec<PriceLevel> = self
            .bid_levels
            .values()
            .rev()
            .take(levels)
            .cloned()
            .collect();

        let ask_levels: Vec<PriceLevel> = self.ask_levels.values().take(levels).cloned().collect();

        let total_bid_liquidity: Decimal = bid_levels.iter().map(|l| l.volume).sum();
        let total_ask_liquidity: Decimal = ask_levels.iter().map(|l| l.volume).sum();

        let mid_price = self.mid_price().unwrap_or(dec!(0));

        LiquidityHeatMap {
            bid_levels,
            ask_levels,
            mid_price,
            total_bid_liquidity,
            total_ask_liquidity,
            timestamp: Utc::now(),
        }
    }

    /// Clear all depth data
    pub fn clear(&mut self) {
        self.bid_levels.clear();
        self.ask_levels.clear();
        self.spread_history.clear();
    }
}

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

    #[test]
    fn test_order_book_skewness_direction() {
        let skewness = OrderBookSkewness {
            bid_liquidity: dec!(1000),
            ask_liquidity: dec!(500),
            skewness_ratio: dec!(0.333),
            imbalance_direction: SkewnessDirection::BidHeavy,
            timestamp: Utc::now(),
        };

        assert_eq!(skewness.imbalance_direction, SkewnessDirection::BidHeavy);
        assert!(skewness.is_significant());
        assert_eq!(skewness.strength(), 33);
    }

    #[test]
    fn test_spread_dynamics_quality() {
        let spread = SpreadDynamics {
            best_bid: dec!(99.8),
            best_ask: dec!(100.2),
            absolute_spread: dec!(0.4),
            relative_spread: dec!(0.004),
            mid_price: dec!(100),
            spread_volatility: dec!(0.001),
            timestamp: Utc::now(),
        };

        assert!(spread.is_tight());
        assert!(!spread.is_wide());
        assert!(spread.quality_score() >= 80);
    }

    #[test]
    fn test_depth_imbalance_pressure() {
        let imbalance = DepthImbalance {
            levels: 5,
            total_bid_volume: dec!(1000),
            total_ask_volume: dec!(500),
            imbalance_ratio: dec!(0.333),
            bid_order_count: 10,
            ask_order_count: 5,
            timestamp: Utc::now(),
        };

        assert_eq!(imbalance.pressure_direction(), SkewnessDirection::BidHeavy);
        assert!(!imbalance.is_extreme());
    }

    #[test]
    fn test_market_depth_analyzer_basic() {
        let token_id = Uuid::new_v4();
        let mut analyzer = MarketDepthAnalyzer::new(token_id);

        // Add bid levels
        analyzer.update_bid_level(dec!(99), dec!(100), 5);
        analyzer.update_bid_level(dec!(98), dec!(200), 10);

        // Add ask levels
        analyzer.update_ask_level(dec!(101), dec!(150), 7);
        analyzer.update_ask_level(dec!(102), dec!(250), 12);

        assert_eq!(analyzer.best_bid(), Some(dec!(99)));
        assert_eq!(analyzer.best_ask(), Some(dec!(101)));
        assert_eq!(analyzer.mid_price(), Some(dec!(100)));
    }

    #[test]
    fn test_calculate_skewness() {
        let token_id = Uuid::new_v4();
        let mut analyzer = MarketDepthAnalyzer::new(token_id);

        // More bids than asks
        analyzer.update_bid_level(dec!(99), dec!(1000), 10);
        analyzer.update_bid_level(dec!(98), dec!(500), 5);
        analyzer.update_ask_level(dec!(101), dec!(200), 2);
        analyzer.update_ask_level(dec!(102), dec!(100), 1);

        let skewness = analyzer.calculate_skewness(2);
        assert!(skewness.skewness_ratio > dec!(0));
        assert_eq!(skewness.imbalance_direction, SkewnessDirection::BidHeavy);
    }

    #[test]
    fn test_calculate_spread_dynamics() {
        let token_id = Uuid::new_v4();
        let mut analyzer = MarketDepthAnalyzer::new(token_id);

        analyzer.update_bid_level(dec!(99), dec!(100), 5);
        analyzer.update_ask_level(dec!(101), dec!(100), 5);

        let spread = analyzer.calculate_spread_dynamics().unwrap();
        assert_eq!(spread.best_bid, dec!(99));
        assert_eq!(spread.best_ask, dec!(101));
        assert_eq!(spread.absolute_spread, dec!(2));
        assert_eq!(spread.mid_price, dec!(100));
    }

    #[test]
    fn test_calculate_depth_imbalance() {
        let token_id = Uuid::new_v4();
        let mut analyzer = MarketDepthAnalyzer::new(token_id);

        analyzer.update_bid_level(dec!(99), dec!(500), 5);
        analyzer.update_bid_level(dec!(98), dec!(300), 3);
        analyzer.update_ask_level(dec!(101), dec!(200), 2);
        analyzer.update_ask_level(dec!(102), dec!(100), 1);

        let imbalance = analyzer.calculate_depth_imbalance(2);
        assert_eq!(imbalance.total_bid_volume, dec!(800));
        assert_eq!(imbalance.total_ask_volume, dec!(300));
        assert!(imbalance.imbalance_ratio > dec!(0));
    }

    #[test]
    fn test_generate_heat_map() {
        let token_id = Uuid::new_v4();
        let mut analyzer = MarketDepthAnalyzer::new(token_id);

        analyzer.update_bid_level(dec!(99), dec!(100), 5);
        analyzer.update_bid_level(dec!(98), dec!(200), 10);
        analyzer.update_ask_level(dec!(101), dec!(150), 7);
        analyzer.update_ask_level(dec!(102), dec!(250), 12);

        let heat_map = analyzer.generate_heat_map(2);
        assert_eq!(heat_map.bid_levels.len(), 2);
        assert_eq!(heat_map.ask_levels.len(), 2);
        assert_eq!(heat_map.total_bid_liquidity, dec!(300));
        assert_eq!(heat_map.total_ask_liquidity, dec!(400));
    }

    #[test]
    fn test_liquidity_heat_map_concentration() {
        let heat_map = LiquidityHeatMap {
            bid_levels: vec![
                PriceLevel {
                    price: dec!(99),
                    volume: dec!(900),
                    order_count: 10,
                },
                PriceLevel {
                    price: dec!(98),
                    volume: dec!(100),
                    order_count: 5,
                },
            ],
            ask_levels: vec![
                PriceLevel {
                    price: dec!(101),
                    volume: dec!(800),
                    order_count: 8,
                },
                PriceLevel {
                    price: dec!(102),
                    volume: dec!(200),
                    order_count: 3,
                },
            ],
            mid_price: dec!(100),
            total_bid_liquidity: dec!(1000),
            total_ask_liquidity: dec!(1000),
            timestamp: Utc::now(),
        };

        let concentration = heat_map.concentration_score();
        // (900 + 800) / 2000 = 0.85 = 85%
        assert_eq!(concentration, 85);
    }
}