Files
mev-beta/orig/pkg/uniswap/advanced_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

83 lines
3.2 KiB
Go

package uniswap
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSqrtPriceX96ToPriceAdvanced(t *testing.T) {
// Test with sqrtPriceX96 = 2^96 (which should give price = 1.0)
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
expected := 1.0
result := SqrtPriceX96ToPriceAdvanced(sqrtPriceX96)
resultFloat, _ := result.Float64()
assert.InDelta(t, expected, resultFloat, 0.0001, "SqrtPriceX96ToPriceAdvanced should convert correctly for 2^96")
}
func TestPriceToSqrtPriceX96Advanced(t *testing.T) {
// Test with price = 1.0 (which should give sqrtPriceX96 = 2^96)
price := new(big.Float).SetFloat64(1.0)
expectedSqrtPriceX96 := new(big.Int)
expectedSqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
result := PriceToSqrtPriceX96Advanced(price)
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(expectedSqrtPriceX96, result)
diff.Abs(diff) // Get absolute value of difference
tolerance := big.NewInt(1000000000000) // Allow difference up to 1e12
assert.True(t, diff.Cmp(tolerance) < 0, "PriceToSqrtPriceX96Advanced should convert correctly for price=1.0")
}
func TestTickToSqrtPriceX96Advanced(t *testing.T) {
// Test with tick = 0 (which should give sqrtPriceX96 = 2^96)
expectedSqrtPriceX96 := new(big.Int)
expectedSqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
result := TickToSqrtPriceX96Advanced(0)
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(expectedSqrtPriceX96, result)
diff.Abs(diff) // Get absolute value of difference
tolerance := big.NewInt(1000000000000) // Allow difference up to 1e12
assert.True(t, diff.Cmp(tolerance) < 0, "TickToSqrtPriceX96Advanced should convert tick 0 correctly")
}
func TestSqrtPriceX96ToTickAdvanced(t *testing.T) {
// Test with sqrtPriceX96 = 2^96 (which should give tick = 0)
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
expected := 0
result := SqrtPriceX96ToTickAdvanced(sqrtPriceX96)
assert.Equal(t, expected, result, "SqrtPriceX96ToTickAdvanced should convert sqrtPriceX96 for price 1.0 correctly")
}
func TestAdvancedRoundTripConversions(t *testing.T) {
// Test sqrtPriceX96 -> price -> sqrtPriceX96 round trip using advanced functions
sqrtPriceX96 := new(big.Int)
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96 (price = 1.0)
price := SqrtPriceX96ToPriceAdvanced(sqrtPriceX96)
resultSqrtPriceX96 := PriceToSqrtPriceX96Advanced(price)
// Allow for small differences due to floating point precision
diff := new(big.Int).Sub(sqrtPriceX96, resultSqrtPriceX96)
diff.Abs(diff)
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "Advanced round trip conversion should be accurate")
// Test tick -> sqrtPriceX96 -> tick round trip
tick := 100000
sqrtPrice := TickToSqrtPriceX96Advanced(tick)
resultTick := SqrtPriceX96ToTickAdvanced(sqrtPrice)
// Allow for small differences due to floating point precision
assert.InDelta(t, tick, resultTick, 1, "Advanced round trip tick conversion should be accurate")
}