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
//! On-chain analytics system
//!
//! This module provides on-chain analytics capabilities including:
//! - Wallet profiling
//! - Token holder analysis
//! - Transaction graph analysis
//! - Smart money tracking

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, HashSet};

/// Wallet profile with trading behavior analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WalletProfile {
    /// Wallet address
    pub address: String,

    /// Total transaction count
    pub tx_count: u64,

    /// Total volume traded (in USD)
    pub total_volume: Decimal,

    /// Number of unique tokens traded
    pub unique_tokens: usize,

    /// Win rate (profitable trades / total trades)
    pub win_rate: f64,

    /// Average profit per trade
    pub avg_profit: Decimal,

    /// Wallet age (days)
    pub age_days: u64,

    /// Activity score (0-100)
    pub activity_score: u32,

    /// Risk score (0-100, higher = riskier)
    pub risk_score: u32,

    /// Wallet category
    pub category: WalletCategory,

    /// Last activity timestamp
    pub last_activity: DateTime<Utc>,

    /// Created timestamp
    pub created_at: DateTime<Utc>,
}

/// Wallet category classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WalletCategory {
    /// High-frequency trader
    HFT,

    /// Long-term holder
    Holder,

    /// Whale (large holder)
    Whale,

    /// Smart money (consistently profitable)
    SmartMoney,

    /// Bot (automated trading)
    Bot,

    /// Casual user
    Casual,

    /// Suspicious (potential manipulation)
    Suspicious,
}

impl WalletProfile {
    /// Create a new wallet profile
    pub fn new(address: String) -> Self {
        Self {
            address,
            tx_count: 0,
            total_volume: Decimal::ZERO,
            unique_tokens: 0,
            win_rate: 0.0,
            avg_profit: Decimal::ZERO,
            age_days: 0,
            activity_score: 0,
            risk_score: 50,
            category: WalletCategory::Casual,
            last_activity: Utc::now(),
            created_at: Utc::now(),
        }
    }

    /// Update profile with new transaction
    pub fn update_with_tx(&mut self, volume: Decimal, profit: Decimal, _token: String) {
        self.tx_count += 1;
        self.total_volume += volume;
        self.last_activity = Utc::now();

        // Update average profit
        let total_profit = self.avg_profit * Decimal::from(self.tx_count - 1) + profit;
        self.avg_profit = total_profit / Decimal::from(self.tx_count);

        // Recalculate scores
        self.recalculate_scores();
    }

    /// Recalculate activity and risk scores
    fn recalculate_scores(&mut self) {
        // Activity score based on transaction frequency and volume
        let volume_score = (self.total_volume / dec!(1000000))
            .min(dec!(50))
            .to_u32()
            .unwrap_or(0);
        let tx_score = (self.tx_count / 10).min(50) as u32;
        self.activity_score = volume_score + tx_score;

        // Risk score based on various factors
        let mut risk = 50u32;

        // High win rate might indicate insider trading
        if self.win_rate > 0.8 {
            risk += 20;
        }

        // Very high activity might indicate bot
        if self.tx_count > 1000 {
            risk += 15;
        }

        self.risk_score = risk.min(100);

        // Update category based on behavior
        self.category = self.classify_wallet();
    }

    /// Classify wallet based on trading behavior
    fn classify_wallet(&self) -> WalletCategory {
        // Whale: > $10M volume
        if self.total_volume > dec!(10000000) {
            return WalletCategory::Whale;
        }

        // Smart money: high win rate and profitable
        if self.win_rate > 0.75 && self.avg_profit > dec!(1000) {
            return WalletCategory::SmartMoney;
        }

        // HFT: high transaction count with low average volume
        if self.tx_count > 500 && self.total_volume / Decimal::from(self.tx_count) < dec!(1000) {
            return WalletCategory::HFT;
        }

        // Bot: very high transaction count with consistent patterns
        if self.tx_count > 1000 {
            return WalletCategory::Bot;
        }

        // Suspicious: high risk score
        if self.risk_score > 80 {
            return WalletCategory::Suspicious;
        }

        // Holder: low transaction count, long age
        if self.tx_count < 50 && self.age_days > 365 {
            return WalletCategory::Holder;
        }

        WalletCategory::Casual
    }

    /// Check if wallet is smart money
    pub fn is_smart_money(&self) -> bool {
        matches!(self.category, WalletCategory::SmartMoney)
    }

    /// Check if wallet is suspicious
    pub fn is_suspicious(&self) -> bool {
        matches!(self.category, WalletCategory::Suspicious) || self.risk_score > 80
    }
}

/// Token holder analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenHolderAnalysis {
    /// Token symbol
    pub token_symbol: String,

    /// Total number of holders
    pub total_holders: usize,

    /// Top 10 holders concentration (%)
    pub top10_concentration: f64,

    /// Top 100 holders concentration (%)
    pub top100_concentration: f64,

    /// Whale count (holders with > 1% of supply)
    pub whale_count: usize,

    /// Average holding time (days)
    pub avg_holding_time: f64,

    /// Holder growth rate (30-day)
    pub holder_growth_rate: f64,

    /// Distribution score (0-100, higher = more distributed)
    pub distribution_score: u32,

    /// Updated timestamp
    pub updated_at: DateTime<Utc>,
}

impl TokenHolderAnalysis {
    /// Create new token holder analysis
    pub fn new(token_symbol: String) -> Self {
        Self {
            token_symbol,
            total_holders: 0,
            top10_concentration: 0.0,
            top100_concentration: 0.0,
            whale_count: 0,
            avg_holding_time: 0.0,
            holder_growth_rate: 0.0,
            distribution_score: 0,
            updated_at: Utc::now(),
        }
    }

    /// Analyze token holders
    pub fn analyze(&mut self, holders: &[(String, Decimal)], total_supply: Decimal) {
        self.total_holders = holders.len();

        // Calculate concentration
        let mut sorted_holders = holders.to_vec();
        sorted_holders.sort_by(|a, b| b.1.cmp(&a.1));

        // Top 10 concentration
        let top10_sum: Decimal = sorted_holders
            .iter()
            .take(10)
            .map(|(_, amount)| amount)
            .sum();
        self.top10_concentration = if total_supply > Decimal::ZERO {
            (top10_sum / total_supply * dec!(100))
                .to_f64()
                .unwrap_or(0.0)
        } else {
            0.0
        };

        // Top 100 concentration
        let top100_sum: Decimal = sorted_holders
            .iter()
            .take(100)
            .map(|(_, amount)| amount)
            .sum();
        self.top100_concentration = if total_supply > Decimal::ZERO {
            (top100_sum / total_supply * dec!(100))
                .to_f64()
                .unwrap_or(0.0)
        } else {
            0.0
        };

        // Count whales (holders with > 1% of supply)
        let whale_threshold = total_supply * dec!(0.01);
        self.whale_count = sorted_holders
            .iter()
            .filter(|(_, amount)| *amount > whale_threshold)
            .count();

        // Calculate distribution score (higher is better)
        self.distribution_score = if self.top10_concentration < 10.0 {
            90
        } else if self.top10_concentration < 20.0 {
            75
        } else if self.top10_concentration < 40.0 {
            50
        } else if self.top10_concentration < 60.0 {
            25
        } else {
            10
        };

        self.updated_at = Utc::now();
    }

    /// Check if distribution is healthy
    pub fn is_healthy_distribution(&self) -> bool {
        self.distribution_score >= 50 && self.top10_concentration < 50.0
    }
}

/// Transaction graph node (wallet)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphNode {
    /// On-chain wallet address.
    pub address: String,
    /// Set of addresses this wallet has directly interacted with.
    pub connections: HashSet<String>,
    /// Total number of on-chain interactions.
    pub total_interactions: u64,
    /// Cumulative value transferred to/from this wallet.
    pub total_value_transferred: Decimal,
}

/// Transaction graph for analyzing wallet interactions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionGraph {
    /// Graph nodes (wallets)
    pub nodes: HashMap<String, GraphNode>,

    /// Edges (wallet interactions)
    pub edges: Vec<(String, String, Decimal)>,
}

impl TransactionGraph {
    /// Create new transaction graph
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
            edges: Vec::new(),
        }
    }

    /// Add transaction to graph
    pub fn add_transaction(&mut self, from: String, to: String, value: Decimal) {
        // Add or update from node
        let from_node = self.nodes.entry(from.clone()).or_insert_with(|| GraphNode {
            address: from.clone(),
            connections: HashSet::new(),
            total_interactions: 0,
            total_value_transferred: Decimal::ZERO,
        });
        from_node.connections.insert(to.clone());
        from_node.total_interactions += 1;
        from_node.total_value_transferred += value;

        // Add or update to node
        let to_node = self.nodes.entry(to.clone()).or_insert_with(|| GraphNode {
            address: to.clone(),
            connections: HashSet::new(),
            total_interactions: 0,
            total_value_transferred: Decimal::ZERO,
        });
        to_node.connections.insert(from.clone());
        to_node.total_interactions += 1;
        to_node.total_value_transferred += value;

        // Add edge
        self.edges.push((from, to, value));
    }

    /// Find highly connected wallets (potential hubs)
    pub fn find_hubs(&self, min_connections: usize) -> Vec<String> {
        self.nodes
            .iter()
            .filter(|(_, node)| node.connections.len() >= min_connections)
            .map(|(addr, _)| addr.clone())
            .collect()
    }

    /// Detect potential money laundering rings (circular transactions)
    pub fn detect_rings(&self, max_depth: usize) -> Vec<Vec<String>> {
        let mut rings = Vec::new();

        for start_addr in self.nodes.keys() {
            if let Some(ring) = self.find_ring(start_addr, start_addr, &mut Vec::new(), max_depth) {
                if ring.len() >= 3 && !rings.contains(&ring) {
                    rings.push(ring);
                }
            }
        }

        rings
    }

    /// Helper function to find rings using DFS
    fn find_ring(
        &self,
        current: &str,
        target: &str,
        path: &mut Vec<String>,
        depth: usize,
    ) -> Option<Vec<String>> {
        if depth == 0 {
            return None;
        }

        path.push(current.to_string());

        if path.len() > 1 && current == target {
            return Some(path.clone());
        }

        if let Some(node) = self.nodes.get(current) {
            for next in &node.connections {
                if path.len() == 1 || next != &path[path.len() - 2] {
                    if let Some(ring) = self.find_ring(next, target, path, depth - 1) {
                        return Some(ring);
                    }
                }
            }
        }

        path.pop();
        None
    }
}

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

/// Smart money tracker
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartMoneyTracker {
    /// Tracked wallets
    pub tracked_wallets: HashMap<String, WalletProfile>,

    /// Recent trades by smart money
    pub recent_trades: Vec<SmartMoneyTrade>,

    /// Token accumulation by smart money
    pub accumulation: HashMap<String, Decimal>,
}

/// Smart money trade record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartMoneyTrade {
    /// Wallet address that executed the trade.
    pub wallet: String,
    /// Token symbol or address traded.
    pub token: String,
    /// Buy or sell direction.
    pub side: TradeSide,
    /// Trade size.
    pub amount: Decimal,
    /// Execution price.
    pub price: Decimal,
    /// Time the trade was recorded on-chain.
    pub timestamp: DateTime<Utc>,
}

/// Trade side
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TradeSide {
    /// Token was purchased.
    Buy,
    /// Token was sold.
    Sell,
}

impl SmartMoneyTracker {
    /// Create new smart money tracker
    pub fn new() -> Self {
        Self {
            tracked_wallets: HashMap::new(),
            recent_trades: Vec::new(),
            accumulation: HashMap::new(),
        }
    }

    /// Add wallet to tracking
    pub fn track_wallet(&mut self, profile: WalletProfile) {
        if profile.is_smart_money() {
            self.tracked_wallets
                .insert(profile.address.clone(), profile);
        }
    }

    /// Record smart money trade
    pub fn record_trade(
        &mut self,
        wallet: String,
        token: String,
        side: TradeSide,
        amount: Decimal,
        price: Decimal,
    ) {
        // Only track if wallet is in tracked list
        if self.tracked_wallets.contains_key(&wallet) {
            let trade = SmartMoneyTrade {
                wallet: wallet.clone(),
                token: token.clone(),
                side,
                amount,
                price,
                timestamp: Utc::now(),
            };

            self.recent_trades.push(trade);

            // Update accumulation
            let value = amount * price;
            let entry = self.accumulation.entry(token).or_insert(Decimal::ZERO);

            match side {
                TradeSide::Buy => *entry += value,
                TradeSide::Sell => *entry -= value,
            }

            // Keep only last 1000 trades
            if self.recent_trades.len() > 1000 {
                self.recent_trades.remove(0);
            }
        }
    }

    /// Get tokens being accumulated by smart money
    pub fn get_accumulating_tokens(&self) -> Vec<(String, Decimal)> {
        let mut tokens: Vec<_> = self
            .accumulation
            .iter()
            .filter(|(_, value)| **value > Decimal::ZERO)
            .map(|(token, value)| (token.clone(), *value))
            .collect();

        tokens.sort_by(|a, b| b.1.cmp(&a.1));
        tokens
    }

    /// Get tokens being distributed by smart money
    pub fn get_distributing_tokens(&self) -> Vec<(String, Decimal)> {
        let mut tokens: Vec<_> = self
            .accumulation
            .iter()
            .filter(|(_, value)| **value < Decimal::ZERO)
            .map(|(token, value)| (token.clone(), value.abs()))
            .collect();

        tokens.sort_by(|a, b| b.1.cmp(&a.1));
        tokens
    }

    /// Get smart money sentiment for a token (positive = bullish)
    pub fn get_sentiment(&self, token: &str) -> Decimal {
        self.accumulation
            .get(token)
            .copied()
            .unwrap_or(Decimal::ZERO)
    }
}

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

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

    #[test]
    fn test_wallet_profile() {
        let mut profile = WalletProfile::new("wallet1".to_string());

        profile.update_with_tx(dec!(1000), dec!(100), "BTC".to_string());
        profile.update_with_tx(dec!(2000), dec!(200), "ETH".to_string());

        assert_eq!(profile.tx_count, 2);
        assert_eq!(profile.total_volume, dec!(3000));
        assert_eq!(profile.avg_profit, dec!(150));
    }

    #[test]
    fn test_token_holder_analysis() {
        let mut analysis = TokenHolderAnalysis::new("BTC".to_string());

        let holders = vec![
            ("whale1".to_string(), dec!(5000)),
            ("whale2".to_string(), dec!(3000)),
            ("holder1".to_string(), dec!(100)),
            ("holder2".to_string(), dec!(50)),
        ];

        analysis.analyze(&holders, dec!(10000));

        assert_eq!(analysis.total_holders, 4);
        assert_eq!(analysis.whale_count, 2);
        assert!(analysis.top10_concentration > 0.0);
    }

    #[test]
    fn test_transaction_graph() {
        let mut graph = TransactionGraph::new();

        graph.add_transaction("A".to_string(), "B".to_string(), dec!(100));
        graph.add_transaction("B".to_string(), "C".to_string(), dec!(200));
        graph.add_transaction("A".to_string(), "C".to_string(), dec!(150));

        assert_eq!(graph.nodes.len(), 3);
        assert_eq!(graph.edges.len(), 3);

        let hubs = graph.find_hubs(2);
        assert!(!hubs.is_empty());
    }

    #[test]
    fn test_smart_money_tracker() {
        let mut tracker = SmartMoneyTracker::new();

        let mut profile = WalletProfile::new("smart1".to_string());
        profile.category = WalletCategory::SmartMoney;
        tracker.track_wallet(profile);

        tracker.record_trade(
            "smart1".to_string(),
            "BTC".to_string(),
            TradeSide::Buy,
            dec!(1),
            dec!(50000),
        );

        let accumulating = tracker.get_accumulating_tokens();
        assert_eq!(accumulating.len(), 1);
        assert_eq!(accumulating[0].0, "BTC");
    }
}