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:
249
orig/pkg/arbitrum/discovery/arbitrage.go
Normal file
249
orig/pkg/arbitrum/discovery/arbitrage.go
Normal file
@@ -0,0 +1,249 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
exchangeMath "github.com/fraktal/mev-beta/pkg/math"
|
||||
)
|
||||
|
||||
// ArbitrageCalculator handles arbitrage opportunity calculations
|
||||
type ArbitrageCalculator struct {
|
||||
logger *logger.Logger
|
||||
config *ArbitrageConfig
|
||||
mathCalc *exchangeMath.MathCalculator
|
||||
}
|
||||
|
||||
// NewArbitrageCalculator creates a new arbitrage calculator
|
||||
func NewArbitrageCalculator(logger *logger.Logger, config *ArbitrageConfig, mathCalc *exchangeMath.MathCalculator) *ArbitrageCalculator {
|
||||
return &ArbitrageCalculator{
|
||||
logger: logger,
|
||||
config: config,
|
||||
mathCalc: mathCalc,
|
||||
}
|
||||
}
|
||||
|
||||
// findArbitrageOpportunities finds arbitrage opportunities across all pools
|
||||
func (ac *ArbitrageCalculator) findArbitrageOpportunities(ctx context.Context, gasPrice *big.Int, pools map[common.Address]*PoolInfoDetailed, logger *logger.Logger, config *ArbitrageConfig, mathCalc *exchangeMath.MathCalculator) []*ArbitrageOpportunityDetailed {
|
||||
opportunities := make([]*ArbitrageOpportunityDetailed, 0)
|
||||
|
||||
// Group pools by token pairs
|
||||
tokenPairPools := ac.groupPoolsByTokenPairs(pools)
|
||||
|
||||
// Check each token pair for arbitrage
|
||||
for tokenPair, pools := range tokenPairPools {
|
||||
if len(pools) < 2 {
|
||||
continue // Need at least 2 pools for arbitrage
|
||||
}
|
||||
|
||||
// Check all pool combinations
|
||||
for i := 0; i < len(pools); i++ {
|
||||
for j := i + 1; j < len(pools); j++ {
|
||||
poolA := pools[i]
|
||||
poolB := pools[j]
|
||||
|
||||
// Skip if same factory type (no arbitrage opportunity)
|
||||
if poolA.FactoryType == poolB.FactoryType {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate arbitrage
|
||||
arb := ac.calculateArbitrage(poolA, poolB, gasPrice, tokenPair, mathCalc)
|
||||
if arb != nil && arb.NetProfit.Sign() > 0 {
|
||||
opportunities = append(opportunities, arb)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by net profit (highest first)
|
||||
for i := 0; i < len(opportunities)-1; i++ {
|
||||
for j := i + 1; j < len(opportunities); j++ {
|
||||
if opportunities[i].NetProfit.Cmp(opportunities[j].NetProfit) < 0 {
|
||||
opportunities[i], opportunities[j] = opportunities[j], opportunities[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return opportunities
|
||||
}
|
||||
|
||||
// calculateArbitrage calculates arbitrage between two pools
|
||||
func (ac *ArbitrageCalculator) calculateArbitrage(poolA, poolB *PoolInfoDetailed, gasPrice *big.Int, tokenPair string, mathCalc *exchangeMath.MathCalculator) *ArbitrageOpportunityDetailed {
|
||||
// Skip pools with zero or nil reserves (uninitialized pools)
|
||||
if poolA.Reserve0 == nil || poolA.Reserve1 == nil || poolB.Reserve0 == nil || poolB.Reserve1 == nil ||
|
||||
poolA.Reserve0.Sign() <= 0 || poolA.Reserve1.Sign() <= 0 || poolB.Reserve0.Sign() <= 0 || poolB.Reserve1.Sign() <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get math calculators for each pool type
|
||||
mathA := mathCalc.GetMathForExchange(poolA.FactoryType)
|
||||
mathB := mathCalc.GetMathForExchange(poolB.FactoryType)
|
||||
|
||||
// Get spot prices
|
||||
priceA, err := mathA.GetSpotPrice(poolA.Reserve0, poolA.Reserve1)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if priceA is valid (not zero, infinity, or NaN)
|
||||
priceAFloat, _ := priceA.Float64()
|
||||
if priceA.Cmp(big.NewFloat(0)) == 0 || math.IsInf(priceAFloat, 0) || math.IsNaN(priceAFloat) {
|
||||
return nil // Invalid priceA value
|
||||
}
|
||||
|
||||
priceB, err := mathB.GetSpotPrice(poolB.Reserve0, poolB.Reserve1)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if priceB is valid (not zero, infinity, or NaN)
|
||||
priceBFloat, _ := priceB.Float64()
|
||||
if priceB.Cmp(big.NewFloat(0)) == 0 || math.IsInf(priceBFloat, 0) || math.IsNaN(priceBFloat) {
|
||||
return nil // Invalid priceB value
|
||||
}
|
||||
|
||||
// Calculate price difference
|
||||
priceDiff := new(big.Float).Sub(priceA, priceB)
|
||||
|
||||
// Additional check if priceA is infinity, NaN, or zero before division
|
||||
priceAFloatCheck, _ := priceA.Float64()
|
||||
priceBFloatCheck, _ := priceB.Float64()
|
||||
|
||||
if math.IsNaN(priceAFloatCheck) || math.IsNaN(priceBFloatCheck) ||
|
||||
math.IsInf(priceAFloatCheck, 0) || math.IsInf(priceBFloatCheck, 0) ||
|
||||
priceA.Cmp(big.NewFloat(0)) == 0 {
|
||||
return nil // Invalid price values
|
||||
}
|
||||
|
||||
// Perform the division
|
||||
priceDiff.Quo(priceDiff, priceA)
|
||||
|
||||
// Check if the result of the division is valid (not NaN or Infinity)
|
||||
priceDiffFloat, accuracy := priceDiff.Float64()
|
||||
if math.IsNaN(priceDiffFloat) || math.IsInf(priceDiffFloat, 0) || accuracy != big.Exact {
|
||||
return nil // Invalid price difference value
|
||||
}
|
||||
|
||||
// Check if price difference exceeds minimum threshold
|
||||
minThreshold, exists := ac.config.ProfitMargins["arbitrage"]
|
||||
if !exists {
|
||||
minThreshold = 0.001 // Default to 0.1% if not specified
|
||||
}
|
||||
if abs(priceDiffFloat) < minThreshold {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Calculate optimal arbitrage amount (simplified)
|
||||
amountIn := big.NewInt(100000000000000000) // 0.1 ETH test amount
|
||||
|
||||
// Calculate amounts
|
||||
amountOutA, _ := mathA.CalculateAmountOut(amountIn, poolA.Reserve0, poolA.Reserve1, poolA.Fee)
|
||||
if amountOutA == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
amountOutB, _ := mathB.CalculateAmountIn(amountOutA, poolB.Reserve1, poolB.Reserve0, poolB.Fee)
|
||||
if amountOutB == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Calculate profit
|
||||
profit := new(big.Int).Sub(amountOutB, amountIn)
|
||||
if profit.Sign() <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Calculate gas cost
|
||||
gasCost := new(big.Int).Mul(gasPrice, big.NewInt(300000)) // ~300k gas
|
||||
|
||||
// Net profit
|
||||
netProfit := new(big.Int).Sub(profit, gasCost)
|
||||
if netProfit.Sign() <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert to USD (simplified - assume ETH price)
|
||||
profitUSD := float64(netProfit.Uint64()) / 1e18 * 2000 // Assume $2000 ETH
|
||||
|
||||
if profitUSD < ac.config.MinProfitUSD {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Calculate price impacts with validation
|
||||
priceImpactA, errA := mathA.CalculatePriceImpact(amountIn, poolA.Reserve0, poolA.Reserve1)
|
||||
priceImpactB, errB := mathB.CalculatePriceImpact(amountOutA, poolB.Reserve1, poolB.Reserve0)
|
||||
|
||||
// Validate price impacts to prevent NaN or Infinity
|
||||
if errA != nil || errB != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if price impacts are valid numbers
|
||||
if math.IsNaN(priceImpactA) || math.IsInf(priceImpactA, 0) ||
|
||||
math.IsNaN(priceImpactB) || math.IsInf(priceImpactB, 0) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ArbitrageOpportunityDetailed{
|
||||
ID: fmt.Sprintf("arb_%d_%s", time.Now().Unix(), tokenPair),
|
||||
Type: "arbitrage",
|
||||
TokenIn: poolA.Token0,
|
||||
TokenOut: poolA.Token1,
|
||||
AmountIn: amountIn,
|
||||
ExpectedAmountOut: amountOutA,
|
||||
ActualAmountOut: amountOutB,
|
||||
Profit: profit,
|
||||
ProfitUSD: profitUSD,
|
||||
ProfitMargin: priceDiffFloat,
|
||||
GasCost: gasCost,
|
||||
NetProfit: netProfit,
|
||||
ExchangeA: poolA.FactoryType,
|
||||
ExchangeB: poolB.FactoryType,
|
||||
PoolA: poolA.Address,
|
||||
PoolB: poolB.Address,
|
||||
PriceImpactA: priceImpactA,
|
||||
PriceImpactB: priceImpactB,
|
||||
Confidence: 0.8,
|
||||
RiskScore: 0.3,
|
||||
ExecutionTime: time.Duration(15) * time.Second,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
func abs(x float64) float64 {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// groupPoolsByTokenPairs groups pools by token pairs
|
||||
func (ac *ArbitrageCalculator) groupPoolsByTokenPairs(pools map[common.Address]*PoolInfoDetailed) map[string][]*PoolInfoDetailed {
|
||||
groups := make(map[string][]*PoolInfoDetailed)
|
||||
|
||||
for _, pool := range pools {
|
||||
if !pool.Active {
|
||||
continue
|
||||
}
|
||||
|
||||
// Create token pair key (sorted)
|
||||
var pairKey string
|
||||
if pool.Token0.Big().Cmp(pool.Token1.Big()) < 0 {
|
||||
pairKey = fmt.Sprintf("%s-%s", pool.Token0.Hex(), pool.Token1.Hex())
|
||||
} else {
|
||||
pairKey = fmt.Sprintf("%s-%s", pool.Token1.Hex(), pool.Token0.Hex())
|
||||
}
|
||||
|
||||
groups[pairKey] = append(groups[pairKey], pool)
|
||||
}
|
||||
|
||||
return groups
|
||||
}
|
||||
851
orig/pkg/arbitrum/discovery/core.go
Normal file
851
orig/pkg/arbitrum/discovery/core.go
Normal file
@@ -0,0 +1,851 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
exchangeMath "github.com/fraktal/mev-beta/pkg/math"
|
||||
)
|
||||
|
||||
// MarketDiscovery manages pool discovery and market building
|
||||
type MarketDiscovery struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
config *MarketConfig
|
||||
mathCalc *exchangeMath.MathCalculator
|
||||
|
||||
// Market state
|
||||
pools map[common.Address]*PoolInfoDetailed
|
||||
tokens map[common.Address]*TokenInfo
|
||||
factories map[common.Address]*FactoryInfo
|
||||
routers map[common.Address]*RouterInfo
|
||||
mu sync.RWMutex
|
||||
|
||||
// Performance tracking
|
||||
poolsDiscovered uint64
|
||||
arbitrageOpps uint64
|
||||
lastScanTime time.Time
|
||||
totalScanTime time.Duration
|
||||
}
|
||||
|
||||
// MarketConfig represents the configuration for market discovery
|
||||
type MarketConfig struct {
|
||||
Version string `yaml:"version"`
|
||||
Network string `yaml:"network"`
|
||||
ChainID int64 `yaml:"chain_id"`
|
||||
Tokens map[string]*TokenConfigInfo `yaml:"tokens"`
|
||||
Factories map[string]*FactoryConfig `yaml:"factories"`
|
||||
Routers map[string]*RouterConfig `yaml:"routers"`
|
||||
PriorityPools []PriorityPoolConfig `yaml:"priority_pools"`
|
||||
MarketScan MarketScanConfig `yaml:"market_scan"`
|
||||
Arbitrage ArbitrageConfig `yaml:"arbitrage"`
|
||||
Logging LoggingConfig `yaml:"logging"`
|
||||
Risk RiskConfig `yaml:"risk"`
|
||||
Monitoring MonitoringConfig `yaml:"monitoring"`
|
||||
}
|
||||
|
||||
type TokenConfigInfo struct {
|
||||
Address string `yaml:"address"`
|
||||
Symbol string `yaml:"symbol"`
|
||||
Decimals int `yaml:"decimals"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type FactoryConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Type string `yaml:"type"`
|
||||
InitCodeHash string `yaml:"init_code_hash"`
|
||||
FeeTiers []uint32 `yaml:"fee_tiers"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type RouterConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Factory string `yaml:"factory"`
|
||||
Type string `yaml:"type"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type PriorityPoolConfig struct {
|
||||
Pool string `yaml:"pool"`
|
||||
Factory string `yaml:"factory"`
|
||||
Token0 string `yaml:"token0"`
|
||||
Token1 string `yaml:"token1"`
|
||||
Fee uint32 `yaml:"fee"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type MarketScanConfig struct {
|
||||
ScanInterval int `yaml:"scan_interval"`
|
||||
MaxPools int `yaml:"max_pools"`
|
||||
MinLiquidityUSD float64 `yaml:"min_liquidity_usd"`
|
||||
MinVolume24hUSD float64 `yaml:"min_volume_24h_usd"`
|
||||
Discovery PoolDiscoveryConfig `yaml:"discovery"`
|
||||
}
|
||||
|
||||
type PoolDiscoveryConfig struct {
|
||||
MaxBlocksBack uint64 `yaml:"max_blocks_back"`
|
||||
MinPoolAge uint64 `yaml:"min_pool_age"`
|
||||
DiscoveryInterval uint64 `yaml:"discovery_interval"`
|
||||
}
|
||||
|
||||
type ArbitrageConfig struct {
|
||||
MinProfitUSD float64 `yaml:"min_profit_usd"`
|
||||
MaxSlippage float64 `yaml:"max_slippage"`
|
||||
MaxGasPrice float64 `yaml:"max_gas_price"`
|
||||
ProfitMargins map[string]float64 `yaml:"profit_margins"`
|
||||
}
|
||||
|
||||
type LoggingConfig struct {
|
||||
Level string `yaml:"level"`
|
||||
Files map[string]string `yaml:"files"`
|
||||
RealTime map[string]interface{} `yaml:"real_time"`
|
||||
}
|
||||
|
||||
type RiskConfig struct {
|
||||
MaxPositionETH float64 `yaml:"max_position_eth"`
|
||||
MaxDailyLossETH float64 `yaml:"max_daily_loss_eth"`
|
||||
MaxConcurrentTxs int `yaml:"max_concurrent_txs"`
|
||||
CircuitBreaker map[string]interface{} `yaml:"circuit_breaker"`
|
||||
}
|
||||
|
||||
type MonitoringConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
UpdateInterval int `yaml:"update_interval"`
|
||||
Metrics []string `yaml:"metrics"`
|
||||
}
|
||||
|
||||
// PoolInfoDetailed represents detailed pool information for market discovery
|
||||
type PoolInfoDetailed struct {
|
||||
Address common.Address `json:"address"`
|
||||
Factory common.Address `json:"factory"`
|
||||
FactoryType string `json:"factory_type"`
|
||||
Token0 common.Address `json:"token0"`
|
||||
Token1 common.Address `json:"token1"`
|
||||
Fee uint32 `json:"fee"`
|
||||
Reserve0 *big.Int `json:"reserve0"`
|
||||
Reserve1 *big.Int `json:"reserve1"`
|
||||
Liquidity *big.Int `json:"liquidity"`
|
||||
SqrtPriceX96 *big.Int `json:"sqrt_price_x96,omitempty"` // For V3 pools
|
||||
Tick int32 `json:"tick,omitempty"` // For V3 pools
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
Volume24h *big.Int `json:"volume_24h"`
|
||||
Priority int `json:"priority"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type TokenInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Symbol string `json:"symbol"`
|
||||
Name string `json:"name"`
|
||||
Decimals uint8 `json:"decimals"`
|
||||
Priority int `json:"priority"`
|
||||
LastPrice *big.Int `json:"last_price"`
|
||||
Volume24h *big.Int `json:"volume_24h"`
|
||||
}
|
||||
|
||||
type FactoryInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Type string `json:"type"`
|
||||
InitCodeHash common.Hash `json:"init_code_hash"`
|
||||
FeeTiers []uint32 `json:"fee_tiers"`
|
||||
PoolCount uint64 `json:"pool_count"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
type RouterInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Factory common.Address `json:"factory"`
|
||||
Type string `json:"type"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
// MarketScanResult represents the result of a market scan
|
||||
type MarketScanResult struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
BlockNumber uint64 `json:"block_number"`
|
||||
PoolsScanned int `json:"pools_scanned"`
|
||||
NewPoolsFound int `json:"new_pools_found"`
|
||||
ArbitrageOpps []*ArbitrageOpportunityDetailed `json:"arbitrage_opportunities"`
|
||||
TopPools []*PoolInfoDetailed `json:"top_pools"`
|
||||
ScanDuration time.Duration `json:"scan_duration"`
|
||||
GasPrice *big.Int `json:"gas_price"`
|
||||
NetworkConditions map[string]interface{} `json:"network_conditions"`
|
||||
}
|
||||
|
||||
type ArbitrageOpportunityDetailed struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
TokenIn common.Address `json:"token_in"`
|
||||
TokenOut common.Address `json:"token_out"`
|
||||
AmountIn *big.Int `json:"amount_in"`
|
||||
ExpectedAmountOut *big.Int `json:"expected_amount_out"`
|
||||
ActualAmountOut *big.Int `json:"actual_amount_out"`
|
||||
Profit *big.Int `json:"profit"`
|
||||
ProfitUSD float64 `json:"profit_usd"`
|
||||
ProfitMargin float64 `json:"profit_margin"`
|
||||
GasCost *big.Int `json:"gas_cost"`
|
||||
NetProfit *big.Int `json:"net_profit"`
|
||||
ExchangeA string `json:"exchange_a"`
|
||||
ExchangeB string `json:"exchange_b"`
|
||||
PoolA common.Address `json:"pool_a"`
|
||||
PoolB common.Address `json:"pool_b"`
|
||||
PriceA float64 `json:"price_a"`
|
||||
PriceB float64 `json:"price_b"`
|
||||
PriceImpactA float64 `json:"price_impact_a"`
|
||||
PriceImpactB float64 `json:"price_impact_b"`
|
||||
CapitalRequired float64 `json:"capital_required"`
|
||||
GasCostUSD float64 `json:"gas_cost_usd"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
RiskScore float64 `json:"risk_score"`
|
||||
ExecutionTime time.Duration `json:"execution_time"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// PoolDiscoveryResult represents pool discovery results
|
||||
type PoolDiscoveryResult struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
FromBlock uint64 `json:"from_block"`
|
||||
ToBlock uint64 `json:"to_block"`
|
||||
NewPools []*PoolInfoDetailed `json:"new_pools"`
|
||||
PoolsFound int `json:"pools_found"`
|
||||
ScanDuration time.Duration `json:"scan_duration"`
|
||||
}
|
||||
|
||||
// NewMarketDiscovery creates a new market discovery instance
|
||||
func NewMarketDiscovery(client *ethclient.Client, logger *logger.Logger, configPath string) (*MarketDiscovery, error) {
|
||||
// Load configuration
|
||||
config, err := LoadMarketConfig(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
|
||||
// Initialize math calculator
|
||||
mathCalc := exchangeMath.NewMathCalculator()
|
||||
|
||||
md := &MarketDiscovery{
|
||||
client: client,
|
||||
logger: logger,
|
||||
config: config,
|
||||
mathCalc: mathCalc,
|
||||
pools: make(map[common.Address]*PoolInfoDetailed),
|
||||
tokens: make(map[common.Address]*TokenInfo),
|
||||
factories: make(map[common.Address]*FactoryInfo),
|
||||
routers: make(map[common.Address]*RouterInfo),
|
||||
}
|
||||
|
||||
// Load initial configuration
|
||||
if err := md.loadInitialMarkets(); err != nil {
|
||||
return nil, fmt.Errorf("failed to load initial markets: %w", err)
|
||||
}
|
||||
|
||||
logger.Info("Market discovery initialized with comprehensive pool detection")
|
||||
return md, nil
|
||||
}
|
||||
|
||||
// LoadMarketConfig loads market configuration from YAML file
|
||||
func LoadMarketConfig(configPath string) (*MarketConfig, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var config MarketConfig
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// loadInitialMarkets loads initial tokens, factories, and priority pools
|
||||
func (md *MarketDiscovery) loadInitialMarkets() error {
|
||||
md.mu.Lock()
|
||||
defer md.mu.Unlock()
|
||||
|
||||
// Load tokens
|
||||
for _, token := range md.config.Tokens {
|
||||
tokenAddr := common.HexToAddress(token.Address)
|
||||
md.tokens[tokenAddr] = &TokenInfo{
|
||||
Address: tokenAddr,
|
||||
Symbol: token.Symbol,
|
||||
Decimals: uint8(token.Decimals),
|
||||
Priority: token.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load factories
|
||||
for _, factory := range md.config.Factories {
|
||||
factoryAddr := common.HexToAddress(factory.Address)
|
||||
md.factories[factoryAddr] = &FactoryInfo{
|
||||
Address: factoryAddr,
|
||||
Type: factory.Type,
|
||||
InitCodeHash: common.HexToHash(factory.InitCodeHash),
|
||||
FeeTiers: factory.FeeTiers,
|
||||
Priority: factory.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load routers
|
||||
for _, router := range md.config.Routers {
|
||||
routerAddr := common.HexToAddress(router.Address)
|
||||
factoryAddr := common.Address{}
|
||||
if router.Factory != "" {
|
||||
for _, f := range md.config.Factories {
|
||||
if f.Type == router.Factory {
|
||||
factoryAddr = common.HexToAddress(f.Address)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
md.routers[routerAddr] = &RouterInfo{
|
||||
Address: routerAddr,
|
||||
Factory: factoryAddr,
|
||||
Type: router.Type,
|
||||
Priority: router.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load priority pools
|
||||
for _, poolConfig := range md.config.PriorityPools {
|
||||
poolAddr := common.HexToAddress(poolConfig.Pool)
|
||||
token0 := common.HexToAddress(poolConfig.Token0)
|
||||
token1 := common.HexToAddress(poolConfig.Token1)
|
||||
|
||||
// Find factory
|
||||
var factoryAddr common.Address
|
||||
var factoryType string
|
||||
for _, f := range md.config.Factories {
|
||||
if f.Type == poolConfig.Factory {
|
||||
factoryAddr = common.HexToAddress(f.Address)
|
||||
factoryType = f.Type
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
pool := &PoolInfoDetailed{
|
||||
Address: poolAddr,
|
||||
Factory: factoryAddr,
|
||||
FactoryType: factoryType,
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Fee: poolConfig.Fee,
|
||||
Priority: poolConfig.Priority,
|
||||
Active: true,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
md.pools[poolAddr] = pool
|
||||
}
|
||||
|
||||
md.logger.Info(fmt.Sprintf("Loaded initial markets: %d tokens, %d factories, %d routers, %d priority pools",
|
||||
len(md.tokens), len(md.factories), len(md.routers), len(md.pools)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildComprehensiveMarkets builds markets for all exchanges and top token pairs
|
||||
func (md *MarketDiscovery) buildComprehensiveMarkets() error {
|
||||
md.logger.Info("🏗️ Building comprehensive markets for all exchanges and top tokens")
|
||||
|
||||
// Get top tokens (sorted by priority)
|
||||
topTokens := md.getTopTokens(10) // Reduced from 20 to 10 tokens to reduce load
|
||||
md.logger.Info(fmt.Sprintf("💼 Found %d top tokens for market building", len(topTokens)))
|
||||
|
||||
// Build markets for each factory
|
||||
marketsBuilt := 0
|
||||
for factoryAddr, factoryInfo := range md.factories {
|
||||
markets, err := md.buildFactoryMarkets(factoryAddr, factoryInfo, topTokens)
|
||||
if err != nil {
|
||||
md.logger.Error(fmt.Sprintf("Failed to build markets for factory %s: %v", factoryAddr.Hex(), err))
|
||||
continue
|
||||
}
|
||||
|
||||
marketsBuilt += len(markets)
|
||||
md.logger.Info(fmt.Sprintf("✅ Built %d markets for %s factory", len(markets), factoryInfo.Type))
|
||||
}
|
||||
|
||||
md.logger.Info(fmt.Sprintf("📊 Total markets built: %d", marketsBuilt))
|
||||
|
||||
// Log available markets
|
||||
md.logAvailableMarkets()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getTopTokens returns the top N tokens sorted by priority
|
||||
func (md *MarketDiscovery) getTopTokens(limit int) []*TokenInfo {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
|
||||
// Convert map to slice
|
||||
tokens := make([]*TokenInfo, 0, len(md.tokens))
|
||||
for _, token := range md.tokens {
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
|
||||
// Sort by priority (highest first)
|
||||
for i := 0; i < len(tokens)-1; i++ {
|
||||
for j := i + 1; j < len(tokens); j++ {
|
||||
if tokens[i].Priority < tokens[j].Priority {
|
||||
tokens[i], tokens[j] = tokens[j], tokens[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Limit to top N (reduced for performance)
|
||||
limit = 10 // Reduced from 20 to 10 to reduce load
|
||||
if len(tokens) > limit {
|
||||
tokens = tokens[:limit]
|
||||
}
|
||||
|
||||
return tokens
|
||||
}
|
||||
|
||||
// buildFactoryMarkets builds markets for a specific factory and token pairs
|
||||
func (md *MarketDiscovery) buildFactoryMarkets(factoryAddr common.Address, factoryInfo *FactoryInfo, tokens []*TokenInfo) ([]*PoolInfoDetailed, error) {
|
||||
var markets []*PoolInfoDetailed
|
||||
|
||||
// Find WETH token (most important for pairing)
|
||||
var wethToken *TokenInfo
|
||||
for _, token := range tokens {
|
||||
if token.Symbol == "WETH" {
|
||||
wethToken = token
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If no WETH found, use the highest priority token
|
||||
if wethToken == nil && len(tokens) > 0 {
|
||||
wethToken = tokens[0]
|
||||
}
|
||||
|
||||
// Build markets for each token pair
|
||||
for i, tokenA := range tokens {
|
||||
for j := i + 1; j < len(tokens); j++ {
|
||||
tokenB := tokens[j]
|
||||
|
||||
// Build markets for this token pair
|
||||
pairMarkets, err := md.buildTokenPairMarkets(factoryAddr, factoryInfo, tokenA, tokenB)
|
||||
if err != nil {
|
||||
md.logger.Debug(fmt.Sprintf("Failed to build markets for %s-%s pair: %v", tokenA.Symbol, tokenB.Symbol, err))
|
||||
continue
|
||||
}
|
||||
|
||||
markets = append(markets, pairMarkets...)
|
||||
}
|
||||
|
||||
// Also build markets for token-WETH pairs if WETH exists and is not this token
|
||||
if wethToken != nil && tokenA.Address != wethToken.Address {
|
||||
wethMarkets, err := md.buildTokenPairMarkets(factoryAddr, factoryInfo, tokenA, wethToken)
|
||||
if err != nil {
|
||||
md.logger.Debug(fmt.Sprintf("Failed to build markets for %s-WETH pair: %v", tokenA.Symbol, err))
|
||||
continue
|
||||
}
|
||||
|
||||
markets = append(markets, wethMarkets...)
|
||||
}
|
||||
}
|
||||
|
||||
// Add built markets to tracking
|
||||
md.mu.Lock()
|
||||
for _, market := range markets {
|
||||
// Only add if not already tracking
|
||||
if _, exists := md.pools[market.Address]; !exists {
|
||||
md.pools[market.Address] = market
|
||||
}
|
||||
}
|
||||
md.mu.Unlock()
|
||||
|
||||
return markets, nil
|
||||
}
|
||||
|
||||
// buildTokenPairMarkets builds markets for a specific token pair and factory
|
||||
func (md *MarketDiscovery) buildTokenPairMarkets(factoryAddr common.Address, factoryInfo *FactoryInfo, tokenA, tokenB *TokenInfo) ([]*PoolInfoDetailed, error) {
|
||||
var markets []*PoolInfoDetailed
|
||||
|
||||
// For factories with fee tiers (Uniswap V3 style), build markets for each fee tier
|
||||
if len(factoryInfo.FeeTiers) > 0 {
|
||||
// Build markets for each fee tier
|
||||
for _, feeTier := range factoryInfo.FeeTiers {
|
||||
// Generate deterministic pool address using CREATE2
|
||||
poolAddr, err := md.calculatePoolAddress(factoryAddr, factoryInfo, tokenA, tokenB, feeTier)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
market := &PoolInfoDetailed{
|
||||
Address: poolAddr,
|
||||
Factory: factoryAddr,
|
||||
FactoryType: factoryInfo.Type,
|
||||
Token0: tokenA.Address,
|
||||
Token1: tokenB.Address,
|
||||
Fee: feeTier,
|
||||
Reserve0: big.NewInt(0),
|
||||
Reserve1: big.NewInt(0),
|
||||
Liquidity: big.NewInt(0),
|
||||
SqrtPriceX96: big.NewInt(0),
|
||||
Tick: 0,
|
||||
LastUpdated: time.Now(),
|
||||
Volume24h: big.NewInt(0),
|
||||
Priority: (tokenA.Priority + tokenB.Priority) / 2,
|
||||
Active: true,
|
||||
}
|
||||
|
||||
markets = append(markets, market)
|
||||
}
|
||||
} else {
|
||||
// For factories without fee tiers (Uniswap V2 style), build a single market
|
||||
// Generate deterministic pool address using CREATE2
|
||||
poolAddr, err := md.calculatePoolAddress(factoryAddr, factoryInfo, tokenA, tokenB, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
market := &PoolInfoDetailed{
|
||||
Address: poolAddr,
|
||||
Factory: factoryAddr,
|
||||
FactoryType: factoryInfo.Type,
|
||||
Token0: tokenA.Address,
|
||||
Token1: tokenB.Address,
|
||||
Reserve0: big.NewInt(0),
|
||||
Reserve1: big.NewInt(0),
|
||||
Liquidity: big.NewInt(0),
|
||||
LastUpdated: time.Now(),
|
||||
Volume24h: big.NewInt(0),
|
||||
Priority: (tokenA.Priority + tokenB.Priority) / 2,
|
||||
Active: true,
|
||||
}
|
||||
|
||||
markets = append(markets, market)
|
||||
}
|
||||
|
||||
return markets, nil
|
||||
}
|
||||
|
||||
// calculatePoolAddress calculates the deterministic pool address using CREATE2
|
||||
func (md *MarketDiscovery) calculatePoolAddress(factoryAddr common.Address, factoryInfo *FactoryInfo, tokenA, tokenB *TokenInfo, feeTier uint32) (common.Address, error) {
|
||||
// Sort tokens to ensure consistent ordering
|
||||
token0, token1 := tokenA.Address, tokenB.Address
|
||||
if token0.Big().Cmp(token1.Big()) > 0 {
|
||||
token0, token1 = token1, token0
|
||||
}
|
||||
|
||||
switch factoryInfo.Type {
|
||||
case "uniswap_v3", "camelot_v3", "algebra":
|
||||
// For Uniswap V3 style factories with fee tiers
|
||||
return md.calculateUniswapV3PoolAddress(factoryAddr, factoryInfo, token0, token1, feeTier)
|
||||
case "uniswap_v2", "sushiswap":
|
||||
// For Uniswap V2 style factories
|
||||
return md.calculateUniswapV2PoolAddress(factoryAddr, factoryInfo, token0, token1)
|
||||
case "balancer_v2":
|
||||
// For Balancer (simplified - in practice would need more info)
|
||||
return md.calculateBalancerPoolAddress(factoryAddr, token0, token1)
|
||||
case "curve":
|
||||
// For Curve (simplified - in practice would need more info)
|
||||
return md.calculateCurvePoolAddress(factoryAddr, token0, token1)
|
||||
default:
|
||||
// Generic CREATE2 calculation
|
||||
return md.calculateGenericPoolAddress(factoryAddr, factoryInfo, token0, token1, feeTier)
|
||||
}
|
||||
}
|
||||
|
||||
// calculateUniswapV3PoolAddress calculates pool address for Uniswap V3 style factories
|
||||
func (md *MarketDiscovery) calculateUniswapV3PoolAddress(factoryAddr common.Address, factoryInfo *FactoryInfo, token0, token1 common.Address, feeTier uint32) (common.Address, error) {
|
||||
// Encode the pool key: keccak256(abi.encode(token0, token1, fee))
|
||||
poolKey := crypto.Keccak256(append(append(token0.Bytes(), token1.Bytes()...), big.NewInt(int64(feeTier)).Bytes()...))
|
||||
|
||||
// Calculate CREATE2 address
|
||||
// keccak256(0xff ++ address ++ salt ++ keccak256(init_code))[12:]
|
||||
salt := poolKey
|
||||
initCodeHash := factoryInfo.InitCodeHash.Bytes()
|
||||
|
||||
create2Input := append([]byte{0xff}, factoryAddr.Bytes()...)
|
||||
create2Input = append(create2Input, salt...)
|
||||
create2Input = append(create2Input, initCodeHash...)
|
||||
|
||||
poolAddrBytes := crypto.Keccak256(create2Input)
|
||||
|
||||
// Take last 20 bytes for address
|
||||
poolAddr := common.BytesToAddress(poolAddrBytes[12:])
|
||||
|
||||
return poolAddr, nil
|
||||
}
|
||||
|
||||
// calculateUniswapV2PoolAddress calculates pool address for Uniswap V2 style factories
|
||||
func (md *MarketDiscovery) calculateUniswapV2PoolAddress(factoryAddr common.Address, factoryInfo *FactoryInfo, token0, token1 common.Address) (common.Address, error) {
|
||||
// For Uniswap V2: keccak256(0xff ++ address ++ keccak256(token0 ++ token1) ++ initcode_hash)[12:]
|
||||
poolKey := crypto.Keccak256(append(token0.Bytes(), token1.Bytes()...))
|
||||
|
||||
create2Input := append([]byte{0xff}, factoryAddr.Bytes()...)
|
||||
create2Input = append(create2Input, poolKey...)
|
||||
create2Input = append(create2Input, factoryInfo.InitCodeHash.Bytes()...)
|
||||
|
||||
poolAddrBytes := crypto.Keccak256(create2Input)
|
||||
|
||||
// Take last 20 bytes for address
|
||||
poolAddr := common.BytesToAddress(poolAddrBytes[12:])
|
||||
|
||||
return poolAddr, nil
|
||||
}
|
||||
|
||||
// calculateBalancerPoolAddress calculates pool address for Balancer pools (simplified)
|
||||
func (md *MarketDiscovery) calculateBalancerPoolAddress(factoryAddr, token0, token1 common.Address) (common.Address, error) {
|
||||
// Simplified implementation - in practice would need more complex logic
|
||||
// For Balancer V2, pool addresses are typically determined by the vault
|
||||
// This is a placeholder implementation
|
||||
placeholder := crypto.Keccak256(append(append(factoryAddr.Bytes(), token0.Bytes()...), token1.Bytes()...))
|
||||
return common.BytesToAddress(placeholder[12:]), nil
|
||||
}
|
||||
|
||||
// calculateCurvePoolAddress calculates pool address for Curve pools (simplified)
|
||||
func (md *MarketDiscovery) calculateCurvePoolAddress(factoryAddr, token0, token1 common.Address) (common.Address, error) {
|
||||
// Simplified implementation - Curve pools are typically deployed via factories
|
||||
// with more complex logic. This is a placeholder implementation
|
||||
placeholder := crypto.Keccak256(append(append(factoryAddr.Bytes(), token0.Bytes()...), token1.Bytes()...))
|
||||
return common.BytesToAddress(placeholder[12:]), nil
|
||||
}
|
||||
|
||||
// calculateGenericPoolAddress calculates pool address for generic factories
|
||||
func (md *MarketDiscovery) calculateGenericPoolAddress(factoryAddr common.Address, factoryInfo *FactoryInfo, token0, token1 common.Address, feeTier uint32) (common.Address, error) {
|
||||
// Generic CREATE2 calculation using tokens and fee as salt
|
||||
saltInput := append(append(token0.Bytes(), token1.Bytes()...), big.NewInt(int64(feeTier)).Bytes()...)
|
||||
salt := crypto.Keccak256(saltInput)
|
||||
|
||||
create2Input := append([]byte{0xff}, factoryAddr.Bytes()...)
|
||||
create2Input = append(create2Input, salt...)
|
||||
create2Input = append(create2Input, factoryInfo.InitCodeHash.Bytes()...)
|
||||
|
||||
poolAddrBytes := crypto.Keccak256(create2Input)
|
||||
|
||||
// Take last 20 bytes for address
|
||||
poolAddr := common.BytesToAddress(poolAddrBytes[12:])
|
||||
|
||||
return poolAddr, nil
|
||||
}
|
||||
|
||||
// logAvailableMarkets logs all available markets grouped by exchange
|
||||
func (md *MarketDiscovery) logAvailableMarkets() {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
|
||||
// Group markets by factory type
|
||||
marketsByFactory := make(map[string][]*PoolInfoDetailed)
|
||||
for _, pool := range md.pools {
|
||||
factoryType := pool.FactoryType
|
||||
marketsByFactory[factoryType] = append(marketsByFactory[factoryType], pool)
|
||||
}
|
||||
|
||||
// Log markets for each factory
|
||||
md.logger.Info("📈 Available Markets by Exchange:")
|
||||
for factoryType, pools := range marketsByFactory {
|
||||
// Count unique token pairs
|
||||
tokenPairs := make(map[string]bool)
|
||||
for _, pool := range pools {
|
||||
// Handle empty addresses to prevent slice bounds panic
|
||||
token0Display := "unknown"
|
||||
token1Display := "unknown"
|
||||
if len(pool.Token0.Hex()) > 0 {
|
||||
if len(pool.Token0.Hex()) > 6 {
|
||||
token0Display = pool.Token0.Hex()[:6]
|
||||
} else {
|
||||
token0Display = pool.Token0.Hex()
|
||||
}
|
||||
}
|
||||
if len(pool.Token1.Hex()) > 0 {
|
||||
if len(pool.Token1.Hex()) > 6 {
|
||||
token1Display = pool.Token1.Hex()[:6]
|
||||
} else {
|
||||
token1Display = pool.Token1.Hex()
|
||||
}
|
||||
}
|
||||
pairKey := fmt.Sprintf("%s-%s", token0Display, token1Display)
|
||||
tokenPairs[pairKey] = true
|
||||
}
|
||||
|
||||
md.logger.Info(fmt.Sprintf(" %s: %d pools, %d unique token pairs",
|
||||
factoryType, len(pools), len(tokenPairs)))
|
||||
|
||||
// Log top 5 pools by priority
|
||||
for i, pool := range pools {
|
||||
if i >= 5 {
|
||||
break
|
||||
}
|
||||
md.logger.Debug(fmt.Sprintf(" 🏦 Pool %s (%s-%s, Fee: %d)",
|
||||
pool.Address.Hex()[:10],
|
||||
pool.Token0.Hex()[:6],
|
||||
pool.Token1.Hex()[:6],
|
||||
pool.Fee))
|
||||
}
|
||||
|
||||
if len(pools) > 5 {
|
||||
md.logger.Debug(fmt.Sprintf(" ... and %d more pools", len(pools)-5))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DiscoverPools discovers pools from factories within a block range
|
||||
func (md *MarketDiscovery) DiscoverPools(ctx context.Context, fromBlock, toBlock uint64) (*PoolDiscoveryResult, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
discovered := &PoolDiscoveryResult{
|
||||
Timestamp: startTime,
|
||||
FromBlock: fromBlock,
|
||||
ToBlock: toBlock,
|
||||
NewPools: make([]*PoolInfoDetailed, 0),
|
||||
}
|
||||
|
||||
// Discover pools from each factory
|
||||
for factoryAddr, factoryInfo := range md.factories {
|
||||
pools, err := md.discoverPoolsFromFactory(ctx, factoryAddr, factoryInfo, fromBlock, toBlock)
|
||||
if err != nil {
|
||||
md.logger.Error(fmt.Sprintf("Failed to discover pools from factory %s: %v", factoryAddr.Hex(), err))
|
||||
continue
|
||||
}
|
||||
|
||||
discovered.NewPools = append(discovered.NewPools, pools...)
|
||||
}
|
||||
|
||||
discovered.PoolsFound = len(discovered.NewPools)
|
||||
discovered.ScanDuration = time.Since(startTime)
|
||||
|
||||
md.poolsDiscovered += uint64(discovered.PoolsFound)
|
||||
return discovered, nil
|
||||
}
|
||||
|
||||
// ScanForArbitrage scans all pools for arbitrage opportunities
|
||||
func (md *MarketDiscovery) ScanForArbitrage(ctx context.Context, blockNumber uint64) (*MarketScanResult, error) {
|
||||
startTime := time.Now()
|
||||
md.lastScanTime = startTime
|
||||
|
||||
result := &MarketScanResult{
|
||||
Timestamp: startTime,
|
||||
BlockNumber: blockNumber,
|
||||
ArbitrageOpps: make([]*ArbitrageOpportunityDetailed, 0),
|
||||
TopPools: make([]*PoolInfoDetailed, 0),
|
||||
NetworkConditions: make(map[string]interface{}),
|
||||
}
|
||||
|
||||
// Update pool states
|
||||
if err := md.updatePoolStates(ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to update pool states: %w", err)
|
||||
}
|
||||
|
||||
// Get current gas price
|
||||
gasPrice, err := md.client.SuggestGasPrice(ctx)
|
||||
if err != nil {
|
||||
gasPrice = big.NewInt(5000000000) // 5 gwei fallback
|
||||
}
|
||||
result.GasPrice = gasPrice
|
||||
|
||||
// Scan for arbitrage opportunities
|
||||
opportunities := md.findArbitrageOpportunities(ctx, gasPrice)
|
||||
result.ArbitrageOpps = opportunities
|
||||
result.PoolsScanned = len(md.pools)
|
||||
|
||||
// Get top pools by liquidity
|
||||
result.TopPools = md.getTopPoolsByLiquidity(10)
|
||||
|
||||
result.ScanDuration = time.Since(startTime)
|
||||
md.totalScanTime += result.ScanDuration
|
||||
|
||||
md.arbitrageOpps += uint64(len(opportunities))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetStatistics returns market discovery statistics
|
||||
func (md *MarketDiscovery) GetStatistics() map[string]interface{} {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
|
||||
return map[string]interface{}{
|
||||
"pools_tracked": len(md.pools),
|
||||
"tokens_tracked": len(md.tokens),
|
||||
"factories_tracked": len(md.factories),
|
||||
"pools_discovered": md.poolsDiscovered,
|
||||
"arbitrage_opportunities": md.arbitrageOpps,
|
||||
"last_scan_time": md.lastScanTime,
|
||||
"total_scan_time": md.totalScanTime.String(),
|
||||
}
|
||||
}
|
||||
|
||||
// BuildComprehensiveMarkets builds comprehensive markets for all exchanges and top tokens
|
||||
// This should be called after initialization is complete to avoid deadlocks
|
||||
func (md *MarketDiscovery) BuildComprehensiveMarkets() error {
|
||||
return md.buildComprehensiveMarkets()
|
||||
}
|
||||
|
||||
// getTopPoolsByLiquidity returns top pools sorted by liquidity
|
||||
func (md *MarketDiscovery) getTopPoolsByLiquidity(limit int) []*PoolInfoDetailed {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
|
||||
pools := make([]*PoolInfoDetailed, 0, len(md.pools))
|
||||
for _, pool := range md.pools {
|
||||
if pool.Active && pool.Liquidity != nil {
|
||||
pools = append(pools, pool)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by liquidity (highest first)
|
||||
for i := 0; i < len(pools)-1; i++ {
|
||||
for j := i + 1; j < len(pools); j++ {
|
||||
if pools[i].Liquidity.Cmp(pools[j].Liquidity) < 0 {
|
||||
pools[i], pools[j] = pools[j], pools[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(pools) > limit {
|
||||
pools = pools[:limit]
|
||||
}
|
||||
|
||||
return pools
|
||||
}
|
||||
|
||||
// findArbitrageOpportunities finds arbitrage opportunities across all pools
|
||||
func (md *MarketDiscovery) findArbitrageOpportunities(ctx context.Context, gasPrice *big.Int) []*ArbitrageOpportunityDetailed {
|
||||
// Create arbitrage calculator
|
||||
calculator := NewArbitrageCalculator(md.logger, &md.config.Arbitrage, md.mathCalc)
|
||||
|
||||
md.mu.RLock()
|
||||
pools := make(map[common.Address]*PoolInfoDetailed, len(md.pools))
|
||||
for addr, pool := range md.pools {
|
||||
pools[addr] = pool
|
||||
}
|
||||
md.mu.RUnlock()
|
||||
|
||||
return calculator.findArbitrageOpportunities(ctx, gasPrice, pools, md.logger, &md.config.Arbitrage, md.mathCalc)
|
||||
}
|
||||
|
||||
// updatePoolStates updates the state of all tracked pools
|
||||
func (md *MarketDiscovery) updatePoolStates(ctx context.Context) error {
|
||||
// Create pool state manager
|
||||
manager := NewPoolStateManager(md.client, md.logger)
|
||||
|
||||
md.mu.Lock()
|
||||
pools := make(map[common.Address]*PoolInfoDetailed, len(md.pools))
|
||||
for addr, pool := range md.pools {
|
||||
pools[addr] = pool
|
||||
}
|
||||
md.mu.Unlock()
|
||||
|
||||
return manager.updatePoolStates(ctx, pools, &md.mu, md.logger)
|
||||
}
|
||||
|
||||
// discoverPoolsFromFactory discovers pools from a specific factory
|
||||
func (md *MarketDiscovery) discoverPoolsFromFactory(ctx context.Context, factoryAddr common.Address, factoryInfo *FactoryInfo, fromBlock, toBlock uint64) ([]*PoolInfoDetailed, error) {
|
||||
// Implementation would query factory events for pool creation
|
||||
return []*PoolInfoDetailed{}, nil
|
||||
}
|
||||
281
orig/pkg/arbitrum/discovery/pool_state.go
Normal file
281
orig/pkg/arbitrum/discovery/pool_state.go
Normal file
@@ -0,0 +1,281 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
)
|
||||
|
||||
// PoolStateManager handles the management of pool states
|
||||
type PoolStateManager struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
// NewPoolStateManager creates a new pool state manager
|
||||
func NewPoolStateManager(client *ethclient.Client, logger *logger.Logger) *PoolStateManager {
|
||||
return &PoolStateManager{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// updatePoolStates updates the state of all tracked pools
|
||||
func (psm *PoolStateManager) updatePoolStates(ctx context.Context, pools map[common.Address]*PoolInfoDetailed, mu sync.Locker, logger *logger.Logger) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
logger.Info("🔄 Updating pool states for all tracked pools")
|
||||
|
||||
updatedCount := 0
|
||||
errorCount := 0
|
||||
|
||||
// Update state for each pool
|
||||
for _, pool := range pools {
|
||||
// Skip inactive pools
|
||||
if !pool.Active {
|
||||
continue
|
||||
}
|
||||
|
||||
// Update pool state based on protocol type
|
||||
switch pool.FactoryType {
|
||||
case "uniswap_v2", "sushiswap", "camelot_v2":
|
||||
if err := psm.updateUniswapV2PoolState(ctx, pool); err != nil {
|
||||
logger.Debug(fmt.Sprintf("Failed to update Uniswap V2 pool %s: %v", pool.Address.Hex(), err))
|
||||
errorCount++
|
||||
continue
|
||||
}
|
||||
case "uniswap_v3", "camelot_v3", "algebra":
|
||||
if err := psm.updateUniswapV3PoolState(ctx, pool); err != nil {
|
||||
logger.Debug(fmt.Sprintf("Failed to update Uniswap V3 pool %s: %v", pool.Address.Hex(), err))
|
||||
errorCount++
|
||||
continue
|
||||
}
|
||||
case "balancer_v2":
|
||||
if err := psm.updateBalancerPoolState(ctx, pool); err != nil {
|
||||
logger.Debug(fmt.Sprintf("Failed to update Balancer pool %s: %v", pool.Address.Hex(), err))
|
||||
errorCount++
|
||||
continue
|
||||
}
|
||||
case "curve":
|
||||
if err := psm.updateCurvePoolState(ctx, pool); err != nil {
|
||||
logger.Debug(fmt.Sprintf("Failed to update Curve pool %s: %v", pool.Address.Hex(), err))
|
||||
errorCount++
|
||||
continue
|
||||
}
|
||||
default:
|
||||
// For unknown protocols, skip updating state
|
||||
logger.Debug(fmt.Sprintf("Skipping state update for unknown protocol pool %s (%s)", pool.Address.Hex(), pool.FactoryType))
|
||||
continue
|
||||
}
|
||||
|
||||
updatedCount++
|
||||
pool.LastUpdated = time.Now()
|
||||
}
|
||||
|
||||
logger.Info(fmt.Sprintf("✅ Updated %d pool states, %d errors", updatedCount, errorCount))
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateUniswapV2PoolState updates the state of a Uniswap V2 style pool
|
||||
func (psm *PoolStateManager) updateUniswapV2PoolState(ctx context.Context, pool *PoolInfoDetailed) error {
|
||||
// Generate a deterministic reserve value based on pool address for testing
|
||||
// In a real implementation, you'd make an actual contract call
|
||||
|
||||
// Use last 8 bytes of address to generate deterministic reserves
|
||||
poolAddrBytes := pool.Address.Bytes()
|
||||
reserveSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
reserveSeed = (reserveSeed << 8) | uint64(poolAddrBytes[len(poolAddrBytes)-1-i])
|
||||
}
|
||||
|
||||
// Generate deterministic reserves (in token units, scaled appropriately)
|
||||
reserve0 := big.NewInt(int64(reserveSeed % 1000000000000000000)) // 0-1 ETH equivalent
|
||||
reserve1 := big.NewInt(int64((reserveSeed >> 32) % 1000000000000000000))
|
||||
|
||||
// Scale reserves appropriately (assume token decimals)
|
||||
// This is a simplified approach - in reality you'd look up token decimals
|
||||
reserve0.Mul(reserve0, big.NewInt(1000000000000)) // Scale by 10^12
|
||||
reserve1.Mul(reserve1, big.NewInt(1000000000000)) // Scale by 10^12
|
||||
|
||||
pool.Reserve0 = reserve0
|
||||
pool.Reserve1 = reserve1
|
||||
|
||||
pool.Liquidity = big.NewInt(0).Add(reserve0, reserve1) // Simplified liquidity
|
||||
|
||||
// Update 24h volume (simulated)
|
||||
volumeSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
volumeSeed = (volumeSeed << 8) | uint64(poolAddrBytes[i])
|
||||
}
|
||||
pool.Volume24h = big.NewInt(int64(volumeSeed % 10000000000000000000)) // 0-10 ETH equivalent
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateUniswapV3PoolState updates the state of a Uniswap V3 style pool
|
||||
func (psm *PoolStateManager) updateUniswapV3PoolState(ctx context.Context, pool *PoolInfoDetailed) error {
|
||||
// For Uniswap V3, we need to get slot0 data and liquidity
|
||||
// Since we can't make the actual contract calls without bindings, we'll use deterministic generation
|
||||
|
||||
poolAddrBytes := pool.Address.Bytes()
|
||||
|
||||
// Generate deterministic slot0-like values
|
||||
sqrtPriceSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
sqrtPriceSeed = (sqrtPriceSeed << 8) | uint64(poolAddrBytes[len(poolAddrBytes)-1-i])
|
||||
}
|
||||
|
||||
// Generate sqrtPriceX96 (should be 96-bit fixed point number)
|
||||
// For simplicity, we'll use a value that represents a reasonable price
|
||||
sqrtPriceX96 := big.NewInt(int64(sqrtPriceSeed % 1000000000000000000))
|
||||
sqrtPriceX96.Mul(sqrtPriceX96, big.NewInt(10000000000000000)) // Scale appropriately
|
||||
|
||||
liquiditySeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
liquiditySeed = (liquiditySeed << 8) | uint64(poolAddrBytes[i])
|
||||
}
|
||||
|
||||
liquidity := big.NewInt(int64(liquiditySeed % 1000000000000000000)) // Larger liquidity values
|
||||
liquidity.Mul(liquidity, big.NewInt(100)) // Scale up to simulate larger liquidity
|
||||
|
||||
pool.SqrtPriceX96 = sqrtPriceX96
|
||||
pool.Liquidity = liquidity
|
||||
|
||||
// Generate reserves from sqrtPrice and liquidity (simplified)
|
||||
// In reality, you'd derive reserves from actual contract state
|
||||
reserve0 := big.NewInt(0).Div(liquidity, big.NewInt(1000000)) // Simplified calculation
|
||||
reserve1 := big.NewInt(0).Mul(liquidity, big.NewInt(1000)) // Simplified calculation
|
||||
|
||||
pool.Reserve0 = reserve0
|
||||
pool.Reserve1 = reserve1
|
||||
|
||||
// Update 24h volume (simulated)
|
||||
volumeSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
volumeSeed = (volumeSeed << 8) | uint64(poolAddrBytes[(i+4)%len(poolAddrBytes)])
|
||||
}
|
||||
// Use big.Int to avoid overflow
|
||||
var volumeBig *big.Int
|
||||
if volumeSeed > math.MaxInt64 {
|
||||
volumeBig = big.NewInt(math.MaxInt64)
|
||||
} else {
|
||||
volumeBig = big.NewInt(int64(volumeSeed))
|
||||
}
|
||||
volumeBig.Mod(volumeBig, big.NewInt(1000000000000000000)) // Mod by 1 ETH
|
||||
volumeBig.Mul(volumeBig, big.NewInt(100)) // Scale to 100 ETH max
|
||||
pool.Volume24h = volumeBig
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateBalancerPoolState updates the state of a Balancer pool
|
||||
func (psm *PoolStateManager) updateBalancerPoolState(ctx context.Context, pool *PoolInfoDetailed) error {
|
||||
// Simplified Balancer pool state update
|
||||
poolAddrBytes := pool.Address.Bytes()
|
||||
|
||||
// Generate deterministic reserves for Balancer pools
|
||||
reserve0 := big.NewInt(0)
|
||||
reserve1 := big.NewInt(0)
|
||||
|
||||
for i := 0; i < len(poolAddrBytes) && i < 8; i++ {
|
||||
reserve0.Add(reserve0, big.NewInt(int64(poolAddrBytes[i])<<uint(i*8)))
|
||||
}
|
||||
|
||||
for i := 0; i < len(poolAddrBytes) && i < 8; i++ {
|
||||
reserve1.Add(reserve1, big.NewInt(int64(poolAddrBytes[(i+8)%len(poolAddrBytes)])<<uint(i*8)))
|
||||
}
|
||||
|
||||
// Scale appropriately
|
||||
reserve0.Div(reserve0, big.NewInt(1000000000000000)) // Scale down
|
||||
reserve1.Div(reserve1, big.NewInt(1000000000000000)) // Scale down
|
||||
|
||||
pool.Reserve0 = reserve0
|
||||
pool.Reserve1 = reserve1
|
||||
|
||||
pool.Liquidity = big.NewInt(0).Add(reserve0, reserve1)
|
||||
|
||||
// Update 24h volume (simulated)
|
||||
volumeSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
volumeSeed = (volumeSeed << 8) | uint64(poolAddrBytes[(i*2)%len(poolAddrBytes)])
|
||||
}
|
||||
// Use big.Int to avoid overflow
|
||||
var volumeBig *big.Int
|
||||
if volumeSeed > math.MaxInt64 {
|
||||
volumeBig = big.NewInt(math.MaxInt64)
|
||||
} else {
|
||||
volumeBig = big.NewInt(int64(volumeSeed))
|
||||
}
|
||||
volumeBig.Mod(volumeBig, big.NewInt(1000000000000000000)) // Mod by 1 ETH
|
||||
volumeBig.Mul(volumeBig, big.NewInt(50)) // Scale to 50 ETH max
|
||||
pool.Volume24h = volumeBig
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateCurvePoolState updates the state of a Curve pool
|
||||
func (psm *PoolStateManager) updateCurvePoolState(ctx context.Context, pool *PoolInfoDetailed) error {
|
||||
// Simplified Curve pool state update
|
||||
poolAddrBytes := pool.Address.Bytes()
|
||||
|
||||
// Generate deterministic reserves for Curve pools (typically stablecoin pools)
|
||||
reserve0 := big.NewInt(1000000000000000000) // 1 unit (scaled)
|
||||
reserve1 := big.NewInt(1000000000000000000) // 1 unit (scaled)
|
||||
|
||||
// Adjust based on address for variety
|
||||
addrModifier := uint64(0)
|
||||
for i := 0; i < 4 && i < len(poolAddrBytes); i++ {
|
||||
addrModifier += uint64(poolAddrBytes[i])
|
||||
}
|
||||
|
||||
// Convert uint64 to int64 safely
|
||||
modValue := addrModifier % 1000000
|
||||
var reserveMultiplier *big.Int
|
||||
if modValue > math.MaxInt64 {
|
||||
reserveMultiplier = big.NewInt(math.MaxInt64)
|
||||
} else {
|
||||
reserveMultiplier = big.NewInt(int64(modValue))
|
||||
}
|
||||
reserve0.Mul(reserve0, reserveMultiplier)
|
||||
// Convert uint64 to int64 safely for reserve multiplier
|
||||
multiplierValue := (addrModifier * 2) % 1000000
|
||||
var reserve1Multiplier *big.Int
|
||||
if multiplierValue > math.MaxInt64 {
|
||||
reserve1Multiplier = big.NewInt(math.MaxInt64)
|
||||
} else {
|
||||
reserve1Multiplier = big.NewInt(int64(multiplierValue))
|
||||
}
|
||||
reserve1.Mul(reserve1, reserve1Multiplier)
|
||||
|
||||
pool.Reserve0 = reserve0
|
||||
pool.Reserve1 = reserve1
|
||||
|
||||
pool.Liquidity = big.NewInt(0).Add(reserve0, reserve1)
|
||||
|
||||
// Update 24h volume (simulated)
|
||||
volumeSeed := uint64(0)
|
||||
for i := 0; i < 8 && i < len(poolAddrBytes); i++ {
|
||||
volumeSeed = (volumeSeed << 8) | uint64(poolAddrBytes[(i*3)%len(poolAddrBytes)])
|
||||
}
|
||||
// Use big.Int to avoid overflow
|
||||
var volumeBig *big.Int
|
||||
if volumeSeed > math.MaxInt64 {
|
||||
volumeBig = big.NewInt(math.MaxInt64)
|
||||
} else {
|
||||
volumeBig = big.NewInt(int64(volumeSeed))
|
||||
}
|
||||
volumeBig.Mod(volumeBig, big.NewInt(1000000000000000000)) // Mod by 1 ETH
|
||||
volumeBig.Mul(volumeBig, big.NewInt(20)) // Scale to 20 ETH max
|
||||
pool.Volume24h = volumeBig
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user