Overview
The Liquidator Bot monitors Drift user accounts for undercollateralized positions and executes liquidations to maintain protocol solvency. It identifies users whose collateral has fallen below maintenance margin requirements and closes their positions using two primary strategies:- Perp Liquidation with Fill: Closes perpetual positions by matching against resting limit orders in the orderbook
- Spot Liquidation with Swap: Repays spot borrows using atomic swaps via Jupiter
How It Works
Margin Status Detection
The liquidator tracks three margin states:- Safe:
total_collateral >= margin_requirement(sufficient collateral) - High Risk:
free_margin < 10% of margin_requirement(approaching liquidation) - Liquidatable:
total_collateral < margin_requirement(requires liquidation)
Data Freshness Validation
To ensure accurate liquidation decisions, the bot validates:- User Account Freshness: Must be updated within 100 slots (~40 seconds)
- Oracle Price Freshness: Must be updated within 50 slots (~20 seconds)
- Pyth Price Freshness: Must be updated within 5 seconds
Liquidation Strategies
Perp Liquidation with Fill
- Identify the largest perpetual position
- Query the DLOB for makers on the opposite side (bids for shorts, asks for longs)
- Build a
liquidate_perp_with_filltransaction - Execute the liquidation, paying the liquidatee and earning liquidator rewards
Spot Liquidation with Swap
- Find borrow positions (skipping dust amounts)
- Identify the largest deposit to use as collateral
- Query Jupiter API for optimal swap route
- Build a
liquidate_spot_with_swaptransaction - Atomically repay the borrow using the swapped collateral
Rate Limiting
The bot implements a 5-slot (~2 second) rate limit between successive liquidation attempts on the same user. This prevents transaction spam when positions require multiple liquidations.Running the Liquidator Bot
Basic Usage
Advanced Configuration
Configuration
CLI Flags
Enable the liquidator bot mode. When set, the bot monitors user accounts for liquidatable positions instead of filling orders.
Minimum collateral threshold (in USDC microunits, 6 decimals) for considering accounts for liquidation. Accounts with total collateral below this value are skipped to avoid liquidating dust positions.Default: 1,000,000 = 1 USDCExample:
--min-collateral 5000000 sets a 5 USDC minimum thresholdEnable spot liquidation strategy using Jupiter swaps. When disabled, the bot only liquidates perpetual positions.Set to
false to disable: --use-spot-liquidation falseConnect to Solana mainnet-beta. When set to false, connects to devnet. Can also be set via the
MAINNET environment variable.Base priority fee in microlamports per compute unit. Liquidations use dynamic priority fees (60th percentile) to ensure timely execution during network congestion.
Enable dry-run mode. Liquidations are simulated but not actually executed on the network. Useful for testing strategy without risking funds. Can also be set via the
DRY_RUN environment variable.Sub-account ID to use for the bot’s Drift account. Allows running multiple bots with different sub-accounts from the same wallet.
Environment Variables
Required
Base58-encoded private key for the bot’s Solana wallet. This wallet should have sufficient SOL for transaction fees and USDC for liquidation collateral.
Solana RPC endpoint URL. Use a high-performance RPC provider for best results.Example:
https://api.mainnet-beta.solana.comDrift gRPC endpoint for real-time account and slot updates.Example:
https://api.rpcpool.comAuthentication token for the gRPC endpoint.
Pyth Lazer access token for real-time price feeds. Critical for accurate margin calculations.
Optional
Port for the Prometheus metrics and dashboard HTTP server. The liquidator exposes additional dashboard data showing high-risk users and their margin status.
Monitoring
The Liquidator Bot exposes several HTTP endpoints for monitoring:http://localhost:9898/metrics- Prometheus metricshttp://localhost:9898/health- Health check endpointhttp://localhost:9898/dashboard- Human-readable dashboard showing high-risk usershttp://localhost:9898/api/dashboard- JSON API for dashboard data
Dashboard Data
The liquidator dashboard displays:- High-Risk Users: Users with free margin < 10% of margin requirement
- Margin Status: Current collateral, margin requirement, and free margin ratio
- Position Details: Perp and spot positions for each high-risk user
- Oracle Prices: Current prices for all markets
- Staleness Indicators: Flags for stale user accounts or oracle prices
Key Metrics
- Liquidations: Total number of successful liquidation transactions
- Liquidation Revenue: Profit from liquidator rewards
- High-Risk Users: Count of users approaching liquidation
- Stale Data Rejections: Count of liquidations skipped due to stale data
Performance Tuning
Risk Management
Data Staleness
The bot automatically rejects liquidations when:- User account data is >100 slots old
- Oracle prices are >50 slots old
- Pyth prices are >5 seconds old
Rate Limiting
A 5-slot rate limit prevents excessive transaction spam on the same user. If a position requires multiple liquidations, the bot waits between attempts.Compute Budget
Liquidation transactions can be complex, especially spot swaps with Jupiter. The bot includes appropriate compute budget instructions to prevent failures.Common Issues
No Liquidations Found
This is normal during stable market conditions. The bot continuously monitors all users but only acts when positions become undercollateralized.Stale Data Warnings
If you see frequent “stale data” warnings:- Verify your gRPC connection is stable
- Check that oracle accounts are updating (network issues can cause delays)
- Ensure your system clock is synchronized
Transaction Failures
If liquidation transactions fail:- Check that your wallet has sufficient collateral
- Verify you have enough SOL for transaction fees
- Review transaction logs for specific error messages
- Consider increasing priority fees during network congestion
Insufficient Funds
Liquidations may require the bot to temporarily take on positions. Ensure your Drift account has:- Sufficient USDC collateral
- Available deposits in relevant spot markets
- Margin headroom to handle position transfers
Liquidation Strategies in Detail
Perp-with-Fill Strategy
Source:src/liquidator.rs (referenced in README.md)
This strategy is used for perpetual futures positions:
- Position Analysis: Identifies the largest perp position by notional value
- Maker Discovery: Queries DLOB for makers on the opposite side
- Transaction Building: Creates
liquidate_perp_with_fillinstruction - Reward Calculation: Liquidator earns a percentage of the position’s value
Spot-with-Swap Strategy
Source:src/liquidator.rs (referenced in README.md)
This strategy is used for spot borrow positions:
- Borrow Identification: Finds spot positions with negative balance (borrows)
- Collateral Selection: Chooses largest deposit to use for repayment
- Route Discovery: Queries Jupiter for optimal swap path
- Atomic Execution: Swaps collateral and repays borrow in single transaction
Source Code Reference
The Liquidator Bot implementation can be found insrc/liquidator.rs. Key components:
- LiquidatorBot struct: Main bot structure with user tracking
- Margin Status Checking (
src/liquidator.rs:99-144): Data freshness validation - Dashboard Updates (
src/liquidator.rs:164-200): High-risk user tracking - Liquidation Strategies: Perp-with-fill and spot-with-swap implementations
- Rate Limiting (
src/liquidator.rs:46): 5-slot minimum between attempts