Skip to content

A DeFi portfolio management system that automatically rebalances capital between different investment strategies based on performance metrics. Built on Solana using the Anchor framework.

Notifications You must be signed in to change notification settings

donjne/portfolio-rebalancer

Repository files navigation

Solana DeFi Portfolio Rebalancer

A DeFi portfolio management system that automatically rebalances capital between different investment strategies based on performance metrics. Built on Solana using the Anchor framework.

Architecture

Core Components

  1. Portfolio Management: Initialize and manage investment portfolios
  2. Strategy Registry: Register strategies across different DeFi protocols
  3. Performance Engine: Mathematical scoring system with weighted formula:
    • Yield Rate: 45% weight
    • Capital Balance: 35% weight
    • Inverse Volatility: 20% weight
  4. Ranking System: Percentile-based ranking with dynamic threshold calculation
  5. Capital Operations: Extract from underperformers, redistribute to top performers

Account Structure

  • Portfolio: Main portfolio account with manager, thresholds, and totals
  • Strategy: Individual strategy with performance metrics and protocol details
  • CapitalPosition: Position tracking for different protocol types
  • Multi-Protocol Support: Stable lending, yield farming, liquid staking

Mathematical Focus Areas

Performance Scoring Algorithm

Performance Score = (Normalized Yield × 45%) + (Normalized Balance × 35%) + (Normalized Inverse Volatility × 20%)

Protocol-Specific Extraction

  • Solend: Simple balance withdrawal with utilization-based fees
  • Orca: LP token withdrawal using AMM mathematics (x*y=k formula)
  • Marinade: Unstaking with epoch delays and penalty calculations

Risk Management

  • Dynamic threshold calculation based on portfolio volatility
  • Diversification constraints (max 40% single strategy allocation)
  • Mathematical safety with overflow protection throughout

Prerequisites

  • Solana CLI: v1.18+
  • Anchor Framework: v0.30.1
  • Node.js: v18+
  • Rust: Latest stable version

Installation

  1. Clone the repository
git clone https://github.com/your-username/portfolio-rebalancer
cd portfolio-rebalancer
  1. Install dependencies
npm install
# or
yarn install
  1. Build the program
anchor build

Running Tests

Start Test Validator

# Terminal 1: Start Solana test validator
solana-test-validator

Run Tests

# Terminal 2: Run the test suite
anchor test --skip-local-validator

Expected Test Flow

The tests demonstrate the complete rebalancing workflow:

  1. Initialize Portfolio - Create portfolio with manager and thresholds
  2. Register Strategies - Add 3 strategies across different protocols
  3. Deposit Capital - Fund strategies with test tokens
  4. Update Performance - Set different performance metrics
  5. Execute Ranking - Calculate scores and percentile rankings
  6. Extract Capital - Remove funds from underperformers
  7. Redistribute Capital - Allocate to top performers
  8. Withdraw from Strategy - Demonstrate withdraw functionality

Key Instructions

Portfolio Management

  • initialize_portfolio - Create new portfolio
  • register_strategy - Add investment strategy
  • deposit_to_strategy - Manager deposits funds
  • withdraw_from_strategy - Manager withdraws funds

Performance & Rebalancing

  • update_performance - Update strategy metrics and calculate scores
  • execute_ranking_cycle - Rank strategies and identify rebalancing targets
  • extract_capital - Remove capital from underperformers
  • redistribute_capital - Allocate capital to top performers

Protocol Support

Stable Lending (Solend)

  • Simple deposit/withdrawal mechanics
  • Utilization-based fee calculation
  • Minimum 0.1 SOL deposit requirement

Yield Farming (Orca)

  • LP token position management
  • AMM mathematics for proportional withdrawals
  • Slippage and fee calculations
  • Minimum 0.5 SOL deposit requirement

Liquid Staking (Marinade)

  • Staking/unstaking with epoch delays
  • Validator commission handling
  • Penalty calculations for immediate unstaking
  • Minimum 1 SOL deposit requirement

License

This project is for assessment purposes to demonstrate Solana development skills in DeFi portfolio management.

About

A DeFi portfolio management system that automatically rebalances capital between different investment strategies based on performance metrics. Built on Solana using the Anchor framework.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published