Files
mev-beta/pkg/market/manager.go

201 lines
5.2 KiB
Go

package market
import (
"context"
"fmt"
"sync"
"time"
"github.com/your-username/mev-beta/internal/config"
"github.com/your-username/mev-beta/internal/logger"
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
"golang.org/x/sync/singleflight"
)
// 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) {
// This is a simplified implementation
// In practice, you would interact with the Ethereum blockchain to get real data
// For now, we'll return mock data
pool := &PoolData{
Address: poolAddress,
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), // WETH
Fee: 3000, // 0.3%
Liquidity: uint256.NewInt(1000000000000000000), // 1 ETH equivalent
SqrtPriceX96: uint256.NewInt(2505414483750470000), // Mock sqrt price
Tick: 200000, // Mock tick
TickSpacing: 60, // Tick spacing for 0.3% fee
LastUpdated: time.Now(),
}
mm.logger.Debug(fmt.Sprintf("Fetched pool data for %s", poolAddress.Hex()))
return pool, nil
}
// 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
}