Files
mev-beta/orig/pkg/uniswap/optimized_cached_test.go
Administrator 803de231ba 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>
2025-11-10 10:14:26 +01:00

80 lines
2.9 KiB
Go

package uniswap
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSqrtPriceX96ToPriceOptimizedCached(t *testing.T) {
// Test with sqrtPriceX96 = 2^96 (should give price = 1.0)
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
result := SqrtPriceX96ToPriceOptimizedCached(sqrtPriceX96)
expected := new(big.Float).SetFloat64(1.0)
resultFloat, _ := result.Float64()
expectedFloat, _ := expected.Float64()
assert.InDelta(t, expectedFloat, resultFloat, 0.0001, "Result should be close to 1.0")
}
func TestPriceToSqrtPriceX96OptimizedCached(t *testing.T) {
// Test with price = 1.0 (should give sqrtPriceX96 = 2^96)
price := new(big.Float).SetFloat64(1.0)
result := PriceToSqrtPriceX96OptimizedCached(price)
expected, _ := new(big.Int).SetString("79228162514264337593543950336", 10) // 2^96
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(expected, result)
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Result should be close to 2^96")
}
func TestTickToSqrtPriceX96OptimizedCached(t *testing.T) {
// Test with tick = 0 (should give sqrtPriceX96 = 2^96)
tick := 0
result := TickToSqrtPriceX96OptimizedCached(tick)
expected, _ := new(big.Int).SetString("79228162514264337593543950336", 10) // 2^96
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(expected, result)
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Result should be close to 2^96 for tick 0")
}
func TestSqrtPriceX96ToTickOptimizedCached(t *testing.T) {
// Test with sqrtPriceX96 = 2^96 (should give tick = 0)
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
result := SqrtPriceX96ToTickOptimizedCached(sqrtPriceX96)
expected := 0
assert.Equal(t, expected, result, "Result should be 0 for sqrtPriceX96 = 2^96")
}
func TestOptimizedCachedRoundTripConversions(t *testing.T) {
// Test sqrtPriceX96 -> price -> sqrtPriceX96 round trip with optimized cached functions
// Since these functions are aliases to the Advanced versions, this tests the same functionality
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96 (price = 1.0)
price := SqrtPriceX96ToPriceOptimizedCached(sqrtPriceX96)
resultSqrtPriceX96 := PriceToSqrtPriceX96OptimizedCached(price)
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(sqrtPriceX96, resultSqrtPriceX96)
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Round trip conversion should be accurate")
// Test tick -> sqrtPriceX96 -> tick round trip with optimized cached functions
tick := 100000
sqrtPrice := TickToSqrtPriceX96OptimizedCached(tick)
resultTick := SqrtPriceX96ToTickOptimizedCached(sqrtPrice)
// Allow for small differences due to floating point precision
assert.InDelta(t, tick, resultTick, 1, "Round trip tick conversion should be accurate")
}