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
//! Smart Order Routing (SOR) for optimal execution across venues
//!
//! This module provides intelligent routing of orders across multiple
//! liquidity venues to achieve best execution price and minimize costs.

use crate::error::{CoreError, Result};
use crate::models::{Order, OrderType};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Trading venue information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Venue {
    /// Unique identifier for this venue
    pub id: Uuid,
    /// Human-readable venue name
    pub name: String,
    /// Fee rate charged by this venue as a fraction
    pub fee_rate: Decimal,
    /// Liquidity available for trading on this venue
    pub available_liquidity: Decimal,
    /// Current mid price on this venue
    pub current_price: Decimal,
    /// Estimated slippage fraction for a typical order
    pub estimated_slippage: Decimal,
    /// Network round-trip latency to this venue in milliseconds
    pub latency_ms: u64,
}

impl Venue {
    /// Create a new venue with the given name and fee rate
    pub fn new(name: impl Into<String>, fee_rate: Decimal) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: name.into(),
            fee_rate,
            available_liquidity: Decimal::ZERO,
            current_price: Decimal::ZERO,
            estimated_slippage: Decimal::ZERO,
            latency_ms: 0,
        }
    }

    /// Set available liquidity on this venue
    pub fn with_liquidity(mut self, liquidity: Decimal) -> Self {
        self.available_liquidity = liquidity;
        self
    }

    /// Set current price on this venue
    pub fn with_price(mut self, price: Decimal) -> Self {
        self.current_price = price;
        self
    }

    /// Set estimated slippage for this venue
    pub fn with_slippage(mut self, slippage: Decimal) -> Self {
        self.estimated_slippage = slippage;
        self
    }

    /// Set network latency for this venue
    pub fn with_latency(mut self, latency_ms: u64) -> Self {
        self.latency_ms = latency_ms;
        self
    }

    /// Calculate total cost for executing given amount
    pub fn calculate_cost(&self, amount: Decimal, order_type: OrderType) -> Decimal {
        let base_cost = amount * self.current_price;
        let fee = base_cost * self.fee_rate;
        let slippage_cost = base_cost * self.estimated_slippage;

        match order_type {
            OrderType::Buy => base_cost + fee + slippage_cost,
            OrderType::Sell => base_cost - fee - slippage_cost,
        }
    }

    /// Calculate effective price including fees and slippage
    pub fn effective_price(&self, order_type: OrderType) -> Decimal {
        match order_type {
            OrderType::Buy => {
                self.current_price * (dec!(1) + self.fee_rate + self.estimated_slippage)
            }
            OrderType::Sell => {
                self.current_price * (dec!(1) - self.fee_rate - self.estimated_slippage)
            }
        }
    }
}

/// Route allocation for an order
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouteAllocation {
    /// Venue this allocation is routed to
    pub venue_id: Uuid,
    /// Human-readable name of the venue
    pub venue_name: String,
    /// Quantity allocated to this venue
    pub amount: Decimal,
    /// Estimated execution price at this venue
    pub estimated_price: Decimal,
    /// Estimated total cost at this venue including fees and slippage
    pub estimated_cost: Decimal,
}

/// Order routing strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RoutingStrategy {
    /// Route to venue with best price
    BestPrice,
    /// Route to minimize slippage
    MinimizeSlippage,
    /// Route to minimize fees
    MinimizeFees,
    /// Balance between price, slippage, and fees
    Balanced,
    /// Distribute across multiple venues
    Distributed,
}

/// Smart order router
#[derive(Debug, Clone)]
pub struct SmartOrderRouter {
    /// Available trading venues
    venues: Vec<Venue>,
    /// Routing strategy to apply
    strategy: RoutingStrategy,
}

impl SmartOrderRouter {
    /// Create a new smart order router with the given strategy
    pub fn new(strategy: RoutingStrategy) -> Self {
        Self {
            venues: Vec::new(),
            strategy,
        }
    }

    /// Register a venue with the router
    pub fn add_venue(&mut self, venue: Venue) {
        self.venues.push(venue);
    }

    /// Change the routing strategy
    pub fn set_strategy(&mut self, strategy: RoutingStrategy) {
        self.strategy = strategy;
    }

    /// Route an order across venues
    pub fn route(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        if self.venues.is_empty() {
            return Err(CoreError::Validation(
                "No venues available for routing".to_string(),
            ));
        }

        match self.strategy {
            RoutingStrategy::BestPrice => self.route_best_price(order),
            RoutingStrategy::MinimizeSlippage => self.route_min_slippage(order),
            RoutingStrategy::MinimizeFees => self.route_min_fees(order),
            RoutingStrategy::Balanced => self.route_balanced(order),
            RoutingStrategy::Distributed => self.route_distributed(order),
        }
    }

    fn route_best_price(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        // Find venue with best effective price
        let best_venue = match order.order_type {
            OrderType::Buy => self
                .venues
                .iter()
                .filter(|v| v.available_liquidity >= order.amount)
                .min_by(|a, b| {
                    a.effective_price(order.order_type)
                        .cmp(&b.effective_price(order.order_type))
                }),
            OrderType::Sell => self
                .venues
                .iter()
                .filter(|v| v.available_liquidity >= order.amount)
                .max_by(|a, b| {
                    a.effective_price(order.order_type)
                        .cmp(&b.effective_price(order.order_type))
                }),
        };

        if let Some(venue) = best_venue {
            Ok(vec![RouteAllocation {
                venue_id: venue.id,
                venue_name: venue.name.clone(),
                amount: order.amount,
                estimated_price: venue.effective_price(order.order_type),
                estimated_cost: venue.calculate_cost(order.amount, order.order_type),
            }])
        } else {
            // Split across multiple venues if no single venue has enough liquidity
            self.route_distributed(order)
        }
    }

    fn route_min_slippage(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        let best_venue = self
            .venues
            .iter()
            .filter(|v| v.available_liquidity >= order.amount)
            .min_by(|a, b| a.estimated_slippage.cmp(&b.estimated_slippage));

        if let Some(venue) = best_venue {
            Ok(vec![RouteAllocation {
                venue_id: venue.id,
                venue_name: venue.name.clone(),
                amount: order.amount,
                estimated_price: venue.effective_price(order.order_type),
                estimated_cost: venue.calculate_cost(order.amount, order.order_type),
            }])
        } else {
            self.route_distributed(order)
        }
    }

    fn route_min_fees(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        let best_venue = self
            .venues
            .iter()
            .filter(|v| v.available_liquidity >= order.amount)
            .min_by(|a, b| a.fee_rate.cmp(&b.fee_rate));

        if let Some(venue) = best_venue {
            Ok(vec![RouteAllocation {
                venue_id: venue.id,
                venue_name: venue.name.clone(),
                amount: order.amount,
                estimated_price: venue.effective_price(order.order_type),
                estimated_cost: venue.calculate_cost(order.amount, order.order_type),
            }])
        } else {
            self.route_distributed(order)
        }
    }

    fn route_balanced(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        // Score each venue based on multiple factors
        let mut scored_venues: Vec<_> = self
            .venues
            .iter()
            .map(|venue| {
                let price_score = self.calculate_price_score(venue, order.order_type);
                let slippage_score = dec!(1) - venue.estimated_slippage;
                let fee_score = dec!(1) - venue.fee_rate;
                let liquidity_score = (venue.available_liquidity / order.amount).min(dec!(1));

                // Weighted average
                let total_score = price_score * dec!(0.4)
                    + slippage_score * dec!(0.3)
                    + fee_score * dec!(0.2)
                    + liquidity_score * dec!(0.1);

                (venue, total_score)
            })
            .collect();

        scored_venues.sort_by(|a, b| b.1.cmp(&a.1));

        if let Some((best_venue, _)) = scored_venues.first() {
            if best_venue.available_liquidity >= order.amount {
                Ok(vec![RouteAllocation {
                    venue_id: best_venue.id,
                    venue_name: best_venue.name.clone(),
                    amount: order.amount,
                    estimated_price: best_venue.effective_price(order.order_type),
                    estimated_cost: best_venue.calculate_cost(order.amount, order.order_type),
                }])
            } else {
                self.route_distributed(order)
            }
        } else {
            Err(CoreError::Validation(
                "No suitable venues found".to_string(),
            ))
        }
    }

    fn route_distributed(&self, order: &Order) -> Result<Vec<RouteAllocation>> {
        let mut allocations = Vec::new();
        let mut remaining = order.amount;

        // Sort venues by effective price
        let mut sorted_venues = self.venues.clone();
        match order.order_type {
            OrderType::Buy => {
                sorted_venues.sort_by(|a, b| {
                    a.effective_price(order.order_type)
                        .cmp(&b.effective_price(order.order_type))
                });
            }
            OrderType::Sell => {
                sorted_venues.sort_by(|a, b| {
                    b.effective_price(order.order_type)
                        .cmp(&a.effective_price(order.order_type))
                });
            }
        }

        for venue in sorted_venues {
            if remaining == Decimal::ZERO {
                break;
            }

            let allocation_amount = remaining.min(venue.available_liquidity);
            if allocation_amount > Decimal::ZERO {
                allocations.push(RouteAllocation {
                    venue_id: venue.id,
                    venue_name: venue.name.clone(),
                    amount: allocation_amount,
                    estimated_price: venue.effective_price(order.order_type),
                    estimated_cost: venue.calculate_cost(allocation_amount, order.order_type),
                });
                remaining -= allocation_amount;
            }
        }

        if remaining > Decimal::ZERO {
            return Err(CoreError::InsufficientLiquidity(format!(
                "Cannot fulfill order: {} remaining after distribution",
                remaining
            )));
        }

        Ok(allocations)
    }

    fn calculate_price_score(&self, venue: &Venue, order_type: OrderType) -> Decimal {
        let all_prices: Vec<Decimal> = self
            .venues
            .iter()
            .map(|v| v.effective_price(order_type))
            .collect();

        let min_price = all_prices.iter().min().copied().unwrap_or(Decimal::ZERO);
        let max_price = all_prices.iter().max().copied().unwrap_or(Decimal::ONE);

        if max_price == min_price {
            return dec!(1);
        }

        match order_type {
            OrderType::Buy => {
                // Lower price is better for buy
                (max_price - venue.effective_price(order_type)) / (max_price - min_price)
            }
            OrderType::Sell => {
                // Higher price is better for sell
                (venue.effective_price(order_type) - min_price) / (max_price - min_price)
            }
        }
    }

    /// Calculate total execution quality score
    pub fn calculate_execution_quality(&self, allocations: &[RouteAllocation]) -> Decimal {
        if allocations.is_empty() {
            return Decimal::ZERO;
        }

        let total_amount: Decimal = allocations.iter().map(|a| a.amount).sum();
        if total_amount == Decimal::ZERO {
            return Decimal::ZERO;
        }

        let weighted_price: Decimal = allocations
            .iter()
            .map(|a| a.estimated_price * a.amount)
            .sum::<Decimal>()
            / total_amount;

        // Score based on how close to best available price
        let best_price = allocations
            .iter()
            .map(|a| a.estimated_price)
            .min()
            .unwrap_or(Decimal::ZERO);

        if weighted_price == Decimal::ZERO {
            return Decimal::ZERO;
        }

        best_price / weighted_price
    }
}

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

    fn create_test_order(amount: Decimal, order_type: OrderType) -> Order {
        Order {
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            order_type,
            amount,
            price_btc: dec!(100),
            total_btc: amount * dec!(100),
            status: crate::models::OrderStatus::Pending,
            btc_address: None,
            btc_txid: None,
            created_at: Utc::now(),
            completed_at: None,
        }
    }

    #[test]
    fn test_venue_cost_calculation() {
        let venue = Venue::new("Test Venue", dec!(0.001))
            .with_liquidity(dec!(1000))
            .with_price(dec!(100))
            .with_slippage(dec!(0.002));

        let buy_cost = venue.calculate_cost(dec!(10), OrderType::Buy);
        let sell_cost = venue.calculate_cost(dec!(10), OrderType::Sell);

        assert!(buy_cost > dec!(1000)); // Base + fees + slippage
        assert!(sell_cost < dec!(1000)); // Base - fees - slippage
    }

    #[test]
    fn test_best_price_routing() {
        let mut router = SmartOrderRouter::new(RoutingStrategy::BestPrice);

        router.add_venue(
            Venue::new("Venue A", dec!(0.003))
                .with_liquidity(dec!(1000))
                .with_price(dec!(100))
                .with_slippage(dec!(0.001)),
        );

        router.add_venue(
            Venue::new("Venue B", dec!(0.002))
                .with_liquidity(dec!(1000))
                .with_price(dec!(99))
                .with_slippage(dec!(0.001)),
        );

        let order = create_test_order(dec!(100), OrderType::Buy);
        let routes = router.route(&order).unwrap();

        assert_eq!(routes.len(), 1);
        assert_eq!(routes[0].venue_name, "Venue B"); // Better price
    }

    #[test]
    fn test_distributed_routing() {
        let mut router = SmartOrderRouter::new(RoutingStrategy::Distributed);

        router.add_venue(
            Venue::new("Venue A", dec!(0.001))
                .with_liquidity(dec!(100))
                .with_price(dec!(100))
                .with_slippage(dec!(0.001)),
        );

        router.add_venue(
            Venue::new("Venue B", dec!(0.001))
                .with_liquidity(dec!(150))
                .with_price(dec!(101))
                .with_slippage(dec!(0.001)),
        );

        let order = create_test_order(dec!(200), OrderType::Buy);
        let routes = router.route(&order).unwrap();

        assert_eq!(routes.len(), 2);
        let total: Decimal = routes.iter().map(|r| r.amount).sum();
        assert_eq!(total, order.amount);
    }

    #[test]
    fn test_insufficient_liquidity() {
        let mut router = SmartOrderRouter::new(RoutingStrategy::BestPrice);

        router.add_venue(
            Venue::new("Venue A", dec!(0.001))
                .with_liquidity(dec!(50))
                .with_price(dec!(100))
                .with_slippage(dec!(0.001)),
        );

        let order = create_test_order(dec!(200), OrderType::Buy);
        let result = router.route(&order);

        assert!(result.is_err());
    }

    #[test]
    fn test_min_slippage_routing() {
        let mut router = SmartOrderRouter::new(RoutingStrategy::MinimizeSlippage);

        router.add_venue(
            Venue::new("Venue A", dec!(0.001))
                .with_liquidity(dec!(1000))
                .with_price(dec!(100))
                .with_slippage(dec!(0.005)),
        );

        router.add_venue(
            Venue::new("Venue B", dec!(0.001))
                .with_liquidity(dec!(1000))
                .with_price(dec!(100))
                .with_slippage(dec!(0.001)),
        );

        let order = create_test_order(dec!(100), OrderType::Buy);
        let routes = router.route(&order).unwrap();

        assert_eq!(routes.len(), 1);
        assert_eq!(routes[0].venue_name, "Venue B"); // Lower slippage
    }

    #[test]
    fn test_execution_quality_score() {
        let router = SmartOrderRouter::new(RoutingStrategy::Balanced);

        let allocations = vec![
            RouteAllocation {
                venue_id: Uuid::new_v4(),
                venue_name: "Venue A".to_string(),
                amount: dec!(50),
                estimated_price: dec!(100),
                estimated_cost: dec!(5000),
            },
            RouteAllocation {
                venue_id: Uuid::new_v4(),
                venue_name: "Venue B".to_string(),
                amount: dec!(50),
                estimated_price: dec!(102),
                estimated_cost: dec!(5100),
            },
        ];

        let quality = router.calculate_execution_quality(&allocations);
        assert!(quality > Decimal::ZERO);
        assert!(quality <= dec!(1));
    }
}