Files
mev-beta/docs/architecture.md
2025-09-14 06:21:10 -05:00

103 lines
3.9 KiB
Markdown

# MEV Bot Architecture
This document describes the high-level architecture of the MEV bot.
## Components
### 1. Arbitrum Monitor
Responsible for monitoring the Arbitrum sequencer for L2 messages and new blocks to identify potential opportunities.
Key responsibilities:
- Connect to Arbitrum RPC endpoint
- Monitor new blocks as they are added to the sequencer
- Parse L2 messages to identify potential swap transactions
- Extract transaction details from both blocks and L2 messages
- Process transactions with minimal latency (every 250ms)
### 2. Event Parser
Analyzes transactions to detect DEX interactions and extract relevant event data.
Key responsibilities:
- Identify DEX contract interactions (Uniswap V2/V3, SushiSwap)
- Parse transaction data for swap events
- Extract token addresses, amounts, and pricing information
- Convert raw blockchain data into structured events
### 3. Market Pipeline
Processes transactions through multiple stages for comprehensive analysis.
Key responsibilities:
- Multi-stage transaction processing
- Concurrent processing with worker pools
- Transaction decoding and event parsing
- Market analysis using Uniswap V3 math
- Arbitrage opportunity detection
### 4. Market Scanner
Analyzes potential swap transactions to determine if they create arbitrage opportunities.
Key responsibilities:
- Calculate price impact of swaps using Uniswap V3 mathematics
- Scan for arbitrage opportunities across multiple pools
- Estimate profitability after gas costs
- Filter opportunities based on configured thresholds
- Use worker pools for concurrent processing
### 5. Uniswap Pricing
Handles all Uniswap V3 pricing calculations with precision.
Key responsibilities:
- Convert between sqrtPriceX96 and ticks
- Calculate price impact of swaps
- Work with liquidity values for precise calculations
- Implement Uniswap V3 mathematical formulas
- Handle fixed-point arithmetic without floating-point operations
### 6. Transaction Executor
Responsible for executing profitable arbitrage transactions.
Key responsibilities:
- Construct arbitrage transactions
- Optimize gas usage for maximum profitability
- Submit transactions to flashbots or similar services
- Handle transaction confirmation and error recovery
## Data Flow
1. Arbitrum Monitor reads L2 messages and new blocks from the sequencer
2. Event Parser identifies DEX interactions and extracts event data
3. Market Pipeline processes transactions through multiple analysis stages
4. Market Scanner analyzes swaps and identifies arbitrage opportunities
5. Profitable opportunities are sent to Transaction Executor
6. Transaction Executor constructs and submits arbitrage transactions
## Configuration
The bot is configured through config/config.yaml which allows customization of:
- Arbitrum RPC endpoints and WebSocket connections
- Polling intervals for block processing (as low as 250ms)
- Profit thresholds for opportunity filtering
- Gas price multipliers for transaction optimization
- Logging settings for debugging and monitoring
## L2 Message Processing
The MEV bot specifically focuses on reading and parsing Arbitrum sequencer L2 messages to find potential opportunities:
### L2 Message Monitoring
- Connects to both RPC and WebSocket endpoints for redundancy
- Subscribes to real-time L2 message feeds
- Processes messages with minimal delay for competitive advantage
- Implements fallback mechanisms for connection stability
### Message Parsing
- Identifies pool swaps and router swaps from L2 messages
- Extracts transaction parameters before they are included in blocks
- Calculates potential price impact from message data
- Prioritizes high-value opportunities for immediate analysis
### Real-time Processing
- Implements 250ms processing intervals for rapid opportunity detection
- Uses WebSocket connections for real-time data feeds
- Maintains low-latency processing for competitive MEV extraction
- Balances between processing speed and accuracy