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
//! Auto-compounding staking implementation
//!
//! Automatically reinvests staking rewards to maximize returns through compound interest.

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

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

/// Auto-compounding staking vault
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoCompoundingVault {
    /// Vault ID
    pub id: Uuid,
    /// Token ID being staked
    pub token_id: Uuid,
    /// Total staked amount (including compounded rewards)
    pub total_staked: Decimal,
    /// Base APR (annual percentage rate)
    pub base_apr: Decimal,
    /// Compound frequency (times per year)
    pub compounds_per_year: u32,
    /// Effective APY (including compounding)
    pub effective_apy: Decimal,
    /// Last compound time
    pub last_compound_time: DateTime<Utc>,
    /// Total rewards distributed
    pub total_rewards_distributed: Decimal,
    /// Minimum gas cost for compounding
    pub min_gas_cost: Decimal,
    /// Created at
    pub created_at: DateTime<Utc>,
}

/// User's auto-compounding position
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompoundingPosition {
    /// Position ID
    pub id: Uuid,
    /// Vault ID
    pub vault_id: Uuid,
    /// User ID
    pub user_id: Uuid,
    /// Principal amount (original deposit)
    pub principal: Decimal,
    /// Current value (including compounded rewards)
    pub current_value: Decimal,
    /// Total rewards earned
    pub total_rewards_earned: Decimal,
    /// Number of compounds
    pub compound_count: u64,
    /// Share of vault (percentage)
    pub share_percentage: Decimal,
    /// Staked at
    pub staked_at: DateTime<Utc>,
    /// Last compound time
    pub last_compound_at: DateTime<Utc>,
}

/// Compound event record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompoundEvent {
    /// Event ID
    pub id: Uuid,
    /// Vault ID
    pub vault_id: Uuid,
    /// Reward amount compounded
    pub reward_amount: Decimal,
    /// Gas cost (if applicable)
    pub gas_cost: Decimal,
    /// Net reward (after gas)
    pub net_reward: Decimal,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

/// Compound strategy
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum CompoundStrategy {
    /// Fixed frequency (e.g., daily, weekly)
    FixedFrequency,
    /// Threshold-based (compound when rewards exceed threshold)
    ThresholdBased,
    /// Gas-optimized (compound when gas cost is favorable)
    GasOptimized,
    /// Hybrid (combine multiple strategies)
    Hybrid,
}

impl AutoCompoundingVault {
    /// Create a new auto-compounding vault
    pub fn new(
        token_id: Uuid,
        base_apr: Decimal,
        compounds_per_year: u32,
        min_gas_cost: Decimal,
    ) -> Self {
        let effective_apy = Self::calculate_apy(base_apr, compounds_per_year);

        Self {
            id: Uuid::new_v4(),
            token_id,
            total_staked: Decimal::ZERO,
            base_apr,
            compounds_per_year,
            effective_apy,
            last_compound_time: Utc::now(),
            total_rewards_distributed: Decimal::ZERO,
            min_gas_cost,
            created_at: Utc::now(),
        }
    }

    /// Calculate APY from APR and compound frequency
    /// Formula: APY = (1 + APR/n)^n - 1
    pub fn calculate_apy(apr: Decimal, compounds_per_year: u32) -> Decimal {
        if compounds_per_year == 0 {
            return apr;
        }

        let rate_per_period = apr / Decimal::from(compounds_per_year) / dec!(100);
        let mut result = dec!(1.0);

        for _ in 0..compounds_per_year {
            result *= dec!(1.0) + rate_per_period;
        }

        (result - dec!(1.0)) * dec!(100)
    }

    /// Stake and create a compounding position
    pub fn stake(&mut self, user_id: Uuid, amount: Decimal) -> Result<CompoundingPosition> {
        if amount <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Stake amount must be positive".to_string(),
            ));
        }

        let now = Utc::now();
        self.total_staked += amount;

        // Calculate share percentage
        let share_percentage = if self.total_staked > Decimal::ZERO {
            (amount / self.total_staked) * dec!(100)
        } else {
            dec!(100)
        };

        Ok(CompoundingPosition {
            id: Uuid::new_v4(),
            vault_id: self.id,
            user_id,
            principal: amount,
            current_value: amount,
            total_rewards_earned: Decimal::ZERO,
            compound_count: 0,
            share_percentage,
            staked_at: now,
            last_compound_at: now,
        })
    }

    /// Compound rewards for a position
    pub fn compound(
        &mut self,
        position: &mut CompoundingPosition,
        gas_cost: Decimal,
    ) -> Result<CompoundEvent> {
        // Calculate time since last compound
        let now = Utc::now();
        let time_diff = (now - position.last_compound_at).num_seconds() as f64;
        let year_in_seconds = 365.25 * 24.0 * 3600.0;
        let time_fraction =
            Decimal::from_f64_retain(time_diff / year_in_seconds).unwrap_or(Decimal::ZERO);

        // Calculate rewards: current_value * (APR / 100) * time_fraction
        let reward_amount = position.current_value * (self.base_apr / dec!(100)) * time_fraction;

        // Check if reward is worth compounding (gas cost consideration)
        let net_reward = reward_amount - gas_cost;
        if net_reward <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Reward too small to cover gas cost".to_string(),
            ));
        }

        // Add reward to current value (compounding)
        position.current_value += net_reward;
        position.total_rewards_earned += net_reward;
        position.compound_count += 1;
        position.last_compound_at = now;

        // Update vault
        self.total_staked += net_reward;
        self.total_rewards_distributed += reward_amount;
        self.last_compound_time = now;

        Ok(CompoundEvent {
            id: Uuid::new_v4(),
            vault_id: self.id,
            reward_amount,
            gas_cost,
            net_reward,
            timestamp: now,
        })
    }

    /// Calculate optimal compound frequency based on gas costs
    pub fn calculate_optimal_frequency(&self, position_size: Decimal, gas_price: Decimal) -> u32 {
        // Simplified model: compound when rewards > 2x gas cost
        let target_reward = gas_price * dec!(2);
        let time_to_target =
            target_reward / (position_size * self.base_apr / dec!(100) / dec!(365));

        // Convert to compounds per year
        let days_between_compounds = time_to_target.max(dec!(1));
        let compounds_per_year = (dec!(365) / days_between_compounds).to_u32().unwrap_or(365);

        compounds_per_year.clamp(1, 365)
    }

    /// Withdraw from position
    pub fn withdraw(
        &mut self,
        position: &mut CompoundingPosition,
        amount: Decimal,
    ) -> Result<Decimal> {
        if amount <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Withdrawal amount must be positive".to_string(),
            ));
        }

        if amount > position.current_value {
            return Err(CoreError::InsufficientBalance {
                required: amount,
                available: position.current_value,
            });
        }

        position.current_value -= amount;
        self.total_staked -= amount;

        Ok(amount)
    }

    /// Get vault statistics
    pub fn get_stats(&self) -> VaultStats {
        VaultStats {
            total_staked: self.total_staked,
            base_apr: self.base_apr,
            effective_apy: self.effective_apy,
            compounds_per_year: self.compounds_per_year,
            total_rewards_distributed: self.total_rewards_distributed,
        }
    }
}

/// Vault statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VaultStats {
    /// Total amount staked in the vault.
    pub total_staked: Decimal,
    /// Base annual percentage rate before compounding.
    pub base_apr: Decimal,
    /// Effective annual percentage yield after compounding.
    pub effective_apy: Decimal,
    /// Number of compounding events per year.
    pub compounds_per_year: u32,
    /// Total rewards distributed to stakers.
    pub total_rewards_distributed: Decimal,
}

/// Manager for auto-compounding vaults
#[derive(Debug, Clone, Default)]
pub struct AutoCompoundingManager {
    vaults: HashMap<Uuid, AutoCompoundingVault>,
    positions: HashMap<Uuid, Vec<CompoundingPosition>>,
    compound_events: Vec<CompoundEvent>,
}

impl AutoCompoundingManager {
    /// Create a new manager
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a vault
    pub fn create_vault(
        &mut self,
        token_id: Uuid,
        base_apr: Decimal,
        compounds_per_year: u32,
        min_gas_cost: Decimal,
    ) -> Uuid {
        let vault = AutoCompoundingVault::new(token_id, base_apr, compounds_per_year, min_gas_cost);
        let vault_id = vault.id;
        self.vaults.insert(vault_id, vault);
        vault_id
    }

    /// Stake in a vault
    pub fn stake(
        &mut self,
        vault_id: Uuid,
        user_id: Uuid,
        amount: Decimal,
    ) -> Result<CompoundingPosition> {
        let vault = self
            .vaults
            .get_mut(&vault_id)
            .ok_or(CoreError::NotFound("Vault not found".to_string()))?;

        let position = vault.stake(user_id, amount)?;
        self.positions
            .entry(user_id)
            .or_default()
            .push(position.clone());

        Ok(position)
    }

    /// Compound rewards for a user's position
    pub fn compound_position(
        &mut self,
        vault_id: Uuid,
        user_id: Uuid,
        position_id: Uuid,
        gas_cost: Decimal,
    ) -> Result<CompoundEvent> {
        let vault = self
            .vaults
            .get_mut(&vault_id)
            .ok_or(CoreError::NotFound("Vault not found".to_string()))?;

        let user_positions = self
            .positions
            .get_mut(&user_id)
            .ok_or(CoreError::NotFound("No positions found".to_string()))?;

        let position = user_positions
            .iter_mut()
            .find(|p| p.id == position_id)
            .ok_or(CoreError::NotFound("Position not found".to_string()))?;

        let event = vault.compound(position, gas_cost)?;
        self.compound_events.push(event.clone());

        Ok(event)
    }

    /// Compound all positions in a vault (batch operation)
    pub fn compound_all(
        &mut self,
        vault_id: Uuid,
        gas_cost_per_position: Decimal,
    ) -> Result<Vec<CompoundEvent>> {
        let mut events = Vec::new();

        // Collect all user IDs that have positions in this vault
        let user_ids: Vec<Uuid> = self
            .positions
            .iter()
            .filter_map(|(user_id, positions)| {
                if positions.iter().any(|p| p.vault_id == vault_id) {
                    Some(*user_id)
                } else {
                    None
                }
            })
            .collect();

        for user_id in user_ids {
            let position_ids: Vec<Uuid> = self
                .positions
                .get(&user_id)
                .map(|positions| {
                    positions
                        .iter()
                        .filter(|p| p.vault_id == vault_id)
                        .map(|p| p.id)
                        .collect()
                })
                .unwrap_or_default();

            for position_id in position_ids {
                if let Ok(event) =
                    self.compound_position(vault_id, user_id, position_id, gas_cost_per_position)
                {
                    events.push(event);
                }
            }
        }

        Ok(events)
    }

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

    /// Get vault
    pub fn get_vault(&self, vault_id: Uuid) -> Option<&AutoCompoundingVault> {
        self.vaults.get(&vault_id)
    }

    /// Calculate projected returns
    pub fn calculate_projection(
        &self,
        vault_id: Uuid,
        principal: Decimal,
        years: u32,
    ) -> Result<ProjectedReturns> {
        let vault = self
            .vaults
            .get(&vault_id)
            .ok_or(CoreError::NotFound("Vault not found".to_string()))?;

        let rate_per_period = vault.base_apr / Decimal::from(vault.compounds_per_year) / dec!(100);
        let total_periods = vault.compounds_per_year * years;

        let mut final_value = principal;
        for _ in 0..total_periods {
            final_value *= dec!(1.0) + rate_per_period;
        }

        let total_return = final_value - principal;
        let total_return_percentage = (total_return / principal) * dec!(100);

        Ok(ProjectedReturns {
            principal,
            final_value,
            total_return,
            total_return_percentage,
            years,
            effective_apy: vault.effective_apy,
        })
    }
}

/// Projected returns for a vault
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectedReturns {
    /// Initial principal amount invested.
    pub principal: Decimal,
    /// Final value after compounding over the projection period.
    pub final_value: Decimal,
    /// Total absolute return (final_value - principal).
    pub total_return: Decimal,
    /// Total return expressed as a percentage of principal.
    pub total_return_percentage: Decimal,
    /// Number of years used for projection.
    pub years: u32,
    /// Effective annual percentage yield applied.
    pub effective_apy: Decimal,
}

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

    #[test]
    fn test_apy_calculation() {
        // 10% APR compounded daily should give ~10.52% APY
        let apy = AutoCompoundingVault::calculate_apy(dec!(10), 365);
        assert!(apy > dec!(10.5) && apy < dec!(10.6));
    }

    #[test]
    fn test_vault_creation() {
        let vault = AutoCompoundingVault::new(Uuid::new_v4(), dec!(10), 365, dec!(0.01));
        assert_eq!(vault.base_apr, dec!(10));
        assert_eq!(vault.compounds_per_year, 365);
        assert!(vault.effective_apy > dec!(10));
    }

    #[test]
    fn test_stake_and_compound() {
        let mut vault = AutoCompoundingVault::new(Uuid::new_v4(), dec!(10), 365, dec!(0.01));
        let user_id = Uuid::new_v4();

        // Stake
        let mut position = vault.stake(user_id, dec!(1000)).unwrap();
        assert_eq!(position.principal, dec!(1000));
        assert_eq!(position.current_value, dec!(1000));

        // Simulate time passing by setting last_compound_at to 1 day ago
        position.last_compound_at = chrono::Utc::now() - chrono::Duration::days(1);
        let event = vault.compound(&mut position, dec!(0.01)).unwrap();

        // Should have earned some rewards (approx 1000 * 10% / 365 = ~0.27)
        assert!(event.net_reward > Decimal::ZERO);
        assert!(position.current_value > dec!(1000));
        assert_eq!(position.compound_count, 1);
    }

    #[test]
    fn test_gas_cost_prevents_small_compounds() {
        let mut vault = AutoCompoundingVault::new(Uuid::new_v4(), dec!(10), 365, dec!(0.01));
        let user_id = Uuid::new_v4();
        let mut position = vault.stake(user_id, dec!(10)).unwrap(); // Small amount

        // Try to compound immediately with high gas cost
        let result = vault.compound(&mut position, dec!(10));
        assert!(result.is_err()); // Should fail due to gas cost
    }

    #[test]
    fn test_optimal_frequency_calculation() {
        let vault = AutoCompoundingVault::new(Uuid::new_v4(), dec!(10), 365, dec!(0.01));

        // For a very large position with low gas, very frequent compounding is optimal
        let frequency_large = vault.calculate_optimal_frequency(dec!(1000000), dec!(0.01));

        // For a smaller position with same gas, less frequent compounding is optimal
        let frequency_medium = vault.calculate_optimal_frequency(dec!(10000), dec!(0.01));

        // For a very small position, even less frequent
        let frequency_small = vault.calculate_optimal_frequency(dec!(100), dec!(0.01));

        // Verify the relationship: larger positions should compound more frequently
        assert!(frequency_large >= frequency_medium);
        assert!(frequency_medium >= frequency_small);
    }

    #[test]
    fn test_manager_operations() {
        let mut manager = AutoCompoundingManager::new();
        let token_id = Uuid::new_v4();

        // Create vault
        let vault_id = manager.create_vault(token_id, dec!(10), 365, dec!(0.01));

        // Stake
        let user_id = Uuid::new_v4();
        let _position = manager.stake(vault_id, user_id, dec!(1000)).unwrap();

        // Check positions
        let positions = manager.get_user_positions(user_id);
        assert_eq!(positions.len(), 1);
        assert_eq!(positions[0].principal, dec!(1000));
    }

    #[test]
    fn test_projection_calculation() {
        let mut manager = AutoCompoundingManager::new();
        let vault_id = manager.create_vault(Uuid::new_v4(), dec!(10), 365, dec!(0.01));

        // Project 5 years
        let projection = manager
            .calculate_projection(vault_id, dec!(1000), 5)
            .unwrap();

        assert_eq!(projection.principal, dec!(1000));
        assert!(projection.final_value > dec!(1600)); // Should be ~1648 with 10% APR compounded
        assert!(projection.total_return_percentage > dec!(60));
    }
}