Skip to content

Latest commit

 

History

History
199 lines (154 loc) · 6.5 KB

File metadata and controls

199 lines (154 loc) · 6.5 KB

Yield Strategy Abstraction Implementation

Overview

This implementation addresses issue #296 by abstracting the yield logic through a YieldStrategyTrait interface. The SoroSusu contract can now accept any external contract address that implements this trait, providing flexibility and upgradability for yield generation strategies.

Architecture

Core Components

  1. YieldStrategyTrait - Abstract interface defining standard yield operations
  2. YieldStrategyRegistry - Registry for managing approved yield strategies
  3. YieldDelegation Structure - Updated to use abstract strategy addresses
  4. Circuit Breaker Integration - Updated to monitor yield strategies instead of hardcoded AMMs

Key Benefits

  • Flexibility: Protocol can upgrade to better yield opportunities without core logic changes
  • Security: Strategies must be registered and validated before use
  • Composability: Any contract implementing the trait can be used as a yield strategy
  • Risk Management: Circuit breaker monitors all registered strategies

Implementation Details

YieldStrategyTrait Interface

pub trait YieldStrategyTrait {
    fn initialize(env: Env, admin: Address, config: YieldStrategyConfig);
    fn deposit(env: Env, from: Address, amount: i128, params: DepositParams) -> Result<YieldInfo, YieldStrategyError>;
    fn withdraw(env: Env, to: Address, params: WithdrawalParams) -> Result<YieldInfo, YieldStrategyError>;
    fn get_estimated_yield(env: Env, amount: i128, period_seconds: u64) -> Result<YieldEstimate, YieldStrategyError>;
    fn get_yield_info(env: Env, user: Address) -> Result<YieldInfo, YieldStrategyError>;
    fn get_strategy_info(env: Env) -> Result<YieldStrategyConfig, YieldStrategyError>;
    fn update_config(env: Env, admin: Address, config: YieldStrategyConfig) -> Result<(), YieldStrategyError>;
    fn emergency_withdraw(env: Env, to: Address, amount: i128) -> Result<YieldInfo, YieldStrategyError>;
    fn health_check(env: Env) -> Result<bool, YieldStrategyError>;
}

Data Structures

YieldStrategyConfig

  • Strategy metadata and configuration
  • APY settings, limits, and operational parameters
  • Admin controls and emergency settings

YieldInfo

  • Current state of user's yield position
  • Total deposits, current balance, and yield earned
  • Strategy performance metrics

DepositParams & WithdrawalParams

  • Flexible parameters for yield operations
  • Support for minimum APY requirements, lockup periods
  • Emergency withdrawal options

Strategy Types

pub enum StrategyType {
    AMM,                    // Automated Market Maker
    Lending,                // Lending Protocol
    LiquidStaking,          // Liquid Staking
    YieldAggregator,        // Yield Aggregator
    Custom,                 // Custom Strategy
}

Usage Examples

Registering a New Yield Strategy

// Admin registers a new strategy
let strategy_config = YieldStrategyConfig {
    strategy_name: Symbol::new(&env, "NewAMMStrategy"),
    strategy_version: 1,
    min_deposit_amount: 1_000_000,
    max_deposit_amount: Some(10_000_000_000),
    default_apy_bps: 500,
    auto_compound_enabled: true,
    emergency_withdrawal_enabled: true,
    is_active: true,
    admin_address: admin,
};

contract.register_yield_strategy(
    admin,
    strategy_address,
    StrategyType::AMM,
    strategy_config
);

Proposing Yield Delegation with New Strategy

// User proposes yield delegation using abstract strategy
contract.propose_yield_delegation(
    user,
    circle_id,
    5000, // 50% delegation
    strategy_address,
    StrategyType::AMM
);

Circuit Breaker Integration

The circuit breaker has been updated to work with the abstract interface:

  • register_yield_strategy(): Register strategies for monitoring
  • update_health_metrics(): Update strategy health metrics
  • emergency_withdraw(): Emergency withdrawal through strategy interface

Emergency Unwind Flow

  1. Circuit breaker detects unhealthy strategy
  2. Calls emergency_withdraw() on strategy contract
  3. Funds returned to protected vault
  4. Delegation marked as completed

Migration from Hardcoded AMM

Before (Hardcoded)

pub struct YieldDelegation {
    pub pool_address: Address,     // Hardcoded AMM address
    pub pool_type: YieldPoolType,  // Fixed pool type
    // ... other fields
}

After (Abstract)

pub struct YieldDelegation {
    pub strategy_address: Address,     // Any strategy implementing trait
    pub strategy_type: StrategyType,   // Flexible strategy classification
    pub strategy_info: Option<YieldInfo>, // Current strategy state
    // ... other fields
}

Security Considerations

Strategy Registration

  • Only admin can register new strategies
  • Health check required before registration
  • Strategy must implement required interface

Circuit Breaker Protection

  • All registered strategies monitored
  • Automatic emergency withdrawal on trigger
  • Health metrics tracking per strategy

Access Control

  • Strategy admin controls configuration
  • Emergency withdrawal requires circuit breaker trigger
  • User authorization for yield operations

Testing

Comprehensive test suite included in yield_strategy_tests.rs:

  • MockYieldStrategy: Complete mock implementation for testing
  • Deposit/Withdrawal Tests: Verify core functionality
  • Yield Estimation Tests: Test yield calculations
  • Emergency Withdrawal Tests: Verify circuit breaker integration
  • Health Check Tests: Test strategy monitoring

Future Enhancements

Strategy Composition

  • Support for multiple strategies per delegation
  • Automatic rebalancing between strategies
  • Risk-weighted allocation

Advanced Monitoring

  • Real-time strategy performance metrics
  • Predictive health monitoring
  • Automated strategy rotation

Governance Integration

  • Community strategy proposals
  • Voting on strategy selection
  • Performance-based rewards

Conclusion

This implementation successfully abstracts the yield logic, addressing issue #296 requirements:

Abstract Interface: YieldStrategyTrait provides standard interface
External Contract Support: Any contract implementing trait can be used
Deposit/Withdrawal/Yield Functions: Core operations abstracted
Upgradability: Protocol can upgrade strategies without core changes
Circuit Breaker Integration: Risk management maintained

The protocol now has the flexibility to adapt to new yield opportunities while maintaining security and risk management through the circuit breaker system.