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
/// Futarchy: Prediction Market Governance
///
/// Futarchy is a governance model where decisions are made based on prediction markets.
/// The idea: "Vote on values, but bet on beliefs."
///
/// # How it works
/// 1. A proposal is created with a measurable success metric
/// 2. Two conditional prediction markets are created:
///    - Market A: "What will the metric be IF the proposal passes?"
///    - Market B: "What will the metric be IF the proposal does NOT pass?"
/// 3. Traders bet on both markets based on their beliefs
/// 4. The proposal passes if Market A predicts a better outcome than Market B
/// 5. After a decision period, only the correct market settles
///
/// # Example
/// Proposal: "Reduce trading fees by 50%"
/// Metric: "Total 30-day trading volume"
/// - Market A traders bet on volume if fees are reduced
/// - Market B traders bet on volume if fees stay the same
/// - If Market A predicts higher volume, the proposal passes
use crate::error::{CoreError, Result};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Metric type for measuring proposal success
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SuccessMetric {
    /// Trading volume in BTC
    TradingVolume,
    /// Total value locked (TVL) in BTC
    TotalValueLocked,
    /// Number of active users
    ActiveUsers,
    /// Average transaction value
    AvgTransactionValue,
    /// Platform revenue
    PlatformRevenue,
    /// Token price
    TokenPrice,
}

/// Conditional market for a specific outcome
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConditionalMarket {
    /// Market ID
    pub market_id: String,
    /// Proposal this market is for
    pub proposal_id: String,
    /// Condition: true = proposal passes, false = proposal fails
    pub condition: bool,
    /// Current prediction (in metric units)
    pub predicted_value: Decimal,
    /// Total liquidity in the market
    pub liquidity: Decimal,
    /// Individual positions: user_id -> position
    pub positions: HashMap<String, MarketPosition>,
    /// Market resolved
    pub resolved: bool,
    /// Actual outcome (only set after resolution)
    pub actual_value: Option<Decimal>,
}

/// User position in a conditional market
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketPosition {
    /// User ID
    pub user_id: String,
    /// Amount staked
    pub stake: Decimal,
    /// Predicted value when position opened
    pub predicted_value: Decimal,
    /// Timestamp of position
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

/// Futarchy proposal with dual conditional markets
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FutarchyProposal {
    /// Proposal ID
    pub proposal_id: String,
    /// Title of the proposal
    pub title: String,
    /// Description
    pub description: String,
    /// Success metric to be predicted
    pub metric: SuccessMetric,
    /// Market if proposal passes (condition = true)
    pub pass_market: ConditionalMarket,
    /// Market if proposal does NOT pass (condition = false)
    pub fail_market: ConditionalMarket,
    /// Prediction period end time
    pub prediction_end: chrono::DateTime<chrono::Utc>,
    /// Measurement period end (when actual outcome is known)
    pub measurement_end: chrono::DateTime<chrono::Utc>,
    /// Proposal status
    pub status: FutarchyProposalStatus,
    /// Decision made (if status = Decided)
    pub decision: Option<bool>,
    /// Creator
    pub creator: String,
    /// Created at
    pub created_at: chrono::DateTime<chrono::Utc>,
}

/// Status of a futarchy proposal
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FutarchyProposalStatus {
    /// Markets are open for prediction
    PredictionPhase,
    /// Prediction phase ended, decision made
    Decided,
    /// Waiting for actual outcome to measure
    AwaitingOutcome,
    /// Resolved with actual outcome
    Resolved,
    /// Cancelled
    Cancelled,
}

/// Futarchy governance manager
pub struct FutarchyGovernor {
    /// Minimum liquidity required per market
    pub min_liquidity: Decimal,
    /// Minimum prediction period (hours)
    pub min_prediction_period: i64,
    /// Measurement period after decision (days)
    pub measurement_period_days: i64,
}

impl FutarchyGovernor {
    /// Create a new futarchy governor
    pub fn new(
        min_liquidity: Decimal,
        min_prediction_period: i64,
        measurement_period_days: i64,
    ) -> Self {
        Self {
            min_liquidity,
            min_prediction_period,
            measurement_period_days,
        }
    }

    /// Create a futarchy proposal with conditional markets
    pub fn create_proposal(
        &self,
        title: String,
        description: String,
        metric: SuccessMetric,
        creator: String,
        prediction_period_hours: i64,
    ) -> Result<FutarchyProposal> {
        if prediction_period_hours < self.min_prediction_period {
            return Err(CoreError::Validation(format!(
                "Prediction period must be at least {} hours",
                self.min_prediction_period
            )));
        }

        let now = chrono::Utc::now();
        let proposal_id = Uuid::new_v4().to_string();

        let pass_market = ConditionalMarket {
            market_id: format!("{}-pass", proposal_id),
            proposal_id: proposal_id.clone(),
            condition: true,
            predicted_value: Decimal::ZERO,
            liquidity: Decimal::ZERO,
            positions: HashMap::new(),
            resolved: false,
            actual_value: None,
        };

        let fail_market = ConditionalMarket {
            market_id: format!("{}-fail", proposal_id),
            proposal_id: proposal_id.clone(),
            condition: false,
            predicted_value: Decimal::ZERO,
            liquidity: Decimal::ZERO,
            positions: HashMap::new(),
            resolved: false,
            actual_value: None,
        };

        Ok(FutarchyProposal {
            proposal_id,
            title,
            description,
            metric,
            pass_market,
            fail_market,
            prediction_end: now + chrono::Duration::hours(prediction_period_hours),
            measurement_end: now
                + chrono::Duration::hours(prediction_period_hours)
                + chrono::Duration::days(self.measurement_period_days),
            status: FutarchyProposalStatus::PredictionPhase,
            decision: None,
            creator,
            created_at: now,
        })
    }

    /// Place a bet on a conditional market
    pub fn place_bet(
        &self,
        proposal: &mut FutarchyProposal,
        user_id: String,
        condition: bool,
        stake: Decimal,
        predicted_value: Decimal,
    ) -> Result<()> {
        // Check proposal status
        if proposal.status != FutarchyProposalStatus::PredictionPhase {
            return Err(CoreError::InvalidState(
                "Proposal is not in prediction phase".to_string(),
            ));
        }

        // Check if prediction period has ended
        if chrono::Utc::now() > proposal.prediction_end {
            return Err(CoreError::InvalidState(
                "Prediction period has ended".to_string(),
            ));
        }

        // Validate stake
        if stake <= Decimal::ZERO {
            return Err(CoreError::Validation("Stake must be positive".to_string()));
        }

        // Select the market
        let market = if condition {
            &mut proposal.pass_market
        } else {
            &mut proposal.fail_market
        };

        // Update market liquidity
        market.liquidity += stake;

        // Update predicted value (weighted average)
        let total_stake = market.positions.values().map(|p| p.stake).sum::<Decimal>() + stake;
        let weighted_sum = market
            .positions
            .values()
            .map(|p| p.stake * p.predicted_value)
            .sum::<Decimal>()
            + stake * predicted_value;
        market.predicted_value = weighted_sum / total_stake;

        // Add position
        let position = MarketPosition {
            user_id: user_id.clone(),
            stake,
            predicted_value,
            timestamp: chrono::Utc::now(),
        };

        market.positions.insert(user_id, position);

        Ok(())
    }

    /// Make decision based on prediction markets
    pub fn make_decision(&self, proposal: &mut FutarchyProposal) -> Result<bool> {
        // Check status
        if proposal.status != FutarchyProposalStatus::PredictionPhase {
            return Err(CoreError::InvalidState(
                "Proposal is not in prediction phase".to_string(),
            ));
        }

        // Check if prediction period has ended
        if chrono::Utc::now() < proposal.prediction_end {
            return Err(CoreError::InvalidState(
                "Prediction period has not ended yet".to_string(),
            ));
        }

        // Check minimum liquidity
        if proposal.pass_market.liquidity < self.min_liquidity
            || proposal.fail_market.liquidity < self.min_liquidity
        {
            proposal.status = FutarchyProposalStatus::Cancelled;
            return Err(CoreError::InvalidState(
                "Insufficient liquidity in markets".to_string(),
            ));
        }

        // Decision: proposal passes if pass_market predicts better outcome
        let decision = proposal.pass_market.predicted_value > proposal.fail_market.predicted_value;

        proposal.decision = Some(decision);
        proposal.status = FutarchyProposalStatus::Decided;

        Ok(decision)
    }

    /// Resolve markets after actual outcome is measured
    pub fn resolve_markets(
        &self,
        proposal: &mut FutarchyProposal,
        actual_value: Decimal,
    ) -> Result<()> {
        // Check status
        if proposal.status != FutarchyProposalStatus::Decided
            && proposal.status != FutarchyProposalStatus::AwaitingOutcome
        {
            return Err(CoreError::InvalidState(
                "Proposal must be decided before resolution".to_string(),
            ));
        }

        // Check if measurement period has ended
        if chrono::Utc::now() < proposal.measurement_end {
            proposal.status = FutarchyProposalStatus::AwaitingOutcome;
            return Err(CoreError::InvalidState(
                "Measurement period has not ended yet".to_string(),
            ));
        }

        // Resolve only the correct market (based on actual decision)
        let decision = proposal
            .decision
            .ok_or_else(|| CoreError::InvalidState("No decision made".to_string()))?;

        if decision {
            // Proposal passed, resolve pass_market
            proposal.pass_market.resolved = true;
            proposal.pass_market.actual_value = Some(actual_value);
        } else {
            // Proposal failed, resolve fail_market
            proposal.fail_market.resolved = true;
            proposal.fail_market.actual_value = Some(actual_value);
        }

        proposal.status = FutarchyProposalStatus::Resolved;

        Ok(())
    }

    /// Calculate payout for a user in a resolved market
    pub fn calculate_payout(&self, market: &ConditionalMarket, user_id: &str) -> Result<Decimal> {
        // Check if market is resolved
        if !market.resolved {
            return Err(CoreError::InvalidState(
                "Market not resolved yet".to_string(),
            ));
        }

        let actual_value = market
            .actual_value
            .ok_or_else(|| CoreError::InvalidState("Actual value not set".to_string()))?;

        // Get user position
        let position = market
            .positions
            .get(user_id)
            .ok_or_else(|| CoreError::NotFound("User has no position".to_string()))?;

        // Simple payout model: accuracy-weighted distribution
        // Users who predicted closer to actual value get more payout

        // Calculate prediction error for this user
        let user_error = (position.predicted_value - actual_value).abs();

        // Calculate total weighted stakes (inverse of errors)
        let mut total_weighted_stake = Decimal::ZERO;
        for pos in market.positions.values() {
            let error = (pos.predicted_value - actual_value).abs();
            let weight = if error == Decimal::ZERO {
                Decimal::from(1000) // High weight for exact predictions
            } else {
                Decimal::ONE / error
            };
            total_weighted_stake += pos.stake * weight;
        }

        // Calculate user's weight
        let user_weight = if user_error == Decimal::ZERO {
            Decimal::from(1000)
        } else {
            Decimal::ONE / user_error
        };

        let user_weighted_stake = position.stake * user_weight;

        // User's share of total liquidity
        let payout = (user_weighted_stake / total_weighted_stake) * market.liquidity;

        Ok(payout)
    }

    /// Get market statistics
    pub fn get_market_stats(&self, market: &ConditionalMarket) -> MarketStats {
        let num_participants = market.positions.len();
        let avg_prediction = if num_participants > 0 {
            market.predicted_value
        } else {
            Decimal::ZERO
        };

        // Calculate prediction variance
        let predictions: Vec<Decimal> = market
            .positions
            .values()
            .map(|p| p.predicted_value)
            .collect();
        let variance = if predictions.len() > 1 {
            let mean = avg_prediction;
            let sum_squared_diff: Decimal =
                predictions.iter().map(|&p| (p - mean) * (p - mean)).sum();
            sum_squared_diff / Decimal::from(predictions.len())
        } else {
            Decimal::ZERO
        };

        MarketStats {
            liquidity: market.liquidity,
            num_participants,
            predicted_value: market.predicted_value,
            prediction_variance: variance,
            resolved: market.resolved,
            actual_value: market.actual_value,
        }
    }
}

/// Market statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketStats {
    /// Total liquidity available in the prediction market.
    pub liquidity: Decimal,
    /// Number of distinct participants who have traded.
    pub num_participants: usize,
    /// Market-implied expected value of the outcome.
    pub predicted_value: Decimal,
    /// Variance of predictions across participants.
    pub prediction_variance: Decimal,
    /// Whether the market has been resolved.
    pub resolved: bool,
    /// Actual outcome value after resolution, if resolved.
    pub actual_value: Option<Decimal>,
}

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

    #[test]
    fn test_create_proposal() {
        let governor = FutarchyGovernor::new(
            Decimal::from(100), // min liquidity
            24,                 // min prediction period (hours)
            30,                 // measurement period (days)
        );

        let proposal = governor
            .create_proposal(
                "Reduce trading fees".to_string(),
                "Reduce trading fees by 50%".to_string(),
                SuccessMetric::TradingVolume,
                "creator123".to_string(),
                48, // 48 hours prediction period
            )
            .unwrap();

        assert_eq!(proposal.title, "Reduce trading fees");
        assert_eq!(proposal.status, FutarchyProposalStatus::PredictionPhase);
        assert!(proposal.pass_market.liquidity == Decimal::ZERO);
        assert!(proposal.fail_market.liquidity == Decimal::ZERO);
    }

    #[test]
    fn test_place_bet() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 24, 30);

        let mut proposal = governor
            .create_proposal(
                "Test proposal".to_string(),
                "Test".to_string(),
                SuccessMetric::TradingVolume,
                "creator".to_string(),
                48,
            )
            .unwrap();

        // Alice bets on pass market
        governor
            .place_bet(
                &mut proposal,
                "alice".to_string(),
                true, // pass condition
                Decimal::from(50),
                Decimal::from(1000), // predicts volume of 1000
            )
            .unwrap();

        assert_eq!(proposal.pass_market.liquidity, Decimal::from(50));
        assert_eq!(proposal.pass_market.predicted_value, Decimal::from(1000));

        // Bob bets on pass market with different prediction
        governor
            .place_bet(
                &mut proposal,
                "bob".to_string(),
                true,
                Decimal::from(50),
                Decimal::from(1200), // predicts volume of 1200
            )
            .unwrap();

        // Average: (50*1000 + 50*1200) / 100 = 1100
        assert_eq!(proposal.pass_market.liquidity, Decimal::from(100));
        assert_eq!(proposal.pass_market.predicted_value, Decimal::from(1100));
    }

    #[test]
    fn test_make_decision() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30); // 0 min period for testing

        let mut proposal = governor
            .create_proposal(
                "Test proposal".to_string(),
                "Test".to_string(),
                SuccessMetric::TradingVolume,
                "creator".to_string(),
                1, // 1 hour
            )
            .unwrap();

        // Set prediction end to past
        proposal.prediction_end = chrono::Utc::now() - chrono::Duration::hours(1);

        // Add liquidity to both markets
        proposal.pass_market.liquidity = Decimal::from(150);
        proposal.pass_market.predicted_value = Decimal::from(1200); // Higher prediction

        proposal.fail_market.liquidity = Decimal::from(150);
        proposal.fail_market.predicted_value = Decimal::from(1000); // Lower prediction

        let decision = governor.make_decision(&mut proposal).unwrap();

        assert!(decision); // Should pass because pass_market predicts better outcome
        assert_eq!(proposal.status, FutarchyProposalStatus::Decided);
        assert_eq!(proposal.decision, Some(true));
    }

    #[test]
    fn test_resolve_markets() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30);

        let mut proposal = governor
            .create_proposal(
                "Test proposal".to_string(),
                "Test".to_string(),
                SuccessMetric::TradingVolume,
                "creator".to_string(),
                1,
            )
            .unwrap();

        // Setup for resolution
        proposal.status = FutarchyProposalStatus::Decided;
        proposal.decision = Some(true); // Proposal passed
        proposal.measurement_end = chrono::Utc::now() - chrono::Duration::hours(1);

        governor
            .resolve_markets(&mut proposal, Decimal::from(1150))
            .unwrap();

        assert_eq!(proposal.status, FutarchyProposalStatus::Resolved);
        assert!(proposal.pass_market.resolved);
        assert_eq!(proposal.pass_market.actual_value, Some(Decimal::from(1150)));
        assert!(!proposal.fail_market.resolved); // Fail market not resolved (proposal passed)
    }

    #[test]
    fn test_calculate_payout() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30);

        let mut market = ConditionalMarket {
            market_id: "test".to_string(),
            proposal_id: "test".to_string(),
            condition: true,
            predicted_value: Decimal::from(1100),
            liquidity: Decimal::from(200),
            positions: HashMap::new(),
            resolved: true,
            actual_value: Some(Decimal::from(1150)), // Actual outcome
        };

        // Alice predicted 1200 (error: 50)
        market.positions.insert(
            "alice".to_string(),
            MarketPosition {
                user_id: "alice".to_string(),
                stake: Decimal::from(100),
                predicted_value: Decimal::from(1200),
                timestamp: chrono::Utc::now(),
            },
        );

        // Bob predicted 1100 (error: 50)
        market.positions.insert(
            "bob".to_string(),
            MarketPosition {
                user_id: "bob".to_string(),
                stake: Decimal::from(100),
                predicted_value: Decimal::from(1100),
                timestamp: chrono::Utc::now(),
            },
        );

        // Both have same error, should split evenly
        let alice_payout = governor.calculate_payout(&market, "alice").unwrap();
        let bob_payout = governor.calculate_payout(&market, "bob").unwrap();

        assert_eq!(alice_payout, Decimal::from(100));
        assert_eq!(bob_payout, Decimal::from(100));
        assert_eq!(alice_payout + bob_payout, market.liquidity);
    }

    #[test]
    fn test_market_stats() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30);

        let mut market = ConditionalMarket {
            market_id: "test".to_string(),
            proposal_id: "test".to_string(),
            condition: true,
            predicted_value: Decimal::from(1100),
            liquidity: Decimal::from(200),
            positions: HashMap::new(),
            resolved: false,
            actual_value: None,
        };

        market.positions.insert(
            "alice".to_string(),
            MarketPosition {
                user_id: "alice".to_string(),
                stake: Decimal::from(100),
                predicted_value: Decimal::from(1200),
                timestamp: chrono::Utc::now(),
            },
        );

        market.positions.insert(
            "bob".to_string(),
            MarketPosition {
                user_id: "bob".to_string(),
                stake: Decimal::from(100),
                predicted_value: Decimal::from(1000),
                timestamp: chrono::Utc::now(),
            },
        );

        let stats = governor.get_market_stats(&market);

        assert_eq!(stats.liquidity, Decimal::from(200));
        assert_eq!(stats.num_participants, 2);
        assert_eq!(stats.predicted_value, Decimal::from(1100));
        assert!(!stats.resolved);
        assert_eq!(stats.actual_value, None);
    }

    #[test]
    fn test_insufficient_liquidity() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30);

        let mut proposal = governor
            .create_proposal(
                "Test proposal".to_string(),
                "Test".to_string(),
                SuccessMetric::TradingVolume,
                "creator".to_string(),
                1,
            )
            .unwrap();

        proposal.prediction_end = chrono::Utc::now() - chrono::Duration::hours(1);

        // Insufficient liquidity
        proposal.pass_market.liquidity = Decimal::from(50); // Less than min 100
        proposal.fail_market.liquidity = Decimal::from(150);

        let result = governor.make_decision(&mut proposal);

        assert!(result.is_err());
        assert_eq!(proposal.status, FutarchyProposalStatus::Cancelled);
    }

    #[test]
    fn test_decision_based_on_better_prediction() {
        let governor = FutarchyGovernor::new(Decimal::from(100), 0, 30);

        let mut proposal = governor
            .create_proposal(
                "Test proposal".to_string(),
                "Test".to_string(),
                SuccessMetric::TradingVolume,
                "creator".to_string(),
                1,
            )
            .unwrap();

        proposal.prediction_end = chrono::Utc::now() - chrono::Duration::hours(1);

        // Fail market predicts better outcome
        proposal.pass_market.liquidity = Decimal::from(150);
        proposal.pass_market.predicted_value = Decimal::from(1000);

        proposal.fail_market.liquidity = Decimal::from(150);
        proposal.fail_market.predicted_value = Decimal::from(1500); // Higher is better

        let decision = governor.make_decision(&mut proposal).unwrap();

        assert!(!decision); // Should NOT pass because fail_market predicts better outcome
        assert_eq!(proposal.decision, Some(false));
    }
}