feat: create v2-prep branch with comprehensive planning
Restructured project for V2 refactor: **Structure Changes:** - Moved all V1 code to orig/ folder (preserved with git mv) - Created docs/planning/ directory - Added orig/README_V1.md explaining V1 preservation **Planning Documents:** - 00_V2_MASTER_PLAN.md: Complete architecture overview - Executive summary of critical V1 issues - High-level component architecture diagrams - 5-phase implementation roadmap - Success metrics and risk mitigation - 07_TASK_BREAKDOWN.md: Atomic task breakdown - 99+ hours of detailed tasks - Every task < 2 hours (atomic) - Clear dependencies and success criteria - Organized by implementation phase **V2 Key Improvements:** - Per-exchange parsers (factory pattern) - Multi-layer strict validation - Multi-index pool cache - Background validation pipeline - Comprehensive observability **Critical Issues Addressed:** - Zero address tokens (strict validation + cache enrichment) - Parsing accuracy (protocol-specific parsers) - No audit trail (background validation channel) - Inefficient lookups (multi-index cache) - Stats disconnection (event-driven metrics) Next Steps: 1. Review planning documents 2. Begin Phase 1: Foundation (P1-001 through P1-010) 3. Implement parsers in Phase 2 4. Build cache system in Phase 3 5. Add validation pipeline in Phase 4 6. Migrate and test in Phase 5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
303
orig/pkg/market/manager.go
Normal file
303
orig/pkg/market/manager.go
Normal file
@@ -0,0 +1,303 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/holiman/uint256"
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/config"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
"github.com/fraktal/mev-beta/pkg/uniswap"
|
||||
)
|
||||
|
||||
// MarketManager manages market data and pool information
|
||||
type MarketManager struct {
|
||||
config *config.UniswapConfig
|
||||
logger *logger.Logger
|
||||
pools map[string]*PoolData
|
||||
mu sync.RWMutex
|
||||
cacheGroup singleflight.Group
|
||||
cacheDuration time.Duration
|
||||
maxCacheSize int
|
||||
}
|
||||
|
||||
// PoolData represents data for a Uniswap V3 pool
|
||||
type PoolData struct {
|
||||
Address common.Address
|
||||
Token0 common.Address
|
||||
Token1 common.Address
|
||||
Fee int64
|
||||
Liquidity *uint256.Int
|
||||
SqrtPriceX96 *uint256.Int
|
||||
Tick int
|
||||
TickSpacing int
|
||||
LastUpdated time.Time
|
||||
}
|
||||
|
||||
// NewMarketManager creates a new market manager
|
||||
func NewMarketManager(cfg *config.UniswapConfig, logger *logger.Logger) *MarketManager {
|
||||
return &MarketManager{
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
pools: make(map[string]*PoolData),
|
||||
cacheDuration: time.Duration(cfg.Cache.Expiration) * time.Second,
|
||||
maxCacheSize: cfg.Cache.MaxSize,
|
||||
}
|
||||
}
|
||||
|
||||
// GetPool retrieves pool data, either from cache or by fetching it
|
||||
func (mm *MarketManager) GetPool(ctx context.Context, poolAddress common.Address) (*PoolData, error) {
|
||||
// Check if we have it in cache and it's still valid
|
||||
poolKey := poolAddress.Hex()
|
||||
|
||||
mm.mu.RLock()
|
||||
if pool, exists := mm.pools[poolKey]; exists {
|
||||
// Check if cache is still valid
|
||||
if time.Since(pool.LastUpdated) < mm.cacheDuration {
|
||||
mm.mu.RUnlock()
|
||||
return pool, nil
|
||||
}
|
||||
}
|
||||
mm.mu.RUnlock()
|
||||
|
||||
// Use singleflight to prevent duplicate requests for the same pool
|
||||
result, err, _ := mm.cacheGroup.Do(poolKey, func() (interface{}, error) {
|
||||
return mm.fetchPoolData(ctx, poolAddress)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pool := result.(*PoolData)
|
||||
|
||||
// Update cache
|
||||
mm.mu.Lock()
|
||||
// Check if we need to evict old entries
|
||||
if len(mm.pools) >= mm.maxCacheSize {
|
||||
mm.evictOldest()
|
||||
}
|
||||
mm.pools[poolKey] = pool
|
||||
mm.mu.Unlock()
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// fetchPoolData fetches pool data from the blockchain
|
||||
func (mm *MarketManager) fetchPoolData(ctx context.Context, poolAddress common.Address) (*PoolData, error) {
|
||||
// Validate that this is not a router address before attempting pool operations
|
||||
knownRouters := map[common.Address]bool{
|
||||
common.HexToAddress("0xE592427A0AEce92De3Edee1F18E0157C05861564"): true, // Uniswap V3 Router
|
||||
common.HexToAddress("0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24"): true, // Uniswap V2 Router02
|
||||
common.HexToAddress("0xA51afAFe0263b40EdaEf0Df8781eA9aa03E381a3"): true, // Universal Router
|
||||
common.HexToAddress("0x1111111254EEB25477B68fb85Ed929f73A960582"): true, // 1inch Router v5
|
||||
common.HexToAddress("0xC36442b4a4522E871399CD717aBDD847Ab11FE88"): true, // Uniswap V3 Position Manager
|
||||
common.HexToAddress("0x87d66368cD08a7Ca42252f5ab44B2fb6d1Fb8d15"): true, // TraderJoe Router
|
||||
common.HexToAddress("0x82dfd2b94222bDB603Aa6B34A8D37311ab3DB800"): true, // Another router
|
||||
common.HexToAddress("0x1b81D678ffb9C0263b24A97847620C99d213eB14"): true, // Another router
|
||||
common.HexToAddress("0x0000000000000000000000000000000000000001"): true, // Our placeholder address
|
||||
common.HexToAddress("0x0000000000000000000000000000000000000002"): true, // Our router conflict placeholder
|
||||
}
|
||||
|
||||
if knownRouters[poolAddress] {
|
||||
return nil, fmt.Errorf("cannot fetch pool data for router address %s", poolAddress.Hex())
|
||||
}
|
||||
|
||||
// Check for addresses starting with 0xDEAD (our derived placeholders)
|
||||
if len(poolAddress.Bytes()) >= 2 && poolAddress.Bytes()[0] == 0xDE && poolAddress.Bytes()[1] == 0xAD {
|
||||
return nil, fmt.Errorf("cannot fetch pool data for placeholder address %s", poolAddress.Hex())
|
||||
}
|
||||
|
||||
// Connect to Ethereum client
|
||||
// Get RPC endpoint from config or environment
|
||||
rpcEndpoint := os.Getenv("ARBITRUM_RPC_ENDPOINT")
|
||||
if rpcEndpoint == "" {
|
||||
rpcEndpoint = "https://arbitrum-mainnet.core.chainstack.com/53c30e7a941160679fdcc396c894fc57" // fallback
|
||||
}
|
||||
client, err := ethclient.Dial(rpcEndpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to Ethereum node: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Create Uniswap V3 pool interface
|
||||
pool := uniswap.NewUniswapV3Pool(poolAddress, client)
|
||||
|
||||
// Validate that this is a real pool contract
|
||||
if !uniswap.IsValidPool(ctx, client, poolAddress) {
|
||||
return nil, fmt.Errorf("invalid pool contract at address %s", poolAddress.Hex())
|
||||
}
|
||||
|
||||
// Fetch real pool state from the blockchain
|
||||
poolState, err := pool.GetPoolState(ctx)
|
||||
if err != nil {
|
||||
mm.logger.Warn(fmt.Sprintf("Failed to fetch real pool state for %s, using fallback data: %v", poolAddress.Hex(), err))
|
||||
|
||||
// Fallback to realistic mock data with per-pool variation
|
||||
poolData := &PoolData{
|
||||
Address: poolAddress,
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
|
||||
Token1: common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), // WETH on Arbitrum
|
||||
Fee: 3000, // 0.3%
|
||||
Liquidity: uint256.NewInt(1000000000000000000), // 1 ETH equivalent
|
||||
SqrtPriceX96: uint256.NewInt(2505414483750470000), // Realistic price
|
||||
Tick: 200000, // Corresponding tick
|
||||
TickSpacing: 60, // Tick spacing for 0.3% fee
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
// Add some variation based on pool address to make different pools have different data
|
||||
addressBytes := poolAddress.Bytes()
|
||||
variation := int64(addressBytes[19]) // Use last byte for variation
|
||||
|
||||
// Vary liquidity by up to ±50%
|
||||
liquidityVariation := (variation - 128) * 5000000000000000 // ±0.05 ETH per unit
|
||||
baseLiquidity := int64(1000000000000000000)
|
||||
newLiquidityValue := baseLiquidity + liquidityVariation
|
||||
if newLiquidityValue > 0 {
|
||||
poolData.Liquidity = uint256.NewInt(uint64(newLiquidityValue))
|
||||
}
|
||||
|
||||
// Vary price slightly
|
||||
priceVariation := (variation - 128) * 10000000000000
|
||||
basePrice := int64(2505414483750470000)
|
||||
newPriceValue := basePrice + priceVariation
|
||||
if newPriceValue > 0 {
|
||||
poolData.SqrtPriceX96 = uint256.NewInt(uint64(newPriceValue))
|
||||
}
|
||||
|
||||
return poolData, nil
|
||||
}
|
||||
|
||||
// Create PoolData from real blockchain state
|
||||
poolData := &PoolData{
|
||||
Address: poolAddress,
|
||||
Token0: poolState.Token0,
|
||||
Token1: poolState.Token1,
|
||||
Fee: poolState.Fee,
|
||||
Liquidity: poolState.Liquidity,
|
||||
SqrtPriceX96: poolState.SqrtPriceX96,
|
||||
Tick: poolState.Tick,
|
||||
TickSpacing: getTickSpacing(poolState.Fee),
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
mm.logger.Debug(fmt.Sprintf("Fetched real pool data for %s: Token0=%s, Token1=%s, Fee=%d",
|
||||
poolAddress.Hex(), poolState.Token0.Hex(), poolState.Token1.Hex(), poolState.Fee))
|
||||
|
||||
return poolData, nil
|
||||
}
|
||||
|
||||
// getTickSpacing returns the tick spacing for a given fee tier
|
||||
func getTickSpacing(fee int64) int {
|
||||
switch fee {
|
||||
case 100: // 0.01%
|
||||
return 1
|
||||
case 500: // 0.05%
|
||||
return 10
|
||||
case 3000: // 0.3%
|
||||
return 60
|
||||
case 10000: // 1%
|
||||
return 200
|
||||
default:
|
||||
return 60 // Default to medium spacing
|
||||
}
|
||||
}
|
||||
|
||||
// evictOldest removes the oldest entry from the cache
|
||||
func (mm *MarketManager) evictOldest() {
|
||||
oldestKey := ""
|
||||
var oldestTime time.Time
|
||||
|
||||
for key, pool := range mm.pools {
|
||||
if oldestKey == "" || pool.LastUpdated.Before(oldestTime) {
|
||||
oldestKey = key
|
||||
oldestTime = pool.LastUpdated
|
||||
}
|
||||
}
|
||||
|
||||
if oldestKey != "" {
|
||||
delete(mm.pools, oldestKey)
|
||||
mm.logger.Debug(fmt.Sprintf("Evicted pool %s from cache", oldestKey))
|
||||
}
|
||||
}
|
||||
|
||||
// UpdatePool updates pool data
|
||||
func (mm *MarketManager) UpdatePool(poolAddress common.Address, liquidity *uint256.Int, sqrtPriceX96 *uint256.Int, tick int) {
|
||||
poolKey := poolAddress.Hex()
|
||||
|
||||
mm.mu.Lock()
|
||||
defer mm.mu.Unlock()
|
||||
|
||||
if pool, exists := mm.pools[poolKey]; exists {
|
||||
pool.Liquidity = liquidity
|
||||
pool.SqrtPriceX96 = sqrtPriceX96
|
||||
pool.Tick = tick
|
||||
pool.LastUpdated = time.Now()
|
||||
} else {
|
||||
// Create new pool entry
|
||||
pool := &PoolData{
|
||||
Address: poolAddress,
|
||||
Liquidity: liquidity,
|
||||
SqrtPriceX96: sqrtPriceX96,
|
||||
Tick: tick,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
mm.pools[poolKey] = pool
|
||||
}
|
||||
}
|
||||
|
||||
// GetPoolsByTokens retrieves pools for a pair of tokens
|
||||
func (mm *MarketManager) GetPoolsByTokens(token0, token1 common.Address) []*PoolData {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
|
||||
pools := make([]*PoolData, 0)
|
||||
|
||||
for _, pool := range mm.pools {
|
||||
// Check if this pool contains the token pair
|
||||
if (pool.Token0 == token0 && pool.Token1 == token1) ||
|
||||
(pool.Token0 == token1 && pool.Token1 == token0) {
|
||||
pools = append(pools, pool)
|
||||
}
|
||||
}
|
||||
|
||||
return pools
|
||||
}
|
||||
|
||||
// GetAllPools returns all cached pools
|
||||
func (mm *MarketManager) GetAllPools() []*PoolData {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
|
||||
pools := make([]*PoolData, 0, len(mm.pools))
|
||||
for _, pool := range mm.pools {
|
||||
pools = append(pools, pool)
|
||||
}
|
||||
|
||||
return pools
|
||||
}
|
||||
|
||||
// ClearCache clears all cached pool data
|
||||
func (mm *MarketManager) ClearCache() {
|
||||
mm.mu.Lock()
|
||||
defer mm.mu.Unlock()
|
||||
|
||||
mm.pools = make(map[string]*PoolData)
|
||||
mm.logger.Info("Cleared pool cache")
|
||||
}
|
||||
|
||||
// GetCacheStats returns cache statistics
|
||||
func (mm *MarketManager) GetCacheStats() (int, int) {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
|
||||
return len(mm.pools), mm.maxCacheSize
|
||||
}
|
||||
Reference in New Issue
Block a user