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
//! Bitcoin payment tracking models for order fulfillment

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;

/// Bitcoin payment order tracking
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PaymentOrder {
    /// Unique identifier for this payment order
    pub payment_order_id: Uuid,
    /// Associated order ID
    pub order_id: Uuid,
    /// User making the payment
    pub user_id: Uuid,
    /// Token being purchased
    pub token_id: Uuid,
    /// Generated Bitcoin address for this payment
    pub btc_address: String,
    /// Expected payment amount in BTC
    pub expected_amount_btc: Decimal,
    /// Actual received amount in BTC
    pub received_amount_btc: Option<Decimal>,
    /// Payment status
    pub status: PaymentStatus,
    /// Bitcoin transaction ID (txid) when detected
    pub btc_txid: Option<String>,
    /// Number of confirmations
    pub confirmations: i32,
    /// Required confirmations for fulfillment
    pub required_confirmations: i32,
    /// When payment was first detected
    pub detected_at: Option<DateTime<Utc>>,
    /// When payment reached required confirmations
    pub confirmed_at: Option<DateTime<Utc>>,
    /// Expiration time for payment (typically 24 hours)
    pub expires_at: DateTime<Utc>,
    /// When order was fulfilled/executed
    pub fulfilled_at: Option<DateTime<Utc>>,
    /// HD wallet derivation path for address generation
    pub derivation_path: Option<String>,
    /// Notes or error messages
    pub notes: Option<String>,
    /// Timestamp when this payment order was created
    pub created_at: DateTime<Utc>,
    /// Timestamp when this payment order was last updated
    pub updated_at: DateTime<Utc>,
}

/// Payment order status
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum PaymentStatus {
    /// Waiting for payment
    #[default]
    Pending,
    /// Payment detected but not yet confirmed
    Detected,
    /// Payment confirmed and processing
    Confirming,
    /// Payment confirmed and order fulfilled
    Completed,
    /// Payment amount insufficient
    Underpaid,
    /// Payment amount exceeded
    Overpaid,
    /// Payment expired without completion
    Expired,
    /// Payment failed or error occurred
    Failed,
    /// Refund initiated
    Refunding,
    /// Refund completed
    Refunded,
}

impl fmt::Display for PaymentStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PaymentStatus::Pending => write!(f, "pending"),
            PaymentStatus::Detected => write!(f, "detected"),
            PaymentStatus::Confirming => write!(f, "confirming"),
            PaymentStatus::Completed => write!(f, "completed"),
            PaymentStatus::Underpaid => write!(f, "underpaid"),
            PaymentStatus::Overpaid => write!(f, "overpaid"),
            PaymentStatus::Expired => write!(f, "expired"),
            PaymentStatus::Failed => write!(f, "failed"),
            PaymentStatus::Refunding => write!(f, "refunding"),
            PaymentStatus::Refunded => write!(f, "refunded"),
        }
    }
}

impl fmt::Display for PaymentOrder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "PaymentOrder({}, addr={}, amount={} BTC, status={})",
            self.payment_order_id, self.btc_address, self.expected_amount_btc, self.status
        )
    }
}

impl PaymentOrder {
    /// Check if payment order has expired
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at && self.status == PaymentStatus::Pending
    }

    /// Check if payment is complete
    pub fn is_completed(&self) -> bool {
        self.status == PaymentStatus::Completed
    }

    /// Check if payment has enough confirmations
    pub fn has_enough_confirmations(&self) -> bool {
        self.confirmations >= self.required_confirmations
    }

    /// Calculate payment variance percentage
    pub fn payment_variance_percent(&self) -> Option<Decimal> {
        self.received_amount_btc.map(|received| {
            if self.expected_amount_btc == dec!(0) {
                return dec!(0);
            }
            ((received - self.expected_amount_btc) / self.expected_amount_btc) * dec!(100)
        })
    }

    /// Check if payment amount is acceptable (within tolerance)
    pub fn is_amount_acceptable(&self, tolerance_percent: Decimal) -> bool {
        if let Some(variance) = self.payment_variance_percent() {
            // Allow slight overpayment, but underpayment must be within tolerance
            if variance >= dec!(0) {
                return true; // Overpayment is acceptable
            }
            variance.abs() <= tolerance_percent
        } else {
            false
        }
    }

    /// Get time remaining until expiration
    pub fn time_until_expiration(&self) -> chrono::Duration {
        self.expires_at - Utc::now()
    }

    /// Get time since payment was created
    pub fn age(&self) -> chrono::Duration {
        Utc::now() - self.created_at
    }

    /// Calculate refund amount (if applicable)
    pub fn calculate_refund_amount(&self) -> Option<Decimal> {
        match self.status {
            PaymentStatus::Overpaid => self
                .received_amount_btc
                .map(|received| (received - self.expected_amount_btc).max(dec!(0))),
            PaymentStatus::Failed | PaymentStatus::Expired => self.received_amount_btc,
            _ => None,
        }
    }
}

/// Request to create a payment order
#[derive(Debug, Deserialize)]
pub struct CreatePaymentOrderRequest {
    /// Trade order this payment will fulfil
    pub order_id: Uuid,
    /// BTC amount expected from the payer
    pub expected_amount_btc: Decimal,
    /// Override for the number of required confirmations (uses default if omitted)
    pub required_confirmations: Option<i32>,
}

impl CreatePaymentOrderRequest {
    /// Validate that the request has valid parameters
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.expected_amount_btc <= dec!(0) {
            return Err(ValidationError(
                "Payment amount must be positive".to_string(),
            ));
        }
        if self.expected_amount_btc < dec!(0.00001) {
            return Err(ValidationError(
                "Payment amount too small (minimum 0.00001 BTC)".to_string(),
            ));
        }
        if let Some(confs) = self.required_confirmations {
            if !(1..=6).contains(&confs) {
                return Err(ValidationError(
                    "Required confirmations must be between 1 and 6".to_string(),
                ));
            }
        }
        Ok(())
    }
}

/// Bitcoin transaction confirmation event
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PaymentConfirmation {
    /// Unique identifier for this confirmation event
    pub confirmation_id: Uuid,
    /// Payment order this confirmation belongs to
    pub payment_order_id: Uuid,
    /// Transaction ID
    pub txid: String,
    /// Number of confirmations at time of event
    pub confirmations: i32,
    /// Amount in BTC
    pub amount_btc: Decimal,
    /// Block hash containing the transaction
    pub block_hash: Option<String>,
    /// Block height
    pub block_height: Option<i64>,
    /// Timestamp of block
    pub block_time: Option<DateTime<Utc>>,
    /// Transaction fee paid
    pub fee_btc: Option<Decimal>,
    /// Timestamp when this confirmation was detected
    pub detected_at: DateTime<Utc>,
}

impl fmt::Display for PaymentConfirmation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "PaymentConfirmation(txid={}, confs={}, amount={} BTC)",
            self.txid, self.confirmations, self.amount_btc
        )
    }
}

/// Bitcoin address pool for pre-generated addresses
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct BitcoinAddress {
    /// Unique identifier for this address record
    pub address_id: Uuid,
    /// Bitcoin address
    pub address: String,
    /// HD wallet derivation path
    pub derivation_path: String,
    /// Whether address is currently assigned to an order
    pub is_assigned: bool,
    /// Payment order ID if assigned
    pub assigned_to_payment_order_id: Option<Uuid>,
    /// When address was assigned
    pub assigned_at: Option<DateTime<Utc>>,
    /// Address type (p2pkh, p2wpkh, p2sh, etc.)
    pub address_type: BitcoinAddressType,
    /// Timestamp when this address record was created
    pub created_at: DateTime<Utc>,
}

/// Bitcoin address type
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum BitcoinAddressType {
    /// Legacy P2PKH (starts with 1)
    P2pkh,
    /// Native SegWit P2WPKH (starts with bc1)
    #[default]
    P2wpkh,
    /// Nested SegWit P2SH (starts with 3)
    P2sh,
    /// Taproot P2TR (starts with bc1p)
    P2tr,
}

impl fmt::Display for BitcoinAddressType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BitcoinAddressType::P2pkh => write!(f, "P2PKH"),
            BitcoinAddressType::P2wpkh => write!(f, "P2WPKH"),
            BitcoinAddressType::P2sh => write!(f, "P2SH"),
            BitcoinAddressType::P2tr => write!(f, "P2TR"),
        }
    }
}

/// Payment monitoring configuration
pub struct PaymentMonitorConfig;

impl PaymentMonitorConfig {
    /// Default payment expiration time (24 hours)
    pub const DEFAULT_EXPIRATION_HOURS: i64 = 24;

    /// Default required confirmations for standard orders
    pub const DEFAULT_REQUIRED_CONFIRMATIONS: i32 = 2;

    /// Required confirmations for large orders (>0.1 BTC)
    pub const HIGH_VALUE_REQUIRED_CONFIRMATIONS: i32 = 3;

    /// Payment amount tolerance percentage (allow 1% underpayment due to fees)
    pub const PAYMENT_TOLERANCE_PERCENT: Decimal = dec!(1);

    /// Polling interval for monitoring payments (seconds)
    pub const MONITORING_POLL_INTERVAL_SECS: u64 = 30;

    /// Maximum retries for payment monitoring before marking as failed
    pub const MAX_MONITORING_RETRIES: i32 = 100;

    /// Threshold for high-value orders requiring extra confirmations (BTC)
    pub const HIGH_VALUE_THRESHOLD_BTC: Decimal = dec!(0.1);

    /// Get required confirmations based on amount
    pub fn get_required_confirmations(amount_btc: Decimal) -> i32 {
        if amount_btc >= Self::HIGH_VALUE_THRESHOLD_BTC {
            Self::HIGH_VALUE_REQUIRED_CONFIRMATIONS
        } else {
            Self::DEFAULT_REQUIRED_CONFIRMATIONS
        }
    }

    /// Calculate expiration time from now
    pub fn calculate_expiration() -> DateTime<Utc> {
        Utc::now() + chrono::Duration::hours(Self::DEFAULT_EXPIRATION_HOURS)
    }

    /// Check if payment should be marked as expired
    pub fn should_expire(payment: &PaymentOrder) -> bool {
        payment.is_expired()
            && matches!(
                payment.status,
                PaymentStatus::Pending | PaymentStatus::Detected
            )
    }

    /// Check if payment monitoring should continue
    pub fn should_continue_monitoring(payment: &PaymentOrder, retry_count: i32) -> bool {
        if retry_count >= Self::MAX_MONITORING_RETRIES {
            return false;
        }
        matches!(
            payment.status,
            PaymentStatus::Pending | PaymentStatus::Detected | PaymentStatus::Confirming
        )
    }
}

/// Payment statistics for admin dashboard
#[derive(Debug, Serialize)]
pub struct PaymentStats {
    /// Total number of payment orders created
    pub total_orders: i64,
    /// Number of orders currently awaiting payment
    pub pending_orders: i64,
    /// Number of orders that completed successfully
    pub completed_orders: i64,
    /// Number of orders that expired
    pub expired_orders: i64,
    /// Number of orders that failed
    pub failed_orders: i64,
    /// Total BTC volume across completed orders
    pub total_volume_btc: Decimal,
    /// Average time to confirmation in minutes
    pub avg_confirmation_time_minutes: Option<f64>,
    /// Percentage of orders that completed successfully
    pub success_rate_percent: Decimal,
}

/// Payment webhook event (for external integrations)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentWebhookEvent {
    /// Type of payment event being signalled
    pub event_type: PaymentEventType,
    /// Payment order this event relates to
    pub payment_order_id: Uuid,
    /// Bitcoin address of the payment
    pub btc_address: String,
    /// BTC amount involved in this event
    pub amount_btc: Decimal,
    /// Number of confirmations at the time of this event
    pub confirmations: i32,
    /// Bitcoin transaction ID (if available)
    pub txid: Option<String>,
    /// Timestamp when this event was generated
    pub timestamp: DateTime<Utc>,
}

/// Type of payment event
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PaymentEventType {
    /// Payment order created
    Created,
    /// Payment detected on blockchain
    Detected,
    /// Confirmation count updated
    ConfirmationUpdate,
    /// Payment fully confirmed
    Confirmed,
    /// Order fulfilled
    Completed,
    /// Payment expired
    Expired,
    /// Payment failed
    Failed,
}

impl fmt::Display for PaymentEventType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PaymentEventType::Created => write!(f, "created"),
            PaymentEventType::Detected => write!(f, "detected"),
            PaymentEventType::ConfirmationUpdate => write!(f, "confirmation_update"),
            PaymentEventType::Confirmed => write!(f, "confirmed"),
            PaymentEventType::Completed => write!(f, "completed"),
            PaymentEventType::Expired => write!(f, "expired"),
            PaymentEventType::Failed => write!(f, "failed"),
        }
    }
}

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

    #[test]
    fn test_payment_expiration() {
        let payment = PaymentOrder {
            payment_order_id: Uuid::new_v4(),
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            btc_address: "bc1qtest".to_string(),
            expected_amount_btc: dec!(0.001),
            received_amount_btc: None,
            status: PaymentStatus::Pending,
            btc_txid: None,
            confirmations: 0,
            required_confirmations: 2,
            detected_at: None,
            confirmed_at: None,
            expires_at: Utc::now() - chrono::Duration::hours(1),
            fulfilled_at: None,
            derivation_path: Some("m/84'/0'/0'/0/0".to_string()),
            notes: None,
            created_at: Utc::now() - chrono::Duration::hours(25),
            updated_at: Utc::now(),
        };

        assert!(payment.is_expired());
    }

    #[test]
    fn test_payment_variance() {
        let mut payment = PaymentOrder {
            payment_order_id: Uuid::new_v4(),
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            btc_address: "bc1qtest".to_string(),
            expected_amount_btc: dec!(0.001),
            received_amount_btc: Some(dec!(0.00095)), // 5% underpayment
            status: PaymentStatus::Detected,
            btc_txid: Some("test_txid".to_string()),
            confirmations: 1,
            required_confirmations: 2,
            detected_at: Some(Utc::now()),
            confirmed_at: None,
            expires_at: Utc::now() + chrono::Duration::hours(1),
            fulfilled_at: None,
            derivation_path: Some("m/84'/0'/0'/0/0".to_string()),
            notes: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let variance = payment.payment_variance_percent().unwrap();
        assert_eq!(variance, dec!(-5));

        // Should not be acceptable with 1% tolerance
        assert!(!payment.is_amount_acceptable(dec!(1)));

        // Should be acceptable with 5% tolerance
        assert!(payment.is_amount_acceptable(dec!(5)));

        // Overpayment should always be acceptable
        payment.received_amount_btc = Some(dec!(0.0011));
        assert!(payment.is_amount_acceptable(dec!(1)));
    }

    #[test]
    fn test_required_confirmations() {
        // Small amount: 2 confirmations
        assert_eq!(
            PaymentMonitorConfig::get_required_confirmations(dec!(0.01)),
            2
        );

        // Large amount: 3 confirmations
        assert_eq!(
            PaymentMonitorConfig::get_required_confirmations(dec!(0.15)),
            3
        );
    }

    #[test]
    fn test_has_enough_confirmations() {
        let payment = PaymentOrder {
            payment_order_id: Uuid::new_v4(),
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            btc_address: "bc1qtest".to_string(),
            expected_amount_btc: dec!(0.001),
            received_amount_btc: Some(dec!(0.001)),
            status: PaymentStatus::Confirming,
            btc_txid: Some("test_txid".to_string()),
            confirmations: 2,
            required_confirmations: 2,
            detected_at: Some(Utc::now()),
            confirmed_at: None,
            expires_at: Utc::now() + chrono::Duration::hours(1),
            fulfilled_at: None,
            derivation_path: Some("m/84'/0'/0'/0/0".to_string()),
            notes: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert!(payment.has_enough_confirmations());
    }

    #[test]
    fn test_calculate_refund_amount() {
        // Overpaid scenario
        let payment = PaymentOrder {
            payment_order_id: Uuid::new_v4(),
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            btc_address: "bc1qtest".to_string(),
            expected_amount_btc: dec!(0.001),
            received_amount_btc: Some(dec!(0.0015)),
            status: PaymentStatus::Overpaid,
            btc_txid: Some("test_txid".to_string()),
            confirmations: 2,
            required_confirmations: 2,
            detected_at: Some(Utc::now()),
            confirmed_at: Some(Utc::now()),
            expires_at: Utc::now() + chrono::Duration::hours(1),
            fulfilled_at: None,
            derivation_path: Some("m/84'/0'/0'/0/0".to_string()),
            notes: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let refund = payment.calculate_refund_amount();
        assert_eq!(refund, Some(dec!(0.0005)));
    }

    #[test]
    fn test_create_payment_order_validation() {
        let valid_request = CreatePaymentOrderRequest {
            order_id: Uuid::new_v4(),
            expected_amount_btc: dec!(0.001),
            required_confirmations: Some(2),
        };
        assert!(valid_request.validate().is_ok());

        // Too small
        let invalid_request = CreatePaymentOrderRequest {
            order_id: Uuid::new_v4(),
            expected_amount_btc: dec!(0.000001),
            required_confirmations: Some(2),
        };
        assert!(invalid_request.validate().is_err());

        // Invalid confirmations
        let invalid_request = CreatePaymentOrderRequest {
            order_id: Uuid::new_v4(),
            expected_amount_btc: dec!(0.001),
            required_confirmations: Some(10),
        };
        assert!(invalid_request.validate().is_err());
    }
}