Keep-rs: Rust Keeper Bots for Drift Protocol
Keep-rs is a suite of high-performance keeper bots written in Rust for the Drift Protocol, a decentralized exchange on Solana. These bots provide essential infrastructure for the protocol by filling orders and liquidating undercollateralized positions.What are Keeper Bots?
Keeper bots are automated programs that perform critical functions in decentralized finance protocols:- Filler Bots match orders and provide liquidity by filling swap orders against resting limit orders
- Liquidator Bots protect the protocol by closing undercollateralized positions before they become insolvent
Filler Bot
Match swift orders and auction orders against resting liquidity
Liquidator Bot
Liquidate undercollateralized perp and spot positions
Docker Deployment
Deploy bots using Docker containers
Configuration
Configure environment variables and bot parameters
Key Features
High Performance Architecture
- Multi-threaded Design: Separate threads for event processing, transaction building, and confirmation
- Real-time Updates: gRPC subscriptions for instant account, oracle, and transaction updates
- Efficient Memory Usage: Custom allocator (mimalloc) and optimized data structures
- Batch Processing: Processes up to 32 gRPC events per batch for maximum throughput
Advanced Trading Capabilities
- DLOB Integration: Full decentralized limit order book for finding crosses and maker orders
- Swift Order Support: WebSocket streaming for off-chain order matching
- Dynamic Priority Fees: Automatic fee calculation based on network conditions (60th percentile)
- Atomic Operations: Liquidations with integrated swaps and fills
Production-Ready Monitoring
- Prometheus Metrics: Comprehensive metrics exposed on
/metricsendpoint - Health Checks: Built-in health endpoint for monitoring
- Dashboard: Web dashboard for real-time bot status at
/dashboard - Structured Logging: Detailed logging with configurable log levels
Risk Management
- Dry Run Mode: Test strategies without sending real transactions
- Rate Limiting: Prevents excessive transaction attempts (5 slot minimum between liquidations)
- Compute Unit Limits: Configurable CU limits to prevent failed transactions
- Competition Handling: Graceful handling of failed fills and partial fills
Filler Bot Overview
The perp filler bot matches swift orders and on-chain auction orders against resting liquidity on the DLOB. It continuously:- Monitors the order book for crossing opportunities
- Receives swift orders via WebSocket
- Attempts to uncross resting limit orders
- Fills auction orders when profitable
- Tracks transaction success and competition metrics
Fillers earn fees by providing liquidity and reducing spreads on the protocol. They compete with AMMs and other fillers to capture opportunities.
Event Flow
The filler processes multiple event sources simultaneously:- gRPC Slot Updates: Triggers orderbook refresh and cross detection
- gRPC Account Updates: Updates user positions and orders in the DLOB
- gRPC Transaction Updates: Confirms sent transactions and updates metrics
- Swift Order WebSocket: Receives off-chain orders for immediate matching
Liquidator Bot Overview
The liquidator bot protects the protocol by closing undercollateralized positions. It implements a sophisticated “liquidate with fill” strategy:Perp Liquidations
- Monitors all user accounts for margin violations (
total_collateral < margin_requirement) - Tracks high-risk users (free margin < 20% of requirement)
- Finds the largest perp position to liquidate
- Queries DLOB for best maker orders to match against
- Executes
liquidate_perp_with_filltransaction
Spot Liquidations
- Identifies borrow positions that need liquidation
- Uses the largest deposit as collateral
- Queries Jupiter aggregator for optimal swap routes
- Executes
liquidate_spot_with_swaptransaction with atomic swap
Margin Status Tracking
The liquidator categorizes users into three states:- Liquidatable:
total_collateral < margin_requirement- immediate liquidation attempted - High Risk:
free_margin < 20% of margin_requirement- monitored on every oracle update - Safe: All other users - checked periodically
Docker Deployment
Keep-rs includes a production-ready Dockerfile with a multi-stage build:- Downloads the latest
libdrift_ffi_sys.sofrom GitHub releases - Builds the release binary with optimizations
- Creates a minimal runtime image with only required dependencies
- Exposes port 9898 for metrics and health checks
The metrics endpoint is exposed on port 9898 by default. You can customize this with the
METRICS_PORT environment variable.Next Steps
Installation
Set up your development environment and build Keep-rs
Quick Start
Get your first bot running in minutes