Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions contracts/predictify-hybrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod fees;
mod governance;
mod graceful_degradation;
mod market_analytics;
mod market_id_generator;
mod markets;
mod monitoring;
mod oracles;
Expand Down Expand Up @@ -69,6 +70,7 @@ use crate::config::{
};
use crate::events::EventEmitter;
use crate::graceful_degradation::{OracleBackup, OracleHealth};
use crate::market_id_generator::MarketIdGenerator;
use crate::reentrancy_guard::ReentrancyGuard;
use alloc::format;
use soroban_sdk::{
Expand Down Expand Up @@ -218,13 +220,8 @@ impl PredictifyHybrid {
panic_with_error!(env, Error::InvalidQuestion);
}

// Generate a unique market ID
let counter_key = Symbol::new(&env, "MarketCounter");
let counter: u32 = env.storage().persistent().get(&counter_key).unwrap_or(0);
let new_counter = counter + 1;
env.storage().persistent().set(&counter_key, &new_counter);

let market_id = Symbol::new(&env, &format!("market_{}", new_counter));
// Generate a unique collision-resistant market ID
let market_id = MarketIdGenerator::generate_market_id(&env, &admin);

// Calculate end time
let seconds_per_day: u64 = 24 * 60 * 60;
Expand Down Expand Up @@ -2099,7 +2096,9 @@ impl PredictifyHybrid {
function: String,
inputs: Vec<String>,
) -> Result<performance_benchmarks::BenchmarkResult, Error> {
performance_benchmarks::PerformanceBenchmarkManager::benchmark_gas_usage(&env, function, inputs)
performance_benchmarks::PerformanceBenchmarkManager::benchmark_gas_usage(
&env, function, inputs,
)
}

/// Benchmark storage usage for a specific operation
Expand Down Expand Up @@ -2145,7 +2144,9 @@ impl PredictifyHybrid {
env: Env,
operation: performance_benchmarks::StorageOperation,
) -> Result<performance_benchmarks::BenchmarkResult, Error> {
performance_benchmarks::PerformanceBenchmarkManager::benchmark_storage_usage(&env, operation)
performance_benchmarks::PerformanceBenchmarkManager::benchmark_storage_usage(
&env, operation,
)
}

/// Benchmark oracle call performance for a specific oracle provider
Expand Down Expand Up @@ -2187,7 +2188,9 @@ impl PredictifyHybrid {
env: Env,
oracle: OracleProvider,
) -> Result<performance_benchmarks::BenchmarkResult, Error> {
performance_benchmarks::PerformanceBenchmarkManager::benchmark_oracle_call_performance(&env, oracle)
performance_benchmarks::PerformanceBenchmarkManager::benchmark_oracle_call_performance(
&env, oracle,
)
}

/// Benchmark batch operations performance
Expand Down Expand Up @@ -2235,7 +2238,9 @@ impl PredictifyHybrid {
env: Env,
operations: Vec<performance_benchmarks::BatchOperation>,
) -> Result<performance_benchmarks::BenchmarkResult, Error> {
performance_benchmarks::PerformanceBenchmarkManager::benchmark_batch_operations(&env, operations)
performance_benchmarks::PerformanceBenchmarkManager::benchmark_batch_operations(
&env, operations,
)
}

/// Benchmark scalability with large markets and user counts
Expand Down Expand Up @@ -2277,7 +2282,11 @@ impl PredictifyHybrid {
market_size: u32,
user_count: u32,
) -> Result<performance_benchmarks::BenchmarkResult, Error> {
performance_benchmarks::PerformanceBenchmarkManager::benchmark_scalability(&env, market_size, user_count)
performance_benchmarks::PerformanceBenchmarkManager::benchmark_scalability(
&env,
market_size,
user_count,
)
}

/// Generate comprehensive performance report
Expand Down Expand Up @@ -2318,7 +2327,10 @@ impl PredictifyHybrid {
env: Env,
benchmark_suite: performance_benchmarks::PerformanceBenchmarkSuite,
) -> Result<performance_benchmarks::PerformanceReport, Error> {
performance_benchmarks::PerformanceBenchmarkManager::generate_performance_report(&env, benchmark_suite)
performance_benchmarks::PerformanceBenchmarkManager::generate_performance_report(
&env,
benchmark_suite,
)
}

/// Validate performance against thresholds
Expand Down Expand Up @@ -2360,7 +2372,9 @@ impl PredictifyHybrid {
metrics: performance_benchmarks::PerformanceMetrics,
thresholds: performance_benchmarks::PerformanceThresholds,
) -> Result<bool, Error> {
performance_benchmarks::PerformanceBenchmarkManager::validate_performance_thresholds(&env, metrics, thresholds)
performance_benchmarks::PerformanceBenchmarkManager::validate_performance_thresholds(
&env, metrics, thresholds,
)
}
}

Expand Down
36 changes: 23 additions & 13 deletions contracts/predictify-hybrid/src/market_analytics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(dead_code)]

use soroban_sdk::{contracttype, vec, Address, Env, Map, String, Symbol, Vec};
use crate::errors::Error;
use crate::types::*;
use soroban_sdk::{contracttype, vec, Address, Env, Map, String, Symbol, Vec};

/// Market Analytics module for comprehensive data analysis and insights
///
Expand Down Expand Up @@ -164,11 +164,11 @@ impl MarketAnalyticsManager {

for (user, outcome) in market.votes.iter() {
let stake = market.stakes.get(user.clone()).unwrap_or(0);

// Count votes per outcome
let vote_count = outcome_distribution.get(outcome.clone()).unwrap_or(0);
outcome_distribution.set(outcome.clone(), vote_count + 1);

// Sum stakes per outcome
let outcome_stake = total_stake_by_outcome.get(outcome.clone()).unwrap_or(0);
total_stake_by_outcome.set(outcome.clone(), outcome_stake + stake);
Expand Down Expand Up @@ -338,7 +338,11 @@ impl MarketAnalyticsManager {
.ok_or(Error::MarketNotFound)?;

let total_disputes = market.dispute_stakes.len() as u32;
let resolved_disputes = if market.state == MarketState::Resolved { total_disputes } else { 0 };
let resolved_disputes = if market.state == MarketState::Resolved {
total_disputes
} else {
0
};
let pending_disputes = total_disputes - resolved_disputes;
let dispute_stakes = market.total_dispute_stakes();

Expand Down Expand Up @@ -372,7 +376,10 @@ impl MarketAnalyticsManager {
}

/// Get participation metrics for a specific market
pub fn get_participation_metrics(env: &Env, market_id: Symbol) -> Result<ParticipationMetrics, Error> {
pub fn get_participation_metrics(
env: &Env,
market_id: Symbol,
) -> Result<ParticipationMetrics, Error> {
let market = env
.storage()
.persistent()
Expand Down Expand Up @@ -431,7 +438,7 @@ impl MarketAnalyticsManager {
if let Some(market) = env.storage().persistent().get::<Symbol, Market>(&market_id) {
let participants = market.votes.len() as u32;
let stake = market.total_staked;

total_participation += participants;
total_stake += stake;

Expand All @@ -441,7 +448,7 @@ impl MarketAnalyticsManager {

// Rank markets by participation
market_performance_ranking.set(market_id.clone(), participants);

// Categorize markets (simplified)
market_categories.set(market_id.clone(), String::from_str(env, "prediction"));
}
Expand All @@ -467,7 +474,10 @@ impl MarketAnalyticsManager {

let resolution_efficiency = 90; // Placeholder

comparative_metrics.set(String::from_str(env, "avg_participation"), average_participation as i128);
comparative_metrics.set(
String::from_str(env, "avg_participation"),
average_participation as i128,
);
comparative_metrics.set(String::from_str(env, "avg_stake"), average_stake);
comparative_metrics.set(String::from_str(env, "success_rate"), success_rate as i128);

Expand Down Expand Up @@ -507,13 +517,13 @@ impl MarketAnalyticsManager {
// Simplified volatility calculation
let total_stake = market.total_staked;
let average_stake = total_stake / market.votes.len() as i128;

let mut variance = 0;
for stake in stake_values.iter() {
let diff = stake - average_stake;
variance += diff * diff;
}

let volatility = (variance / market.votes.len() as i128) / 1000; // Scale down
volatility as u32
}
Expand Down Expand Up @@ -558,7 +568,7 @@ impl MarketAnalyticsManager {
} else {
String::from_str(&market.votes.env(), "manual")
}
},
}
_ => String::from_str(&market.votes.env(), "pending"),
}
}
Expand All @@ -571,7 +581,7 @@ impl MarketAnalyticsManager {
} else {
0
};

(participation + stake_ratio) / 2
}
}
}
Loading
Loading