- Add complete Market Manager package with in-memory storage and CRUD operations - Implement arbitrage detection with profit calculations and thresholds - Add database adapter with PostgreSQL schema for persistence - Create comprehensive logging system with specialized log files - Add detailed documentation and implementation plans - Include example application and comprehensive test suite - Update Makefile with market manager build targets - Add check-implementations command for verification
1 line
4.0 KiB
Markdown
1 line
4.0 KiB
Markdown
# Market Manager\n\nThe Market Manager is a core component of the MEV bot that handles market data collection, storage, and analysis to identify arbitrage opportunities across different DEX protocols on Arbitrum.\n\n## Features\n\n- **Market Data Management**: Store and manage market data for multiple DEX pools\n- **Data Verification**: Verify market data from sequencer against on-chain data\n- **Arbitrage Detection**: Detect arbitrage opportunities between markets\n- **Persistent Storage**: Save market data to database for historical analysis\n- **In-Memory Caching**: Fast access to frequently used market data\n\n## Installation\n\n```bash\ngo get github.com/fraktal/mev-beta/pkg/marketmanager\n```\n\n## Usage\n\n### Basic Market Manager Setup\n\n```go\npackage main\n\nimport (\n \"github.com/fraktal/mev-beta/pkg/marketmanager\"\n \"time\"\n)\n\nfunc main() {\n // Create a new market manager\n config := &marketmanager.MarketManagerConfig{\n VerificationWindow: 500 * time.Millisecond,\n MaxMarkets: 1000,\n }\n \n manager := marketmanager.NewMarketManager(config)\n \n // Create and add markets\n market := marketmanager.NewMarket(\n factoryAddress,\n poolAddress,\n token0Address,\n token1Address,\n fee,\n \"TOKEN0_TOKEN1\",\n \"0x..._0x...\",\n \"UniswapV3\",\n )\n \n manager.AddMarket(market)\n}\n```\n\n### Arbitrage Detection\n\n```go\n// Create arbitrage detector\nminProfit := big.NewInt(10000000000000000) // 0.01 ETH\nminROI := 0.1 // 0.1%\ndetector := marketmanager.NewArbitrageDetector(minProfit, minROI)\n\n// Get markets and detect opportunities\nmarkets, _ := manager.GetMarketsByRawTicker(\"TOKEN0_TOKEN1\")\nopportunities := detector.DetectArbitrageOpportunities(markets)\n\nfor _, opportunity := range opportunities {\n fmt.Printf(\"Arbitrage opportunity: %f%% ROI\\n\", opportunity.ROI)\n}\n```\n\n## Core Concepts\n\n### Market Structure\n\nThe `Market` struct contains all relevant information about a DEX pool:\n\n- **Addresses**: Factory, pool, and token addresses\n- **Fee**: Pool fee in basis points\n- **Ticker**: Formatted token pair symbols\n- **RawTicker**: Formatted token pair addresses\n- **Key**: Unique identifier generated from market parameters\n- **Price Data**: Current price, liquidity, and Uniswap V3 parameters\n- **Metadata**: Status, timestamps, and protocol information\n\n### Market Storage\n\nMarkets are organized in a two-level map structure:\n\n```go\ntype Markets map[string]map[string]*Market // map[rawTicker]map[marketKey]*Market\n```\n\nThis allows efficient retrieval of markets by token pair and unique identification.\n\n### Data Verification\n\nMarket data from the sequencer is initially marked as \"possible\" and then verified against on-chain data within a configurable time window (default 500ms).\n\n### Arbitrage Detection\n\nThe arbitrage detector:\n\n1. Sorts markets by price (lowest to highest)\n2. Checks each combination for profit opportunities\n3. Calculates price impact and gas costs\n4. Validates against minimum profit and ROI thresholds\n\n## Database Integration\n\nThe market manager includes a database adapter for persistent storage:\n\n- **Market Data**: Core market information\n- **Price Data**: Timestamped price and liquidity data with versioning\n- **Arbitrage Opportunities**: Detected opportunities for analysis\n- **Market Events**: Parsed DEX events (swaps, liquidity changes)\n\n## Performance Considerations\n\n- **In-Memory Caching**: Frequently accessed markets are cached for fast retrieval\n- **Batch Operations**: Database operations are batched for efficiency\n- **Connection Pooling**: Database connections are pooled for resource efficiency\n- **Data Eviction**: Old markets are evicted when storage limits are reached\n\n## Testing\n\nThe package includes comprehensive tests for all core functionality:\n\n```bash\ngo test ./pkg/marketmanager/...\n```\n\n## Contributing\n\nContributions are welcome! Please read our contributing guidelines before submitting pull requests.\n\n## License\n\nMIT License |