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
//! Cross-chain bridge integration for asset transfers
use crate::error::CoreError;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Supported blockchain networks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Chain {
    /// Bitcoin mainnet
    Bitcoin,
    /// Ethereum mainnet
    Ethereum,
    /// Polygon PoS
    Polygon,
    /// Arbitrum One
    Arbitrum,
    /// Optimism
    Optimism,
    /// Base
    Base,
    /// Stacks
    Stacks,
}

impl Chain {
    /// Get the chain ID for the network
    pub fn chain_id(&self) -> u64 {
        match self {
            Chain::Bitcoin => 0,
            Chain::Ethereum => 1,
            Chain::Polygon => 137,
            Chain::Arbitrum => 42161,
            Chain::Optimism => 10,
            Chain::Base => 8453,
            Chain::Stacks => 1,
        }
    }

    /// Get the native currency symbol
    pub fn native_currency(&self) -> &str {
        match self {
            Chain::Bitcoin => "BTC",
            Chain::Ethereum => "ETH",
            Chain::Polygon => "MATIC",
            Chain::Arbitrum => "ETH",
            Chain::Optimism => "ETH",
            Chain::Base => "ETH",
            Chain::Stacks => "STX",
        }
    }
}

/// Bridge provider abstraction
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BridgeProvider {
    /// Our native bridge
    Native,
    /// LayerZero omnichain protocol
    LayerZero,
    /// Wormhole bridge
    Wormhole,
    /// Stargate Finance
    Stargate,
    /// Axelar Network
    Axelar,
    /// Multichain (formerly Anyswap)
    Multichain,
}

impl BridgeProvider {
    /// Get supported chains for this provider
    pub fn supported_chains(&self) -> Vec<Chain> {
        match self {
            BridgeProvider::Native => vec![Chain::Bitcoin, Chain::Stacks],
            BridgeProvider::LayerZero => vec![
                Chain::Ethereum,
                Chain::Polygon,
                Chain::Arbitrum,
                Chain::Optimism,
                Chain::Base,
            ],
            BridgeProvider::Wormhole => vec![
                Chain::Ethereum,
                Chain::Polygon,
                Chain::Arbitrum,
                Chain::Optimism,
            ],
            BridgeProvider::Stargate => vec![
                Chain::Ethereum,
                Chain::Polygon,
                Chain::Arbitrum,
                Chain::Optimism,
            ],
            BridgeProvider::Axelar => vec![Chain::Ethereum, Chain::Polygon, Chain::Arbitrum],
            BridgeProvider::Multichain => vec![Chain::Ethereum, Chain::Polygon, Chain::Arbitrum],
        }
    }

    /// Get estimated bridge fee as percentage
    pub fn fee_percentage(&self) -> Decimal {
        match self {
            BridgeProvider::Native => Decimal::new(10, 3), // 0.01% (0.001)
            BridgeProvider::LayerZero => Decimal::new(5, 3), // 0.005%
            BridgeProvider::Wormhole => Decimal::new(15, 3), // 0.015%
            BridgeProvider::Stargate => Decimal::new(6, 3), // 0.006%
            BridgeProvider::Axelar => Decimal::new(20, 3), // 0.020%
            BridgeProvider::Multichain => Decimal::new(10, 3), // 0.010%
        }
    }
}

/// Status of a bridge transfer
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BridgeTransferStatus {
    /// Transfer is queued but not yet submitted
    Pending,
    /// Awaiting block confirmations on the source chain
    Confirming,
    /// Transfer is being processed by the bridge
    InProgress,
    /// Transfer completed successfully
    Completed,
    /// Transfer failed
    Failed,
    /// Transfer was refunded to the sender
    Refunded,
}

/// Bridge transfer record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BridgeTransfer {
    /// Unique identifier of this transfer
    pub id: Uuid,
    /// User initiating the transfer
    pub user_id: Uuid,
    /// Token being transferred
    pub token_id: Uuid,
    /// Chain the transfer originates from
    pub source_chain: Chain,
    /// Chain the transfer is destined for
    pub destination_chain: Chain,
    /// Bridge protocol to use
    pub provider: BridgeProvider,
    /// Amount being transferred
    pub amount: Decimal,
    /// Bridge fee charged
    pub fee: Decimal,
    /// Current status of the transfer
    pub status: BridgeTransferStatus,
    /// Transaction hash on the source chain
    pub source_tx_hash: Option<String>,
    /// Transaction hash on the destination chain
    pub destination_tx_hash: Option<String>,
    /// Timestamp when the transfer was created
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Timestamp of the most recent status update
    pub updated_at: chrono::DateTime<chrono::Utc>,
    /// Timestamp when the transfer completed
    pub completed_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl BridgeTransfer {
    /// Create a new bridge transfer
    pub fn new(
        user_id: Uuid,
        token_id: Uuid,
        source_chain: Chain,
        destination_chain: Chain,
        provider: BridgeProvider,
        amount: Decimal,
    ) -> Result<Self, CoreError> {
        if amount <= Decimal::ZERO {
            return Err(CoreError::InvalidAmount);
        }

        // Validate chains are supported by provider
        let supported = provider.supported_chains();
        if !supported.contains(&source_chain) || !supported.contains(&destination_chain) {
            return Err(CoreError::InvalidBridgeRoute);
        }

        if source_chain == destination_chain {
            return Err(CoreError::InvalidBridgeRoute);
        }

        let fee = amount * provider.fee_percentage();
        let now = chrono::Utc::now();

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            token_id,
            source_chain,
            destination_chain,
            provider,
            amount,
            fee,
            status: BridgeTransferStatus::Pending,
            source_tx_hash: None,
            destination_tx_hash: None,
            created_at: now,
            updated_at: now,
            completed_at: None,
        })
    }

    /// Get the net amount after fees
    pub fn net_amount(&self) -> Decimal {
        self.amount - self.fee
    }

    /// Update transfer status
    pub fn update_status(&mut self, status: BridgeTransferStatus) {
        self.status = status.clone();
        self.updated_at = chrono::Utc::now();

        if status == BridgeTransferStatus::Completed {
            self.completed_at = Some(chrono::Utc::now());
        }
    }
}

/// Multi-chain token tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiChainToken {
    /// Token identifier
    pub token_id: Uuid,
    /// Token ticker symbol
    pub symbol: String,
    /// Contract addresses per chain
    pub chain_addresses: HashMap<Chain, String>,
    /// Total circulating supply per chain
    pub total_supply_by_chain: HashMap<Chain, Decimal>,
}

impl MultiChainToken {
    /// Create a new multi-chain token
    pub fn new(token_id: Uuid, symbol: String) -> Self {
        Self {
            token_id,
            symbol,
            chain_addresses: HashMap::new(),
            total_supply_by_chain: HashMap::new(),
        }
    }

    /// Add a chain deployment
    pub fn add_chain(&mut self, chain: Chain, address: String, initial_supply: Decimal) {
        self.chain_addresses.insert(chain, address);
        self.total_supply_by_chain.insert(chain, initial_supply);
    }

    /// Get total supply across all chains
    pub fn total_supply(&self) -> Decimal {
        self.total_supply_by_chain.values().sum()
    }

    /// Update supply on a specific chain
    pub fn update_chain_supply(&mut self, chain: Chain, new_supply: Decimal) {
        self.total_supply_by_chain.insert(chain, new_supply);
    }

    /// Get chains where token is deployed
    pub fn deployed_chains(&self) -> Vec<Chain> {
        self.chain_addresses.keys().copied().collect()
    }
}

/// Cross-chain arbitrage opportunity detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossChainArbitrage {
    /// Token with the price discrepancy
    pub token_id: Uuid,
    /// Chain where the token is cheaper (buy side)
    pub buy_chain: Chain,
    /// Chain where the token is more expensive (sell side)
    pub sell_chain: Chain,
    /// Price on the buy chain
    pub buy_price: Decimal,
    /// Price on the sell chain
    pub sell_price: Decimal,
    /// Expected profit as a percentage of cost
    pub profit_percentage: Decimal,
    /// Bridge provider to use for the transfer
    pub bridge_provider: BridgeProvider,
    /// Estimated net profit after fees
    pub estimated_profit: Decimal,
    /// Timestamp when this opportunity was detected
    pub detected_at: chrono::DateTime<chrono::Utc>,
}

impl CrossChainArbitrage {
    /// Detect arbitrage opportunity
    pub fn detect(
        token_id: Uuid,
        chain_prices: HashMap<Chain, Decimal>,
        bridge_provider: BridgeProvider,
        amount: Decimal,
    ) -> Option<Self> {
        let mut opportunities = Vec::new();

        // Find all pairs of chains with price differences
        let chains: Vec<_> = chain_prices.keys().copied().collect();
        for i in 0..chains.len() {
            for j in (i + 1)..chains.len() {
                let chain_a = chains[i];
                let chain_b = chains[j];
                let price_a = chain_prices[&chain_a];
                let price_b = chain_prices[&chain_b];

                // Check if both chains are supported by the provider
                let supported = bridge_provider.supported_chains();
                if !supported.contains(&chain_a) || !supported.contains(&chain_b) {
                    continue;
                }

                // Calculate profit considering bridge fees
                let bridge_fee_pct = bridge_provider.fee_percentage();
                let bridge_fee = amount * bridge_fee_pct;

                if price_a < price_b {
                    // Buy on chain A, sell on chain B
                    let buy_cost = amount * price_a;
                    let sell_revenue = (amount - bridge_fee) * price_b;
                    let profit = sell_revenue - buy_cost;
                    let profit_pct = (profit / buy_cost) * Decimal::from(100);

                    if profit > Decimal::ZERO {
                        opportunities.push(CrossChainArbitrage {
                            token_id,
                            buy_chain: chain_a,
                            sell_chain: chain_b,
                            buy_price: price_a,
                            sell_price: price_b,
                            profit_percentage: profit_pct,
                            bridge_provider: bridge_provider.clone(),
                            estimated_profit: profit,
                            detected_at: chrono::Utc::now(),
                        });
                    }
                } else if price_b < price_a {
                    // Buy on chain B, sell on chain A
                    let buy_cost = amount * price_b;
                    let sell_revenue = (amount - bridge_fee) * price_a;
                    let profit = sell_revenue - buy_cost;
                    let profit_pct = (profit / buy_cost) * Decimal::from(100);

                    if profit > Decimal::ZERO {
                        opportunities.push(CrossChainArbitrage {
                            token_id,
                            buy_chain: chain_b,
                            sell_chain: chain_a,
                            buy_price: price_b,
                            sell_price: price_a,
                            profit_percentage: profit_pct,
                            bridge_provider: bridge_provider.clone(),
                            estimated_profit: profit,
                            detected_at: chrono::Utc::now(),
                        });
                    }
                }
            }
        }

        // Return the most profitable opportunity
        opportunities
            .into_iter()
            .max_by(|a, b| a.estimated_profit.cmp(&b.estimated_profit))
    }

    /// Check if opportunity is still profitable after time decay
    pub fn is_still_profitable(&self, min_profit_percentage: Decimal) -> bool {
        self.profit_percentage >= min_profit_percentage
    }
}

/// Multi-chain portfolio view
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiChainPortfolio {
    /// User who owns this portfolio
    pub user_id: Uuid,
    /// Token balances indexed by chain, then token ID
    pub balances_by_chain: HashMap<Chain, HashMap<Uuid, Decimal>>,
    /// Timestamp of the most recent balance update
    pub last_updated: chrono::DateTime<chrono::Utc>,
}

impl MultiChainPortfolio {
    /// Create a new multi-chain portfolio
    pub fn new(user_id: Uuid) -> Self {
        Self {
            user_id,
            balances_by_chain: HashMap::new(),
            last_updated: chrono::Utc::now(),
        }
    }

    /// Add balance on a specific chain
    pub fn add_balance(&mut self, chain: Chain, token_id: Uuid, amount: Decimal) {
        self.balances_by_chain
            .entry(chain)
            .or_default()
            .insert(token_id, amount);
        self.last_updated = chrono::Utc::now();
    }

    /// Get balance for a token across all chains
    pub fn total_balance(&self, token_id: Uuid) -> Decimal {
        self.balances_by_chain
            .values()
            .filter_map(|balances| balances.get(&token_id))
            .sum()
    }

    /// Get balance for a token on a specific chain
    pub fn balance_on_chain(&self, chain: Chain, token_id: Uuid) -> Decimal {
        self.balances_by_chain
            .get(&chain)
            .and_then(|balances| balances.get(&token_id))
            .copied()
            .unwrap_or(Decimal::ZERO)
    }

    /// Get all chains where user has balances
    pub fn active_chains(&self) -> Vec<Chain> {
        self.balances_by_chain
            .iter()
            .filter(|(_, balances)| !balances.is_empty())
            .map(|(chain, _)| *chain)
            .collect()
    }

    /// Get total number of unique tokens across all chains
    pub fn unique_tokens(&self) -> usize {
        let mut tokens = std::collections::HashSet::<&Uuid>::new();
        for balances in self.balances_by_chain.values() {
            tokens.extend(balances.keys());
        }
        tokens.len()
    }

    /// Calculate total portfolio value (requires price data)
    pub fn total_value(&self, prices: &HashMap<(Chain, Uuid), Decimal>) -> Decimal {
        let mut total = Decimal::ZERO;
        for (chain, balances) in &self.balances_by_chain {
            for (token_id, amount) in balances {
                if let Some(price) = prices.get(&(*chain, *token_id)) {
                    total += amount * price;
                }
            }
        }
        total
    }
}

/// Cross-chain risk analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossChainRiskMetrics {
    /// Portfolio identifier (matches user_id in MultiChainPortfolio)
    pub portfolio_id: Uuid,
    /// Percentage of portfolio value concentrated on each chain
    pub chain_concentration: HashMap<Chain, Decimal>,
    /// Bridge risk score (0-100, higher = riskier)
    pub bridge_risk_score: Decimal,
    /// Chain diversification score (0-100, higher = better)
    pub chain_diversification_score: Decimal,
    /// Suggested rebalance actions to improve diversification
    pub recommended_rebalance: Vec<RebalanceAction>,
}

/// Recommended rebalance action for cross-chain portfolio
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RebalanceAction {
    /// Token to be moved
    pub token_id: Uuid,
    /// Source chain for the transfer
    pub from_chain: Chain,
    /// Destination chain for the transfer
    pub to_chain: Chain,
    /// Amount to transfer
    pub amount: Decimal,
    /// Human-readable reason for the recommendation
    pub reason: String,
}

impl CrossChainRiskMetrics {
    /// Calculate risk metrics for a multi-chain portfolio
    pub fn calculate(
        portfolio: &MultiChainPortfolio,
        prices: &HashMap<(Chain, Uuid), Decimal>,
    ) -> Self {
        let total_value = portfolio.total_value(prices);
        let mut chain_concentration = HashMap::new();

        // Calculate chain concentration
        for (chain, balances) in &portfolio.balances_by_chain {
            let mut chain_value = Decimal::ZERO;
            for (token_id, amount) in balances {
                if let Some(price) = prices.get(&(*chain, *token_id)) {
                    chain_value += amount * price;
                }
            }
            if total_value > Decimal::ZERO {
                let concentration = (chain_value / total_value) * Decimal::from(100);
                chain_concentration.insert(*chain, concentration);
            }
        }

        // Calculate bridge risk score (higher concentration = higher risk)
        let max_concentration = chain_concentration
            .values()
            .max()
            .copied()
            .unwrap_or(Decimal::ZERO);
        let bridge_risk_score = max_concentration; // Simple: max concentration = risk

        // Calculate diversification score
        let num_chains = portfolio.active_chains().len();
        let ideal_concentration = Decimal::from(100) / Decimal::from(num_chains.max(1));
        let mut variance = Decimal::ZERO;
        for concentration in chain_concentration.values() {
            let diff = concentration - ideal_concentration;
            variance += diff * diff;
        }
        // Use standard deviation (sqrt of variance) and scale to 0-100
        // Lower deviation = higher score
        let std_dev = if variance > Decimal::ZERO {
            // Approximate sqrt for demonstration (use proper sqrt in production)
            let v = variance / Decimal::from(num_chains.max(1));
            v / Decimal::from(10) // Normalize to roughly 0-10 range
        } else {
            Decimal::ZERO
        };
        let diversification_score =
            (Decimal::from(100) - std_dev.min(Decimal::from(100))).max(Decimal::ZERO);

        // Generate rebalance recommendations if needed
        let mut recommended_rebalance = Vec::new();
        if max_concentration > Decimal::from(60) {
            // If any chain has > 60% concentration, recommend rebalancing
            // This is a simplified version - real implementation would be more sophisticated
            for (chain, concentration) in &chain_concentration {
                if concentration > &Decimal::from(60) {
                    recommended_rebalance.push(RebalanceAction {
                        token_id: Uuid::new_v4(), // Would need actual token data
                        from_chain: *chain,
                        to_chain: Chain::Bitcoin, // Would calculate optimal target
                        amount: Decimal::from(1000), // Would calculate optimal amount
                        reason: format!("High concentration on {:?} ({}%)", chain, concentration),
                    });
                }
            }
        }

        Self {
            portfolio_id: portfolio.user_id,
            chain_concentration,
            bridge_risk_score,
            chain_diversification_score: diversification_score,
            recommended_rebalance,
        }
    }
}

/// Chain-specific fee optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChainFeeOptimizer {
    /// Chain for which fees were optimized
    pub chain: Chain,
    /// Current observed gas price
    pub current_gas_price: Decimal,
    /// Recommended gas price to use
    pub recommended_gas_price: Decimal,
    /// Estimated total transaction cost at the recommended price
    pub estimated_cost: Decimal,
    /// Optimal time to submit the transaction
    pub optimal_execution_time: chrono::DateTime<chrono::Utc>,
}

impl ChainFeeOptimizer {
    /// Optimize transaction fees for a specific chain
    pub fn optimize(chain: Chain, urgency: TransactionUrgency) -> Self {
        let (current_gas, recommended_gas, delay_minutes) = match urgency {
            TransactionUrgency::Immediate => (Decimal::from(50), Decimal::from(50), 0),
            TransactionUrgency::Normal => (Decimal::from(50), Decimal::from(30), 5),
            TransactionUrgency::Low => (Decimal::from(50), Decimal::from(20), 15),
        };

        let estimated_cost = recommended_gas * Decimal::new(21000, 0); // Base gas limit
        let optimal_time = chrono::Utc::now() + chrono::Duration::minutes(delay_minutes);

        Self {
            chain,
            current_gas_price: current_gas,
            recommended_gas_price: recommended_gas,
            estimated_cost,
            optimal_execution_time: optimal_time,
        }
    }

    /// Calculate potential savings
    pub fn potential_savings(&self) -> Decimal {
        (self.current_gas_price - self.recommended_gas_price) * Decimal::new(21000, 0)
    }
}

/// Transaction urgency level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionUrgency {
    /// Must confirm in the next block
    Immediate,
    /// Normal priority — confirm within a few minutes
    Normal,
    /// Low priority — can wait for cheaper fees
    Low,
}

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

    #[test]
    fn test_chain_info() {
        assert_eq!(Chain::Ethereum.chain_id(), 1);
        assert_eq!(Chain::Ethereum.native_currency(), "ETH");
        assert_eq!(Chain::Bitcoin.chain_id(), 0);
        assert_eq!(Chain::Bitcoin.native_currency(), "BTC");
    }

    #[test]
    fn test_bridge_provider_supported_chains() {
        let native = BridgeProvider::Native;
        assert!(native.supported_chains().contains(&Chain::Bitcoin));

        let layerzero = BridgeProvider::LayerZero;
        assert!(layerzero.supported_chains().contains(&Chain::Ethereum));
    }

    #[test]
    fn test_bridge_transfer_creation() {
        let transfer = BridgeTransfer::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            Chain::Ethereum,
            Chain::Polygon,
            BridgeProvider::LayerZero,
            Decimal::from(100),
        )
        .unwrap();

        assert_eq!(transfer.source_chain, Chain::Ethereum);
        assert_eq!(transfer.destination_chain, Chain::Polygon);
        assert_eq!(transfer.status, BridgeTransferStatus::Pending);
    }

    #[test]
    fn test_bridge_transfer_invalid_amount() {
        let result = BridgeTransfer::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            Chain::Ethereum,
            Chain::Polygon,
            BridgeProvider::LayerZero,
            Decimal::ZERO,
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_bridge_transfer_same_chain() {
        let result = BridgeTransfer::new(
            Uuid::new_v4(),
            Uuid::new_v4(),
            Chain::Ethereum,
            Chain::Ethereum,
            BridgeProvider::LayerZero,
            Decimal::from(100),
        );

        assert!(result.is_err());
    }

    #[test]
    fn test_multi_chain_token() {
        let mut token = MultiChainToken::new(Uuid::new_v4(), "TEST".to_string());
        token.add_chain(Chain::Ethereum, "0x123".to_string(), Decimal::from(1000));
        token.add_chain(Chain::Polygon, "0x456".to_string(), Decimal::from(2000));

        assert_eq!(token.total_supply(), Decimal::from(3000));
        assert_eq!(token.deployed_chains().len(), 2);
    }

    #[test]
    fn test_cross_chain_arbitrage_detection() {
        let mut prices = HashMap::new();
        prices.insert(Chain::Ethereum, Decimal::from(100));
        prices.insert(Chain::Polygon, Decimal::from(105));

        let arb = CrossChainArbitrage::detect(
            Uuid::new_v4(),
            prices,
            BridgeProvider::LayerZero,
            Decimal::from(10),
        );

        assert!(arb.is_some());
        let arb = arb.unwrap();
        assert_eq!(arb.buy_chain, Chain::Ethereum);
        assert_eq!(arb.sell_chain, Chain::Polygon);
    }

    #[test]
    fn test_multi_chain_portfolio() {
        let mut portfolio = MultiChainPortfolio::new(Uuid::new_v4());
        let token_id = Uuid::new_v4();

        portfolio.add_balance(Chain::Ethereum, token_id, Decimal::from(100));
        portfolio.add_balance(Chain::Polygon, token_id, Decimal::from(200));

        assert_eq!(portfolio.total_balance(token_id), Decimal::from(300));
        assert_eq!(
            portfolio.balance_on_chain(Chain::Ethereum, token_id),
            Decimal::from(100)
        );
        assert_eq!(portfolio.active_chains().len(), 2);
    }

    #[test]
    fn test_cross_chain_risk_metrics() {
        let mut portfolio = MultiChainPortfolio::new(Uuid::new_v4());
        let token_id = Uuid::new_v4();

        portfolio.add_balance(Chain::Ethereum, token_id, Decimal::from(100));
        portfolio.add_balance(Chain::Polygon, token_id, Decimal::from(50));

        let mut prices = HashMap::new();
        prices.insert((Chain::Ethereum, token_id), Decimal::from(10));
        prices.insert((Chain::Polygon, token_id), Decimal::from(10));

        let metrics = CrossChainRiskMetrics::calculate(&portfolio, &prices);
        assert!(metrics.bridge_risk_score > Decimal::ZERO);
        assert!(metrics.chain_diversification_score > Decimal::ZERO);
    }

    #[test]
    fn test_fee_optimizer() {
        let optimizer = ChainFeeOptimizer::optimize(Chain::Ethereum, TransactionUrgency::Normal);
        assert!(optimizer.recommended_gas_price <= optimizer.current_gas_price);
        assert!(optimizer.potential_savings() >= Decimal::ZERO);
    }
}