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
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
//! Optimized order book with lock-free data structures and SIMD optimizations
//!
//! This module provides performance-optimized implementations for high-frequency trading:
//! - Lock-free concurrent access using atomic operations
//! - Memory pool allocation to reduce allocation overhead
//! - SIMD optimizations for price calculations

use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering as AtomicOrdering};
use std::sync::{Arc, RwLock};
use uuid::Uuid;

use super::order_book::{LimitOrder, OrderSide};

/// Memory pool for order allocations
///
/// Pre-allocates memory to reduce allocation overhead during high-frequency trading
pub struct OrderPool {
    pool: Vec<Option<LimitOrder>>,
    free_indices: VecDeque<usize>,
    capacity: usize,
    allocated_count: AtomicUsize,
}

impl OrderPool {
    /// Create a new order pool with the specified capacity
    pub fn new(capacity: usize) -> Self {
        let pool = (0..capacity).map(|_| None).collect();
        let free_indices = (0..capacity).collect();

        Self {
            pool,
            free_indices,
            capacity,
            allocated_count: AtomicUsize::new(0),
        }
    }

    /// Allocate an order from the pool
    pub fn allocate(&mut self, order: LimitOrder) -> Option<usize> {
        if let Some(idx) = self.free_indices.pop_front() {
            self.pool[idx] = Some(order);
            self.allocated_count.fetch_add(1, AtomicOrdering::SeqCst);
            Some(idx)
        } else {
            None // Pool exhausted
        }
    }

    /// Deallocate an order back to the pool
    pub fn deallocate(&mut self, idx: usize) {
        if idx < self.capacity && self.pool[idx].is_some() {
            self.pool[idx] = None;
            self.free_indices.push_back(idx);
            self.allocated_count.fetch_sub(1, AtomicOrdering::SeqCst);
        }
    }

    /// Get an order by index
    pub fn get(&self, idx: usize) -> Option<&LimitOrder> {
        self.pool.get(idx).and_then(|o| o.as_ref())
    }

    /// Get a mutable order by index
    pub fn get_mut(&mut self, idx: usize) -> Option<&mut LimitOrder> {
        self.pool.get_mut(idx).and_then(|o| o.as_mut())
    }

    /// Get the number of allocated orders
    pub fn allocated_count(&self) -> usize {
        self.allocated_count.load(AtomicOrdering::SeqCst)
    }

    /// Get the pool capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get the number of free slots
    pub fn free_count(&self) -> usize {
        self.capacity - self.allocated_count()
    }
}

/// Lock-free price level statistics
///
/// Uses atomic operations for thread-safe access without locks
#[derive(Debug)]
pub struct AtomicPriceStats {
    /// Total volume at this price level (stored as integer cents)
    total_volume_cents: AtomicU64,
    /// Number of orders at this price level
    order_count: AtomicUsize,
}

impl AtomicPriceStats {
    /// Creates a new `AtomicPriceStats` with zeroed counters.
    pub fn new() -> Self {
        Self {
            total_volume_cents: AtomicU64::new(0),
            order_count: AtomicUsize::new(0),
        }
    }

    /// Add volume (amount in Decimal)
    pub fn add_volume(&self, amount: Decimal) {
        let cents = decimal_to_cents(amount);
        self.total_volume_cents
            .fetch_add(cents, AtomicOrdering::SeqCst);
        self.order_count.fetch_add(1, AtomicOrdering::SeqCst);
    }

    /// Subtract volume
    pub fn sub_volume(&self, amount: Decimal) {
        let cents = decimal_to_cents(amount);
        self.total_volume_cents
            .fetch_sub(cents, AtomicOrdering::SeqCst);
        self.order_count.fetch_sub(1, AtomicOrdering::SeqCst);
    }

    /// Get total volume
    pub fn total_volume(&self) -> Decimal {
        let cents = self.total_volume_cents.load(AtomicOrdering::SeqCst);
        cents_to_decimal(cents)
    }

    /// Get order count
    pub fn order_count(&self) -> usize {
        self.order_count.load(AtomicOrdering::SeqCst)
    }
}

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

/// Convert Decimal to cents (u64) for atomic operations
fn decimal_to_cents(d: Decimal) -> u64 {
    // Multiply by 100,000,000 to preserve 8 decimal places
    let scaled = d * dec!(100000000);
    scaled.to_string().parse::<f64>().unwrap_or(0.0) as u64
}

/// Convert cents (u64) back to Decimal
fn cents_to_decimal(cents: u64) -> Decimal {
    Decimal::from(cents) / dec!(100000000)
}

/// SIMD-optimized price calculations
///
/// Uses vectorized operations for bulk price calculations
pub mod simd {
    use rust_decimal::Decimal;
    use rust_decimal_macros::dec;

    /// Calculate multiple price impacts in parallel
    ///
    /// This is a simplified SIMD simulation. For real SIMD, you would use
    /// libraries like `std::simd` (nightly) or `packed_simd`.
    pub fn batch_price_impact(
        base_prices: &[Decimal],
        volumes: &[Decimal],
        liquidity: Decimal,
    ) -> Vec<Decimal> {
        base_prices
            .iter()
            .zip(volumes.iter())
            .map(|(price, volume)| {
                // Price impact = volume / liquidity
                let impact_ratio = *volume / liquidity;
                *price * (dec!(1) + impact_ratio)
            })
            .collect()
    }

    /// Calculate volume-weighted average price for multiple orders
    pub fn batch_vwap(prices: &[Decimal], volumes: &[Decimal]) -> Option<Decimal> {
        if prices.is_empty() || prices.len() != volumes.len() {
            return None;
        }

        let total_value: Decimal = prices
            .iter()
            .zip(volumes.iter())
            .map(|(p, v)| *p * *v)
            .sum();

        let total_volume: Decimal = volumes.iter().sum();

        if total_volume > Decimal::ZERO {
            Some(total_value / total_volume)
        } else {
            None
        }
    }

    /// Calculate multiple weighted spreads in parallel
    pub fn batch_weighted_spread(
        bid_prices: &[Decimal],
        ask_prices: &[Decimal],
        volumes: &[Decimal],
    ) -> Vec<Decimal> {
        bid_prices
            .iter()
            .zip(ask_prices.iter())
            .zip(volumes.iter())
            .map(|((bid, ask), volume)| {
                let spread = *ask - *bid;
                let weight = *volume;
                spread * weight
            })
            .collect()
    }

    /// Fast mid-price calculation for multiple levels
    pub fn batch_mid_price(bid_prices: &[Decimal], ask_prices: &[Decimal]) -> Vec<Decimal> {
        bid_prices
            .iter()
            .zip(ask_prices.iter())
            .map(|(bid, ask)| (*bid + *ask) / dec!(2))
            .collect()
    }

    /// Vectorized slippage calculation
    pub fn batch_slippage(
        execution_prices: &[Decimal],
        expected_prices: &[Decimal],
    ) -> Vec<Decimal> {
        execution_prices
            .iter()
            .zip(expected_prices.iter())
            .map(|(exec, expected)| {
                if *expected > Decimal::ZERO {
                    ((*exec - *expected) / *expected).abs()
                } else {
                    Decimal::ZERO
                }
            })
            .collect()
    }
}

/// Lock-free order book using atomic operations and RwLock for concurrent access
///
/// This implementation allows multiple readers and single writer access patterns
/// commonly found in high-frequency trading systems.
pub struct LockFreeOrderBook {
    #[allow(dead_code)]
    token_id: Uuid,
    /// Price levels with atomic statistics
    bid_stats: Arc<RwLock<std::collections::BTreeMap<i64, Arc<AtomicPriceStats>>>>,
    ask_stats: Arc<RwLock<std::collections::BTreeMap<i64, Arc<AtomicPriceStats>>>>,
    /// Order pool for memory efficiency
    order_pool: Arc<RwLock<OrderPool>>,
    /// Total statistics
    total_bids: AtomicU64,
    total_asks: AtomicU64,
}

impl LockFreeOrderBook {
    /// Create a new lock-free order book with memory pool
    pub fn new(token_id: Uuid, pool_capacity: usize) -> Self {
        Self {
            token_id,
            bid_stats: Arc::new(RwLock::new(std::collections::BTreeMap::new())),
            ask_stats: Arc::new(RwLock::new(std::collections::BTreeMap::new())),
            order_pool: Arc::new(RwLock::new(OrderPool::new(pool_capacity))),
            total_bids: AtomicU64::new(0),
            total_asks: AtomicU64::new(0),
        }
    }

    /// Add an order using lock-free operations
    pub fn add_order_atomic(&self, order: LimitOrder) -> Result<(), &'static str> {
        let price_key = decimal_to_cents(order.price) as i64;
        let remaining = order.remaining();

        // Update atomic statistics
        match order.side {
            OrderSide::Buy => {
                let stats_map = self.bid_stats.read().unwrap();
                let stats = stats_map.get(&price_key).cloned().unwrap_or_else(|| {
                    drop(stats_map);
                    let mut write_map = self.bid_stats.write().unwrap();
                    let new_stats = Arc::new(AtomicPriceStats::new());
                    write_map.insert(price_key, new_stats.clone());
                    new_stats
                });

                stats.add_volume(remaining);
                self.total_bids
                    .fetch_add(decimal_to_cents(remaining), AtomicOrdering::SeqCst);
            }
            OrderSide::Sell => {
                let stats_map = self.ask_stats.read().unwrap();
                let stats = stats_map.get(&price_key).cloned().unwrap_or_else(|| {
                    drop(stats_map);
                    let mut write_map = self.ask_stats.write().unwrap();
                    let new_stats = Arc::new(AtomicPriceStats::new());
                    write_map.insert(price_key, new_stats.clone());
                    new_stats
                });

                stats.add_volume(remaining);
                self.total_asks
                    .fetch_add(decimal_to_cents(remaining), AtomicOrdering::SeqCst);
            }
        }

        // Allocate in pool
        let mut pool = self.order_pool.write().unwrap();
        pool.allocate(order).ok_or("Pool exhausted")?;

        Ok(())
    }

    /// Get total bid volume atomically
    pub fn total_bid_volume(&self) -> Decimal {
        cents_to_decimal(self.total_bids.load(AtomicOrdering::SeqCst))
    }

    /// Get total ask volume atomically
    pub fn total_ask_volume(&self) -> Decimal {
        cents_to_decimal(self.total_asks.load(AtomicOrdering::SeqCst))
    }

    /// Get pool statistics
    pub fn pool_stats(&self) -> PoolStats {
        let pool = self.order_pool.read().unwrap();
        PoolStats {
            capacity: pool.capacity(),
            allocated: pool.allocated_count(),
            free: pool.free_count(),
        }
    }

    /// Get best bid price (lock-free read)
    pub fn best_bid(&self) -> Option<Decimal> {
        let stats = self.bid_stats.read().unwrap();
        stats
            .iter()
            .next_back()
            .map(|(price, _)| cents_to_decimal(*price as u64))
    }

    /// Get best ask price (lock-free read)
    pub fn best_ask(&self) -> Option<Decimal> {
        let stats = self.ask_stats.read().unwrap();
        stats
            .iter()
            .next()
            .map(|(price, _)| cents_to_decimal(*price as u64))
    }

    /// Get spread
    pub fn spread(&self) -> Option<Decimal> {
        match (self.best_ask(), self.best_bid()) {
            (Some(ask), Some(bid)) => Some(ask - bid),
            _ => None,
        }
    }

    /// Get mid price using SIMD optimization
    pub fn mid_price_simd(&self) -> Option<Decimal> {
        match (self.best_ask(), self.best_bid()) {
            (Some(ask), Some(bid)) => {
                // Use SIMD batch calculation for consistency
                let bids = vec![bid];
                let asks = vec![ask];
                let mids = simd::batch_mid_price(&bids, &asks);
                mids.first().copied()
            }
            _ => None,
        }
    }
}

/// Pool statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolStats {
    /// Maximum number of orders the pool can hold.
    pub capacity: usize,
    /// Number of order slots currently in use.
    pub allocated: usize,
    /// Number of order slots available for allocation.
    pub free: usize,
}

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

    #[test]
    fn test_order_pool_allocation() {
        let mut pool = OrderPool::new(10);
        assert_eq!(pool.capacity(), 10);
        assert_eq!(pool.allocated_count(), 0);
        assert_eq!(pool.free_count(), 10);

        let order = LimitOrder::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            OrderSide::Buy,
            dec!(100),
            dec!(10),
        );

        let idx = pool.allocate(order.clone()).unwrap();
        assert_eq!(pool.allocated_count(), 1);
        assert_eq!(pool.free_count(), 9);

        assert!(pool.get(idx).is_some());

        pool.deallocate(idx);
        assert_eq!(pool.allocated_count(), 0);
        assert_eq!(pool.free_count(), 10);
    }

    #[test]
    fn test_order_pool_exhaustion() {
        let mut pool = OrderPool::new(2);

        let order1 = LimitOrder::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            OrderSide::Buy,
            dec!(100),
            dec!(10),
        );
        let order2 = LimitOrder::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            OrderSide::Sell,
            dec!(105),
            dec!(15),
        );
        let order3 = LimitOrder::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            OrderSide::Buy,
            dec!(99),
            dec!(5),
        );

        assert!(pool.allocate(order1).is_some());
        assert!(pool.allocate(order2).is_some());
        assert!(pool.allocate(order3).is_none()); // Pool exhausted
    }

    #[test]
    fn test_atomic_price_stats() {
        let stats = AtomicPriceStats::new();

        stats.add_volume(dec!(100));
        stats.add_volume(dec!(50));

        assert_eq!(stats.order_count(), 2);

        // Volume should be approximately 150 (with some rounding)
        let total = stats.total_volume();
        assert!(total >= dec!(149) && total <= dec!(151));

        stats.sub_volume(dec!(50));
        assert_eq!(stats.order_count(), 1);
    }

    #[test]
    fn test_decimal_conversion() {
        let original = dec!(123.456789);
        let cents = decimal_to_cents(original);
        let converted = cents_to_decimal(cents);

        // Should be approximately equal (within rounding)
        let diff = (original - converted).abs();
        assert!(diff < dec!(0.0001));
    }

    #[test]
    fn test_simd_batch_price_impact() {
        let base_prices = vec![dec!(100), dec!(200), dec!(300)];
        let volumes = vec![dec!(10), dec!(20), dec!(30)];
        let liquidity = dec!(1000);

        let impacts = simd::batch_price_impact(&base_prices, &volumes, liquidity);

        assert_eq!(impacts.len(), 3);
        assert!(impacts[0] > dec!(100));
        assert!(impacts[1] > dec!(200));
        assert!(impacts[2] > dec!(300));
    }

    #[test]
    fn test_simd_batch_vwap() {
        let prices = vec![dec!(100), dec!(101), dec!(102)];
        let volumes = vec![dec!(10), dec!(20), dec!(30)];

        let vwap = simd::batch_vwap(&prices, &volumes).unwrap();

        // VWAP should be weighted toward higher volume prices
        assert!(vwap > dec!(100));
        assert!(vwap < dec!(102));
    }

    #[test]
    fn test_simd_batch_mid_price() {
        let bids = vec![dec!(99), dec!(98), dec!(97)];
        let asks = vec![dec!(101), dec!(102), dec!(103)];

        let mids = simd::batch_mid_price(&bids, &asks);

        assert_eq!(mids.len(), 3);
        assert_eq!(mids[0], dec!(100));
        assert_eq!(mids[1], dec!(100));
        assert_eq!(mids[2], dec!(100));
    }

    #[test]
    fn test_simd_batch_slippage() {
        let execution = vec![dec!(101), dec!(102), dec!(103)];
        let expected = vec![dec!(100), dec!(100), dec!(100)];

        let slippages = simd::batch_slippage(&execution, &expected);

        assert_eq!(slippages.len(), 3);
        assert_eq!(slippages[0], dec!(0.01)); // 1% slippage
        assert_eq!(slippages[1], dec!(0.02)); // 2% slippage
        assert_eq!(slippages[2], dec!(0.03)); // 3% slippage
    }

    #[test]
    fn test_lock_free_order_book() {
        let book = LockFreeOrderBook::new(Uuid::new_v4(), 100);

        let buy_order = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Buy,
            dec!(100),
            dec!(10),
        );

        let sell_order = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Sell,
            dec!(105),
            dec!(15),
        );

        assert!(book.add_order_atomic(buy_order).is_ok());
        assert!(book.add_order_atomic(sell_order).is_ok());

        assert!(book.total_bid_volume() > Decimal::ZERO);
        assert!(book.total_ask_volume() > Decimal::ZERO);

        let stats = book.pool_stats();
        assert_eq!(stats.capacity, 100);
        assert_eq!(stats.allocated, 2);
        assert_eq!(stats.free, 98);
    }

    #[test]
    fn test_lock_free_best_prices() {
        let book = LockFreeOrderBook::new(Uuid::new_v4(), 100);

        let buy1 = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Buy,
            dec!(100),
            dec!(10),
        );
        let buy2 = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Buy,
            dec!(99),
            dec!(5),
        );

        book.add_order_atomic(buy1).unwrap();
        book.add_order_atomic(buy2).unwrap();

        let best = book.best_bid().unwrap();
        // Best bid should be approximately 100
        assert!(best >= dec!(99) && best <= dec!(101));
    }

    #[test]
    fn test_lock_free_spread() {
        let book = LockFreeOrderBook::new(Uuid::new_v4(), 100);

        let buy = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Buy,
            dec!(100),
            dec!(10),
        );
        let sell = LimitOrder::new(
            Uuid::new_v4(),
            book.token_id,
            OrderSide::Sell,
            dec!(105),
            dec!(10),
        );

        book.add_order_atomic(buy).unwrap();
        book.add_order_atomic(sell).unwrap();

        let spread = book.spread().unwrap();
        // Spread should be approximately 5
        assert!(spread >= dec!(4) && spread <= dec!(6));
    }
}