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
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
//! Perpetual futures/contracts system
//!
//! This module implements perpetual futures contracts with:
//! - Long/short positions with leverage
//! - Funding rate mechanism to anchor to spot price
//! - Mark price calculation for liquidations
//! - Isolated and cross margin modes
//! - Insurance fund for socialized losses
//! - Automatic deleveraging (ADL) for bankrupt positions

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::error::{CoreError, Result};

/// Position side (long or short)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PositionSide {
    /// Long position (buy)
    Long,
    /// Short position (sell)
    Short,
}

impl PositionSide {
    /// Get the multiplier for PnL calculation (1 for long, -1 for short)
    pub fn multiplier(&self) -> Decimal {
        match self {
            PositionSide::Long => dec!(1),
            PositionSide::Short => dec!(-1),
        }
    }

    /// Get the opposite side
    pub fn opposite(&self) -> Self {
        match self {
            PositionSide::Long => PositionSide::Short,
            PositionSide::Short => PositionSide::Long,
        }
    }
}

/// Margin mode for positions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MarginMode {
    /// Isolated margin (position-specific collateral)
    Isolated,
    /// Cross margin (account-level collateral)
    Cross,
}

/// Perpetual position
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerpetualPosition {
    /// Position ID
    pub id: String,
    /// User ID
    pub user_id: String,
    /// Token/market symbol
    pub symbol: String,
    /// Position side
    pub side: PositionSide,
    /// Position size (in contracts)
    pub size: Decimal,
    /// Entry price (average)
    pub entry_price: Decimal,
    /// Leverage (1x to 125x)
    pub leverage: Decimal,
    /// Margin mode
    pub margin_mode: MarginMode,
    /// Initial margin (collateral)
    pub initial_margin: Decimal,
    /// Maintenance margin requirement
    pub maintenance_margin: Decimal,
    /// Unrealized PnL
    pub unrealized_pnl: Decimal,
    /// Realized PnL
    pub realized_pnl: Decimal,
    /// Liquidation price
    pub liquidation_price: Decimal,
    /// Funding payments accumulated
    pub funding_payments: Decimal,
    /// Created at
    pub created_at: DateTime<Utc>,
    /// Updated at
    pub updated_at: DateTime<Utc>,
}

impl PerpetualPosition {
    /// Create a new perpetual position
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: String,
        user_id: String,
        symbol: String,
        side: PositionSide,
        size: Decimal,
        entry_price: Decimal,
        leverage: Decimal,
        margin_mode: MarginMode,
    ) -> Result<Self> {
        if size <= dec!(0) {
            return Err(CoreError::Validation("Size must be positive".to_string()));
        }
        if entry_price <= dec!(0) {
            return Err(CoreError::Validation(
                "Entry price must be positive".to_string(),
            ));
        }
        if leverage < dec!(1) || leverage > dec!(125) {
            return Err(CoreError::Validation(
                "Leverage must be between 1x and 125x".to_string(),
            ));
        }

        let notional_value = size * entry_price;
        let initial_margin = notional_value / leverage;

        // Maintenance margin is typically 50% of initial margin
        let maintenance_margin = initial_margin * dec!(0.5);

        // Calculate liquidation price
        let liquidation_price = Self::calculate_liquidation_price(
            side,
            entry_price,
            initial_margin,
            maintenance_margin,
            size,
        )?;

        let now = Utc::now();
        Ok(Self {
            id,
            user_id,
            symbol,
            side,
            size,
            entry_price,
            leverage,
            margin_mode,
            initial_margin,
            maintenance_margin,
            unrealized_pnl: dec!(0),
            realized_pnl: dec!(0),
            liquidation_price,
            funding_payments: dec!(0),
            created_at: now,
            updated_at: now,
        })
    }

    /// Calculate liquidation price
    fn calculate_liquidation_price(
        side: PositionSide,
        entry_price: Decimal,
        initial_margin: Decimal,
        maintenance_margin: Decimal,
        size: Decimal,
    ) -> Result<Decimal> {
        if size == dec!(0) {
            return Err(CoreError::Validation(
                "Cannot calculate liquidation for zero size".to_string(),
            ));
        }

        let margin_diff = initial_margin - maintenance_margin;

        match side {
            PositionSide::Long => {
                // Long liquidation: entry_price - (margin_diff / size)
                Ok(entry_price - (margin_diff / size))
            }
            PositionSide::Short => {
                // Short liquidation: entry_price + (margin_diff / size)
                Ok(entry_price + (margin_diff / size))
            }
        }
    }

    /// Update unrealized PnL based on current mark price
    pub fn update_unrealized_pnl(&mut self, mark_price: Decimal) {
        let price_diff = mark_price - self.entry_price;
        self.unrealized_pnl = self.side.multiplier() * price_diff * self.size;
        self.updated_at = Utc::now();
    }

    /// Check if position should be liquidated
    pub fn should_liquidate(&self, mark_price: Decimal) -> bool {
        match self.side {
            PositionSide::Long => mark_price <= self.liquidation_price,
            PositionSide::Short => mark_price >= self.liquidation_price,
        }
    }

    /// Calculate current margin ratio
    pub fn margin_ratio(&self) -> Decimal {
        let total_margin = self.initial_margin + self.unrealized_pnl - self.funding_payments;
        if total_margin <= dec!(0) {
            return dec!(0);
        }
        total_margin / (self.size * self.entry_price)
    }

    /// Apply funding payment
    pub fn apply_funding(&mut self, funding_rate: Decimal, mark_price: Decimal) {
        let notional = self.size * mark_price;
        let payment = self.side.multiplier() * funding_rate * notional;

        // Long pays short when funding is positive
        // Short pays long when funding is negative
        self.funding_payments += payment;
        self.updated_at = Utc::now();
    }

    /// Close position and realize PnL
    pub fn close(&mut self, exit_price: Decimal) -> Decimal {
        let price_diff = exit_price - self.entry_price;
        let pnl = self.side.multiplier() * price_diff * self.size;

        self.realized_pnl += pnl + self.unrealized_pnl - self.funding_payments;
        self.unrealized_pnl = dec!(0);
        self.size = dec!(0);
        self.updated_at = Utc::now();

        self.realized_pnl
    }

    /// Reduce position size (partial close)
    pub fn reduce_size(&mut self, reduce_amount: Decimal, exit_price: Decimal) -> Result<Decimal> {
        if reduce_amount > self.size {
            return Err(CoreError::Validation(
                "Reduce amount exceeds position size".to_string(),
            ));
        }

        let price_diff = exit_price - self.entry_price;
        let pnl = self.side.multiplier() * price_diff * reduce_amount;

        self.realized_pnl += pnl;
        self.size -= reduce_amount;

        // Recalculate margins proportionally
        let size_ratio = self.size / (self.size + reduce_amount);
        self.initial_margin *= size_ratio;
        self.maintenance_margin *= size_ratio;

        // Recalculate liquidation price
        self.liquidation_price = Self::calculate_liquidation_price(
            self.side,
            self.entry_price,
            self.initial_margin,
            self.maintenance_margin,
            self.size,
        )?;

        self.updated_at = Utc::now();
        Ok(pnl)
    }

    /// Increase position size (add to position)
    pub fn increase_size(&mut self, add_amount: Decimal, add_price: Decimal) -> Result<()> {
        if add_amount <= dec!(0) {
            return Err(CoreError::Validation(
                "Add amount must be positive".to_string(),
            ));
        }

        let old_notional = self.size * self.entry_price;
        let new_notional = add_amount * add_price;
        let total_size = self.size + add_amount;

        // Calculate new average entry price
        self.entry_price = (old_notional + new_notional) / total_size;
        self.size = total_size;

        // Update margins
        let add_notional = add_amount * add_price;
        let add_margin = add_notional / self.leverage;
        self.initial_margin += add_margin;
        self.maintenance_margin = self.initial_margin * dec!(0.5);

        // Recalculate liquidation price
        self.liquidation_price = Self::calculate_liquidation_price(
            self.side,
            self.entry_price,
            self.initial_margin,
            self.maintenance_margin,
            self.size,
        )?;

        self.updated_at = Utc::now();
        Ok(())
    }
}

/// Funding rate information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FundingRate {
    /// Market symbol
    pub symbol: String,
    /// Current funding rate (per 8 hours typically)
    pub rate: Decimal,
    /// Mark price
    pub mark_price: Decimal,
    /// Index price (spot price)
    pub index_price: Decimal,
    /// Premium (mark - index)
    pub premium: Decimal,
    /// Next funding time
    pub next_funding_time: DateTime<Utc>,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

impl FundingRate {
    /// Create a new funding rate
    pub fn new(
        symbol: String,
        mark_price: Decimal,
        index_price: Decimal,
        funding_interval_hours: i64,
    ) -> Self {
        let premium = mark_price - index_price;

        // Funding rate = premium / index_price
        // Typically capped at +/- 0.05% per 8 hours
        let rate = if index_price > dec!(0) {
            let raw_rate = premium / index_price;
            // Cap at +/- 0.0005 (0.05%)
            raw_rate.max(dec!(-0.0005)).min(dec!(0.0005))
        } else {
            dec!(0)
        };

        let now = Utc::now();
        let next_funding_time = now + chrono::Duration::hours(funding_interval_hours);

        Self {
            symbol,
            rate,
            mark_price,
            index_price,
            premium,
            next_funding_time,
            timestamp: now,
        }
    }

    /// Update funding rate with new prices
    pub fn update(&mut self, mark_price: Decimal, index_price: Decimal) {
        self.mark_price = mark_price;
        self.index_price = index_price;
        self.premium = mark_price - index_price;

        self.rate = if index_price > dec!(0) {
            let raw_rate = self.premium / index_price;
            raw_rate.max(dec!(-0.0005)).min(dec!(0.0005))
        } else {
            dec!(0)
        };

        self.timestamp = Utc::now();
    }
}

/// Insurance fund for handling bankrupt positions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InsuranceFund {
    /// Total fund balance
    pub balance: Decimal,
    /// Contributions from liquidation fees
    pub total_contributions: Decimal,
    /// Payouts for bankrupt positions
    pub total_payouts: Decimal,
    /// Number of liquidations covered
    pub liquidations_covered: u64,
    /// Created at
    pub created_at: DateTime<Utc>,
    /// Updated at
    pub updated_at: DateTime<Utc>,
}

impl InsuranceFund {
    /// Create a new insurance fund
    pub fn new(initial_balance: Decimal) -> Self {
        let now = Utc::now();
        Self {
            balance: initial_balance,
            total_contributions: initial_balance,
            total_payouts: dec!(0),
            liquidations_covered: 0,
            created_at: now,
            updated_at: now,
        }
    }

    /// Add contribution to fund (from liquidation fees)
    pub fn add_contribution(&mut self, amount: Decimal) -> Result<()> {
        if amount < dec!(0) {
            return Err(CoreError::Validation(
                "Contribution must be non-negative".to_string(),
            ));
        }

        self.balance += amount;
        self.total_contributions += amount;
        self.updated_at = Utc::now();
        Ok(())
    }

    /// Use fund to cover bankrupt position
    pub fn cover_bankruptcy(&mut self, loss: Decimal) -> Result<bool> {
        if loss < dec!(0) {
            return Err(CoreError::Validation(
                "Loss must be non-negative".to_string(),
            ));
        }

        if self.balance >= loss {
            // Fund can cover the loss
            self.balance -= loss;
            self.total_payouts += loss;
            self.liquidations_covered += 1;
            self.updated_at = Utc::now();
            Ok(true)
        } else {
            // Fund cannot cover - need ADL (Auto-Deleveraging)
            Ok(false)
        }
    }

    /// Get fund health ratio
    pub fn health_ratio(&self) -> Decimal {
        if self.total_payouts == dec!(0) {
            return dec!(100);
        }
        (self.balance / self.total_payouts) * dec!(100)
    }
}

/// Perpetual futures manager
#[derive(Debug)]
pub struct PerpetualManager {
    /// Active positions by user
    positions: HashMap<String, Vec<PerpetualPosition>>,
    /// Funding rates by symbol
    funding_rates: HashMap<String, FundingRate>,
    /// Insurance fund
    insurance_fund: InsuranceFund,
    /// Funding interval in hours
    funding_interval_hours: i64,
}

impl PerpetualManager {
    /// Create a new perpetual manager
    pub fn new(initial_insurance_balance: Decimal, funding_interval_hours: i64) -> Self {
        Self {
            positions: HashMap::new(),
            funding_rates: HashMap::new(),
            insurance_fund: InsuranceFund::new(initial_insurance_balance),
            funding_interval_hours,
        }
    }

    /// Open a new position
    #[allow(clippy::too_many_arguments)]
    pub fn open_position(
        &mut self,
        id: String,
        user_id: String,
        symbol: String,
        side: PositionSide,
        size: Decimal,
        entry_price: Decimal,
        leverage: Decimal,
        margin_mode: MarginMode,
    ) -> Result<PerpetualPosition> {
        let position = PerpetualPosition::new(
            id,
            user_id.clone(),
            symbol,
            side,
            size,
            entry_price,
            leverage,
            margin_mode,
        )?;

        self.positions
            .entry(user_id)
            .or_default()
            .push(position.clone());

        Ok(position)
    }

    /// Get user positions
    pub fn get_user_positions(&self, user_id: &str) -> Vec<&PerpetualPosition> {
        self.positions
            .get(user_id)
            .map(|positions| positions.iter().collect())
            .unwrap_or_default()
    }

    /// Update funding rate for a symbol
    pub fn update_funding_rate(
        &mut self,
        symbol: String,
        mark_price: Decimal,
        index_price: Decimal,
    ) {
        self.funding_rates
            .entry(symbol.clone())
            .and_modify(|fr| fr.update(mark_price, index_price))
            .or_insert_with(|| {
                FundingRate::new(symbol, mark_price, index_price, self.funding_interval_hours)
            });
    }

    /// Apply funding to all positions for a symbol
    pub fn apply_funding(&mut self, symbol: &str) -> Result<()> {
        let funding_rate = self.funding_rates.get(symbol).ok_or_else(|| {
            CoreError::Validation(format!("No funding rate found for {}", symbol))
        })?;

        let rate = funding_rate.rate;
        let mark_price = funding_rate.mark_price;

        for positions in self.positions.values_mut() {
            for position in positions.iter_mut() {
                if position.symbol == symbol && position.size > dec!(0) {
                    position.apply_funding(rate, mark_price);
                }
            }
        }

        Ok(())
    }

    /// Liquidate positions that hit liquidation price
    pub fn liquidate_positions(
        &mut self,
        symbol: &str,
        mark_price: Decimal,
    ) -> Result<Vec<String>> {
        let mut liquidated_ids = Vec::new();

        for (user_id, positions) in self.positions.iter_mut() {
            for position in positions.iter_mut() {
                if position.symbol == symbol && position.should_liquidate(mark_price) {
                    // Calculate loss
                    position.update_unrealized_pnl(mark_price);
                    let loss = position.initial_margin + position.unrealized_pnl;

                    if loss < dec!(0) {
                        // Bankrupt position - try insurance fund
                        let bankruptcy_loss = -loss;
                        if !self.insurance_fund.cover_bankruptcy(bankruptcy_loss)? {
                            // Insurance fund cannot cover - would need ADL here
                            // For now, just mark as liquidated
                        }
                    }

                    liquidated_ids.push(format!("{}:{}", user_id, position.id));
                    position.close(mark_price);
                }
            }
        }

        Ok(liquidated_ids)
    }

    /// Close a position
    pub fn close_position(
        &mut self,
        user_id: &str,
        position_id: &str,
        exit_price: Decimal,
    ) -> Result<Decimal> {
        let positions = self.positions.get_mut(user_id).ok_or_else(|| {
            CoreError::Validation(format!("No positions found for user {}", user_id))
        })?;

        let position = positions
            .iter_mut()
            .find(|p| p.id == position_id)
            .ok_or_else(|| CoreError::Validation(format!("Position {} not found", position_id)))?;

        Ok(position.close(exit_price))
    }

    /// Get funding rate for symbol
    pub fn get_funding_rate(&self, symbol: &str) -> Option<&FundingRate> {
        self.funding_rates.get(symbol)
    }

    /// Get insurance fund info
    pub fn get_insurance_fund(&self) -> &InsuranceFund {
        &self.insurance_fund
    }

    /// Add contribution to insurance fund
    pub fn add_insurance_contribution(&mut self, amount: Decimal) -> Result<()> {
        self.insurance_fund.add_contribution(amount)
    }
}

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

    #[test]
    fn test_perpetual_position_creation() {
        let position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Long,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        assert_eq!(position.size, dec!(1));
        assert_eq!(position.entry_price, dec!(50000));
        assert_eq!(position.leverage, dec!(10));
        assert_eq!(position.initial_margin, dec!(5000)); // 50000 / 10
    }

    #[test]
    fn test_unrealized_pnl_long() {
        let mut position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Long,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        position.update_unrealized_pnl(dec!(51000));
        assert_eq!(position.unrealized_pnl, dec!(1000)); // (51000 - 50000) * 1
    }

    #[test]
    fn test_unrealized_pnl_short() {
        let mut position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Short,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        position.update_unrealized_pnl(dec!(49000));
        assert_eq!(position.unrealized_pnl, dec!(1000)); // (50000 - 49000) * 1
    }

    #[test]
    fn test_liquidation_long() {
        let position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Long,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        // Long should liquidate when price drops below liquidation price
        assert!(position.liquidation_price < dec!(50000));
        assert!(position.should_liquidate(position.liquidation_price - dec!(1)));
    }

    #[test]
    fn test_funding_rate_calculation() {
        let funding = FundingRate::new(
            "BTC-PERP".to_string(),
            dec!(50100), // Mark price
            dec!(50000), // Index price
            8,
        );

        // Premium = 100, rate should be ~0.002 (0.2%) but capped
        assert!(funding.rate > dec!(0));
        assert!(funding.rate <= dec!(0.0005)); // Capped at 0.05%
    }

    #[test]
    fn test_funding_payment() {
        let mut position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Long,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        // Apply funding with positive rate (longs pay shorts)
        position.apply_funding(dec!(0.0001), dec!(50000));

        // Funding payment should be positive (long pays)
        assert!(position.funding_payments > dec!(0));
    }

    #[test]
    fn test_position_close() {
        let mut position = PerpetualPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "BTC-PERP".to_string(),
            PositionSide::Long,
            dec!(1),
            dec!(50000),
            dec!(10),
            MarginMode::Isolated,
        )
        .unwrap();

        let realized_pnl = position.close(dec!(51000));
        assert_eq!(realized_pnl, dec!(1000)); // Profit
        assert_eq!(position.size, dec!(0));
    }

    #[test]
    fn test_insurance_fund() {
        let mut fund = InsuranceFund::new(dec!(100000));

        fund.add_contribution(dec!(10000)).unwrap();
        assert_eq!(fund.balance, dec!(110000));

        let covered = fund.cover_bankruptcy(dec!(5000)).unwrap();
        assert!(covered);
        assert_eq!(fund.balance, dec!(105000));
        assert_eq!(fund.liquidations_covered, 1);
    }

    #[test]
    fn test_perpetual_manager() {
        let mut manager = PerpetualManager::new(dec!(100000), 8);

        let position = manager
            .open_position(
                "pos1".to_string(),
                "user1".to_string(),
                "BTC-PERP".to_string(),
                PositionSide::Long,
                dec!(1),
                dec!(50000),
                dec!(10),
                MarginMode::Isolated,
            )
            .unwrap();

        assert_eq!(position.size, dec!(1));

        let user_positions = manager.get_user_positions("user1");
        assert_eq!(user_positions.len(), 1);
    }
}