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.
- YieldStrategyTrait - Abstract interface defining standard yield operations
- YieldStrategyRegistry - Registry for managing approved yield strategies
- YieldDelegation Structure - Updated to use abstract strategy addresses
- Circuit Breaker Integration - Updated to monitor yield strategies instead of hardcoded AMMs
- 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
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>;
}- Strategy metadata and configuration
- APY settings, limits, and operational parameters
- Admin controls and emergency settings
- Current state of user's yield position
- Total deposits, current balance, and yield earned
- Strategy performance metrics
- Flexible parameters for yield operations
- Support for minimum APY requirements, lockup periods
- Emergency withdrawal options
pub enum StrategyType {
AMM, // Automated Market Maker
Lending, // Lending Protocol
LiquidStaking, // Liquid Staking
YieldAggregator, // Yield Aggregator
Custom, // Custom 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
);// User proposes yield delegation using abstract strategy
contract.propose_yield_delegation(
user,
circle_id,
5000, // 50% delegation
strategy_address,
StrategyType::AMM
);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
- Circuit breaker detects unhealthy strategy
- Calls
emergency_withdraw()on strategy contract - Funds returned to protected vault
- Delegation marked as completed
pub struct YieldDelegation {
pub pool_address: Address, // Hardcoded AMM address
pub pool_type: YieldPoolType, // Fixed pool type
// ... other fields
}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
}- Only admin can register new strategies
- Health check required before registration
- Strategy must implement required interface
- All registered strategies monitored
- Automatic emergency withdrawal on trigger
- Health metrics tracking per strategy
- Strategy admin controls configuration
- Emergency withdrawal requires circuit breaker trigger
- User authorization for yield operations
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
- Support for multiple strategies per delegation
- Automatic rebalancing between strategies
- Risk-weighted allocation
- Real-time strategy performance metrics
- Predictive health monitoring
- Automated strategy rotation
- Community strategy proposals
- Voting on strategy selection
- Performance-based rewards
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.