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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Advanced governance features
//!
//! This module provides vote delegation, vote escrow (veToken), and conviction voting
//! mechanisms for enhanced governance participation.

use crate::error::{CoreError, Result};
use chrono::{DateTime, Utc};
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::collections::HashMap;
use uuid::Uuid;

/// Vote delegation record
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct VoteDelegation {
    /// Unique identifier for this delegation
    pub delegation_id: Uuid,
    /// User who is delegating their voting power
    pub delegator_user_id: Uuid,
    /// User who receives the delegated voting power
    pub delegate_user_id: Uuid,
    /// Amount of voting power delegated
    pub voting_power: Decimal,
    /// When the delegation becomes active
    pub start_time: DateTime<Utc>,
    /// When the delegation ends (None = permanent)
    pub end_time: Option<DateTime<Utc>>,
    /// Whether the delegation is currently active
    pub is_active: bool,
    /// Timestamp when the delegation was created
    pub created_at: DateTime<Utc>,
    /// Timestamp when the delegation was revoked, if applicable
    pub revoked_at: Option<DateTime<Utc>>,
}

impl VoteDelegation {
    /// Create a new vote delegation
    pub fn new(
        delegator_user_id: Uuid,
        delegate_user_id: Uuid,
        voting_power: Decimal,
        duration_days: Option<u32>,
    ) -> Result<Self> {
        if voting_power <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Voting power must be positive".to_string(),
            ));
        }

        if delegator_user_id == delegate_user_id {
            return Err(CoreError::Validation(
                "Cannot delegate to yourself".to_string(),
            ));
        }

        let start_time = Utc::now();
        let end_time = duration_days.map(|days| start_time + chrono::Duration::days(days as i64));

        Ok(Self {
            delegation_id: Uuid::new_v4(),
            delegator_user_id,
            delegate_user_id,
            voting_power,
            start_time,
            end_time,
            is_active: true,
            created_at: Utc::now(),
            revoked_at: None,
        })
    }

    /// Check if delegation is currently valid
    pub fn is_valid(&self) -> bool {
        let now = Utc::now();
        self.is_active && now >= self.start_time && self.end_time.is_none_or(|end| now <= end)
    }

    /// Revoke delegation
    pub fn revoke(&mut self) {
        self.is_active = false;
        self.revoked_at = Some(Utc::now());
    }
}

/// Vote escrow (veToken) lock
///
/// Users lock tokens for a duration to receive boosted voting power
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct VoteEscrowLock {
    /// Unique identifier for this lock
    pub lock_id: Uuid,
    /// User who created the lock
    pub user_id: Uuid,
    /// Amount of tokens locked
    pub amount: Decimal,
    /// Lock start time
    pub lock_start: DateTime<Utc>,
    /// Lock end time
    pub lock_end: DateTime<Utc>,
    /// Calculated voting power (amount * time multiplier)
    pub voting_power: Decimal,
    /// Whether the lock is still active
    pub is_active: bool,
    /// Timestamp when this lock was created
    pub created_at: DateTime<Utc>,
    /// Timestamp when tokens were unlocked, if applicable
    pub unlocked_at: Option<DateTime<Utc>>,
}

impl VoteEscrowLock {
    /// Maximum lock duration in days (4 years)
    pub const MAX_LOCK_DAYS: i64 = 1461; // 4 * 365 + 1 for leap year

    /// Create a new vote escrow lock
    pub fn new(user_id: Uuid, amount: Decimal, lock_duration_days: i64) -> Result<Self> {
        if amount <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Lock amount must be positive".to_string(),
            ));
        }

        if lock_duration_days <= 0 {
            return Err(CoreError::Validation(
                "Lock duration must be positive".to_string(),
            ));
        }

        if lock_duration_days > Self::MAX_LOCK_DAYS {
            return Err(CoreError::Validation(format!(
                "Lock duration cannot exceed {} days",
                Self::MAX_LOCK_DAYS
            )));
        }

        let lock_start = Utc::now();
        let lock_end = lock_start + chrono::Duration::days(lock_duration_days);

        // Calculate voting power: amount * (lock_duration / max_duration)
        // Longer locks get more voting power per token
        let time_multiplier =
            Decimal::from(lock_duration_days) / Decimal::from(Self::MAX_LOCK_DAYS);
        let voting_power = amount * (Decimal::ONE + time_multiplier);

        Ok(Self {
            lock_id: Uuid::new_v4(),
            user_id,
            amount,
            lock_start,
            lock_end,
            voting_power,
            is_active: true,
            created_at: Utc::now(),
            unlocked_at: None,
        })
    }

    /// Check if lock has expired
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.lock_end
    }

    /// Get remaining lock time in days
    pub fn remaining_days(&self) -> i64 {
        if self.is_expired() {
            return 0;
        }
        (self.lock_end - Utc::now()).num_days()
    }

    /// Calculate current voting power (decays over time)
    pub fn current_voting_power(&self) -> Decimal {
        if !self.is_active || self.is_expired() {
            return Decimal::ZERO;
        }

        let remaining_days = self.remaining_days();
        let time_multiplier = Decimal::from(remaining_days) / Decimal::from(Self::MAX_LOCK_DAYS);
        self.amount * (Decimal::ONE + time_multiplier)
    }

    /// Unlock tokens (with penalty if early)
    pub fn unlock(&mut self, early_unlock_penalty: Decimal) -> Result<Decimal> {
        if !self.is_active {
            return Err(CoreError::InvalidState("Lock is not active".to_string()));
        }

        self.is_active = false;
        self.unlocked_at = Some(Utc::now());

        // Apply penalty if unlocking early
        if !self.is_expired() {
            let penalty_amount = self.amount * early_unlock_penalty;
            Ok(self.amount - penalty_amount)
        } else {
            Ok(self.amount)
        }
    }
}

/// Conviction voting record
///
/// Votes gain strength over time, encouraging long-term commitment
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ConvictionVote {
    /// Unique identifier for this conviction vote
    pub vote_id: Uuid,
    /// Proposal being voted on
    pub proposal_id: Uuid,
    /// User who cast the vote
    pub user_id: Uuid,
    /// Base voting power (from tokens)
    pub base_voting_power: Decimal,
    /// Vote direction (true = for, false = against)
    pub vote_for: bool,
    /// When the vote was cast
    pub vote_time: DateTime<Utc>,
    /// When the vote was removed (if applicable)
    pub removed_at: Option<DateTime<Utc>>,
    /// Whether the vote is still active
    pub is_active: bool,
}

impl ConvictionVote {
    /// Create a new conviction vote
    pub fn new(
        proposal_id: Uuid,
        user_id: Uuid,
        base_voting_power: Decimal,
        vote_for: bool,
    ) -> Result<Self> {
        if base_voting_power <= Decimal::ZERO {
            return Err(CoreError::Validation(
                "Voting power must be positive".to_string(),
            ));
        }

        Ok(Self {
            vote_id: Uuid::new_v4(),
            proposal_id,
            user_id,
            base_voting_power,
            vote_for,
            vote_time: Utc::now(),
            removed_at: None,
            is_active: true,
        })
    }

    /// Calculate current conviction (voting power grows over time)
    ///
    /// Uses formula: conviction = base_power * (1 + time_factor)
    /// where time_factor = sqrt(days_active) / 10
    pub fn current_conviction(&self) -> Decimal {
        if !self.is_active {
            return Decimal::ZERO;
        }

        let days_active = (Utc::now() - self.vote_time).num_days() as f64;
        if days_active < 0.0 {
            return self.base_voting_power;
        }

        // Conviction grows with square root of time (diminishing returns)
        let time_factor = (days_active.sqrt() / 10.0).min(2.0); // Cap at 3x multiplier
        let multiplier = 1.0 + time_factor;

        self.base_voting_power * Decimal::from_f64(multiplier).unwrap_or(Decimal::ONE)
    }

    /// Remove vote (triggers conviction decay)
    pub fn remove(&mut self) {
        self.is_active = false;
        self.removed_at = Some(Utc::now());
    }

    /// Calculate conviction decay (half-life of 7 days after removal)
    pub fn conviction_after_removal(&self) -> Decimal {
        let Some(removed_at) = self.removed_at else {
            return self.current_conviction();
        };

        let days_since_removal = (Utc::now() - removed_at).num_days() as f64;
        if days_since_removal < 0.0 {
            return Decimal::ZERO;
        }

        // Half-life decay: conviction * 0.5^(days / 7)
        let half_lives = days_since_removal / 7.0;
        let decay_factor = 0.5_f64.powf(half_lives);

        self.base_voting_power * Decimal::from_f64(decay_factor).unwrap_or(Decimal::ZERO)
    }
}

/// Delegation manager
pub struct DelegationManager {
    /// All delegations by ID
    delegations: HashMap<Uuid, VoteDelegation>,
    /// Delegator -> List of delegation IDs
    delegator_index: HashMap<Uuid, Vec<Uuid>>,
    /// Delegate -> List of delegation IDs
    delegate_index: HashMap<Uuid, Vec<Uuid>>,
}

impl DelegationManager {
    /// Create a new delegation manager
    pub fn new() -> Self {
        Self {
            delegations: HashMap::new(),
            delegator_index: HashMap::new(),
            delegate_index: HashMap::new(),
        }
    }

    /// Create a new delegation
    pub fn delegate(
        &mut self,
        delegator_user_id: Uuid,
        delegate_user_id: Uuid,
        voting_power: Decimal,
        duration_days: Option<u32>,
    ) -> Result<Uuid> {
        let delegation = VoteDelegation::new(
            delegator_user_id,
            delegate_user_id,
            voting_power,
            duration_days,
        )?;

        let delegation_id = delegation.delegation_id;

        // Update indexes
        self.delegator_index
            .entry(delegator_user_id)
            .or_default()
            .push(delegation_id);

        self.delegate_index
            .entry(delegate_user_id)
            .or_default()
            .push(delegation_id);

        self.delegations.insert(delegation_id, delegation);
        Ok(delegation_id)
    }

    /// Revoke a delegation
    pub fn revoke_delegation(&mut self, delegation_id: Uuid) -> Result<()> {
        let delegation = self
            .delegations
            .get_mut(&delegation_id)
            .ok_or_else(|| CoreError::NotFound("Delegation not found".to_string()))?;

        delegation.revoke();
        Ok(())
    }

    /// Get total voting power for a user (including delegations)
    pub fn get_total_voting_power(&self, user_id: Uuid) -> Decimal {
        let mut total = Decimal::ZERO;

        // Get delegations where user is the delegate
        if let Some(delegation_ids) = self.delegate_index.get(&user_id) {
            for delegation_id in delegation_ids {
                if let Some(delegation) = self.delegations.get(delegation_id) {
                    if delegation.is_valid() {
                        total += delegation.voting_power;
                    }
                }
            }
        }

        total
    }

    /// Get all delegations for a user (as delegator)
    pub fn get_delegations_by_delegator(&self, user_id: Uuid) -> Vec<&VoteDelegation> {
        self.delegator_index
            .get(&user_id)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.delegations.get(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get all delegations received by a user (as delegate)
    pub fn get_delegations_received(&self, user_id: Uuid) -> Vec<&VoteDelegation> {
        self.delegate_index
            .get(&user_id)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.delegations.get(id))
                    .filter(|d| d.is_valid())
                    .collect()
            })
            .unwrap_or_default()
    }
}

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

/// Vote escrow manager
pub struct VoteEscrowManager {
    /// All escrow locks by ID
    locks: HashMap<Uuid, VoteEscrowLock>,
    /// User ID to list of lock IDs
    user_locks: HashMap<Uuid, Vec<Uuid>>,
    /// Early unlock penalty (e.g., 0.5 = 50% penalty)
    early_unlock_penalty: Decimal,
}

impl VoteEscrowManager {
    /// Create a new vote escrow manager with the given early-unlock penalty fraction
    pub fn new(early_unlock_penalty: Decimal) -> Result<Self> {
        if early_unlock_penalty < Decimal::ZERO || early_unlock_penalty > Decimal::ONE {
            return Err(CoreError::Validation(
                "Early unlock penalty must be between 0 and 1".to_string(),
            ));
        }

        Ok(Self {
            locks: HashMap::new(),
            user_locks: HashMap::new(),
            early_unlock_penalty,
        })
    }

    /// Create a new vote escrow lock
    pub fn create_lock(
        &mut self,
        user_id: Uuid,
        amount: Decimal,
        lock_duration_days: i64,
    ) -> Result<Uuid> {
        let lock = VoteEscrowLock::new(user_id, amount, lock_duration_days)?;
        let lock_id = lock.lock_id;

        self.user_locks.entry(user_id).or_default().push(lock_id);

        self.locks.insert(lock_id, lock);
        Ok(lock_id)
    }

    /// Unlock tokens
    pub fn unlock(&mut self, lock_id: Uuid) -> Result<Decimal> {
        let lock = self
            .locks
            .get_mut(&lock_id)
            .ok_or_else(|| CoreError::NotFound("Lock not found".to_string()))?;

        lock.unlock(self.early_unlock_penalty)
    }

    /// Get total voting power for a user from all active locks
    pub fn get_user_voting_power(&self, user_id: Uuid) -> Decimal {
        self.user_locks
            .get(&user_id)
            .map(|lock_ids| {
                lock_ids
                    .iter()
                    .filter_map(|id| self.locks.get(id))
                    .map(|lock| lock.current_voting_power())
                    .sum()
            })
            .unwrap_or(Decimal::ZERO)
    }

    /// Get all locks for a user
    pub fn get_user_locks(&self, user_id: Uuid) -> Vec<&VoteEscrowLock> {
        self.user_locks
            .get(&user_id)
            .map(|ids| ids.iter().filter_map(|id| self.locks.get(id)).collect())
            .unwrap_or_default()
    }

    /// Get lock by ID
    pub fn get_lock(&self, lock_id: Uuid) -> Option<&VoteEscrowLock> {
        self.locks.get(&lock_id)
    }
}

/// Conviction voting manager
pub struct ConvictionVotingManager {
    /// All conviction votes by ID
    votes: HashMap<Uuid, ConvictionVote>,
    /// Proposal ID -> List of vote IDs
    proposal_votes: HashMap<Uuid, Vec<Uuid>>,
    /// User ID -> List of vote IDs
    user_votes: HashMap<Uuid, Vec<Uuid>>,
}

impl ConvictionVotingManager {
    /// Create a new conviction voting manager
    pub fn new() -> Self {
        Self {
            votes: HashMap::new(),
            proposal_votes: HashMap::new(),
            user_votes: HashMap::new(),
        }
    }

    /// Cast a conviction vote
    pub fn cast_vote(
        &mut self,
        proposal_id: Uuid,
        user_id: Uuid,
        base_voting_power: Decimal,
        vote_for: bool,
    ) -> Result<Uuid> {
        let vote = ConvictionVote::new(proposal_id, user_id, base_voting_power, vote_for)?;
        let vote_id = vote.vote_id;

        self.proposal_votes
            .entry(proposal_id)
            .or_default()
            .push(vote_id);

        self.user_votes.entry(user_id).or_default().push(vote_id);

        self.votes.insert(vote_id, vote);
        Ok(vote_id)
    }

    /// Remove a vote
    pub fn remove_vote(&mut self, vote_id: Uuid) -> Result<()> {
        let vote = self
            .votes
            .get_mut(&vote_id)
            .ok_or_else(|| CoreError::NotFound("Vote not found".to_string()))?;

        vote.remove();
        Ok(())
    }

    /// Get total conviction for a proposal
    pub fn get_proposal_conviction(&self, proposal_id: Uuid) -> (Decimal, Decimal) {
        let mut votes_for = Decimal::ZERO;
        let mut votes_against = Decimal::ZERO;

        if let Some(vote_ids) = self.proposal_votes.get(&proposal_id) {
            for vote_id in vote_ids {
                if let Some(vote) = self.votes.get(vote_id) {
                    let conviction = vote.current_conviction();
                    if vote.vote_for {
                        votes_for += conviction;
                    } else {
                        votes_against += conviction;
                    }
                }
            }
        }

        (votes_for, votes_against)
    }

    /// Get all votes for a proposal
    pub fn get_proposal_votes(&self, proposal_id: Uuid) -> Vec<&ConvictionVote> {
        self.proposal_votes
            .get(&proposal_id)
            .map(|ids| ids.iter().filter_map(|id| self.votes.get(id)).collect())
            .unwrap_or_default()
    }

    /// Get all votes by a user
    pub fn get_user_votes(&self, user_id: Uuid) -> Vec<&ConvictionVote> {
        self.user_votes
            .get(&user_id)
            .map(|ids| ids.iter().filter_map(|id| self.votes.get(id)).collect())
            .unwrap_or_default()
    }
}

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

/// Quadratic voting vote record
///
/// Quadratic voting uses a quadratic cost function where the cost of n votes
/// is n^2, making it expensive to concentrate votes on a single option.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuadraticVote {
    /// Unique identifier of this vote record.
    pub vote_id: Uuid,
    /// Proposal this vote was cast on.
    pub proposal_id: Uuid,
    /// User who cast this vote.
    pub user_id: Uuid,
    /// Number of votes cast (actual voting power)
    pub vote_count: u32,
    /// Cost in vote credits (vote_count^2)
    pub credit_cost: Decimal,
    /// Whether this is a vote for or against
    pub in_favor: bool,
    /// Timestamp when the vote was recorded.
    pub created_at: DateTime<Utc>,
}

impl QuadraticVote {
    /// Create a new quadratic vote
    pub fn new(proposal_id: Uuid, user_id: Uuid, vote_count: u32, in_favor: bool) -> Result<Self> {
        if vote_count == 0 {
            return Err(CoreError::Validation(
                "Vote count must be positive".to_string(),
            ));
        }

        // Quadratic cost: n^2
        let credit_cost = Decimal::from(vote_count) * Decimal::from(vote_count);

        Ok(Self {
            vote_id: Uuid::new_v4(),
            proposal_id,
            user_id,
            vote_count,
            credit_cost,
            in_favor,
            created_at: Utc::now(),
        })
    }

    /// Calculate the cost for a given number of votes
    pub fn calculate_cost(vote_count: u32) -> Decimal {
        Decimal::from(vote_count) * Decimal::from(vote_count)
    }

    /// Calculate how many votes can be purchased with given credits
    pub fn votes_from_credits(credits: Decimal) -> u32 {
        // sqrt(credits) = votes
        let credits_f64 = credits.to_f64().unwrap_or(0.0);
        credits_f64.sqrt() as u32
    }
}

/// User's quadratic voting credits
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuadraticVotingCredits {
    /// User who owns these voting credits.
    pub user_id: Uuid,
    /// Total credits allocated to the user
    pub total_credits: Decimal,
    /// Credits spent on votes
    pub spent_credits: Decimal,
    /// Credits available for voting
    pub available_credits: Decimal,
    /// When credits were last allocated
    pub last_allocation: DateTime<Utc>,
}

impl QuadraticVotingCredits {
    /// Create new voting credits for a user
    pub fn new(user_id: Uuid, total_credits: Decimal) -> Self {
        Self {
            user_id,
            total_credits,
            spent_credits: Decimal::ZERO,
            available_credits: total_credits,
            last_allocation: Utc::now(),
        }
    }

    /// Spend credits on a vote
    pub fn spend(&mut self, amount: Decimal) -> Result<()> {
        if amount > self.available_credits {
            return Err(CoreError::Validation(
                "Insufficient voting credits".to_string(),
            ));
        }

        self.spent_credits += amount;
        self.available_credits -= amount;

        Ok(())
    }

    /// Allocate additional credits
    pub fn allocate(&mut self, amount: Decimal) {
        self.total_credits += amount;
        self.available_credits += amount;
        self.last_allocation = Utc::now();
    }

    /// Refund credits (when vote is cancelled)
    pub fn refund(&mut self, amount: Decimal) {
        self.spent_credits = (self.spent_credits - amount).max(Decimal::ZERO);
        self.available_credits = (self.available_credits + amount).min(self.total_credits);
    }
}

/// Quadratic voting manager
#[derive(Debug, Clone, Default)]
pub struct QuadraticVotingManager {
    /// Votes grouped by proposal ID
    votes: HashMap<Uuid, Vec<QuadraticVote>>,
    /// Credit balances keyed by user ID
    user_credits: HashMap<Uuid, QuadraticVotingCredits>,
    /// Tracks total votes per proposal (for/against)
    proposal_votes: HashMap<Uuid, (u32, u32)>, // (votes_for, votes_against)
}

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

    /// Allocate credits to a user
    pub fn allocate_credits(&mut self, user_id: Uuid, credits: Decimal) {
        self.user_credits
            .entry(user_id)
            .and_modify(|c| c.allocate(credits))
            .or_insert_with(|| QuadraticVotingCredits::new(user_id, credits));
    }

    /// Cast a quadratic vote
    pub fn cast_vote(
        &mut self,
        proposal_id: Uuid,
        user_id: Uuid,
        vote_count: u32,
        in_favor: bool,
    ) -> Result<QuadraticVote> {
        // Create vote and calculate cost
        let vote = QuadraticVote::new(proposal_id, user_id, vote_count, in_favor)?;

        // Check if user has enough credits
        let credits = self
            .user_credits
            .get_mut(&user_id)
            .ok_or(CoreError::Validation(
                "User has no voting credits".to_string(),
            ))?;

        // Spend credits
        credits.spend(vote.credit_cost)?;

        // Record vote
        self.votes
            .entry(proposal_id)
            .or_default()
            .push(vote.clone());

        // Update proposal vote totals
        let (for_votes, against_votes) = self.proposal_votes.entry(proposal_id).or_insert((0, 0));
        if in_favor {
            *for_votes += vote_count;
        } else {
            *against_votes += vote_count;
        }

        Ok(vote)
    }

    /// Get user's available credits
    pub fn get_user_credits(&self, user_id: Uuid) -> Option<&QuadraticVotingCredits> {
        self.user_credits.get(&user_id)
    }

    /// Get all votes for a proposal
    pub fn get_proposal_votes(&self, proposal_id: Uuid) -> Vec<&QuadraticVote> {
        self.votes
            .get(&proposal_id)
            .map(|votes| votes.iter().collect())
            .unwrap_or_default()
    }

    /// Get vote totals for a proposal (votes_for, votes_against)
    pub fn get_proposal_totals(&self, proposal_id: Uuid) -> (u32, u32) {
        self.proposal_votes
            .get(&proposal_id)
            .copied()
            .unwrap_or((0, 0))
    }

    /// Calculate quadratic voting result (percentage in favor)
    pub fn calculate_result(&self, proposal_id: Uuid) -> Decimal {
        let (votes_for, votes_against) = self.get_proposal_totals(proposal_id);
        let total_votes = votes_for + votes_against;

        if total_votes == 0 {
            return Decimal::ZERO;
        }

        (Decimal::from(votes_for) / Decimal::from(total_votes)) * Decimal::from(100)
    }

    /// Detect potential collusion (simplified)
    /// Returns users who might be coordinating votes
    pub fn detect_collusion(&self, proposal_id: Uuid) -> Vec<Uuid> {
        let votes = self.get_proposal_votes(proposal_id);
        let mut suspicious_users = Vec::new();

        // Simple heuristic: users who cast exactly the same number of votes
        let mut vote_counts: HashMap<u32, Vec<Uuid>> = HashMap::new();

        for vote in votes {
            vote_counts
                .entry(vote.vote_count)
                .or_default()
                .push(vote.user_id);
        }

        // If 3+ users cast the exact same number of votes, flag them
        for (_count, users) in vote_counts {
            if users.len() >= 3 {
                suspicious_users.extend(users);
            }
        }

        suspicious_users
    }

    /// Calculate Sybil resistance score (0-100)
    /// Higher scores indicate better resistance to Sybil attacks
    pub fn calculate_sybil_score(&self, proposal_id: Uuid) -> Decimal {
        let votes = self.get_proposal_votes(proposal_id);

        if votes.is_empty() {
            return Decimal::ZERO;
        }

        // Calculate Gini coefficient of vote distribution
        // More equal distribution = higher score
        let total_votes: u32 = votes.iter().map(|v| v.vote_count).sum();
        let num_voters = votes.len();

        if total_votes == 0 || num_voters == 0 {
            return Decimal::ZERO;
        }

        let avg_votes = Decimal::from(total_votes) / Decimal::from(num_voters);

        // Calculate variance
        let mut variance = Decimal::ZERO;
        for vote in votes {
            let diff = Decimal::from(vote.vote_count) - avg_votes;
            variance += diff * diff;
        }
        variance /= Decimal::from(num_voters);

        // Lower variance = higher score
        // Normalize to 0-100 scale (simplified)
        let score = Decimal::from(100) / (Decimal::from(1) + variance / Decimal::from(10));

        score.min(Decimal::from(100))
    }
}

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

    #[test]
    fn test_vote_delegation() {
        let delegator = Uuid::new_v4();
        let delegate = Uuid::new_v4();

        let delegation = VoteDelegation::new(delegator, delegate, dec!(1000), Some(30)).unwrap();

        assert!(delegation.is_valid());
        assert_eq!(delegation.voting_power, dec!(1000));
    }

    #[test]
    fn test_vote_delegation_revoke() {
        let delegator = Uuid::new_v4();
        let delegate = Uuid::new_v4();

        let mut delegation =
            VoteDelegation::new(delegator, delegate, dec!(1000), Some(30)).unwrap();

        delegation.revoke();
        assert!(!delegation.is_valid());
    }

    #[test]
    fn test_vote_escrow_lock() {
        let user = Uuid::new_v4();
        let lock = VoteEscrowLock::new(user, dec!(1000), 365).unwrap();

        assert!(lock.voting_power > dec!(1000)); // Should have bonus from lock duration
        assert!(!lock.is_expired());
    }

    #[test]
    fn test_vote_escrow_voting_power() {
        let user = Uuid::new_v4();
        let lock = VoteEscrowLock::new(user, dec!(1000), VoteEscrowLock::MAX_LOCK_DAYS).unwrap();

        // Maximum lock should give 2x voting power
        assert!(lock.voting_power >= dec!(1500)); // At least 1.5x
    }

    #[test]
    fn test_conviction_vote() {
        let proposal = Uuid::new_v4();
        let user = Uuid::new_v4();

        let vote = ConvictionVote::new(proposal, user, dec!(1000), true).unwrap();

        let initial_conviction = vote.current_conviction();
        assert_eq!(initial_conviction, dec!(1000)); // Initial conviction equals base power
    }

    #[test]
    fn test_delegation_manager() {
        let mut manager = DelegationManager::new();
        let delegator = Uuid::new_v4();
        let delegate = Uuid::new_v4();

        let delegation_id = manager
            .delegate(delegator, delegate, dec!(1000), Some(30))
            .unwrap();

        let total_power = manager.get_total_voting_power(delegate);
        assert_eq!(total_power, dec!(1000));

        manager.revoke_delegation(delegation_id).unwrap();
        let total_power_after = manager.get_total_voting_power(delegate);
        assert_eq!(total_power_after, dec!(0));
    }

    #[test]
    fn test_vote_escrow_manager() {
        let mut manager = VoteEscrowManager::new(dec!(0.5)).unwrap();
        let user = Uuid::new_v4();

        let lock_id = manager.create_lock(user, dec!(1000), 365).unwrap();
        let voting_power = manager.get_user_voting_power(user);

        assert!(voting_power > dec!(1000));

        // Early unlock should apply penalty
        let unlocked = manager.unlock(lock_id).unwrap();
        assert_eq!(unlocked, dec!(500)); // 50% penalty
    }

    #[test]
    fn test_conviction_voting_manager() {
        let mut manager = ConvictionVotingManager::new();
        let proposal = Uuid::new_v4();
        let user1 = Uuid::new_v4();
        let user2 = Uuid::new_v4();

        manager
            .cast_vote(proposal, user1, dec!(1000), true)
            .unwrap();
        manager
            .cast_vote(proposal, user2, dec!(500), false)
            .unwrap();

        let (votes_for, votes_against) = manager.get_proposal_conviction(proposal);
        assert_eq!(votes_for, dec!(1000));
        assert_eq!(votes_against, dec!(500));
    }

    #[test]
    fn test_cannot_delegate_to_self() {
        let user = Uuid::new_v4();
        let result = VoteDelegation::new(user, user, dec!(1000), None);
        assert!(result.is_err());
    }

    #[test]
    fn test_vote_escrow_max_lock_validation() {
        let user = Uuid::new_v4();
        let result = VoteEscrowLock::new(user, dec!(1000), VoteEscrowLock::MAX_LOCK_DAYS + 1);
        assert!(result.is_err());
    }
}