Skip to content

Refactor: Centralize Fee Management System in Predictify Hybrid Contract#130

Merged
greatest0fallt1me merged 10 commits into
Predictify-org:masterfrom
1nonlypiece:fee
Jul 6, 2025
Merged

Refactor: Centralize Fee Management System in Predictify Hybrid Contract#130
greatest0fallt1me merged 10 commits into
Predictify-org:masterfrom
1nonlypiece:fee

Conversation

@1nonlypiece

Copy link
Copy Markdown
Contributor

This PR implements a comprehensive refactor of the fee management system in the predictify-hybrid contract, moving from scattered fee logic across multiple files to a dedicated, modular fee management system.

Closes #109

Changes Made

🏗️ New Fee Management Architecture

  • Created contracts/predictify-hybrid/src/fees.rs - Centralized fee management module
  • Implemented comprehensive fee types and structures:
    • FeeConfig - Configurable fee parameters
    • FeeAnalytics - Fee collection statistics and metrics
    • FeeBreakdown - Detailed fee calculation breakdowns
    • FeeValidation - Fee validation results and error reporting

🔧 Core Fee Management Components

FeeManager - Main fee orchestration

  • collect_fees() - Centralized fee collection logic
  • process_creation_fee() - Market creation fee handling
  • get_fee_analytics() - Fee statistics and reporting
  • update_fee_config() - Dynamic fee configuration updates

FeeCalculator - Fee computation engine

  • calculate_platform_fee() - Platform fee calculations
  • calculate_user_payout_after_fees() - User payout computations
  • calculate_fee_breakdown() - Detailed fee breakdowns
  • calculate_dynamic_fee() - Volume-based fee adjustments

FeeValidator - Validation and safety checks

  • validate_admin_permissions() - Admin authorization
  • validate_fee_amount() - Fee amount bounds checking
  • validate_market_for_fee_collection() - Market eligibility validation
  • validate_fee_config() - Configuration integrity checks

FeeUtils - Utility functions

  • can_collect_fees() - Quick eligibility checks
  • get_fee_eligibility() - Detailed eligibility with reasons

FeeConfigManager - Configuration management

  • store_fee_config() - Configuration persistence
  • get_fee_config() - Configuration retrieval
  • reset_to_defaults() - Default configuration restoration

FeeAnalytics - Analytics and reporting

  • calculate_analytics() - Overall fee statistics
  • get_market_fee_stats() - Per-market fee metrics
  • calculate_fee_efficiency() - Fee collection efficiency

🔄 Integration Updates

Updated lib.rs

  • Added fees module integration
  • Exposed new fee-related contract methods:
    • collect_fees()
    • get_fee_analytics()
    • get_fee_config()
    • update_fee_config()
    • validate_market_fees()
  • Replaced direct fee logic with calls to new fee management system

Updated markets.rs

  • Delegated fee logic to new fees module
  • Deprecated old fee-related functions
  • Maintained backward compatibility

Updated voting.rs

  • Delegated fee logic to new fees module
  • Deprecated old fee-related functions
  • Maintained backward compatibility

🧪 Comprehensive Test Suite

  • Added 20+ new fee management tests in test.rs
  • Test coverage includes:
    • Fee collection workflows
    • Fee calculation accuracy
    • Validation logic
    • Configuration management
    • Analytics computation
    • Error handling scenarios
    • Dynamic fee adjustments
    • Admin permission validation

🔒 Safety and Validation Features

  • Fee safety checks - Prevents invalid fee operations
  • Admin permission validation - Ensures only authorized users can modify fees
  • Configuration validation - Validates fee parameter integrity
  • Market eligibility checks - Ensures proper market state for fee collection
  • Error handling - Comprehensive error reporting and recovery

⚙️ Configuration Management

  • Dynamic fee configuration - Runtime fee parameter updates
  • Default configuration - Safe fallback values
  • Configuration persistence - Persistent fee settings
  • Validation rules - Ensures configuration integrity

Technical Details

Fee Configuration Parameters

  • platform_fee_percentage - Platform fee percentage (default: 2%)
  • creation_fee - Market creation fee (default: 10 XLM)
  • min_fee_amount - Minimum fee amount (default: 0.1 XLM)
  • max_fee_amount - Maximum fee amount (default: 100 XLM)
  • collection_threshold - Minimum stakes for fee collection (default: 10 XLM)
  • fees_enabled - Global fee enable/disable flag

Dynamic Fee Adjustments

  • Volume-based discounts - Larger markets get fee reductions
  • Configurable thresholds - Adjustable volume breakpoints
  • Percentage-based calculations - Precise fee computations

Analytics and Reporting

  • Total fees collected - Aggregate fee statistics
  • Markets with fees - Fee collection coverage
  • Average fee per market - Performance metrics
  • Fee efficiency tracking - Collection success rates

Benefits

Modularity - Clean separation of fee concerns
Maintainability - Centralized fee logic for easier updates
Testability - Comprehensive test coverage for all fee operations
Safety - Multiple validation layers and error handling
Flexibility - Dynamic configuration and runtime updates
Analytics - Detailed fee reporting and metrics
Backward Compatibility - Existing functionality preserved

Testing

  • Build Status: ✅ All compilation errors resolved
  • Test Coverage: 20+ new fee management tests added
  • Integration: All existing functionality preserved
  • Validation: Comprehensive error handling and edge cases covered

Breaking Changes

None - This refactor maintains full backward compatibility while adding new functionality.

Migration Notes

No migration required - existing fee collection logic continues to work as before, with enhanced capabilities available through new methods.

…rid contract, including fee collection, validation, analytics, and configuration management
…alities, including fee processing during market creation, fee collection, analytics retrieval, configuration updates, and market fee validation
…ecate old function in Predictify Hybrid contract
…ntract, covering fee collection, validation, configuration, and analytics
… implementations in VotingManager and VotingUtils
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

Thanks for the PR @1nonlypiece

@greatest0fallt1me greatest0fallt1me merged commit 56a1fbd into Predictify-org:master Jul 6, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract Fee Collection and Management Logic to Dedicated fees.rs File

2 participants