kaccy-bitcoin 0.2.0

Bitcoin integration for Kaccy Protocol - HD wallets, UTXO management, and transaction building
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
//! Adaptive fee strategies for Bitcoin transactions
//!
//! Provides intelligent fee management based on user preferences, time constraints,
//! and market conditions.

use crate::client::BitcoinClient;
use crate::error::BitcoinError;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// User's urgency preference for transaction confirmation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionUrgency {
    /// Low priority - can wait hours or days
    Low,
    /// Medium priority - should confirm within a few hours
    Medium,
    /// High priority - should confirm within 1-2 blocks
    High,
    /// Critical priority - must confirm ASAP
    Critical,
}

impl TransactionUrgency {
    /// Get target confirmation blocks for this urgency level
    pub fn target_blocks(&self) -> u32 {
        match self {
            Self::Low => 144,    // ~1 day
            Self::Medium => 6,   // ~1 hour
            Self::High => 2,     // ~20 minutes
            Self::Critical => 1, // Next block
        }
    }

    /// Get multiplier for base fee rate
    pub fn fee_multiplier(&self) -> f64 {
        match self {
            Self::Low => 1.0,
            Self::Medium => 1.5,
            Self::High => 2.0,
            Self::Critical => 3.0,
        }
    }
}

/// Time-based fee adjustment strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeBasedStrategy {
    /// Target confirmation time (UTC)
    pub target_time: DateTime<Utc>,
    /// Minimum acceptable fee rate (sat/vB)
    pub min_fee_rate: f64,
    /// Maximum acceptable fee rate (sat/vB)
    pub max_fee_rate: f64,
    /// Allow fee bumping if needed
    pub allow_bumping: bool,
}

impl TimeBasedStrategy {
    /// Create a new time-based strategy
    pub fn new(target_time: DateTime<Utc>, min_fee_rate: f64, max_fee_rate: f64) -> Self {
        Self {
            target_time,
            min_fee_rate,
            max_fee_rate,
            allow_bumping: true,
        }
    }

    /// Calculate appropriate fee rate based on time remaining
    pub fn calculate_fee_rate(&self, current_fee_rate: f64) -> f64 {
        let now = Utc::now();
        let time_remaining = self.target_time.signed_duration_since(now);

        if time_remaining.num_seconds() <= 0 {
            // Past deadline - use maximum fee
            return self.max_fee_rate;
        }

        // Calculate urgency multiplier based on time remaining
        let hours_remaining = time_remaining.num_hours() as f64;
        let multiplier = if hours_remaining < 1.0 {
            3.0 // Very urgent
        } else if hours_remaining < 6.0 {
            2.0 // Urgent
        } else if hours_remaining < 24.0 {
            1.5 // Moderate
        } else {
            1.0 // Not urgent
        };

        (current_fee_rate * multiplier)
            .max(self.min_fee_rate)
            .min(self.max_fee_rate)
    }

    /// Check if fee bumping is recommended
    pub fn should_bump_fee(&self) -> bool {
        if !self.allow_bumping {
            return false;
        }

        let now = Utc::now();
        let time_remaining = self.target_time.signed_duration_since(now);

        // Recommend bumping if less than 2 hours remain
        time_remaining.num_hours() < 2
    }
}

/// Budget-constrained fee strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetStrategy {
    /// Maximum total fee in satoshis
    pub max_fee_satoshis: u64,
    /// Transaction size in virtual bytes
    pub tx_vbytes: usize,
    /// Minimum acceptable fee rate (sat/vB)
    pub min_fee_rate: f64,
}

impl BudgetStrategy {
    /// Create a new budget-constrained strategy
    pub fn new(max_fee_satoshis: u64, tx_vbytes: usize) -> Self {
        Self {
            max_fee_satoshis,
            tx_vbytes,
            min_fee_rate: 1.0,
        }
    }

    /// Calculate maximum affordable fee rate
    pub fn max_fee_rate(&self) -> f64 {
        self.max_fee_satoshis as f64 / self.tx_vbytes as f64
    }

    /// Calculate recommended fee rate within budget
    pub fn calculate_fee_rate(&self, market_fee_rate: f64) -> Result<f64, BitcoinError> {
        let max_rate = self.max_fee_rate();

        if max_rate < self.min_fee_rate {
            return Err(BitcoinError::InsufficientFunds(
                "Budget too low for minimum fee rate".to_string(),
            ));
        }

        Ok(market_fee_rate.min(max_rate).max(self.min_fee_rate))
    }

    /// Check if transaction fits within budget at given fee rate
    pub fn fits_budget(&self, fee_rate: f64) -> bool {
        let total_fee = (fee_rate * self.tx_vbytes as f64) as u64;
        total_fee <= self.max_fee_satoshis
    }
}

/// Multi-transaction fee planning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiTxFeeStrategy {
    /// Total budget for all transactions (satoshis)
    pub total_budget: u64,
    /// List of planned transactions with sizes
    pub transactions: Vec<PlannedTransaction>,
    /// Global minimum fee rate (sat/vB)
    pub min_fee_rate: f64,
}

/// A planned transaction in a multi-transaction batch
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlannedTransaction {
    /// Transaction ID or label
    pub id: String,
    /// Transaction size in vbytes
    pub vbytes: usize,
    /// Priority level
    pub urgency: TransactionUrgency,
    /// Optional maximum fee for this transaction
    pub max_fee: Option<u64>,
}

impl MultiTxFeeStrategy {
    /// Create a new multi-transaction fee strategy
    pub fn new(total_budget: u64, min_fee_rate: f64) -> Self {
        Self {
            total_budget,
            transactions: Vec::new(),
            min_fee_rate,
        }
    }

    /// Add a transaction to the plan
    pub fn add_transaction(&mut self, tx: PlannedTransaction) {
        self.transactions.push(tx);
    }

    /// Calculate optimal fee allocation across all transactions
    pub fn calculate_fee_allocation(
        &self,
        market_fee_rate: f64,
    ) -> Result<Vec<TxFeeAllocation>, BitcoinError> {
        if self.transactions.is_empty() {
            return Ok(Vec::new());
        }

        // Calculate total size and priority-weighted size
        let mut allocations = Vec::new();

        // Calculate base allocation proportional to size and priority
        let mut remaining_budget = self.total_budget;

        for tx in &self.transactions {
            let priority_multiplier = tx.urgency.fee_multiplier();
            let base_fee = (tx.vbytes as f64 * market_fee_rate * priority_multiplier) as u64;

            // Apply transaction-specific max fee if set
            let allocated_fee = if let Some(max_fee) = tx.max_fee {
                base_fee.min(max_fee)
            } else {
                base_fee
            };

            let allocated_fee = allocated_fee.min(remaining_budget);
            remaining_budget = remaining_budget.saturating_sub(allocated_fee);

            let fee_rate = allocated_fee as f64 / tx.vbytes as f64;

            allocations.push(TxFeeAllocation {
                tx_id: tx.id.clone(),
                allocated_fee,
                fee_rate,
                vbytes: tx.vbytes,
                urgency: tx.urgency,
            });
        }

        // Check if minimum fee rate is met for all transactions
        for alloc in &allocations {
            if alloc.fee_rate < self.min_fee_rate {
                return Err(BitcoinError::InsufficientFunds(format!(
                    "Insufficient budget to meet minimum fee rate for tx: {}",
                    alloc.tx_id
                )));
            }
        }

        Ok(allocations)
    }

    /// Calculate total cost at market rate
    pub fn total_cost_at_rate(&self, fee_rate: f64) -> u64 {
        self.transactions
            .iter()
            .map(|tx| {
                let multiplier = tx.urgency.fee_multiplier();
                (tx.vbytes as f64 * fee_rate * multiplier) as u64
            })
            .sum()
    }
}

/// Fee allocation for a specific transaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxFeeAllocation {
    /// Transaction ID
    pub tx_id: String,
    /// Allocated fee in satoshis
    pub allocated_fee: u64,
    /// Resulting fee rate (sat/vB)
    pub fee_rate: f64,
    /// Transaction size in vbytes
    pub vbytes: usize,
    /// Transaction urgency
    pub urgency: TransactionUrgency,
}

/// Adaptive fee manager
pub struct AdaptiveFeeManager {
    client: BitcoinClient,
}

impl AdaptiveFeeManager {
    /// Create a new adaptive fee manager
    pub fn new(client: BitcoinClient) -> Self {
        Self { client }
    }

    /// Get current market fee rate estimate
    pub fn get_market_fee_rate(&self, target_blocks: u32) -> Result<f64, BitcoinError> {
        let estimate = self.client.estimate_smart_fee(target_blocks as u16)?;
        Ok(estimate.unwrap_or(1.0))
    }

    /// Calculate fee rate based on urgency
    pub fn calculate_urgency_fee_rate(
        &self,
        urgency: TransactionUrgency,
    ) -> Result<f64, BitcoinError> {
        let target_blocks = urgency.target_blocks();
        let market_rate = self.get_market_fee_rate(target_blocks)?;
        let multiplier = urgency.fee_multiplier();
        Ok(market_rate * multiplier)
    }

    /// Calculate fee rate for time-based strategy
    pub fn calculate_time_based_fee_rate(
        &self,
        strategy: &TimeBasedStrategy,
    ) -> Result<f64, BitcoinError> {
        let market_rate = self.get_market_fee_rate(6)?;
        Ok(strategy.calculate_fee_rate(market_rate))
    }

    /// Calculate fee rate for budget strategy
    pub fn calculate_budget_fee_rate(
        &self,
        strategy: &BudgetStrategy,
    ) -> Result<f64, BitcoinError> {
        let market_rate = self.get_market_fee_rate(6)?;
        strategy.calculate_fee_rate(market_rate)
    }

    /// Calculate fee allocation for multi-transaction strategy
    pub fn calculate_multi_tx_allocation(
        &self,
        strategy: &MultiTxFeeStrategy,
    ) -> Result<Vec<TxFeeAllocation>, BitcoinError> {
        let market_rate = self.get_market_fee_rate(6)?;
        strategy.calculate_fee_allocation(market_rate)
    }

    /// Get recommended fee rate with fallback logic
    pub fn get_recommended_fee_rate(
        &self,
        urgency: TransactionUrgency,
        max_fee_rate: Option<f64>,
    ) -> Result<f64, BitcoinError> {
        let mut fee_rate = self.calculate_urgency_fee_rate(urgency)?;

        // Apply maximum cap if provided
        if let Some(max_rate) = max_fee_rate {
            fee_rate = fee_rate.min(max_rate);
        }

        // Ensure minimum of 1 sat/vB
        Ok(fee_rate.max(1.0))
    }
}

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

    #[test]
    fn test_transaction_urgency_target_blocks() {
        assert_eq!(TransactionUrgency::Low.target_blocks(), 144);
        assert_eq!(TransactionUrgency::Medium.target_blocks(), 6);
        assert_eq!(TransactionUrgency::High.target_blocks(), 2);
        assert_eq!(TransactionUrgency::Critical.target_blocks(), 1);
    }

    #[test]
    fn test_transaction_urgency_multiplier() {
        assert_eq!(TransactionUrgency::Low.fee_multiplier(), 1.0);
        assert_eq!(TransactionUrgency::Medium.fee_multiplier(), 1.5);
        assert_eq!(TransactionUrgency::High.fee_multiplier(), 2.0);
        assert_eq!(TransactionUrgency::Critical.fee_multiplier(), 3.0);
    }

    #[test]
    fn test_time_based_strategy() {
        let target_time = Utc::now() + chrono::Duration::hours(12);
        let strategy = TimeBasedStrategy::new(target_time, 1.0, 100.0);

        let fee_rate = strategy.calculate_fee_rate(10.0);
        assert!(fee_rate >= 1.0);
        assert!(fee_rate <= 100.0);
    }

    #[test]
    fn test_budget_strategy() {
        let strategy = BudgetStrategy::new(10_000, 200);

        assert_eq!(strategy.max_fee_rate(), 50.0);
        assert!(strategy.fits_budget(40.0));
        assert!(!strategy.fits_budget(60.0));
    }

    #[test]
    fn test_budget_strategy_calculate_fee_rate() {
        let strategy = BudgetStrategy::new(5_000, 200);

        // Market rate below max - should use market rate
        let result = strategy.calculate_fee_rate(20.0);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 20.0);

        // Market rate above max - should cap at max
        let result = strategy.calculate_fee_rate(30.0);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 25.0); // max_fee_rate = 5000/200 = 25
    }

    #[test]
    fn test_multi_tx_strategy() {
        let mut strategy = MultiTxFeeStrategy::new(50_000, 1.0);

        strategy.add_transaction(PlannedTransaction {
            id: "tx1".to_string(),
            vbytes: 200,
            urgency: TransactionUrgency::High,
            max_fee: None,
        });

        strategy.add_transaction(PlannedTransaction {
            id: "tx2".to_string(),
            vbytes: 150,
            urgency: TransactionUrgency::Low,
            max_fee: None,
        });

        let total_cost = strategy.total_cost_at_rate(10.0);
        assert!(total_cost > 0);
    }

    #[test]
    fn test_multi_tx_fee_allocation() {
        let mut strategy = MultiTxFeeStrategy::new(10_000, 1.0);

        strategy.add_transaction(PlannedTransaction {
            id: "tx1".to_string(),
            vbytes: 200,
            urgency: TransactionUrgency::Medium,
            max_fee: Some(4_000),
        });

        strategy.add_transaction(PlannedTransaction {
            id: "tx2".to_string(),
            vbytes: 200,
            urgency: TransactionUrgency::Low,
            max_fee: None,
        });

        let result = strategy.calculate_fee_allocation(10.0);
        assert!(result.is_ok());

        let allocations = result.unwrap();
        assert_eq!(allocations.len(), 2);

        let total_allocated: u64 = allocations.iter().map(|a| a.allocated_fee).sum();
        assert!(total_allocated <= 10_000);
    }

    #[test]
    fn test_planned_transaction() {
        let tx = PlannedTransaction {
            id: "test_tx".to_string(),
            vbytes: 250,
            urgency: TransactionUrgency::High,
            max_fee: Some(10_000),
        };

        assert_eq!(tx.id, "test_tx");
        assert_eq!(tx.vbytes, 250);
        assert_eq!(tx.urgency, TransactionUrgency::High);
        assert_eq!(tx.max_fee, Some(10_000));
    }
}