The Predictify Hybrid contract features a sophisticated voting and dispute resolution system that combines community consensus with oracle-based resolution. This document outlines the architecture, components, and implementation details of the voting system.
The voting system consists of several key components:
- Voting Structures - Data structures for votes, statistics, and payouts
- Voting Manager - Core voting operations and state management
- Dispute System - Stake-based dispute resolution with dynamic thresholds
- Validation System - Input validation and business rule enforcement
- Analytics System - Voting statistics and market insights
- Utility Functions - Helper functions for common operations
pub struct Vote {
pub user: Address,
pub outcome: String,
pub stake: i128,
pub timestamp: u64,
}Purpose: Represents a user's vote on a prediction market outcome.
Fields:
user: The voter's addressoutcome: The chosen outcome (e.g., "yes", "no")stake: Amount staked in the vote (in stroops)timestamp: When the vote was cast
pub struct VotingStats {
pub total_votes: u32,
pub total_staked: i128,
pub outcome_distribution: Map<String, i128>,
pub unique_voters: u32,
}Purpose: Provides comprehensive analytics about voting activity.
Fields:
total_votes: Number of votes casttotal_staked: Total amount staked across all votesoutcome_distribution: Stake distribution by outcomeunique_voters: Number of unique participants
pub struct PayoutData {
pub user_stake: i128,
pub winning_total: i128,
pub total_pool: i128,
pub fee_percentage: i128,
pub payout_amount: i128,
}Purpose: Calculates user payouts based on voting results.
The system implements sophisticated dispute thresholds that adjust based on market characteristics:
pub struct DisputeThreshold {
pub market_id: Symbol,
pub base_threshold: i128,
pub adjusted_threshold: i128,
pub market_size_factor: i128,
pub activity_factor: i128,
pub complexity_factor: i128,
pub timestamp: u64,
}pub struct ThresholdAdjustmentFactors {
pub market_size_factor: i128,
pub activity_factor: i128,
pub complexity_factor: i128,
pub total_adjustment: i128,
}Adjustment Logic:
- Market Size: Larger markets require higher dispute thresholds
- Activity Level: High-activity markets may need lower thresholds
- Complexity: Complex markets (multiple outcomes) require higher thresholds
pub struct ThresholdHistoryEntry {
pub market_id: Symbol,
pub old_threshold: i128,
pub new_threshold: i128,
pub adjustment_reason: String,
pub adjusted_by: Address,
pub timestamp: u64,
}Purpose: Tracks all threshold adjustments for transparency and auditability.
The VotingManager provides the main interface for voting operations:
pub fn process_vote(
env: &Env,
user: Address,
market_id: Symbol,
outcome: String,
stake: i128,
) -> Result<(), Error>Functionality:
- Validates user authentication
- Checks market state and voting eligibility
- Validates outcome and stake amount
- Records vote and updates market statistics
- Transfers stake from user to contract
pub fn process_dispute(
env: &Env,
user: Address,
market_id: Symbol,
stake: i128,
) -> Result<(), Error>Functionality:
- Validates dispute eligibility
- Checks dispute threshold requirements
- Records dispute stake
- Extends market resolution period
- Triggers dispute resolution process
pub fn process_claim(
env: &Env,
user: Address,
market_id: Symbol,
) -> Result<i128, Error>Functionality:
- Validates market resolution
- Calculates user payout
- Transfers winnings to user
- Marks payout as claimed
- Updates market statistics
The VotingValidator ensures all voting operations comply with business rules:
pub fn validate_user_authentication(user: &Address) -> Result<(), Error>
pub fn validate_admin_authentication(env: &Env, admin: &Address) -> Result<(), Error>pub fn validate_market_for_voting(env: &Env, market: &Market) -> Result<(), Error>
pub fn validate_market_for_dispute(env: &Env, market: &Market) -> Result<(), Error>
pub fn validate_market_for_claim(env: &Env, market: &Market, user: &Address) -> Result<(), Error>pub fn validate_vote_parameters(
env: &Env,
outcome: &String,
valid_outcomes: &Vec<String>,
stake: i128,
) -> Result<(), Error>The ThresholdValidator manages dispute threshold rules:
pub fn validate_threshold_limits(threshold: i128) -> Result<(), Error>
pub fn validate_threshold_adjustment_permissions(env: &Env, admin: &Address) -> Result<(), Error>The VotingAnalytics provides insights into voting patterns:
pub fn calculate_participation_rate(market: &Market) -> f64
pub fn calculate_average_stake(market: &Market) -> i128pub fn calculate_stake_distribution(market: &Market) -> Map<String, i128>
pub fn calculate_voting_power_concentration(market: &Market) -> f64pub fn get_top_voters(market: &Market, limit: usize) -> Vec<(Address, i128)>pub fn transfer_stake(env: &Env, user: &Address, stake: i128) -> Result<(), Error>
pub fn transfer_winnings(env: &Env, user: &Address, amount: i128) -> Result<(), Error>
pub fn transfer_fees(env: &Env, admin: &Address, amount: i128) -> Result<(), Error>pub fn calculate_user_payout(env: &Env, market: &Market, user: &Address) -> Result<i128, Error>
pub fn calculate_fee_amount(market: &Market) -> Result<i128, Error>pub fn has_user_voted(market: &Market, user: &Address) -> bool
pub fn get_user_vote(market: &Market, user: &Address) -> Option<(String, i128)>
pub fn has_user_claimed(market: &Market, user: &Address) -> bool/// Minimum stake amount for voting (0.1 XLM)
pub const MIN_VOTE_STAKE: i128 = 100_000;
/// Minimum stake amount for disputes (10 XLM)
pub const MIN_DISPUTE_STAKE: i128 = 10_000_000;
/// Maximum dispute threshold (100 XLM)
pub const MAX_DISPUTE_THRESHOLD: i128 = 100_000_000;
/// Base dispute threshold (10 XLM)
pub const BASE_DISPUTE_THRESHOLD: i128 = 10_000_000;
/// Market size threshold for large markets (1000 XLM)
pub const LARGE_MARKET_THRESHOLD: i128 = 1_000_000_000;
/// Activity level threshold for high activity (100 votes)
pub const HIGH_ACTIVITY_THRESHOLD: u32 = 100;
/// Platform fee percentage (2%)
pub const FEE_PERCENTAGE: i128 = 2;
/// Dispute extension period in hours
pub const DISPUTE_EXTENSION_HOURS: u32 = 24;use predictify_hybrid::voting::{Vote, VotingManager};
// User votes "yes" with 0.5 XLM stake
VotingManager::process_vote(
&env,
user_address,
market_id,
String::from_str(&env, "yes"),
500_000, // 0.5 XLM
)?;// User disputes market resolution with 15 XLM
VotingManager::process_dispute(
&env,
user_address,
market_id,
15_000_000, // 15 XLM
)?;// User claims their winnings
let payout = VotingManager::process_claim(
&env,
user_address,
market_id,
)?;use predictify_hybrid::voting::VotingUtils;
let stats = VotingUtils::get_voting_stats(&market);
println!("Total votes: {}", stats.total_votes);
println!("Total staked: {} stroops", stats.total_staked);The voting system integrates with the market system through:
- Market state validation
- Outcome validation
- Stake management
- Resolution coordination
Voting results combine with oracle data for hybrid resolution:
- 70% oracle weight
- 30% community consensus weight
- Dispute resolution when oracle and community disagree
The voting system manages platform fees:
- 2% platform fee on all stakes
- Fee collection after market resolution
- Fee distribution to platform admin
The voting system includes comprehensive testing utilities:
pub mod testing {
pub fn create_test_vote(env: &Env, user: Address, outcome: String, stake: i128) -> Vote
pub fn create_test_voting_stats(env: &Env) -> VotingStats
pub fn create_test_payout_data() -> PayoutData
pub fn validate_vote_structure(vote: &Vote) -> Result<(), Error>
pub fn validate_voting_stats(stats: &VotingStats) -> Result<(), Error>
}The voting system includes comprehensive error handling for:
- Invalid market states
- Insufficient stakes
- Authentication failures
- Threshold violations
- Duplicate operations
- Invalid parameters
- Efficient data structures for vote storage
- Optimized threshold calculations
- Minimal storage operations
- Batch processing where possible
- Support for large numbers of voters
- Efficient dispute threshold calculations
- Optimized payout calculations
- Minimal on-chain computation
This voting system provides a robust foundation for prediction market operations with sophisticated dispute resolution and community consensus mechanisms.