Skip to content

Leveraged DLMM Positions on Meteora, Capstone Project for Turbin3, Async Builders Cohort Q1-26

Notifications You must be signed in to change notification settings

Metlev-Labs/metlev-engine

Repository files navigation

Leveraged Meteora DLMM Protocol

Turbin3 Q1 2026 - Capstone Project - A protocol for opening leveraged liquidity provider positions on Meteora DLMM pools with automated risk management.

Overview

This protocol enables experienced Solana LPs to open leveraged DLMM liquidity positions on Meteora where trading fees are the primary source of return. Users deposit SOL or USDC as collateral, and the protocol borrows additional capital to create larger liquidity positions, amplifying fee generation while managing liquidation risk.

Core Value Proposition

  • Fee-First Leverage - Amplify DLMM trading fee yield through conservative leverage
  • Automated Position Management - Protocol handles bin setup, position creation, and risk monitoring
  • Explicit Risk Controls - Clear LTV thresholds, oracle-based health checks, and automated liquidations
  • Capital Efficiency - Earn more fees with the same collateral through leverage

How It Works

  1. Deposit Collateral - User deposits SOL or USDC into protocol vaults
  2. Open Leveraged Position - Protocol borrows additional funds and creates DLMM LP position on Meteora
  3. Earn Fees - Position generates trading fees from Meteora pool activity
  4. Monitor Health - Oracle-based health checks ensure position stays solvent
  5. Close or Liquidate - User can voluntarily close, or liquidators force-close if unhealthy

Target Users

  • Experienced Meteora LPs - Already familiar with DLMM mechanics and bin strategies
  • Leverage-Comfortable DeFi Users - Understand leverage protocols (Kamino, Loopscale)
  • Risk-Aware Yield Optimizers - Comfortable with liquidation risk for higher fee APR

Architecture

Design Pattern: Per-Collateral Configuration

This protocol uses a per-collateral configuration pattern (similar to Aave, Kamino, Solend) where:

  • Each collateral type (SOL, USDC, etc.) has its own CollateralConfig account
  • Risk parameters (LTV, liquidation thresholds, interest rates) are tailored per asset
  • New collateral types can be added dynamically without program upgrades
  • Users can hold multiple positions with different collateral types simultaneously

Benefits:

  • βœ… Risk-appropriate parameters - Volatile assets (SOL) have lower LTV than stablecoins (USDC)
  • βœ… Scalability - Add new collaterals without code changes
  • βœ… Flexibility - Adjust parameters per asset based on market conditions
  • βœ… Capital efficiency - Users can optimize based on their preferred collateral

Program Structure

programs/
└── metlev-engine/
    └── src/
        β”œβ”€β”€ lib.rs                           # Program entry point
        β”œβ”€β”€ state/
        β”‚   β”œβ”€β”€ mod.rs                       # State module exports
        β”‚   β”œβ”€β”€ config.rs                    # Global protocol configuration
        β”‚   β”œβ”€β”€ position.rs                  # User leveraged position state
        β”‚   β”œβ”€β”€ lending_vault.rs             # Lending vault state and accounting
        β”‚   └── lp_position.rs               # LP supplier position and interest
        β”œβ”€β”€ instructions/
        β”‚   β”œβ”€β”€ mod.rs                       # Instruction exports
        β”‚   β”œβ”€β”€ initialize.rs                # Initialize protocol config
        β”‚   β”œβ”€β”€ register_collateral.rs       # Register new collateral type
        β”‚   β”œβ”€β”€ deposit_sol_collateral.rs    # Deposit SOL as collateral
        β”‚   β”œβ”€β”€ deposit_token_collateral.rs  # Deposit SPL tokens as collateral
        β”‚   β”œβ”€β”€ initialize_lending_vault.rs  # Create and seed the lending vault
        β”‚   β”œβ”€β”€ supply.rs                    # LP supplies SOL to the vault
        β”‚   β”œβ”€β”€ withdraw.rs                  # LP withdraws SOL + interest
        β”‚   β”œβ”€β”€ open_position.rs             # Create leveraged DLMM position
        β”‚   β”œβ”€β”€ close_position.rs            # Close position and repay debt
        β”‚   β”œβ”€β”€ liquidate.rs                 # Force-close unhealthy positions
        β”‚   └── update_config.rs             # Update protocol/collateral parameters
        β”œβ”€β”€ utils/
        β”‚   β”œβ”€β”€ mod.rs                       # Utility exports
        β”‚   β”œβ”€β”€ health.rs                    # Health factor calculations
        β”‚   └── oracle.rs                    # Price oracle helpers
        └── errors.rs                        # Custom error definitions

State Accounts

Config (Global Protocol Settings)

pub struct Config {
    pub authority: Pubkey,   // Protocol admin
    pub paused: bool,        // Emergency pause state
    pub bump: u8,
}
  • PDA: ["config"]
  • Manages protocol-level settings and pause state
  • Does NOT store collateral-specific parameters

CollateralConfig (Per-Collateral Risk Parameters)

pub struct CollateralConfig {
    pub mint: Pubkey,                    // Collateral token mint
    pub oracle: Pubkey,                  // Price oracle (Pyth/Switchboard)
    pub max_ltv: u16,                    // Max loan-to-value (basis points)
    pub liquidation_threshold: u16,      // Liquidation trigger (basis points)
    pub liquidation_penalty: u16,        // Penalty for liquidation (basis points)
    pub min_deposit: u64,                // Minimum deposit amount
    pub interest_rate_bps: u16,          // Interest rate (basis points)
    pub oracle_max_age: u64,             // Max oracle staleness (seconds)
    pub enabled: bool,                   // Whether collateral is active
    pub bump: u8,
}
  • PDA: ["collateral_config", mint]
  • Each collateral (SOL, USDC, etc.) has independent risk parameters
  • Allows different LTV ratios for volatile vs stable assets
  • Example: SOL at 75% LTV, USDC at 90% LTV

Position (Per-User Leveraged Position)

pub struct Position {
    pub owner: Pubkey,              // User wallet
    pub collateral_mint: Pubkey,    // Which token is used as collateral
    pub collateral_amount: u64,     // Amount deposited
    pub debt_amount: u64,           // Amount borrowed
    pub meteora_position: Pubkey,   // Reference to DLMM position
    pub created_at: i64,            // Unix timestamp
    pub status: PositionStatus,     // Active/Closed/Liquidated
    pub bump: u8,
}
  • PDA: ["position", owner, collateral_mint]
  • Users can have multiple positions with different collateral types
  • Each position is isolated per collateral mint

LendingVault (On-Chain SOL Vault)

pub struct LendingVault {
    pub total_supplied: u64,    // Total SOL supplied by LPs
    pub total_borrowed: u64,    // Total SOL currently borrowed
    pub interest_rate_bps: u16, // Annual interest rate in basis points
    pub bump: u8,               // LendingVault PDA bump
    pub vault_bump: u8,         // sol_vault PDA bump (for CPI signing)
}
  • PDA: ["lending_vault"]
  • Paired with a sol_vault SystemAccount PDA that holds the actual lamports
  • sol_vault PDA: ["sol_vault", lending_vault]
  • Seeded with rent-exempt minimum on initialization
  • Tracks total supplied and borrowed for utilization calculations

LpPosition (Per-LP Supplier State)

pub struct LpPosition {
    pub lp: Pubkey,             // Supplier wallet
    pub supplied_amount: u64,   // Principal supplied
    pub interest_earned: u64,   // Accrued interest
    pub last_update: i64,       // Unix timestamp of last interest accrual
    pub bump: u8,
}
  • PDA: ["lp_position", lp, lending_vault]
  • Created via init_if_needed to support top-up deposits
  • Interest accrues using simple interest: principal * rate_bps * elapsed / (365 * 24 * 3600 * 10000)
  • Closed (rent returned) on full withdrawal

Implementation Steps

Phase 1: Core Infrastructure

  1. Project Setup

    • Initialize Anchor project structure
    • Define state accounts (Config, CollateralConfig, Position, LendingVault)
    • Create custom error types
    • Set up test environment
  2. Basic Instructions

    • initialize - Set up global protocol config (authority, pause state)
    • register_collateral - Register new collateral types with risk parameters
    • deposit_collateral - Accept deposits for any enabled collateral
    • Base account validation and PDA derivation

Phase 2: Position Management

  1. Open Position Logic

    • open_position - Create leveraged DLMM position
    • Integrate mock lending vault for borrowing
    • CPI to Meteora DLMM to create LP position
    • Store position reference and debt tracking
  2. Close Position Logic

    • close_position - Unwind position voluntarily
    • CPI to Meteora to remove liquidity
    • Repay debt to lending vault
    • Return remaining collateral to user

Phase 3: Risk Management

  1. Health Monitoring

    • Integrate price oracle (Pyth/Switchboard/magicblock)
    • Implement health factor calculation
    • LTV calculation based on collateral value vs debt
  2. Liquidation System

    • liquidate - Force-close unhealthy positions
    • Health check validation
    • Liquidator incentive distribution
    • Bad debt handling

Phase 4: Testing & Refinement

  1. Comprehensive Testing

    • Unit tests for all instructions
    • Integration tests with Meteora devnet
    • Liquidation scenario testing
    • Oracle edge case handling
  2. Security Hardening

    • Reentrancy protection
    • Oracle staleness checks
    • Overflow/underflow validation
    • Access control verification

Key Technical Challenges

1. Meteora DLMM Integration

  • Challenge: CPI to Meteora to create/close DLMM positions
  • Solution: Study Meteora SDK and program interface, implement proper account passing

2. Health Factor Calculation

  • Challenge: Accurately value DLMM position + account for impermanent loss
  • Solution: Oracle-based collateral pricing, conservative LTV ratios

3. Debt Accounting

  • Challenge: Track borrowed amounts and interest accrual
  • Solution: Simple interest model in POC, store principal + timestamp

4. Liquidation Mechanics

  • Challenge: Ensure liquidations are profitable and timely
  • Solution: Clear threshold + liquidator incentives, anyone can liquidate

5. Oracle Integration

  • Challenge: Reliable price feeds for SOL/USDC
  • Solution: Pyth oracle integration with staleness checks

User Stories (POC Scope)

Experienced Solana LP

  • Deposit collateral - Deposit SOL or USDC to open positions
  • Open leveraged position - Create DLMM LP with borrowed funds
  • View position status - Check health factor and liquidation risk
  • Close position - Exit position, repay debt, withdraw collateral

Liquidator / Keeper

  • Check position health - Monitor positions for liquidation
  • Force-close unsafe positions - Liquidate unhealthy positions for reward

Risk Parameters (POC)

Risk parameters are per-collateral, allowing different configurations for volatile vs stable assets.

SOL Collateral (Volatile Asset)

Parameter Value Description
Max LTV 75% Maximum loan-to-value ratio
Liquidation Threshold 80% Health factor triggers liquidation
Liquidation Penalty 5% Penalty paid to liquidator
Min Deposit 0.1 SOL Minimum deposit amount
Interest Rate 5% APR Borrow rate for SOL positions
Oracle Max Age 60 seconds Max staleness for price feeds

USDC Collateral (Stablecoin)

Parameter Value Description
Max LTV 90% Higher LTV due to stability
Liquidation Threshold 95% Closer threshold (less volatility)
Liquidation Penalty 3% Lower penalty (less risk)
Min Deposit 10 USDC Minimum deposit amount
Interest Rate 3% APR Lower rate for stable collateral
Oracle Max Age 60 seconds Max staleness for price feeds

Note: Each collateral type can be added via register_collateral instruction with custom parameters.

Dependencies

[dependencies]
anchor-lang = "0.32.1"
anchor-spl = { version = "0.32.1", features = ["token"] }
pyth-solana-receiver-sdk = "0.2.0"  # Oracle integration

Getting Started

Prerequisites

  • Rust 1.89.0
  • Solana CLI 3.1.6
  • Anchor 0.32.1
  • Node.js 20+
  • Yarn

Installation

# Install dependencies (also sets up git hooks)
yarn install

# Build the program
anchor build

# Run tests
anchor test

Branch Naming Convention

This repo enforces branch naming via a git pre-push hook and CI check. Branches must follow:

feat/<name>      # New feature
fix/<name>       # Bug fix
chore/<name>     # Maintenance
docs/<name>      # Documentation
refactor/<name>  # Code refactoring

The hook is automatically configured when you run yarn install.

Testing Strategy

Unit Tests

  • Config initialization
  • Collateral deposit/withdrawal
  • Debt accounting
  • Health factor calculation
  • Liquidation threshold logic

Integration Tests

  • Full position lifecycle (deposit β†’ open β†’ close)
  • Liquidation scenarios (healthy β†’ unhealthy β†’ liquidated)
  • Oracle price updates
  • Multi-user interactions

Devnet Testing

  • Deploy to Solana devnet
  • Test with real Meteora DLMM pools
  • Monitor liquidation bot behavior
  • Validate oracle integration

Security Considerations

  1. Oracle Manipulation - Use staleness checks, multiple oracle sources
  2. Flash Loan Attacks - Position changes require minimum time delays
  3. Bad Debt Accumulation - Conservative LTV ratios, liquidation buffers
  4. CPI Reentrancy - Proper account validation and state checks

Future Enhancements (Post-POC)

  • Multiple leverage presets (2x, 3x, 5x)
  • Auto-rebalancing based on volatility
  • Fee compounding / auto-reinvestment
  • Partial position closes
  • Integration with real lending protocols (Kamino, Solana)
  • Support for additional DLMM pairs

Resources

CI/CD

Tests run automatically via GitHub Actions on push to main, feat/**, fix/** and on pull requests to main.

The pipeline:

  1. Validates branch naming convention
  2. Builds with Rust 1.89.0 (Rust deps cached)
  3. Installs Solana CLI 3.1.6 (cached after first run)
  4. Installs Anchor CLI 0.32.1 (cached after first run)
  5. Installs Node dependencies (yarn cache)
  6. Runs anchor build + anchor test

Project Status

🚧 In Development - POC Phase

  • Project planning and requirements
  • Project skeleton and base structure
  • Core state accounts (Config, Position, LendingVault, LpPosition)
  • Base instructions (initialize, register_collateral, deposit_collateral)
  • Lending vault (initialize_lending_vault, supply, withdraw with interest accrual)
  • Lending vault test suite with constraint validation
  • CI/CD pipeline (GitHub Actions) with Solana/Anchor/Rust caching
  • Branch naming enforcement (git hook + CI check)
  • Position opening (Meteora DLMM integration via CPI)
  • Health monitoring (oracle integration)
  • Liquidation system
  • Dynamic APY based on vault utilization (kink rate model)
  • Full integration testing and deployment

About

Leveraged DLMM Positions on Meteora, Capstone Project for Turbin3, Async Builders Cohort Q1-26

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors