Overview
Strategy contracts implement yield-generating protocols like Aave, Compound, or leverage strategies. All strategies must implement theIStrategy interface.
IStrategy Interface
All strategies must implement these core functions:contracts/interfaces/IStrategy.sol:4-10
Strategy Architecture
Common Pattern
All strategies follow this architecture:- Immutable References: Vault, router, and protocol addresses
- Access Control:
onlyRoutermodifier for protected functions - Token Approvals: Pre-approve protocol contracts
- Bookkeeping: Track deposited amounts and protocol balances
Aave V3 Strategy
Simple yield strategy using Aave V3 lending:Contract Setup
contracts/strategy/StrategyAave.sol:15-34
Invest Function
Deposit funds into Aave:contracts/strategy/StrategyAave.sol:54-59
Withdraw Function
Withdraw funds back to vault:contracts/strategy/StrategyAave.sol:61-70
Harvest Function
Collect interest profits:contracts/strategy/StrategyAave.sol:72-86
Strategy Balance
Return current strategy value:contracts/strategy/StrategyAave.sol:88-90
APY Estimation
contracts/strategy/StrategyAave.sol:41-49
Aave Leverage Strategy
Advanced strategy using leverage to amplify yields:Contract Setup
contracts/strategy/StrategyAaveLeverage.sol:35-59
Leverage Mechanism
The strategy uses a loop to build leverage:- Supply LINK as collateral
- Borrow WETH (60% of collateral value)
- Swap WETH for LINK
- Supply new LINK as collateral
- Repeat up to
maxDepthtimes
Invest with Leverage
contracts/strategy/StrategyAaveLeverage.sol:154-272
Deleverage
Unwind leverage positions:contracts/strategy/StrategyAaveLeverage.sol:305-377
Leverage Parameters
Adjust leverage settings:contracts/strategy/StrategyAaveLeverage.sol:105-111
Leverage State
contracts/strategy/StrategyAaveLeverage.sol:113-128
LTV Monitoring
contracts/strategy/StrategyAaveLeverage.sol:130-138
Strategy Balance with Debt
contracts/strategy/StrategyAaveLeverage.sol:381-393
Creating New Strategies
Implementation Checklist
-
Implement IStrategy Interface
strategyBalance()- return current strategy valueinvest()- deploy funds into protocolwithdrawToVault()- return funds to vaultharvest()- collect and send profits to vaultdeleverageAll()- unwind positions (can be no-op)
-
Access Control
-
Token Approvals
-
Safe Transfers
-
Error Handling
- Use try-catch for external calls
- Validate amounts and addresses
- Emit events for failures
-
Bookkeeping
- Track deposited principal
- Track borrowed amounts (if applicable)
- Calculate net value correctly
Protocol Interfaces
Aave V3 Pool
contracts/strategy/StrategyAaveLeverage.sol:9-17
Uniswap V2 Router
contracts/strategy/StrategyAaveLeverage.sol:20-28
Price Oracle
contracts/strategy/StrategyAaveLeverage.sol:31-33
OpenZeppelin Dependencies
contracts/strategy/StrategyAave.sol:4-6
Security Best Practices
- Access Control: Always use
onlyRoutermodifier - Safe Math: Solidity 0.8+ has built-in overflow protection
- Safe Transfers: Use OpenZeppelin’s SafeERC20
- Error Handling: Wrap external calls in try-catch
- Slippage Protection: Set minimum output amounts for swaps
- LTV Monitoring: Track leverage ratios for leveraged strategies
- Emergency Pause: Include pause functionality for leverage strategies
- Conservative Parameters: Use safe borrow factors (e.g., 60% instead of 80%)
Testing Strategies
Events
contracts/strategy/StrategyAaveLeverage.sol:70-74