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
//! Governance system for protocol changes

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::fmt;
use uuid::Uuid;

use super::user::ValidationError;

/// Governance proposal
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Proposal {
    /// Unique identifier for this proposal
    pub proposal_id: Uuid,
    /// User who created the proposal
    pub proposer_user_id: Uuid,
    /// Proposal title
    pub title: String,
    /// Detailed description
    pub description: String,
    /// Proposal type
    pub proposal_type: ProposalType,
    /// Voting power required (quorum)
    pub quorum_required: Decimal,
    /// Percentage of votes needed to pass (e.g., 0.66 for 66%)
    pub approval_threshold: Decimal,
    /// Current voting power in favor
    pub votes_for: Decimal,
    /// Current voting power against
    pub votes_against: Decimal,
    /// Current voting power abstained
    pub votes_abstain: Decimal,
    /// Proposal status
    pub status: ProposalStatus,
    /// Voting start time
    pub voting_start: DateTime<Utc>,
    /// Voting end time
    pub voting_end: DateTime<Utc>,
    /// Execution time (if time-locked)
    pub execution_time: Option<DateTime<Utc>>,
    /// Time lock duration in seconds
    pub time_lock_seconds: i64,
    /// Timestamp when this proposal was created
    pub created_at: DateTime<Utc>,
    /// Timestamp when this proposal was last updated
    pub updated_at: DateTime<Utc>,
}

/// Category of governance proposal
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum ProposalType {
    /// Update protocol parameters
    ParameterChange,
    /// Add/remove token from platform
    TokenListing,
    /// Upgrade smart contract
    ContractUpgrade,
    /// Treasury allocation
    TreasurySpend,
    /// Fee structure change
    FeeChange,
    /// Emergency action
    EmergencyAction,
    /// General proposal
    #[default]
    General,
}

impl fmt::Display for ProposalType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProposalType::ParameterChange => write!(f, "parameter_change"),
            ProposalType::TokenListing => write!(f, "token_listing"),
            ProposalType::ContractUpgrade => write!(f, "contract_upgrade"),
            ProposalType::TreasurySpend => write!(f, "treasury_spend"),
            ProposalType::FeeChange => write!(f, "fee_change"),
            ProposalType::EmergencyAction => write!(f, "emergency_action"),
            ProposalType::General => write!(f, "general"),
        }
    }
}

/// Status of a governance proposal.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "lowercase")]
#[derive(Default)]
pub enum ProposalStatus {
    /// Voting in progress
    #[default]
    Active,
    /// Proposal passed, waiting for time lock
    Passed,
    /// Proposal rejected
    Rejected,
    /// Proposal executed
    Executed,
    /// Proposal cancelled
    Cancelled,
    /// Proposal expired without reaching quorum
    Expired,
}

impl fmt::Display for ProposalStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProposalStatus::Active => write!(f, "active"),
            ProposalStatus::Passed => write!(f, "passed"),
            ProposalStatus::Rejected => write!(f, "rejected"),
            ProposalStatus::Executed => write!(f, "executed"),
            ProposalStatus::Cancelled => write!(f, "cancelled"),
            ProposalStatus::Expired => write!(f, "expired"),
        }
    }
}

impl Proposal {
    /// Calculate total votes cast
    pub fn total_votes(&self) -> Decimal {
        self.votes_for + self.votes_against + self.votes_abstain
    }

    /// Check if quorum is reached
    pub fn is_quorum_reached(&self) -> bool {
        self.total_votes() >= self.quorum_required
    }

    /// Check if proposal is approved
    pub fn is_approved(&self) -> bool {
        let total_decisive_votes = self.votes_for + self.votes_against;
        if total_decisive_votes == dec!(0) {
            return false;
        }

        let approval_rate = self.votes_for / total_decisive_votes;
        approval_rate >= self.approval_threshold
    }

    /// Check if voting period has ended
    pub fn is_voting_ended(&self) -> bool {
        Utc::now() >= self.voting_end
    }

    /// Check if proposal can be executed
    pub fn can_execute(&self) -> bool {
        if self.status != ProposalStatus::Passed {
            return false;
        }

        if let Some(exec_time) = self.execution_time {
            Utc::now() >= exec_time
        } else {
            true
        }
    }

    /// Calculate approval percentage
    pub fn approval_percentage(&self) -> Decimal {
        let total_decisive = self.votes_for + self.votes_against;
        if total_decisive == dec!(0) {
            return dec!(0);
        }
        (self.votes_for / total_decisive) * dec!(100)
    }

    /// Calculate participation rate
    pub fn participation_rate(&self, total_voting_power: Decimal) -> Decimal {
        if total_voting_power == dec!(0) {
            return dec!(0);
        }
        (self.total_votes() / total_voting_power) * dec!(100)
    }

    /// Validate proposal
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.title.is_empty() {
            return Err(ValidationError("Title is required".to_string()));
        }
        if self.title.len() > 200 {
            return Err(ValidationError(
                "Title must be at most 200 characters".to_string(),
            ));
        }
        if self.description.is_empty() {
            return Err(ValidationError("Description is required".to_string()));
        }
        if self.quorum_required <= dec!(0) {
            return Err(ValidationError("Quorum must be positive".to_string()));
        }
        if self.approval_threshold <= dec!(0) || self.approval_threshold > dec!(1) {
            return Err(ValidationError(
                "Approval threshold must be between 0 and 1".to_string(),
            ));
        }
        if self.voting_end <= self.voting_start {
            return Err(ValidationError(
                "Voting end must be after voting start".to_string(),
            ));
        }
        if self.time_lock_seconds < 0 {
            return Err(ValidationError("Time lock cannot be negative".to_string()));
        }

        Ok(())
    }
}

impl fmt::Display for Proposal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Proposal({}, title='{}', status={})",
            self.proposal_id, self.title, self.status
        )
    }
}

/// Individual vote on a proposal
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Vote {
    /// Unique identifier for this vote
    pub vote_id: Uuid,
    /// Proposal being voted on
    pub proposal_id: Uuid,
    /// User who cast the vote
    pub voter_user_id: Uuid,
    /// Voting power (based on token holdings)
    pub voting_power: Decimal,
    /// Vote choice
    pub choice: VoteChoice,
    /// Optional reasoning
    pub reason: Option<String>,
    /// Timestamp when the vote was cast
    pub voted_at: DateTime<Utc>,
}

/// Voter's choice on a proposal
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "lowercase")]
#[derive(Default)]
pub enum VoteChoice {
    /// Vote in favour of the proposal
    For,
    /// Vote against the proposal
    Against,
    /// Abstain from the vote
    #[default]
    Abstain,
}

impl fmt::Display for VoteChoice {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VoteChoice::For => write!(f, "for"),
            VoteChoice::Against => write!(f, "against"),
            VoteChoice::Abstain => write!(f, "abstain"),
        }
    }
}

/// Request to create a new governance proposal
#[derive(Debug, Deserialize)]
pub struct CreateProposalRequest {
    /// Short title for the proposal
    pub title: String,
    /// Detailed description of the proposal
    pub description: String,
    /// Category this proposal falls under
    pub proposal_type: ProposalType,
    /// How long voting will be open (hours)
    pub voting_duration_hours: i64,
    /// Optional time-lock delay before execution (hours)
    pub time_lock_hours: Option<i64>,
}

impl CreateProposalRequest {
    /// Validate that the request has sensible values
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.title.is_empty() {
            return Err(ValidationError("Title is required".to_string()));
        }
        if self.title.len() > 200 {
            return Err(ValidationError(
                "Title must be at most 200 characters".to_string(),
            ));
        }
        if self.description.is_empty() {
            return Err(ValidationError("Description is required".to_string()));
        }
        if self.voting_duration_hours <= 0 {
            return Err(ValidationError(
                "Voting duration must be positive".to_string(),
            ));
        }
        if let Some(time_lock) = self.time_lock_hours {
            if time_lock < 0 {
                return Err(ValidationError("Time lock cannot be negative".to_string()));
            }
        }

        Ok(())
    }
}

/// Request to cast a vote on a proposal
#[derive(Debug, Deserialize)]
pub struct CastVoteRequest {
    /// Proposal to vote on
    pub proposal_id: Uuid,
    /// Vote choice (for, against, or abstain)
    pub choice: VoteChoice,
    /// Optional reasoning for the vote
    pub reason: Option<String>,
}

impl CastVoteRequest {
    /// Validate that the request is well-formed
    pub fn validate(&self) -> Result<(), ValidationError> {
        if let Some(ref reason) = self.reason {
            if reason.len() > 1000 {
                return Err(ValidationError(
                    "Reason must be at most 1000 characters".to_string(),
                ));
            }
        }
        Ok(())
    }
}

/// Governance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GovernanceConfig {
    /// Minimum voting power to create proposal
    pub min_proposal_power: Decimal,
    /// Default quorum percentage (e.g., 0.1 for 10%)
    pub default_quorum: Decimal,
    /// Default approval threshold (e.g., 0.66 for 66%)
    pub default_approval_threshold: Decimal,
    /// Default voting duration in hours
    pub default_voting_duration_hours: i64,
    /// Default time lock in hours
    pub default_time_lock_hours: i64,
}

impl Default for GovernanceConfig {
    fn default() -> Self {
        Self {
            min_proposal_power: dec!(1000),         // Need 1000 tokens to propose
            default_quorum: dec!(0.1),              // 10% quorum
            default_approval_threshold: dec!(0.66), // 66% approval
            default_voting_duration_hours: 72,      // 3 days
            default_time_lock_hours: 24,            // 1 day time lock
        }
    }
}

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

    #[test]
    fn test_proposal_quorum() {
        let proposal = Proposal {
            proposal_id: Uuid::new_v4(),
            proposer_user_id: Uuid::new_v4(),
            title: "Test Proposal".to_string(),
            description: "Test Description".to_string(),
            proposal_type: ProposalType::General,
            quorum_required: dec!(1000),
            approval_threshold: dec!(0.66),
            votes_for: dec!(600),
            votes_against: dec!(200),
            votes_abstain: dec!(100),
            status: ProposalStatus::Active,
            voting_start: Utc::now(),
            voting_end: Utc::now() + chrono::Duration::hours(72),
            execution_time: None,
            time_lock_seconds: 0,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert_eq!(proposal.total_votes(), dec!(900));
        assert!(!proposal.is_quorum_reached()); // 900 < 1000

        let mut proposal_with_quorum = proposal.clone();
        proposal_with_quorum.votes_for = dec!(700);
        assert_eq!(proposal_with_quorum.total_votes(), dec!(1000));
        assert!(proposal_with_quorum.is_quorum_reached());
    }

    #[test]
    fn test_proposal_approval() {
        let proposal = Proposal {
            proposal_id: Uuid::new_v4(),
            proposer_user_id: Uuid::new_v4(),
            title: "Test Proposal".to_string(),
            description: "Test Description".to_string(),
            proposal_type: ProposalType::General,
            quorum_required: dec!(100),
            approval_threshold: dec!(0.66),
            votes_for: dec!(700),
            votes_against: dec!(300),
            votes_abstain: dec!(0),
            status: ProposalStatus::Active,
            voting_start: Utc::now(),
            voting_end: Utc::now() + chrono::Duration::hours(72),
            execution_time: None,
            time_lock_seconds: 0,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        // 700 / (700 + 300) = 0.7 = 70% > 66%
        assert!(proposal.is_approved());

        let mut rejected_proposal = proposal.clone();
        rejected_proposal.votes_for = dec!(600);
        rejected_proposal.votes_against = dec!(400);
        // 600 / 1000 = 0.6 = 60% < 66%
        assert!(!rejected_proposal.is_approved());
    }

    #[test]
    fn test_proposal_approval_percentage() {
        let proposal = Proposal {
            proposal_id: Uuid::new_v4(),
            proposer_user_id: Uuid::new_v4(),
            title: "Test Proposal".to_string(),
            description: "Test Description".to_string(),
            proposal_type: ProposalType::General,
            quorum_required: dec!(100),
            approval_threshold: dec!(0.5),
            votes_for: dec!(750),
            votes_against: dec!(250),
            votes_abstain: dec!(0),
            status: ProposalStatus::Active,
            voting_start: Utc::now(),
            voting_end: Utc::now() + chrono::Duration::hours(72),
            execution_time: None,
            time_lock_seconds: 0,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert_eq!(proposal.approval_percentage(), dec!(75));
    }

    #[test]
    fn test_participation_rate() {
        let proposal = Proposal {
            proposal_id: Uuid::new_v4(),
            proposer_user_id: Uuid::new_v4(),
            title: "Test Proposal".to_string(),
            description: "Test Description".to_string(),
            proposal_type: ProposalType::General,
            quorum_required: dec!(100),
            approval_threshold: dec!(0.5),
            votes_for: dec!(300),
            votes_against: dec!(200),
            votes_abstain: dec!(100),
            status: ProposalStatus::Active,
            voting_start: Utc::now(),
            voting_end: Utc::now() + chrono::Duration::hours(72),
            execution_time: None,
            time_lock_seconds: 0,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        // 600 / 2000 = 0.3 = 30%
        assert_eq!(proposal.participation_rate(dec!(2000)), dec!(30));
    }
}