Skip to content

Commit c6a74a1

Browse files
Merge pull request #170 from ryzen-xp/Governance-System
[Feat] :: Created Governance System for Contract Parameter Changes an…
2 parents 8807a57 + 307a4df commit c6a74a1

13 files changed

Lines changed: 1405 additions & 695 deletions

contracts/predictify-hybrid/src/batch_operations.rs

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use crate::types::*;
1212
#[derive(Clone, Debug, PartialEq, Eq)]
1313
#[contracttype]
1414
pub enum BatchOperationType {
15-
Vote, // Batch vote operations
16-
Claim, // Batch claim operations
17-
CreateMarket, // Batch market creation
18-
OracleCall, // Batch oracle calls
19-
Dispute, // Batch dispute operations
20-
Extension, // Batch market extensions
21-
Resolution, // Batch market resolutions
22-
FeeCollection, // Batch fee collection
15+
Vote, // Batch vote operations
16+
Claim, // Batch claim operations
17+
CreateMarket, // Batch market creation
18+
OracleCall, // Batch oracle calls
19+
Dispute, // Batch dispute operations
20+
Extension, // Batch market extensions
21+
Resolution, // Batch market resolutions
22+
FeeCollection, // Batch fee collection
2323
}
2424

2525
#[derive(Clone, Debug)]
@@ -132,7 +132,7 @@ pub struct BatchProcessor;
132132

133133
impl BatchProcessor {
134134
// ===== STORAGE KEYS =====
135-
135+
136136
const BATCH_QUEUE_KEY: &'static str = "batch_operation_queue";
137137
const BATCH_STATS_KEY: &'static str = "batch_operation_statistics";
138138
const BATCH_CONFIG_KEY: &'static str = "batch_operation_config";
@@ -160,12 +160,18 @@ impl BatchProcessor {
160160
gas_efficiency_ratio: 1,
161161
};
162162

163-
env.storage().instance().set(&Symbol::new(env, Self::BATCH_CONFIG_KEY), &config);
164-
env.storage().instance().set(&Symbol::new(env, Self::BATCH_STATS_KEY), &stats);
165-
163+
env.storage()
164+
.instance()
165+
.set(&Symbol::new(env, Self::BATCH_CONFIG_KEY), &config);
166+
env.storage()
167+
.instance()
168+
.set(&Symbol::new(env, Self::BATCH_STATS_KEY), &stats);
169+
166170
// Initialize empty batch queue
167171
let queue: Vec<BatchOperation> = Vec::new(env);
168-
env.storage().instance().set(&Symbol::new(env, Self::BATCH_QUEUE_KEY), &queue);
172+
env.storage()
173+
.instance()
174+
.set(&Symbol::new(env, Self::BATCH_QUEUE_KEY), &queue);
169175

170176
Ok(())
171177
}
@@ -179,29 +185,28 @@ impl BatchProcessor {
179185
}
180186

181187
/// Update batch processor configuration
182-
pub fn update_config(
183-
env: &Env,
184-
admin: &Address,
185-
config: &BatchConfig,
186-
) -> Result<(), Error> {
188+
pub fn update_config(env: &Env, admin: &Address, config: &BatchConfig) -> Result<(), Error> {
187189
// Validate admin permissions
188-
crate::admin::AdminAccessControl::validate_admin_for_action(env, admin, "update_batch_config")?;
190+
crate::admin::AdminAccessControl::validate_admin_for_action(
191+
env,
192+
admin,
193+
"update_batch_config",
194+
)?;
189195

190196
// Validate configuration
191197
Self::validate_batch_config(config)?;
192198

193-
env.storage().instance().set(&Symbol::new(env, Self::BATCH_CONFIG_KEY), config);
199+
env.storage()
200+
.instance()
201+
.set(&Symbol::new(env, Self::BATCH_CONFIG_KEY), config);
194202

195203
Ok(())
196204
}
197205

198206
// ===== BATCH VOTE OPERATIONS =====
199207

200208
/// Process batch vote operations
201-
pub fn batch_vote(
202-
env: &Env,
203-
votes: &Vec<VoteData>,
204-
) -> Result<BatchResult, Error> {
209+
pub fn batch_vote(env: &Env, votes: &Vec<VoteData>) -> Result<BatchResult, Error> {
205210
let config = Self::get_config(env)?;
206211
let start_time = env.ledger().timestamp();
207212
let mut successful_operations = 0;
@@ -255,7 +260,7 @@ impl BatchProcessor {
255260

256261
// Check if market exists and is open
257262
let market = crate::markets::MarketStateManager::get_market(env, &vote_data.market_id)?;
258-
263+
259264
if market.end_time <= env.ledger().timestamp() {
260265
return Err(Error::MarketClosed);
261266
}
@@ -275,10 +280,7 @@ impl BatchProcessor {
275280
// ===== BATCH CLAIM OPERATIONS =====
276281

277282
/// Process batch claim operations
278-
pub fn batch_claim(
279-
env: &Env,
280-
claims: &Vec<ClaimData>,
281-
) -> Result<BatchResult, Error> {
283+
pub fn batch_claim(env: &Env, claims: &Vec<ClaimData>) -> Result<BatchResult, Error> {
282284
let config = Self::get_config(env)?;
283285
let start_time = env.ledger().timestamp();
284286
let mut successful_operations = 0;
@@ -332,7 +334,7 @@ impl BatchProcessor {
332334

333335
// Check if market exists and is resolved
334336
let market = crate::markets::MarketStateManager::get_market(env, &claim_data.market_id)?;
335-
337+
336338
if !market.is_resolved() {
337339
return Err(Error::MarketNotResolved);
338340
}
@@ -356,7 +358,11 @@ impl BatchProcessor {
356358
markets: &Vec<MarketData>,
357359
) -> Result<BatchResult, Error> {
358360
// Validate admin permissions
359-
crate::admin::AdminAccessControl::validate_admin_for_action(env, admin, "batch_create_markets")?;
361+
crate::admin::AdminAccessControl::validate_admin_for_action(
362+
env,
363+
admin,
364+
"batch_create_markets",
365+
)?;
360366

361367
let config = Self::get_config(env)?;
362368
let start_time = env.ledger().timestamp();
@@ -429,10 +435,7 @@ impl BatchProcessor {
429435
// ===== BATCH ORACLE CALLS =====
430436

431437
/// Process batch oracle calls
432-
pub fn batch_oracle_calls(
433-
env: &Env,
434-
feeds: &Vec<OracleFeed>,
435-
) -> Result<BatchResult, Error> {
438+
pub fn batch_oracle_calls(env: &Env, feeds: &Vec<OracleFeed>) -> Result<BatchResult, Error> {
436439
let config = Self::get_config(env)?;
437440
let start_time = env.ledger().timestamp();
438441
let mut successful_operations = 0;
@@ -486,7 +489,7 @@ impl BatchProcessor {
486489

487490
// Check if market exists
488491
let market = crate::markets::MarketStateManager::get_market(env, &feed_data.market_id)?;
489-
492+
490493
if market.is_resolved() {
491494
return Err(Error::MarketAlreadyResolved);
492495
}
@@ -507,9 +510,7 @@ impl BatchProcessor {
507510
// ===== BATCH OPERATION VALIDATION =====
508511

509512
/// Validate batch operations
510-
pub fn validate_batch_operations(
511-
operations: &Vec<BatchOperation>,
512-
) -> Result<(), Error> {
513+
pub fn validate_batch_operations(operations: &Vec<BatchOperation>) -> Result<(), Error> {
513514
if operations.is_empty() {
514515
return Err(Error::InvalidInput);
515516
}
@@ -593,26 +594,28 @@ impl BatchProcessor {
593594
BatchOperationType::FeeCollection => "fee_collection",
594595
};
595596

596-
let current_count = error_counts.get(String::from_str(env, error_type)).unwrap_or(0);
597+
let current_count = error_counts
598+
.get(String::from_str(env, error_type))
599+
.unwrap_or(0);
597600
error_counts.set(String::from_str(env, error_type), current_count + 1);
598601
}
599602

600603
// Create error summary
601604
error_summary.set(
602605
String::from_str(env, "total_errors"),
603-
String::from_str(env, &errors.len().to_string())
606+
String::from_str(env, &errors.len().to_string()),
604607
);
605608

606609
error_summary.set(
607610
String::from_str(env, "error_types"),
608-
String::from_str(env, "See error_counts for breakdown")
611+
String::from_str(env, "See error_counts for breakdown"),
609612
);
610613

611614
// Add error counts
612615
for (error_type, count) in error_counts.iter() {
613616
error_summary.set(
614617
String::from_str(env, &format!("{:?}_errors", error_type)),
615-
String::from_str(env, &count.to_string())
618+
String::from_str(env, &count.to_string()),
616619
);
617620
}
618621

@@ -640,12 +643,16 @@ impl BatchProcessor {
640643

641644
// Update average batch size
642645
if stats.total_batches_processed > 0 {
643-
stats.average_batch_size = stats.total_operations_processed / stats.total_batches_processed;
646+
stats.average_batch_size =
647+
stats.total_operations_processed / stats.total_batches_processed;
644648
}
645649

646650
// Update average execution time
647651
if stats.total_batches_processed > 0 {
648-
let total_time = stats.average_execution_time * (stats.total_batches_processed - 1) as u64 + result.execution_time;
652+
653+
let total_time = stats.average_execution_time
654+
* (stats.total_batches_processed - 1) as u64
655+
+ result.execution_time;
649656
stats.average_execution_time = total_time / stats.total_batches_processed as u64;
650657
}
651658

@@ -655,7 +662,9 @@ impl BatchProcessor {
655662
stats.gas_efficiency_ratio = (success_rate * 100.0) as u64;
656663
}
657664

658-
env.storage().instance().set(&Symbol::new(env, Self::BATCH_STATS_KEY), &stats);
665+
env.storage()
666+
.instance()
667+
.set(&Symbol::new(env, Self::BATCH_STATS_KEY), &stats);
659668

660669
Ok(())
661670
}
@@ -767,7 +776,7 @@ impl BatchUtils {
767776
operation_type: &BatchOperationType,
768777
) -> Result<u32, Error> {
769778
let config = BatchProcessor::get_config(env)?;
770-
779+
771780
match operation_type {
772781
BatchOperationType::Vote => Ok(config.max_batch_size.min(20)),
773782
BatchOperationType::Claim => Ok(config.max_batch_size.min(15)),
@@ -792,15 +801,12 @@ impl BatchUtils {
792801

793802
let success_rate = successful_operations as f64 / total_operations as f64;
794803
let operations_per_gas = total_operations as f64 / gas_used as f64;
795-
804+
796805
success_rate * operations_per_gas
797806
}
798807

799808
/// Estimate gas cost for batch operation
800-
pub fn estimate_gas_cost(
801-
operation_type: &BatchOperationType,
802-
operation_count: u32,
803-
) -> u64 {
809+
pub fn estimate_gas_cost(operation_type: &BatchOperationType, operation_count: u32) -> u64 {
804810
let base_cost = match operation_type {
805811
BatchOperationType::Vote => 1000,
806812
BatchOperationType::Claim => 1500,
@@ -826,7 +832,10 @@ impl BatchTesting {
826832
pub fn create_test_vote_data(env: &Env, market_id: &Symbol) -> VoteData {
827833
VoteData {
828834
market_id: market_id.clone(),
829-
voter: Address::from_string(&String::from_str(env, "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF")),
835+
voter: Address::from_string(&String::from_str(
836+
env,
837+
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF",
838+
)),
830839
outcome: String::from_str(env, "Yes"),
831840
stake_amount: 1_000_000_000, // 100 XLM
832841
}
@@ -836,7 +845,10 @@ impl BatchTesting {
836845
pub fn create_test_claim_data(env: &Env, market_id: &Symbol) -> ClaimData {
837846
ClaimData {
838847
market_id: market_id.clone(),
839-
claimant: Address::from_string(&String::from_str(env, "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF")),
848+
claimant: Address::from_string(&String::from_str(
849+
env,
850+
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF",
851+
)),
840852
expected_amount: 2_000_000_000, // 200 XLM
841853
}
842854
}
@@ -848,7 +860,7 @@ impl BatchTesting {
848860
outcomes: vec![
849861
&env,
850862
String::from_str(env, "Yes"),
851-
String::from_str(env, "No")
863+
String::from_str(env, "No"),
852864
],
853865
duration_days: 30,
854866
oracle_config: crate::types::OracleConfig {
@@ -910,4 +922,4 @@ impl BatchTesting {
910922
execution_time,
911923
})
912924
}
913-
}
925+
}

0 commit comments

Comments
 (0)