forked from Predictify-org/predictify-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.rs
More file actions
904 lines (830 loc) · 33.1 KB
/
Copy pathextensions.rs
File metadata and controls
904 lines (830 loc) · 33.1 KB
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
use soroban_sdk::{contracttype, symbol_short, vec, Address, Env, String, Symbol, Vec};
use crate::err::Error;
use crate::types::*;
/// Market extension management system for Predictify Hybrid contract
///
/// This module provides comprehensive market extension functionality:
/// - Extension validation and approval mechanisms
/// - Extension limits and fee handling
/// - Extension events and logging
/// - Extension history tracking
/// - Extension analytics and reporting
/// - Per-market cumulative extension cap with audit event on cap hit
// ===== EXTENSION CONSTANTS =====
// Note: These constants are now managed by the config module
// Use ConfigManager::get_extension_config() to get current values
const MAX_EXTENSION_DAYS: u32 = crate::config::MAX_EXTENSION_DAYS;
const MIN_EXTENSION_DAYS: u32 = crate::config::MIN_EXTENSION_DAYS;
const EXTENSION_FEE_PER_DAY: i128 = crate::config::EXTENSION_FEE_PER_DAY; // 1 XLM per day in stroops
const MAX_TOTAL_EXTENSIONS: u32 = crate::config::MAX_TOTAL_EXTENSIONS;
/// Storage key for the admin-configurable cumulative extension cap (in days).
/// Stored under `Symbol::new(env, "cum_ext_cap")` in persistent storage.
/// Defaults to 0 (no cap) when absent.
const CUMULATIVE_CAP_KEY: &str = "cum_ext_cap";
// ===== EXTENSION MANAGEMENT =====
/// Comprehensive market extension management system for Predictify Hybrid contracts.
///
/// The ExtensionManager provides a complete solution for extending market durations
/// beyond their original end times. This system includes validation, fee handling,
/// permission checks, and comprehensive tracking of extension history.
///
/// # Core Functionality
///
/// - **Duration Extension**: Safely extend market end times with validation
/// - **Fee Management**: Calculate and handle extension fees automatically
/// - **Permission Control**: Ensure only authorized admins can extend markets
/// - **History Tracking**: Maintain complete extension history for transparency
/// - **Limit Enforcement**: Prevent excessive extensions through configurable limits
///
/// # Extension Process
///
/// 1. **Validation**: Check market state and extension parameters
/// 2. **Authorization**: Verify admin permissions for the market
/// 3. **Fee Calculation**: Determine extension costs based on duration
/// 4. **Market Update**: Modify market end time and record extension
/// 5. **Event Emission**: Broadcast extension event for transparency
///
/// # Economic Model
///
/// Extensions require fees to prevent abuse:
/// - **Per-Day Pricing**: Fixed fee per additional day (configurable)
/// - **Economic Barrier**: Prevents frivolous extension requests
/// - **Revenue Generation**: Fees support platform sustainability
/// - **Fair Access**: Reasonable pricing ensures legitimate use cases
///
/// # Use Cases
///
/// - **Market Adjustment**: Extend markets when circumstances change
/// - **Data Availability**: Allow more time for oracle data collection
/// - **Community Request**: Respond to user demand for longer markets
/// - **Technical Issues**: Compensate for system downtime or problems
///
/// # Example Usage
///
/// ```rust
/// # use soroban_sdk::{Env, Address, Symbol, String};
/// # use predictify_hybrid::extensions::ExtensionManager;
/// # let env = Env::default();
/// # let admin = Address::generate(&env);
/// # let market_id = Symbol::new(&env, "btc_market");
///
/// // Extend market by 7 days
/// let result = ExtensionManager::extend_market_duration(
/// &env,
/// admin.clone(),
/// market_id.clone(),
/// 7, // Additional days
/// String::from_str(&env, "Extended due to high community interest")
/// );
///
/// match result {
/// Ok(()) => println!("Market successfully extended by 7 days"),
/// Err(e) => println!("Extension failed: {:?}", e),
/// }
///
/// // Check extension history
/// let history = ExtensionManager::get_market_extension_history(
/// &env,
/// market_id.clone()
/// ).unwrap();
///
/// println!("Total extensions: {}", history.len());
/// ```
pub struct ExtensionManager;
impl ExtensionManager {
/// Extends a market's duration by the specified number of additional days.
///
/// This function performs comprehensive validation, fee handling, and market
/// updates to safely extend market durations. It includes permission checks,
/// limit enforcement, and complete audit trail maintenance.
///
/// # Parameters
///
/// * `env` - The Soroban environment for blockchain operations
/// * `admin` - Address of the administrator requesting the extension (must authenticate)
/// * `market_id` - Unique identifier of the market to extend
/// * `additional_days` - Number of days to add to the market duration (1-30)
/// * `reason` - Human-readable explanation for the extension request
///
/// # Returns
///
/// Returns `Ok(())` if the extension is successful, or an `Error` if:
/// - Admin lacks permission to extend this market
/// - Extension days are outside allowed range (1-30)
/// - Market has reached maximum total extension limit
/// - Market is not in a state that allows extension
/// - Fee payment fails
///
/// # Example
///
/// ```rust
/// # use soroban_sdk::{Env, Address, Symbol, String};
/// # use predictify_hybrid::extensions::ExtensionManager;
/// # let env = Env::default();
/// # let admin = Address::generate(&env);
/// # let market_id = Symbol::new(&env, "crypto_prediction");
///
/// // Extend market for additional data collection
/// let result = ExtensionManager::extend_market_duration(
/// &env,
/// admin.clone(),
/// market_id.clone(),
/// 14, // Two weeks extension
/// String::from_str(&env, "Oracle data delayed, need more time for accurate resolution")
/// );
///
/// match result {
/// Ok(()) => {
/// println!("Market extended successfully");
/// // Extension fee automatically deducted
/// // Market end time updated
/// // Extension event emitted
/// },
/// Err(e) => println!("Extension failed: {:?}", e),
/// }
/// ```
///
/// # Extension Process
///
/// 1. **Validation Phase**:
/// - Check market exists and is extendable
/// - Validate extension days within limits (1-30)
/// - Verify admin has permission for this market
///
/// 2. **Economic Phase**:
/// - Calculate extension fee (days × fee_per_day)
/// - Process fee payment from admin account
/// - Record fee in extension history
///
/// 3. **Update Phase**:
/// - Extend market end time by specified days
/// - Add extension record to market history
/// - Update total extension counters
///
/// 4. **Event Phase**:
/// - Emit extension event for transparency
/// - Log extension details for audit trail
/// - Update market state in storage
///
/// # Fee Structure
///
/// Extension fees are calculated as:
/// - **Base Rate**: 1 XLM per day (configurable)
/// - **Total Cost**: `additional_days × fee_per_day`
/// - **Payment**: Automatically deducted from admin account
/// - **Refund Policy**: No refunds for completed extensions
///
/// # Security Considerations
///
/// - **Authentication**: Admin must sign the transaction
/// - **Authorization**: Only market admin can extend their markets
/// - **Rate Limiting**: Maximum extensions per market enforced
/// - **Economic Barriers**: Fees prevent spam extensions
pub fn extend_market_duration(
env: &Env,
admin: Address,
market_id: Symbol,
additional_days: u32,
reason: String,
) -> Result<(), Error> {
// Validate extension conditions
ExtensionValidator::validate_extension_conditions(env, &market_id, additional_days)?;
// Check per-call extension limits
ExtensionValidator::check_extension_limits(env, &market_id, additional_days)?;
// Check cumulative extension cap for this market
ExtensionValidator::check_cumulative_cap(env, &market_id, additional_days)?;
// Verify admin permissions
ExtensionValidator::can_extend_market(env, &market_id, &admin)?;
// Handle extension fees
let fee_amount = ExtensionUtils::handle_extension_fees(env, &market_id, additional_days)?;
// Get and update market
let mut market = MarketStateManager::get_market(env, &market_id)?;
// Create extension record
let extension =
MarketExtension::new(env, additional_days, admin.clone(), reason, fee_amount);
// Update market
market.end_time += (additional_days as u64) * 24 * 60 * 60; // Convert days to seconds
market.total_extension_days += additional_days;
market.extension_history.push_back(extension);
// Store updated market
MarketStateManager::update_market(env, &market_id, &market);
// Update the cumulative extension total for this market
ExtensionUtils::increment_extension_total(env, &market_id, additional_days);
// Emit extension event
ExtensionUtils::emit_extension_event(env, &market_id, additional_days, &admin);
Ok(())
}
/// Retrieves the complete extension history for a specific market.
///
/// This function returns a chronological list of all extensions that have been
/// applied to a market, providing complete transparency and audit capabilities
/// for market duration modifications.
///
/// # Parameters
///
/// * `env` - The Soroban environment for blockchain operations
/// * `market_id` - Unique identifier of the market to query
///
/// # Returns
///
/// Returns a `Vec<MarketExtension>` containing all extension records,
/// or an `Error` if the market is not found.
///
/// # Example
///
/// ```rust
/// # use soroban_sdk::{Env, Symbol};
/// # use predictify_hybrid::extensions::ExtensionManager;
/// # let env = Env::default();
/// # let market_id = Symbol::new(&env, "btc_market");
///
/// // Get complete extension history
/// let history = ExtensionManager::get_market_extension_history(
/// &env,
/// market_id.clone()
/// ).unwrap();
///
/// // Analyze extension patterns
/// println!("Total extensions: {}", history.len());
///
/// let mut total_days = 0u32;
/// let mut total_fees = 0i128;
///
/// for extension in history.iter() {
/// total_days += extension.additional_days;
/// total_fees += extension.fee_amount;
///
/// println!("Extension: {} days by {} (fee: {} XLM)",
/// extension.additional_days,
/// extension.admin.to_string(),
/// extension.fee_amount / 10_000_000);
///
/// if let Some(reason) = &extension.reason {
/// println!("Reason: {}", reason.to_string());
/// }
/// }
///
/// println!("Total extended days: {}", total_days);
/// println!("Total fees paid: {} XLM", total_fees / 10_000_000);
/// ```
///
/// # Extension Record Contents
///
/// Each extension record includes:
/// - **Additional Days**: Number of days added in this extension
/// - **Admin Address**: Who requested the extension
/// - **Reason**: Optional explanation for the extension
/// - **Fee Amount**: Cost paid for this extension
/// - **Timestamp**: When the extension was applied
///
/// # Use Cases
///
/// - **Audit Trails**: Complete history for compliance and verification
/// - **Analytics**: Analyze extension patterns and market behavior
/// - **Transparency**: Public record of all market modifications
/// - **Fee Tracking**: Monitor extension costs and revenue
/// - **Pattern Analysis**: Identify markets requiring frequent extensions
pub fn get_market_extension_history(
env: &Env,
market_id: Symbol,
) -> Result<Vec<MarketExtension>, Error> {
let market = MarketStateManager::get_market(env, &market_id)?;
Ok(market.extension_history)
}
/// Retrieves comprehensive extension statistics and capabilities for a market.
///
/// This function provides a complete overview of a market's extension status,
/// including historical data, current limits, and future extension capabilities.
/// Essential for UI display and extension planning.
///
/// # Parameters
///
/// * `env` - The Soroban environment for blockchain operations
/// * `market_id` - Unique identifier of the market to analyze
///
/// # Returns
///
/// Returns an `ExtensionStats` struct containing comprehensive statistics,
/// or an `Error` if the market is not found.
///
/// # Example
///
/// ```rust
/// # use soroban_sdk::{Env, Symbol};
/// # use predictify_hybrid::extensions::ExtensionManager;
/// # let env = Env::default();
/// # let market_id = Symbol::new(&env, "crypto_market");
///
/// // Get extension statistics
/// let stats = ExtensionManager::get_extension_stats(
/// &env,
/// market_id.clone()
/// ).unwrap();
///
/// // Display extension status
/// println!("Extension Statistics for Market: {}", market_id.to_string());
/// println!("─────────────────────────────────────────");
/// println!("Total extensions applied: {}", stats.total_extensions);
/// println!("Total days extended: {}", stats.total_extension_days);
/// println!("Maximum extension days: {}", stats.max_extension_days);
/// println!("Can extend further: {}", stats.can_extend);
/// println!("Extension fee per day: {} XLM",
/// stats.extension_fee_per_day / 10_000_000);
///
/// // Calculate remaining extension capacity
/// let remaining_days = stats.max_extension_days - stats.total_extension_days;
/// println!("Remaining extension capacity: {} days", remaining_days);
///
/// // Estimate cost for maximum extension
/// if stats.can_extend && remaining_days > 0 {
/// let max_cost = (remaining_days as i128) * stats.extension_fee_per_day;
/// println!("Cost to use remaining capacity: {} XLM",
/// max_cost / 10_000_000);
/// }
/// ```
///
/// # Statistics Breakdown
///
/// The `ExtensionStats` struct provides:
/// - **total_extensions**: Number of extension operations performed
/// - **total_extension_days**: Cumulative days added to market
/// - **max_extension_days**: Maximum total days that can be extended
/// - **can_extend**: Whether market is currently extendable
/// - **extension_fee_per_day**: Current cost per day of extension
///
/// # Extension Capacity Analysis
///
/// Statistics enable capacity planning:
/// - **Remaining Capacity**: `max_extension_days - total_extension_days`
/// - **Extension Availability**: Based on market state and limits
/// - **Cost Estimation**: Calculate fees for planned extensions
/// - **Limit Monitoring**: Track approach to maximum extensions
///
/// # Integration Applications
///
/// - **UI Display**: Show extension status and capabilities to users
/// - **Planning Tools**: Help admins plan future extensions
/// - **Cost Estimation**: Calculate extension costs before commitment
/// - **Limit Monitoring**: Alert when approaching extension limits
/// - **Analytics**: Track extension usage patterns across markets
pub fn get_extension_stats(
env: &Env,
market_id: Symbol,
) -> Result<crate::types::ExtensionStats, Error> {
let market = MarketStateManager::get_market(env, &market_id)?;
Ok(crate::types::ExtensionStats {
total_extensions: market.extension_history.len().try_into().unwrap_or(0),
total_extension_days: market.total_extension_days,
max_extension_days: market.max_extension_days,
can_extend: ExtensionValidator::can_extend_market(env, &market_id, &market.admin)
.is_ok(),
extension_fee_per_day: EXTENSION_FEE_PER_DAY,
})
}
/// Checks whether a specific market can be extended by a given administrator.
///
/// This function performs comprehensive validation to determine if a market
/// extension is possible, considering market state, admin permissions,
/// extension limits, and current system configuration.
///
/// # Parameters
///
/// * `env` - The Soroban environment for blockchain operations
/// * `market_id` - Unique identifier of the market to check
/// * `admin` - Address of the administrator requesting extension capability check
///
/// # Returns
///
/// Returns `true` if the market can be extended by this admin, `false` if not,
/// or an `Error` if validation fails due to system issues.
///
/// # Example
///
/// ```rust
/// # use soroban_sdk::{Env, Address, Symbol};
/// # use predictify_hybrid::extensions::ExtensionManager;
/// # let env = Env::default();
/// # let admin = Address::generate(&env);
/// # let market_id = Symbol::new(&env, "prediction_market");
///
/// // Check extension eligibility
/// let can_extend = ExtensionManager::can_extend_market(
/// &env,
/// market_id.clone(),
/// admin.clone()
/// ).unwrap();
///
/// if can_extend {
/// println!("Market can be extended by this admin");
///
/// // Show extension options
/// let stats = ExtensionManager::get_extension_stats(&env, market_id.clone()).unwrap();
/// let remaining = stats.max_extension_days - stats.total_extension_days;
///
/// println!("Remaining extension capacity: {} days", remaining);
/// println!("Cost per day: {} XLM", stats.extension_fee_per_day / 10_000_000);
/// } else {
/// println!("Market cannot be extended:");
/// println!("- Check admin permissions");
/// println!("- Verify market state");
/// println!("- Check extension limits");
/// }
/// ```
///
/// # Validation Criteria
///
/// Extension eligibility requires:
/// - **Market Exists**: Market must be found in storage
/// - **Admin Permission**: Admin must be authorized for this market
/// - **Market State**: Market must be in extendable state
/// - **Extension Limits**: Must not exceed maximum total extensions
/// - **System Status**: Extension system must be operational
///
/// # Common Failure Reasons
///
/// Extensions may be blocked due to:
/// - **Unauthorized Admin**: Admin lacks permission for this market
/// - **Market Finalized**: Market has already been resolved
/// - **Limit Reached**: Maximum extension days already used
/// - **System Maintenance**: Extension system temporarily disabled
/// - **Invalid State**: Market in non-extendable state
///
/// # Use Cases
///
/// - **UI State Management**: Enable/disable extension buttons
/// - **Permission Checking**: Validate admin capabilities
/// - **Pre-validation**: Check before expensive extension operations
/// - **User Feedback**: Provide clear extension availability status
/// - **Access Control**: Enforce extension permission policies
pub fn can_extend_market(env: &Env, market_id: Symbol, admin: Address) -> Result<bool, Error> {
ExtensionValidator::can_extend_market(env, &market_id, &admin)?;
Ok(true)
}
/// Calculates the total fee required for extending a market by specified days.
///
/// This function provides transparent fee calculation based on the current
/// per-day extension rate. The calculation is straightforward: days multiplied
/// by the daily rate, with no hidden fees or complex pricing structures.
///
/// # Parameters
///
/// * `additional_days` - Number of days to extend the market (1-30)
///
/// # Returns
///
/// Returns the total extension fee in stroops (1 XLM = 10,000,000 stroops).
///
/// # Example
///
/// ```rust
/// # use predictify_hybrid::extensions::ExtensionManager;
///
/// // Calculate fees for different extension periods
/// let one_day_fee = ExtensionManager::calculate_extension_fee(1);
/// let one_week_fee = ExtensionManager::calculate_extension_fee(7);
/// let one_month_fee = ExtensionManager::calculate_extension_fee(30);
///
/// println!("Extension Fee Calculator");
/// println!("─────────────────────────");
/// println!("1 day: {} XLM", one_day_fee / 10_000_000);
/// println!("1 week: {} XLM", one_week_fee / 10_000_000);
/// println!("1 month: {} XLM", one_month_fee / 10_000_000);
///
/// // Calculate cost per day
/// let daily_rate = one_day_fee;
/// println!("Daily rate: {} XLM", daily_rate / 10_000_000);
///
/// // Verify linear pricing
/// assert_eq!(one_week_fee, daily_rate * 7);
/// assert_eq!(one_month_fee, daily_rate * 30);
///
/// // Budget planning example
/// let budget_xlm = 50; // 50 XLM budget
/// let budget_stroops = budget_xlm * 10_000_000;
/// let max_days = budget_stroops / daily_rate;
/// println!("With {} XLM budget, can extend up to {} days",
/// budget_xlm, max_days);
/// ```
///
/// # Fee Structure
///
/// Extension fees follow a simple linear model:
/// - **Base Rate**: 1 XLM per day (10,000,000 stroops)
/// - **Linear Scaling**: Total = days × daily_rate
/// - **No Discounts**: Same rate regardless of duration
/// - **No Hidden Fees**: Transparent, predictable pricing
///
/// # Economic Rationale
///
/// The fee structure serves multiple purposes:
/// - **Spam Prevention**: Economic barrier prevents frivolous extensions
/// - **Resource Allocation**: Fees reflect computational and storage costs
/// - **Platform Sustainability**: Revenue supports ongoing operations
/// - **Fair Pricing**: Reasonable rates ensure accessibility
///
/// # Budget Planning
///
/// Use this function for:
/// - **Cost Estimation**: Calculate extension costs before commitment
/// - **Budget Planning**: Determine maximum extension days within budget
/// - **Fee Transparency**: Show users exact costs upfront
/// - **Economic Analysis**: Compare extension costs across markets
///
/// # Integration Notes
///
/// - **UI Display**: Show calculated fees in user interfaces
/// - **Validation**: Verify user has sufficient balance before extension
/// - **Analytics**: Track fee revenue and extension economics
/// - **Planning**: Help users optimize extension timing and duration
pub fn calculate_extension_fee(additional_days: u32) -> i128 {
(additional_days as i128) * EXTENSION_FEE_PER_DAY
}
}
// ===== EXTENSION VALIDATION =====
/// Extension validation utilities
pub struct ExtensionValidator;
impl ExtensionValidator {
/// Validate extension conditions
pub fn validate_extension_conditions(
env: &Env,
market_id: &Symbol,
additional_days: u32,
) -> Result<(), Error> {
// Validate additional days
if additional_days == 0 {
return Err(Error::InvalidDuration);
}
if additional_days > MAX_EXTENSION_DAYS {
return Err(Error::InvalidDuration);
}
// Get market and validate state.
// Extension is only permitted while the market is Active. Any terminal
// or post-resolution state (Resolved, Closed, Cancelled, Ended) must be
// rejected to prevent lifecycle corruption.
let market = MarketStateManager::get_market(env, market_id)?;
match market.state {
MarketState::Resolved | MarketState::Closed | MarketState::Cancelled => {
return Err(Error::ExtensionDenied);
}
MarketState::Ended => {
return Err(Error::ExtensionDenied);
}
MarketState::Active => {}
MarketState::Disputed => {
return Err(Error::ExtensionDenied);
}
}
let current_time = env.ledger().timestamp();
// Reject if the market has already passed its end time.
if current_time >= market.end_time {
return Err(Error::ExtensionDenied);
}
// Reject if the resulting deadline would still be in the past or present.
// This guards against edge cases where end_time is very close to now.
let new_end_time = market
.end_time
.saturating_add((additional_days as u64) * 24 * 60 * 60);
if new_end_time <= current_time {
return Err(Error::ExtensionDenied);
}
// Reject if oracle has already produced a result (market is effectively resolved).
if market.oracle_result.is_some() {
return Err(Error::MarketResolved);
}
Ok(())
}
/// Check extension limits
pub fn check_extension_limits(
env: &Env,
market_id: &Symbol,
additional_days: u32,
) -> Result<(), Error> {
let market = MarketStateManager::get_market(env, market_id)?;
// Check total extension days limit
let new_total_extension_days = market
.total_extension_days
.checked_add(additional_days)
.ok_or(Error::InvalidDuration)?;
if new_total_extension_days > market.max_extension_days {
return Err(Error::InvalidDuration);
}
// Check number of extensions limit
if market.extension_history.len() >= MAX_TOTAL_EXTENSIONS {
return Err(Error::InvalidDuration);
}
Ok(())
}
/// Check if admin can extend market
pub fn can_extend_market(env: &Env, market_id: &Symbol, admin: &Address) -> Result<(), Error> {
let market = MarketStateManager::get_market(env, market_id)?;
// Check if caller is market admin
if market.admin != admin.clone() {
return Err(Error::Unauthorized);
}
Ok(())
}
/// Check cumulative extension cap.
///
/// Reads the admin-configured cap (days) stored under `CUMULATIVE_CAP_KEY`.
/// A cap value of `0` means no cumulative cap is enforced.
/// When the cap is set, the proposed extension is checked against the
/// running total stored under `DataKey::MarketExtensionTotal(market_id)`.
/// On rejection an audit event is emitted before returning the error.
pub fn check_cumulative_cap(
env: &Env,
market_id: &Symbol,
additional_days: u32,
) -> Result<(), Error> {
let cap_key = Symbol::new(env, CUMULATIVE_CAP_KEY);
let cap: u32 = env
.storage()
.persistent()
.get(&cap_key)
.unwrap_or(0u32);
// 0 means no cap configured — skip check
if cap == 0 {
return Ok(());
}
let total_key = crate::storage::DataKey::MarketExtensionTotal(market_id.clone());
let current_total: u32 = env
.storage()
.persistent()
.get(&total_key)
.unwrap_or(0u32);
let new_total = current_total
.checked_add(additional_days)
.ok_or(Error::CumulativeExtensionCapHit)?;
if new_total > cap {
// Emit audit event before returning the error
env.events().publish(
(symbol_short!("cum_cap"), market_id.clone()),
(current_total, cap),
);
return Err(Error::CumulativeExtensionCapHit);
}
Ok(())
}
}
// ===== EXTENSION UTILITIES =====
/// Extension utility functions
pub struct ExtensionUtils;
impl ExtensionUtils {
/// Increment the persistent cumulative extension total for a market.
/// Called after a successful extension so the counter is monotone-increasing.
pub fn increment_extension_total(env: &Env, market_id: &Symbol, additional_days: u32) {
let key = crate::storage::DataKey::MarketExtensionTotal(market_id.clone());
let current: u32 = env.storage().persistent().get(&key).unwrap_or(0u32);
let new_total = current.saturating_add(additional_days);
env.storage().persistent().set(&key, &new_total);
}
/// Handle extension fees
pub fn handle_extension_fees(
env: &Env,
_market_id: &Symbol,
additional_days: u32,
) -> Result<i128, Error> {
let fee_amount = ExtensionManager::calculate_extension_fee(additional_days);
// Get token client for fee collection
let _token_client = MarketUtils::get_token_client(env)?;
// Transfer fees from admin to contract
// Note: In a real implementation, you would need to handle the actual token transfer
// For now, we'll just validate the fee amount
if fee_amount <= 0 {
return Err(Error::InsufficientStake);
}
Ok(fee_amount)
}
/// Emit extension event
pub fn emit_extension_event(
env: &Env,
market_id: &Symbol,
additional_days: u32,
admin: &Address,
) {
// In Soroban, events are emitted using the env.events() API
// For now, we'll use a simple approach with storage
let event_key = symbol_short!("ext_event");
let event_data = ExtensionEvent {
market_id: market_id.clone(),
additional_days,
admin: admin.clone(),
timestamp: env.ledger().timestamp(),
};
env.storage().persistent().set(&event_key, &event_data);
}
/// Get extension events
pub fn get_extension_events(env: &Env) -> Vec<ExtensionEvent> {
let event_key = symbol_short!("ext_event");
match env.storage().persistent().get(&event_key) {
Some(event) => vec![env, event],
None => vec![env],
}
}
}
// ===== EXTENSION TYPES =====
/// Extension event data
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionEvent {
/// Market ID that was extended
pub market_id: Symbol,
/// Additional days added
pub additional_days: u32,
/// Admin who requested extension
pub admin: Address,
/// Extension timestamp
pub timestamp: u64,
}
// ===== EXTENSION TEST HELPERS =====
/// Extension testing utilities
pub struct ExtensionTestHelpers;
impl ExtensionTestHelpers {
/// Create a test extension
pub fn create_test_extension(
env: &Env,
additional_days: u32,
admin: Address,
reason: String,
) -> MarketExtension {
MarketExtension::new(env, additional_days, admin, reason, 100_000_000)
}
/// Simulate market extension
pub fn simulate_market_extension(
env: &Env,
market_id: &Symbol,
admin: Address,
additional_days: u32,
) -> Result<(), Error> {
let reason = String::from_str(env, "Test extension");
ExtensionManager::extend_market_duration(
env,
admin,
market_id.clone(),
additional_days,
reason,
)
}
}
// Import required modules
use crate::markets::{MarketStateManager, MarketUtils};
#[cfg(test)]
mod tests {
use super::*;
use crate::types::ExtensionStats;
use soroban_sdk::testutils::{Address as _, Ledger, LedgerInfo};
#[test]
fn test_extension_validation() {
let env = Env::default();
let contract_id = env.register(crate::PredictifyHybrid, ());
let _admin = Address::generate(&env);
env.as_contract(&contract_id, || {
// Test valid extension days
assert!(ExtensionValidator::validate_extension_conditions(
&env,
&symbol_short!("test"),
5
)
.is_err()); // Market doesn't exist
// Test invalid extension days
assert_eq!(
ExtensionValidator::validate_extension_conditions(&env, &symbol_short!("test"), 0)
.unwrap_err(),
Error::InvalidDuration
);
});
assert_eq!(
ExtensionValidator::validate_extension_conditions(&env, &symbol_short!("test"), 31)
.unwrap_err(),
Error::InvalidDuration
);
}
#[test]
fn test_extension_fee_calculation() {
assert_eq!(
ExtensionManager::calculate_extension_fee(1),
EXTENSION_FEE_PER_DAY
);
assert_eq!(
ExtensionManager::calculate_extension_fee(5),
5 * EXTENSION_FEE_PER_DAY
);
assert_eq!(
ExtensionManager::calculate_extension_fee(30),
30 * EXTENSION_FEE_PER_DAY
);
}
#[test]
fn test_extension_stats() {
let _env = Env::default();
let stats = ExtensionStats {
total_extensions: 2,
total_extension_days: 10,
max_extension_days: 30,
can_extend: true,
extension_fee_per_day: EXTENSION_FEE_PER_DAY,
};
assert_eq!(stats.total_extensions, 2);
assert_eq!(stats.total_extension_days, 10);
assert!(stats.can_extend);
}
}