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:
Administrator
2025-11-10 10:14:26 +01:00
parent 1773daffe7
commit 803de231ba
411 changed files with 20390 additions and 8680 deletions

View File

@@ -0,0 +1,336 @@
package common
import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// Protocol represents supported DEX protocols
type Protocol string
const (
ProtocolUniswapV2 Protocol = "UniswapV2"
ProtocolUniswapV3 Protocol = "UniswapV3"
ProtocolUniswapV4 Protocol = "UniswapV4"
ProtocolCamelotV2 Protocol = "CamelotV2"
ProtocolCamelotV3 Protocol = "CamelotV3"
ProtocolTraderJoeV1 Protocol = "TraderJoeV1"
ProtocolTraderJoeV2 Protocol = "TraderJoeV2"
ProtocolTraderJoeLB Protocol = "TraderJoeLB"
ProtocolCurve Protocol = "Curve"
ProtocolCurveStable Protocol = "CurveStableSwap"
ProtocolCurveCrypto Protocol = "CurveCryptoSwap"
ProtocolCurveTri Protocol = "CurveTricrypto"
ProtocolKyberClassic Protocol = "KyberClassic"
ProtocolKyberElastic Protocol = "KyberElastic"
ProtocolBalancerV2 Protocol = "BalancerV2"
ProtocolBalancerV3 Protocol = "BalancerV3"
ProtocolBalancerV4 Protocol = "BalancerV4"
ProtocolSushiSwapV2 Protocol = "SushiSwapV2"
ProtocolSushiSwapV3 Protocol = "SushiSwapV3"
ProtocolGMX Protocol = "GMX"
ProtocolRadiant Protocol = "Radiant"
ProtocolRamses Protocol = "Ramses"
ProtocolChronos Protocol = "Chronos"
Protocol1Inch Protocol = "1Inch"
ProtocolParaSwap Protocol = "ParaSwap"
Protocol0x Protocol = "0x"
)
// PoolType represents different pool types across protocols
type PoolType string
const (
PoolTypeConstantProduct PoolType = "ConstantProduct" // Uniswap V2 style
PoolTypeConcentrated PoolType = "ConcentratedLiq" // Uniswap V3 style
PoolTypeStableSwap PoolType = "StableSwap" // Curve style
PoolTypeWeighted PoolType = "WeightedPool" // Balancer style
PoolTypeLiquidityBook PoolType = "LiquidityBook" // TraderJoe LB
PoolTypeComposable PoolType = "ComposableStable" // Balancer Composable
PoolTypeDynamic PoolType = "DynamicFee" // Kyber style
PoolTypeGMX PoolType = "GMXPool" // GMX perpetual pools
PoolTypeAlgebraic PoolType = "AlgebraicAMM" // Camelot Algebra
)
// EventType represents different types of DEX events
type EventType string
const (
EventTypeSwap EventType = "Swap"
EventTypeLiquidityAdd EventType = "LiquidityAdd"
EventTypeLiquidityRemove EventType = "LiquidityRemove"
EventTypePoolCreated EventType = "PoolCreated"
EventTypeFeeCollection EventType = "FeeCollection"
EventTypePositionUpdate EventType = "PositionUpdate"
EventTypeFlashLoan EventType = "FlashLoan"
EventTypeMulticall EventType = "Multicall"
EventTypeAggregatorSwap EventType = "AggregatorSwap"
EventTypeBatchSwap EventType = "BatchSwap"
)
// EnhancedDEXEvent represents a comprehensive DEX event with all relevant data
type EnhancedDEXEvent struct {
// Transaction Info
TxHash common.Hash `json:"tx_hash"`
BlockNumber uint64 `json:"block_number"`
BlockHash common.Hash `json:"block_hash"`
TxIndex uint64 `json:"tx_index"`
LogIndex uint64 `json:"log_index"`
From common.Address `json:"from"`
To common.Address `json:"to"`
GasUsed uint64 `json:"gas_used"`
GasPrice *big.Int `json:"gas_price"`
Timestamp time.Time `json:"timestamp"`
// Protocol Info
Protocol Protocol `json:"protocol"`
ProtocolVersion string `json:"protocol_version"`
EventType EventType `json:"event_type"`
ContractAddress common.Address `json:"contract_address"`
FactoryAddress common.Address `json:"factory_address,omitempty"`
RouterAddress common.Address `json:"router_address,omitempty"`
Factory common.Address `json:"factory,omitempty"`
Router common.Address `json:"router,omitempty"`
Sender common.Address `json:"sender,omitempty"`
// Pool Info
PoolAddress common.Address `json:"pool_address"`
PoolType PoolType `json:"pool_type"`
PoolFee uint32 `json:"pool_fee,omitempty"`
PoolTick *big.Int `json:"pool_tick,omitempty"`
SqrtPriceX96 *big.Int `json:"sqrt_price_x96,omitempty"`
Liquidity *big.Int `json:"liquidity,omitempty"`
// Token Info
TokenIn common.Address `json:"token_in"`
TokenOut common.Address `json:"token_out"`
Token0 common.Address `json:"token0,omitempty"`
Token1 common.Address `json:"token1,omitempty"`
TokenInSymbol string `json:"token_in_symbol,omitempty"`
TokenOutSymbol string `json:"token_out_symbol,omitempty"`
TokenInName string `json:"token_in_name,omitempty"`
TokenOutName string `json:"token_out_name,omitempty"`
Token0Symbol string `json:"token0_symbol,omitempty"`
Token1Symbol string `json:"token1_symbol,omitempty"`
TokenInDecimals uint8 `json:"token_in_decimals,omitempty"`
TokenOutDecimals uint8 `json:"token_out_decimals,omitempty"`
Token0Decimals uint8 `json:"token0_decimals,omitempty"`
Token1Decimals uint8 `json:"token1_decimals,omitempty"`
TokenInRiskScore float64 `json:"token_in_risk_score,omitempty"`
TokenOutRiskScore float64 `json:"token_out_risk_score,omitempty"`
// Swap Details
AmountIn *big.Int `json:"amount_in,omitempty"`
AmountOut *big.Int `json:"amount_out,omitempty"`
AmountInUSD float64 `json:"amount_in_usd,omitempty"`
AmountOutUSD float64 `json:"amount_out_usd,omitempty"`
Amount0USD float64 `json:"amount0_usd,omitempty"`
Amount1USD float64 `json:"amount1_usd,omitempty"`
PriceImpact float64 `json:"price_impact,omitempty"`
SlippageBps uint64 `json:"slippage_bps,omitempty"`
EffectivePrice *big.Int `json:"effective_price,omitempty"`
// Liquidity Details (for liquidity events)
LiquidityAmount *big.Int `json:"liquidity_amount,omitempty"`
Amount0 *big.Int `json:"amount0,omitempty"`
Amount1 *big.Int `json:"amount1,omitempty"`
TickLower *big.Int `json:"tick_lower,omitempty"`
TickUpper *big.Int `json:"tick_upper,omitempty"`
PositionId *big.Int `json:"position_id,omitempty"`
// Fee Details
Fee *big.Int `json:"fee,omitempty"`
FeeBps uint64 `json:"fee_bps,omitempty"`
FeeUSD float64 `json:"fee_usd,omitempty"`
FeeTier uint32 `json:"fee_tier,omitempty"`
FeeGrowthGlobal0 *big.Int `json:"fee_growth_global0,omitempty"`
FeeGrowthGlobal1 *big.Int `json:"fee_growth_global1,omitempty"`
// Aggregator Details (for DEX aggregators)
AggregatorSource string `json:"aggregator_source,omitempty"`
RouteHops []RouteHop `json:"route_hops,omitempty"`
MinAmountOut *big.Int `json:"min_amount_out,omitempty"`
Deadline uint64 `json:"deadline,omitempty"`
Recipient common.Address `json:"recipient,omitempty"`
// MEV Details
IsMEV bool `json:"is_mev"`
MEVType string `json:"mev_type,omitempty"`
ProfitUSD float64 `json:"profit_usd,omitempty"`
IsArbitrage bool `json:"is_arbitrage"`
IsSandwich bool `json:"is_sandwich"`
IsLiquidation bool `json:"is_liquidation"`
BundleHash common.Hash `json:"bundle_hash,omitempty"`
// Raw Data
RawLogData []byte `json:"raw_log_data"`
RawTopics []common.Hash `json:"raw_topics"`
DecodedParams map[string]interface{} `json:"decoded_params,omitempty"`
// Validation
IsValid bool `json:"is_valid"`
ValidationErrors []string `json:"validation_errors,omitempty"`
}
// RouteHop represents a hop in a multi-hop swap route
type RouteHop struct {
Protocol Protocol `json:"protocol"`
PoolAddress common.Address `json:"pool_address"`
TokenIn common.Address `json:"token_in"`
TokenOut common.Address `json:"token_out"`
AmountIn *big.Int `json:"amount_in"`
AmountOut *big.Int `json:"amount_out"`
Fee uint32 `json:"fee"`
HopIndex uint8 `json:"hop_index"`
}
// ContractInfo represents information about a DEX contract
type ContractInfo struct {
Address common.Address `json:"address"`
Name string `json:"name"`
Protocol Protocol `json:"protocol"`
Version string `json:"version"`
ContractType ContractType `json:"contract_type"`
IsActive bool `json:"is_active"`
DeployedBlock uint64 `json:"deployed_block"`
FactoryAddress common.Address `json:"factory_address,omitempty"`
Implementation common.Address `json:"implementation,omitempty"`
LastUpdated time.Time `json:"last_updated"`
}
// ContractType represents different types of DEX contracts
type ContractType string
const (
ContractTypeRouter ContractType = "Router"
ContractTypeFactory ContractType = "Factory"
ContractTypePool ContractType = "Pool"
ContractTypeManager ContractType = "Manager"
ContractTypeVault ContractType = "Vault"
ContractTypeAggregator ContractType = "Aggregator"
ContractTypeMulticall ContractType = "Multicall"
)
// PoolInfo represents comprehensive pool information
type PoolInfo struct {
Address common.Address `json:"address"`
Protocol Protocol `json:"protocol"`
PoolType PoolType `json:"pool_type"`
FactoryAddress common.Address `json:"factory_address"`
Token0 common.Address `json:"token0"`
Token1 common.Address `json:"token1"`
Token0Symbol string `json:"token0_symbol"`
Token1Symbol string `json:"token1_symbol"`
Token0Decimals uint8 `json:"token0_decimals"`
Token1Decimals uint8 `json:"token1_decimals"`
Fee uint32 `json:"fee"`
TickSpacing uint32 `json:"tick_spacing,omitempty"`
CreatedBlock uint64 `json:"created_block"`
CreatedTx common.Hash `json:"created_tx"`
TotalLiquidity *big.Int `json:"total_liquidity"`
Reserve0 *big.Int `json:"reserve0,omitempty"`
Reserve1 *big.Int `json:"reserve1,omitempty"`
SqrtPriceX96 *big.Int `json:"sqrt_price_x96,omitempty"`
CurrentTick *big.Int `json:"current_tick,omitempty"`
IsActive bool `json:"is_active"`
LastUpdated time.Time `json:"last_updated"`
TxCount24h uint64 `json:"tx_count_24h"`
Volume24hUSD float64 `json:"volume_24h_usd"`
TVL float64 `json:"tvl_usd"`
}
// FunctionSignature represents a function signature with protocol-specific metadata
type FunctionSignature struct {
Selector [4]byte `json:"selector"`
Name string `json:"name"`
Protocol Protocol `json:"protocol"`
ContractType ContractType `json:"contract_type"`
EventType EventType `json:"event_type"`
Description string `json:"description"`
ABI string `json:"abi,omitempty"`
IsDeprecated bool `json:"is_deprecated"`
RequiredParams []string `json:"required_params"`
OptionalParams []string `json:"optional_params"`
}
// EventSignature represents an event signature with protocol-specific metadata
type EventSignature struct {
Topic0 common.Hash `json:"topic0"`
Name string `json:"name"`
Protocol Protocol `json:"protocol"`
EventType EventType `json:"event_type"`
Description string `json:"description"`
ABI string `json:"abi,omitempty"`
IsIndexed []bool `json:"is_indexed"`
RequiredTopics uint8 `json:"required_topics"`
}
// DEXProtocolConfig represents configuration for a specific DEX protocol
type DEXProtocolConfig struct {
Protocol Protocol `json:"protocol"`
Version string `json:"version"`
IsActive bool `json:"is_active"`
Contracts map[ContractType][]common.Address `json:"contracts"`
Functions map[string]FunctionSignature `json:"functions"`
Events map[string]EventSignature `json:"events"`
PoolTypes []PoolType `json:"pool_types"`
DefaultFeeTiers []uint32 `json:"default_fee_tiers"`
MinLiquidityUSD float64 `json:"min_liquidity_usd"`
MaxSlippageBps uint64 `json:"max_slippage_bps"`
}
// DEXParserInterface defines the interface for protocol-specific parsers
type DEXParserInterface interface {
// Protocol identification
GetProtocol() Protocol
GetSupportedEventTypes() []EventType
GetSupportedContractTypes() []ContractType
// Contract recognition
IsKnownContract(address common.Address) bool
GetContractInfo(address common.Address) (*ContractInfo, error)
// Event parsing
ParseTransactionLogs(tx *types.Transaction, receipt *types.Receipt) ([]*EnhancedDEXEvent, error)
ParseLog(log *types.Log) (*EnhancedDEXEvent, error)
// Function parsing
ParseTransactionData(tx *types.Transaction) (*EnhancedDEXEvent, error)
DecodeFunctionCall(data []byte) (*EnhancedDEXEvent, error)
// Pool discovery
DiscoverPools(fromBlock, toBlock uint64) ([]*PoolInfo, error)
GetPoolInfo(poolAddress common.Address) (*PoolInfo, error)
// Validation
ValidateEvent(event *EnhancedDEXEvent) error
EnrichEventData(event *EnhancedDEXEvent) error
}
// ParseResult represents the result of parsing a transaction or log
type ParseResult struct {
Events []*EnhancedDEXEvent `json:"events"`
NewPools []*PoolInfo `json:"new_pools"`
ParsedContracts []*ContractInfo `json:"parsed_contracts"`
TotalGasUsed uint64 `json:"total_gas_used"`
ProcessingTimeMs uint64 `json:"processing_time_ms"`
Errors []error `json:"errors,omitempty"`
IsSuccessful bool `json:"is_successful"`
}
// ParserMetrics represents metrics for parser performance tracking
type ParserMetrics struct {
TotalTransactionsParsed uint64 `json:"total_transactions_parsed"`
TotalEventsParsed uint64 `json:"total_events_parsed"`
TotalPoolsDiscovered uint64 `json:"total_pools_discovered"`
ParseErrorCount uint64 `json:"parse_error_count"`
AvgProcessingTimeMs float64 `json:"avg_processing_time_ms"`
ProtocolBreakdown map[Protocol]uint64 `json:"protocol_breakdown"`
EventTypeBreakdown map[EventType]uint64 `json:"event_type_breakdown"`
LastProcessedBlock uint64 `json:"last_processed_block"`
StartTime time.Time `json:"start_time"`
LastUpdated time.Time `json:"last_updated"`
}