Skip to main content

Overview

Strategy contracts implement yield-generating protocols like Aave, Compound, or leverage strategies. All strategies must implement the IStrategy interface.

IStrategy Interface

All strategies must implement these core functions:
Source: contracts/interfaces/IStrategy.sol:4-10

Strategy Architecture

Common Pattern

All strategies follow this architecture:
  1. Immutable References: Vault, router, and protocol addresses
  2. Access Control: onlyRouter modifier for protected functions
  3. Token Approvals: Pre-approve protocol contracts
  4. Bookkeeping: Track deposited amounts and protocol balances

Aave V3 Strategy

Simple yield strategy using Aave V3 lending:

Contract Setup

Source: contracts/strategy/StrategyAave.sol:15-34

Invest Function

Deposit funds into Aave:
Source: contracts/strategy/StrategyAave.sol:54-59

Withdraw Function

Withdraw funds back to vault:
Source: contracts/strategy/StrategyAave.sol:61-70

Harvest Function

Collect interest profits:
Source: contracts/strategy/StrategyAave.sol:72-86

Strategy Balance

Return current strategy value:
Source: contracts/strategy/StrategyAave.sol:88-90

APY Estimation

Source: contracts/strategy/StrategyAave.sol:41-49

Aave Leverage Strategy

Advanced strategy using leverage to amplify yields:

Contract Setup

Source: contracts/strategy/StrategyAaveLeverage.sol:35-59

Leverage Mechanism

The strategy uses a loop to build leverage:
  1. Supply LINK as collateral
  2. Borrow WETH (60% of collateral value)
  3. Swap WETH for LINK
  4. Supply new LINK as collateral
  5. Repeat up to maxDepth times

Invest with Leverage

Source: contracts/strategy/StrategyAaveLeverage.sol:154-272

Deleverage

Unwind leverage positions:
Source: contracts/strategy/StrategyAaveLeverage.sol:305-377

Leverage Parameters

Adjust leverage settings:
Source: contracts/strategy/StrategyAaveLeverage.sol:105-111

Leverage State

Source: contracts/strategy/StrategyAaveLeverage.sol:113-128

LTV Monitoring

Source: contracts/strategy/StrategyAaveLeverage.sol:130-138

Strategy Balance with Debt

Source: contracts/strategy/StrategyAaveLeverage.sol:381-393

Creating New Strategies

Implementation Checklist

  1. Implement IStrategy Interface
    • strategyBalance() - return current strategy value
    • invest() - deploy funds into protocol
    • withdrawToVault() - return funds to vault
    • harvest() - collect and send profits to vault
    • deleverageAll() - unwind positions (can be no-op)
  2. Access Control
  3. Token Approvals
  4. Safe Transfers
  5. Error Handling
    • Use try-catch for external calls
    • Validate amounts and addresses
    • Emit events for failures
  6. Bookkeeping
    • Track deposited principal
    • Track borrowed amounts (if applicable)
    • Calculate net value correctly

Protocol Interfaces

Aave V3 Pool

Source: contracts/strategy/StrategyAaveLeverage.sol:9-17

Uniswap V2 Router

Source: contracts/strategy/StrategyAaveLeverage.sol:20-28

Price Oracle

Source: contracts/strategy/StrategyAaveLeverage.sol:31-33

OpenZeppelin Dependencies

Source: contracts/strategy/StrategyAave.sol:4-6

Security Best Practices

  1. Access Control: Always use onlyRouter modifier
  2. Safe Math: Solidity 0.8+ has built-in overflow protection
  3. Safe Transfers: Use OpenZeppelin’s SafeERC20
  4. Error Handling: Wrap external calls in try-catch
  5. Slippage Protection: Set minimum output amounts for swaps
  6. LTV Monitoring: Track leverage ratios for leveraged strategies
  7. Emergency Pause: Include pause functionality for leverage strategies
  8. Conservative Parameters: Use safe borrow factors (e.g., 60% instead of 80%)

Testing Strategies

Events

Source: contracts/strategy/StrategyAaveLeverage.sol:70-74

Next Steps