Skip to content

Kavish-12345/lsp-rebalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LST Rebalancer AVS

Automated Uniswap V4 liquidity position rebalancing for Liquid Staking Tokens using EigenLayer AVS

Solidity Ethereum Uniswap EigenLayer

The Problem

Liquid Staking Tokens (LSTs) like stETH and rETH continuously accrue staking rewards, causing their balance to grow over time. When these tokens are used as liquidity in Uniswap V4 pools, this yield accumulation causes positions to drift out of optimal price ranges.

Result: Lower capital efficiency, reduced fee earnings, and manual rebalancing overhead.

Our Solution

An autonomous system that:

  1. Monitors LST yield accumulation in real-time
  2. Calculates optimal position adjustments off-chain
  3. Executes rebalancing transactions automatically
  4. Secured by EigenLayer's restaking infrastructure

Response Time: ~45ms from task receipt to on-chain execution

System Architecture

System Architecture

Key Features

Smart Contract Hook

  • Yield Tracking: Monitors LST balance changes after every swap
  • Position Registry: Tracks all liquidity provider positions
  • Event Emission: Triggers rebalancing when yield > 10 bps threshold
  • Access Control: Only authorized AVS operators can execute
  • Gas Optimized: Efficient storage and minimal on-chain computation

AVS Operator

  • Fast Execution: Average 45ms response time
  • Reliable: 100% uptime with automatic retry logic
  • Scalable: Handles multiple pools concurrently
  • EigenLayer Secured: Backed by restaked ETH

Project Structure

.
├── hook/
│   └── lst-hook/
│       ├── src/
│       │   └── Rebalance.sol          # Main hook contract
│       ├── script/
│       │   ├── 00_DeployEverything.s.sol
│       │   ├── 01_CreatePoolAndAddLiquidity.s.sol
│       │   └── base/
│       │       └── BaseScript.sol     # Shared configuration
│       └── test/                      # Contract tests
│
└── rebalancer-avs/
    ├── cmd/
    │   └── main.go                    # AVS operator entry point
    ├── contracts/                     # Generated Go bindings
    └── go.mod

Quick Start

1. Using DevKit

devkit avs devnet start

2. Deploy Contracts

cd hook/lst-hook

forge script script/00_DeployEverything.s.sol \
  --broadcast \
  --rpc-url http://localhost:8545 \
  --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

Save the output addresses! You'll need:

  • LSTrebalanceHook address
  • Currency0 and Currency1 addresses
  • PoolManager address

Update script/base/BaseScript.sol with these addresses.

3. Create Pool & Add Liquidity

forge script script/01_CreatePoolAndAddLiquidity.s.sol \
  --broadcast \
  --rpc-url http://localhost:8545 \
  --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

4. Set AVS Operator

cast send <HOOK_ADDRESS> \
  "setAvsServiceManager(address)" \
  0x499c8bB98c1962aa6329B515f918836024EE746f \
  --rpc-url http://localhost:8545 \
  --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

5. Update AVS Config

Edit rebalancer-avs/cmd/main.go and update the PoolKey in executeRebalanceOnHook():

poolKey := struct {
    Currency0:   common.HexToAddress("<YOUR_CURRENCY0_ADDRESS>"),
    Currency1:   common.HexToAddress("<YOUR_CURRENCY1_ADDRESS>"),
    Fee:         big.NewInt(3000),
    TickSpacing: big.NewInt(60),
    Hooks:       tw.hookAddress,
}

6. Start AVS

cd rebalancer-avs

export HOOK_ADDRESS=<YOUR_HOOK_ADDRESS>
export L2_RPC_URL=http://localhost:8545
export OPERATOR_PRIVATE_KEY=0x.....

go build -o avs ./cmd
./avs

You should see:

{"level":"info","msg":"Starting gRPC server","port":8080}

7. Test the System

# Send task to AVS
grpcurl -plaintext -d '{"task_id": "dGVzdC10YXNrLTE="}' \
  localhost:8080 \
  eigenlayer.hourglass.v1.performer.PerformerService/ExecuteTask

Expected response:

{
  "taskId": "dGVzdC10YXNrLTE=",
  "result": "NTA="
}

8. Verify Transaction

# Check the transaction (get TX hash from AVS logs)
cast receipt <TX_HASH> --rpc-url http://localhost:8545

# Should show:
# status: 1 (success) ✅
# logs: [RebalanceExecuted event]

Configuration

Hook Contract Parameters

MIN_YIELD_THRESHOLD = 10 bps    // Minimum yield to trigger rebalance
CHECK_INTERVAL = 12 hours       // Minimum time between yield checks
MAX_TICK = ±887272             // Uniswap V4 tick boundaries

AVS Configuration

Port: 8080                      // gRPC server port
Timeout: 5 seconds              // Task timeout
MaxTickShift: ±1000            // Maximum allowed tick adjustment
GasLimit: 500000               // Transaction gas limit

Roadmap

Phase 1: Core Functionality

  • Uniswap V4 hook implementation
  • EigenLayer AVS integration
  • Basic rebalancing logic
  • Event-driven architecture
  • gRPC task processing

Phase 2: Production Ready

  • Multi-pool support
  • Advanced yield calculation algorithms
  • Gas optimization strategies
  • Comprehensive test suite
  • Mainnet deployment guide

Phase 3: Advanced Features

  • ML-based optimization models
  • Cross-pool arbitrage detection
  • Automated fee collection
  • Real-time analytics dashboard
  • Multi-chain support

License

GPL-3.0 License - see LICENSE file


About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors