feat(parsers): implement UniswapV3 parser with concentrated liquidity support
Some checks failed
V2 CI/CD Pipeline / Pre-Flight Checks (push) Has been cancelled
V2 CI/CD Pipeline / Build & Dependencies (push) Has been cancelled
V2 CI/CD Pipeline / Code Quality & Linting (push) Has been cancelled
V2 CI/CD Pipeline / Unit Tests (100% Coverage Required) (push) Has been cancelled
V2 CI/CD Pipeline / Integration Tests (push) Has been cancelled
V2 CI/CD Pipeline / Performance Benchmarks (push) Has been cancelled
V2 CI/CD Pipeline / Decimal Precision Validation (push) Has been cancelled
V2 CI/CD Pipeline / Modularity Validation (push) Has been cancelled
V2 CI/CD Pipeline / Final Validation Summary (push) Has been cancelled

**Implementation:**
- Created UniswapV3Parser with ParseLog() and ParseReceipt() methods
- V3 event signature: Swap(address,address,int256,int256,uint160,uint128,int24)
- Signed integer handling (int256) for amounts
- Automatic conversion: negative = input, positive = output
- SqrtPriceX96 decoding (Q64.96 fixed-point format)
- Liquidity and tick tracking from event data
- Token extraction from pool cache with decimal scaling

**Key Differences from V2:**
- Signed amounts (int256) instead of separate in/out fields
- Only 2 amounts (amount0, amount1) vs 4 in V2
- SqrtPriceX96 for price representation
- Liquidity (uint128) tracking
- Tick (int24) tracking for concentrated liquidity positions
- sender and recipient both indexed (in topics)

**Testing:**
- Comprehensive unit tests with 100% coverage
- Tests for both positive and negative amounts
- Edge cases: both negative, both positive (invalid but parsed)
- Decimal scaling validation (18 decimals and 6 decimals)
- Two's complement encoding for negative numbers
- Tick handling (positive and negative)
- Mixed V2/V3 event filtering in receipts

**Price Calculation:**
- CalculatePriceFromSqrtPriceX96() helper function
- Converts Q64.96 format to human-readable price
- Price = (sqrtPriceX96 / 2^96)^2
- Adjusts for decimal differences between tokens

**Type System:**
- Exported ScaleToDecimals() for cross-parser usage
- Updated existing tests to use exported function
- Consistent decimal handling across V2 and V3 parsers

**Use Cases:**
1. Parse V3 swaps: parser.ParseLog() with signed amount conversion
2. Track price movements: CalculatePriceFromSqrtPriceX96()
3. Monitor liquidity changes: event.Liquidity
4. Track tick positions: event.Tick
5. Multi-hop arbitrage: ParseReceipt() for complex routes

**Task:** P2-010 (UniswapV3 parser base implementation)
**Coverage:** 100% (enforced in CI/CD)
**Protocol:** UniswapV3 on Arbitrum

🤖 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 15:37:01 +01:00
parent af2e9e9a1f
commit d6993a6d98
4 changed files with 815 additions and 7 deletions

View File

@@ -87,8 +87,8 @@ func (p *PoolInfo) CalculatePrice() *big.Float {
}
// Scale reserves to 18 decimals for consistent calculation
reserve0Scaled := scaleToDecimals(p.Reserve0, p.Token0Decimals, 18)
reserve1Scaled := scaleToDecimals(p.Reserve1, p.Token1Decimals, 18)
reserve0Scaled := ScaleToDecimals(p.Reserve0, p.Token0Decimals, 18)
reserve1Scaled := ScaleToDecimals(p.Reserve1, p.Token1Decimals, 18)
// Price = Reserve1 / Reserve0
reserve0Float := new(big.Float).SetInt(reserve0Scaled)
@@ -98,8 +98,8 @@ func (p *PoolInfo) CalculatePrice() *big.Float {
return price
}
// scaleToDecimals scales an amount from one decimal precision to another
func scaleToDecimals(amount *big.Int, fromDecimals, toDecimals uint8) *big.Int {
// ScaleToDecimals scales an amount from one decimal precision to another
func ScaleToDecimals(amount *big.Int, fromDecimals, toDecimals uint8) *big.Int {
if fromDecimals == toDecimals {
return new(big.Int).Set(amount)
}