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
//! Concentrated liquidity (Uniswap V3 style)
//!
//! This module implements concentrated liquidity pools where liquidity providers
//! can specify price ranges for their liquidity, improving capital efficiency.
//!
//! Key features:
//! - Price ranges (ticks) for liquidity provision
//! - Virtual reserves within active ranges
//! - Fee accumulation per tick
//! - Position NFT representation
//! - Multiple positions per LP
//! - Improved capital efficiency over constant product AMMs

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

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

/// Tick spacing (minimum price movement)
/// Lower spacing = more granularity but higher gas costs
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TickSpacing {
    /// 0.01% spacing (most granular)
    VeryFine = 1,
    /// 0.05% spacing
    Fine = 5,
    /// 0.1% spacing
    Medium = 10,
    /// 0.3% spacing
    Coarse = 30,
    /// 1% spacing (least granular)
    VeryCoarse = 100,
}

impl TickSpacing {
    /// Get the spacing value
    pub fn value(&self) -> i32 {
        *self as i32
    }
}

/// Fee tier for the pool
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FeeTier {
    /// 0.01% fee
    VeryLow = 1,
    /// 0.05% fee (stablecoins)
    Low = 5,
    /// 0.3% fee (standard)
    Medium = 30,
    /// 1% fee (exotic pairs)
    High = 100,
}

impl FeeTier {
    /// Get fee as decimal (e.g., 0.003 for 0.3%)
    pub fn as_decimal(&self) -> Decimal {
        match self {
            FeeTier::VeryLow => dec!(0.0001),
            FeeTier::Low => dec!(0.0005),
            FeeTier::Medium => dec!(0.003),
            FeeTier::High => dec!(0.01),
        }
    }
}

/// Liquidity position in a concentrated liquidity pool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConcentratedPosition {
    /// Position ID (NFT token ID)
    pub id: String,
    /// Owner address
    pub owner: String,
    /// Pool ID
    pub pool_id: String,
    /// Lower tick (price bound)
    pub tick_lower: i32,
    /// Upper tick (price bound)
    pub tick_upper: i32,
    /// Liquidity amount
    pub liquidity: Decimal,
    /// Amount of token0 deposited
    pub amount0: Decimal,
    /// Amount of token1 deposited
    pub amount1: Decimal,
    /// Fees earned in token0
    pub fees_earned0: Decimal,
    /// Fees earned in token1
    pub fees_earned1: Decimal,
    /// Fee growth inside last (for calculating earned fees)
    pub fee_growth_inside0_last: Decimal,
    /// Fee growth inside last for token1
    pub fee_growth_inside1_last: Decimal,
    /// Created at
    pub created_at: DateTime<Utc>,
    /// Updated at
    pub updated_at: DateTime<Utc>,
}

impl ConcentratedPosition {
    /// Create a new concentrated position
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: String,
        owner: String,
        pool_id: String,
        tick_lower: i32,
        tick_upper: i32,
        liquidity: Decimal,
        amount0: Decimal,
        amount1: Decimal,
    ) -> Result<Self> {
        if tick_lower >= tick_upper {
            return Err(CoreError::Validation(
                "Lower tick must be less than upper tick".to_string(),
            ));
        }
        if liquidity <= dec!(0) {
            return Err(CoreError::Validation(
                "Liquidity must be positive".to_string(),
            ));
        }

        let now = Utc::now();
        Ok(Self {
            id,
            owner,
            pool_id,
            tick_lower,
            tick_upper,
            liquidity,
            amount0,
            amount1,
            fees_earned0: dec!(0),
            fees_earned1: dec!(0),
            fee_growth_inside0_last: dec!(0),
            fee_growth_inside1_last: dec!(0),
            created_at: now,
            updated_at: now,
        })
    }

    /// Check if position is in range at current tick
    pub fn is_in_range(&self, current_tick: i32) -> bool {
        current_tick >= self.tick_lower && current_tick < self.tick_upper
    }

    /// Update fees earned
    pub fn update_fees(&mut self, fee_growth_inside0: Decimal, fee_growth_inside1: Decimal) {
        // Calculate fees earned since last update
        let fee_growth_delta0 = fee_growth_inside0 - self.fee_growth_inside0_last;
        let fee_growth_delta1 = fee_growth_inside1 - self.fee_growth_inside1_last;

        self.fees_earned0 += self.liquidity * fee_growth_delta0;
        self.fees_earned1 += self.liquidity * fee_growth_delta1;

        self.fee_growth_inside0_last = fee_growth_inside0;
        self.fee_growth_inside1_last = fee_growth_inside1;
        self.updated_at = Utc::now();
    }

    /// Collect fees
    pub fn collect_fees(&mut self) -> (Decimal, Decimal) {
        let fees0 = self.fees_earned0;
        let fees1 = self.fees_earned1;

        self.fees_earned0 = dec!(0);
        self.fees_earned1 = dec!(0);
        self.updated_at = Utc::now();

        (fees0, fees1)
    }
}

/// Tick information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tick {
    /// Tick index
    pub index: i32,
    /// Liquidity gross (total liquidity at this tick)
    pub liquidity_gross: Decimal,
    /// Liquidity net (liquidity added/removed when crossing)
    pub liquidity_net: Decimal,
    /// Fee growth outside in token0
    pub fee_growth_outside0: Decimal,
    /// Fee growth outside in token1
    pub fee_growth_outside1: Decimal,
    /// Initialized flag
    pub initialized: bool,
}

impl Tick {
    /// Create a new uninitialized tick
    pub fn new(index: i32) -> Self {
        Self {
            index,
            liquidity_gross: dec!(0),
            liquidity_net: dec!(0),
            fee_growth_outside0: dec!(0),
            fee_growth_outside1: dec!(0),
            initialized: false,
        }
    }

    /// Update tick with liquidity delta
    pub fn update(&mut self, liquidity_delta: Decimal, upper: bool) {
        self.liquidity_gross += liquidity_delta.abs();

        if upper {
            self.liquidity_net -= liquidity_delta;
        } else {
            self.liquidity_net += liquidity_delta;
        }

        self.initialized = self.liquidity_gross != dec!(0);
    }
}

/// Concentrated liquidity pool (Uniswap V3 style)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConcentratedLiquidityPool {
    /// Pool ID
    pub id: String,
    /// Token0 symbol
    pub token0: String,
    /// Token1 symbol
    pub token1: String,
    /// Fee tier
    pub fee_tier: FeeTier,
    /// Tick spacing
    pub tick_spacing: TickSpacing,
    /// Current tick
    pub current_tick: i32,
    /// Current sqrt price (Q64.64 format simulation)
    pub sqrt_price: Decimal,
    /// Active liquidity at current price
    pub liquidity: Decimal,
    /// Reserve of token0
    pub reserve0: Decimal,
    /// Reserve of token1
    pub reserve1: Decimal,
    /// Global fee growth for token0
    pub fee_growth_global0: Decimal,
    /// Global fee growth for token1
    pub fee_growth_global1: Decimal,
    /// Tick data
    pub ticks: BTreeMap<i32, Tick>,
    /// Total value locked
    pub tvl: Decimal,
    /// Created at
    pub created_at: DateTime<Utc>,
    /// Updated at
    pub updated_at: DateTime<Utc>,
}

impl ConcentratedLiquidityPool {
    /// Create a new concentrated liquidity pool
    pub fn new(
        id: String,
        token0: String,
        token1: String,
        fee_tier: FeeTier,
        tick_spacing: TickSpacing,
        initial_sqrt_price: Decimal,
    ) -> Self {
        let current_tick = Self::sqrt_price_to_tick(initial_sqrt_price);
        let now = Utc::now();

        Self {
            id,
            token0,
            token1,
            fee_tier,
            tick_spacing,
            current_tick,
            sqrt_price: initial_sqrt_price,
            liquidity: dec!(0),
            reserve0: dec!(0),
            reserve1: dec!(0),
            fee_growth_global0: dec!(0),
            fee_growth_global1: dec!(0),
            ticks: BTreeMap::new(),
            tvl: dec!(0),
            created_at: now,
            updated_at: now,
        }
    }

    /// Convert sqrt price to tick (simplified)
    fn sqrt_price_to_tick(sqrt_price: Decimal) -> i32 {
        // Simplified: tick = log1.0001(price)
        // In production, would use proper fixed-point math
        let price = sqrt_price * sqrt_price;
        if price <= dec!(0) {
            return 0;
        }

        let log_price = (price.to_string().parse::<f64>().unwrap_or(1.0)).ln();
        let log_base = 1.0001_f64.ln();
        (log_price / log_base) as i32
    }

    /// Convert tick to sqrt price (simplified)
    fn tick_to_sqrt_price(tick: i32) -> Decimal {
        // price = 1.0001^tick
        // sqrt_price = 1.0001^(tick/2)
        let price = 1.0001_f64.powi(tick);
        Decimal::from_f64_retain(price.sqrt()).unwrap_or(dec!(1))
    }

    /// Add liquidity to a price range
    pub fn add_liquidity(
        &mut self,
        tick_lower: i32,
        tick_upper: i32,
        amount0_desired: Decimal,
        amount1_desired: Decimal,
    ) -> Result<(Decimal, Decimal, Decimal)> {
        // Validate ticks are aligned to spacing
        if tick_lower % self.tick_spacing.value() != 0
            || tick_upper % self.tick_spacing.value() != 0
        {
            return Err(CoreError::Validation(
                "Ticks must be aligned to tick spacing".to_string(),
            ));
        }

        // Calculate liquidity from amounts
        let liquidity =
            self.calculate_liquidity(tick_lower, tick_upper, amount0_desired, amount1_desired)?;

        // Update ticks
        self.update_tick(tick_lower, liquidity, false)?;
        self.update_tick(tick_upper, liquidity, true)?;

        // If position is in range, update active liquidity
        if self.current_tick >= tick_lower && self.current_tick < tick_upper {
            self.liquidity += liquidity;
        }

        // Calculate actual amounts
        let (amount0, amount1) = self.calculate_amounts(tick_lower, tick_upper, liquidity)?;

        self.reserve0 += amount0;
        self.reserve1 += amount1;
        self.tvl = self.reserve0 + self.reserve1; // Simplified TVL

        self.updated_at = Utc::now();

        Ok((liquidity, amount0, amount1))
    }

    /// Calculate liquidity from token amounts
    fn calculate_liquidity(
        &self,
        tick_lower: i32,
        tick_upper: i32,
        amount0: Decimal,
        amount1: Decimal,
    ) -> Result<Decimal> {
        let sqrt_price_lower = Self::tick_to_sqrt_price(tick_lower);
        let sqrt_price_upper = Self::tick_to_sqrt_price(tick_upper);

        // Simplified liquidity calculation
        // L = amount1 / (sqrt_upper - sqrt_lower)
        // or L = amount0 * sqrt_upper * sqrt_lower / (sqrt_upper - sqrt_lower)

        let liquidity_from_amount1 = if sqrt_price_upper > sqrt_price_lower {
            amount1 / (sqrt_price_upper - sqrt_price_lower)
        } else {
            dec!(0)
        };

        let liquidity_from_amount0 = if sqrt_price_upper > dec!(0) && sqrt_price_lower > dec!(0) {
            amount0 * sqrt_price_upper * sqrt_price_lower / (sqrt_price_upper - sqrt_price_lower)
        } else {
            dec!(0)
        };

        // Take the minimum to ensure both amounts are satisfied
        Ok(liquidity_from_amount0
            .min(liquidity_from_amount1)
            .max(dec!(1)))
    }

    /// Calculate token amounts from liquidity
    fn calculate_amounts(
        &self,
        tick_lower: i32,
        tick_upper: i32,
        liquidity: Decimal,
    ) -> Result<(Decimal, Decimal)> {
        let sqrt_price_lower = Self::tick_to_sqrt_price(tick_lower);
        let sqrt_price_upper = Self::tick_to_sqrt_price(tick_upper);
        let sqrt_price_current = self.sqrt_price;

        let amount0;
        let amount1;

        if self.current_tick < tick_lower {
            // Position is entirely in token0
            amount0 = liquidity * (sqrt_price_upper - sqrt_price_lower)
                / (sqrt_price_upper * sqrt_price_lower);
            amount1 = dec!(0);
        } else if self.current_tick >= tick_upper {
            // Position is entirely in token1
            amount0 = dec!(0);
            amount1 = liquidity * (sqrt_price_upper - sqrt_price_lower);
        } else {
            // Position is active (mixed)
            amount0 = liquidity * (sqrt_price_upper - sqrt_price_current)
                / (sqrt_price_upper * sqrt_price_current);
            amount1 = liquidity * (sqrt_price_current - sqrt_price_lower);
        }

        Ok((amount0.max(dec!(0)), amount1.max(dec!(0))))
    }

    /// Update tick with liquidity delta
    fn update_tick(&mut self, tick: i32, liquidity_delta: Decimal, upper: bool) -> Result<()> {
        let tick_data = self.ticks.entry(tick).or_insert_with(|| Tick::new(tick));
        tick_data.update(liquidity_delta, upper);
        Ok(())
    }

    /// Swap tokens (simplified)
    pub fn swap(&mut self, zero_for_one: bool, amount_in: Decimal) -> Result<Decimal> {
        if amount_in <= dec!(0) {
            return Err(CoreError::Validation(
                "Amount in must be positive".to_string(),
            ));
        }

        // Calculate fee
        let fee_amount = amount_in * self.fee_tier.as_decimal();
        let amount_in_less_fee = amount_in - fee_amount;

        // Simplified constant product swap within active liquidity
        let amount_out = if zero_for_one {
            // Swapping token0 for token1
            if self.reserve0 == dec!(0) || self.liquidity == dec!(0) {
                return Err(CoreError::Validation("Insufficient liquidity".to_string()));
            }

            let amount_out =
                (self.reserve1 * amount_in_less_fee) / (self.reserve0 + amount_in_less_fee);

            self.reserve0 += amount_in;
            self.reserve1 -= amount_out;

            // Update fee growth
            if self.liquidity > dec!(0) {
                self.fee_growth_global0 += fee_amount / self.liquidity;
            }

            amount_out
        } else {
            // Swapping token1 for token0
            if self.reserve1 == dec!(0) || self.liquidity == dec!(0) {
                return Err(CoreError::Validation("Insufficient liquidity".to_string()));
            }

            let amount_out =
                (self.reserve0 * amount_in_less_fee) / (self.reserve1 + amount_in_less_fee);

            self.reserve1 += amount_in;
            self.reserve0 -= amount_out;

            // Update fee growth
            if self.liquidity > dec!(0) {
                self.fee_growth_global1 += fee_amount / self.liquidity;
            }

            amount_out
        };

        // Update price and tick (simplified)
        if self.reserve0 > dec!(0) && self.reserve1 > dec!(0) {
            let new_price = self.reserve1 / self.reserve0;
            self.sqrt_price = Decimal::from_f64_retain(
                new_price.to_string().parse::<f64>().unwrap_or(1.0).sqrt(),
            )
            .unwrap_or(dec!(1));
            self.current_tick = Self::sqrt_price_to_tick(self.sqrt_price);
        }

        self.updated_at = Utc::now();

        Ok(amount_out)
    }

    /// Get price at current tick
    pub fn current_price(&self) -> Decimal {
        self.sqrt_price * self.sqrt_price
    }

    /// Calculate fee growth inside a range
    pub fn calculate_fee_growth_inside(
        &self,
        tick_lower: i32,
        tick_upper: i32,
    ) -> (Decimal, Decimal) {
        let lower_tick = self.ticks.get(&tick_lower);
        let upper_tick = self.ticks.get(&tick_upper);

        let fee_growth_below0 = lower_tick.map(|t| t.fee_growth_outside0).unwrap_or(dec!(0));
        let fee_growth_below1 = lower_tick.map(|t| t.fee_growth_outside1).unwrap_or(dec!(0));

        let fee_growth_above0 = upper_tick.map(|t| t.fee_growth_outside0).unwrap_or(dec!(0));
        let fee_growth_above1 = upper_tick.map(|t| t.fee_growth_outside1).unwrap_or(dec!(0));

        let fee_growth_inside0 = self.fee_growth_global0 - fee_growth_below0 - fee_growth_above0;
        let fee_growth_inside1 = self.fee_growth_global1 - fee_growth_below1 - fee_growth_above1;

        (fee_growth_inside0, fee_growth_inside1)
    }
}

/// Manager for concentrated liquidity positions
#[derive(Debug)]
pub struct ConcentratedLiquidityManager {
    /// Pools by ID
    pools: HashMap<String, ConcentratedLiquidityPool>,
    /// Positions by ID
    positions: HashMap<String, ConcentratedPosition>,
    /// Positions by owner
    positions_by_owner: HashMap<String, Vec<String>>,
    /// Next position ID
    next_position_id: u64,
}

impl ConcentratedLiquidityManager {
    /// Create a new manager
    pub fn new() -> Self {
        Self {
            pools: HashMap::new(),
            positions: HashMap::new(),
            positions_by_owner: HashMap::new(),
            next_position_id: 1,
        }
    }

    /// Create a new pool
    pub fn create_pool(
        &mut self,
        id: String,
        token0: String,
        token1: String,
        fee_tier: FeeTier,
        tick_spacing: TickSpacing,
        initial_sqrt_price: Decimal,
    ) -> Result<()> {
        if self.pools.contains_key(&id) {
            return Err(CoreError::Validation("Pool already exists".to_string()));
        }

        let pool = ConcentratedLiquidityPool::new(
            id.clone(),
            token0,
            token1,
            fee_tier,
            tick_spacing,
            initial_sqrt_price,
        );

        self.pools.insert(id, pool);
        Ok(())
    }

    /// Mint a new position
    #[allow(clippy::too_many_arguments)]
    pub fn mint_position(
        &mut self,
        owner: String,
        pool_id: String,
        tick_lower: i32,
        tick_upper: i32,
        amount0_desired: Decimal,
        amount1_desired: Decimal,
    ) -> Result<String> {
        let pool = self
            .pools
            .get_mut(&pool_id)
            .ok_or_else(|| CoreError::Validation("Pool not found".to_string()))?;

        let (liquidity, amount0, amount1) =
            pool.add_liquidity(tick_lower, tick_upper, amount0_desired, amount1_desired)?;

        let position_id = format!("pos-{}", self.next_position_id);
        self.next_position_id += 1;

        let position = ConcentratedPosition::new(
            position_id.clone(),
            owner.clone(),
            pool_id,
            tick_lower,
            tick_upper,
            liquidity,
            amount0,
            amount1,
        )?;

        self.positions.insert(position_id.clone(), position);
        self.positions_by_owner
            .entry(owner)
            .or_default()
            .push(position_id.clone());

        Ok(position_id)
    }

    /// Get positions by owner
    pub fn get_positions_by_owner(&self, owner: &str) -> Vec<&ConcentratedPosition> {
        self.positions_by_owner
            .get(owner)
            .map(|ids| ids.iter().filter_map(|id| self.positions.get(id)).collect())
            .unwrap_or_default()
    }

    /// Get pool
    pub fn get_pool(&self, pool_id: &str) -> Option<&ConcentratedLiquidityPool> {
        self.pools.get(pool_id)
    }

    /// Swap in a pool
    pub fn swap(
        &mut self,
        pool_id: &str,
        zero_for_one: bool,
        amount_in: Decimal,
    ) -> Result<Decimal> {
        let pool = self
            .pools
            .get_mut(pool_id)
            .ok_or_else(|| CoreError::Validation("Pool not found".to_string()))?;

        pool.swap(zero_for_one, amount_in)
    }
}

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

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

    #[test]
    fn test_concentrated_position_creation() {
        let position = ConcentratedPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "pool1".to_string(),
            -100,
            100,
            dec!(1000),
            dec!(100),
            dec!(100),
        )
        .unwrap();

        assert_eq!(position.liquidity, dec!(1000));
        assert_eq!(position.tick_lower, -100);
        assert_eq!(position.tick_upper, 100);
    }

    #[test]
    fn test_position_in_range() {
        let position = ConcentratedPosition::new(
            "pos1".to_string(),
            "user1".to_string(),
            "pool1".to_string(),
            -100,
            100,
            dec!(1000),
            dec!(100),
            dec!(100),
        )
        .unwrap();

        assert!(position.is_in_range(0));
        assert!(position.is_in_range(-50));
        assert!(position.is_in_range(50));
        assert!(!position.is_in_range(-101));
        assert!(!position.is_in_range(100));
    }

    #[test]
    fn test_pool_creation() {
        let pool = ConcentratedLiquidityPool::new(
            "pool1".to_string(),
            "USDC".to_string(),
            "USDT".to_string(),
            FeeTier::Low,
            TickSpacing::Fine,
            dec!(1),
        );

        assert_eq!(pool.token0, "USDC");
        assert_eq!(pool.token1, "USDT");
        assert_eq!(pool.liquidity, dec!(0));
    }

    #[test]
    fn test_manager_create_pool() {
        let mut manager = ConcentratedLiquidityManager::new();

        manager
            .create_pool(
                "pool1".to_string(),
                "USDC".to_string(),
                "USDT".to_string(),
                FeeTier::Low,
                TickSpacing::Fine,
                dec!(1),
            )
            .unwrap();

        assert!(manager.get_pool("pool1").is_some());
    }

    #[test]
    fn test_mint_position() {
        let mut manager = ConcentratedLiquidityManager::new();

        manager
            .create_pool(
                "pool1".to_string(),
                "USDC".to_string(),
                "USDT".to_string(),
                FeeTier::Low,
                TickSpacing::Medium,
                dec!(1),
            )
            .unwrap();

        let position_id = manager
            .mint_position(
                "user1".to_string(),
                "pool1".to_string(),
                -100,
                100,
                dec!(1000),
                dec!(1000),
            )
            .unwrap();

        assert!(position_id.starts_with("pos-"));

        let positions = manager.get_positions_by_owner("user1");
        assert_eq!(positions.len(), 1);
    }
}