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>
128 lines
4.8 KiB
Go
128 lines
4.8 KiB
Go
package math
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/holiman/uint256"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Test that cached functions produce the same results as original implementations
|
|
func TestCachedFunctionAccuracy(t *testing.T) {
|
|
// Test SqrtPriceX96ToPrice functions
|
|
t.Run("SqrtPriceX96ToPrice", func(t *testing.T) {
|
|
// Use a typical sqrtPriceX96 value (represents price of ~2000 USDC/ETH)
|
|
sqrtPriceX96 := new(big.Int).SetBytes([]byte{0x06, 0x40, 0x84, 0x4A, 0x0E, 0x81, 0x4F, 0x96, 0x19, 0xC1, 0x9C, 0x08})
|
|
|
|
// Original calculation
|
|
sqrtPriceFloat := new(big.Float).SetInt(sqrtPriceX96)
|
|
originalPrice := new(big.Float).Mul(sqrtPriceFloat, sqrtPriceFloat)
|
|
q192 := new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
|
q192Float := new(big.Float).SetInt(q192)
|
|
originalPrice.Quo(originalPrice, q192Float)
|
|
|
|
// Cached calculation
|
|
cachedPrice := SqrtPriceX96ToPriceCached(sqrtPriceX96)
|
|
|
|
// Compare results (should be identical)
|
|
assert.Equal(t, originalPrice.String(), cachedPrice.String(), "Cached and original SqrtPriceX96ToPrice should produce identical results")
|
|
})
|
|
|
|
// Test PriceToSqrtPriceX96 functions
|
|
t.Run("PriceToSqrtPriceX96", func(t *testing.T) {
|
|
// Use a typical price value (represents price of ~2000 USDC/ETH)
|
|
price := new(big.Float).SetFloat64(2000.0)
|
|
|
|
// Original calculation
|
|
q192 := new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
|
q192Float := new(big.Float).SetInt(q192)
|
|
result := new(big.Float).Mul(price, q192Float)
|
|
result.Sqrt(result)
|
|
expectedSqrtPriceX96 := new(big.Int)
|
|
result.Int(expectedSqrtPriceX96)
|
|
|
|
// Cached calculation
|
|
actualSqrtPriceX96 := PriceToSqrtPriceX96Cached(price)
|
|
|
|
// Compare results (should be identical)
|
|
assert.Equal(t, expectedSqrtPriceX96.String(), actualSqrtPriceX96.String(), "Cached and original PriceToSqrtPriceX96 should produce identical results")
|
|
})
|
|
|
|
// Test optimized functions with uint256
|
|
t.Run("SqrtPriceX96ToPriceOptimized", func(t *testing.T) {
|
|
// Use a typical sqrtPriceX96 value
|
|
sqrtPriceX96Big := new(big.Int).SetBytes([]byte{0x06, 0x40, 0x84, 0x4A, 0x0E, 0x81, 0x4F, 0x96, 0x19, 0xC1, 0x9C, 0x08})
|
|
sqrtPriceX96 := uint256.MustFromBig(sqrtPriceX96Big)
|
|
|
|
// Cached calculation
|
|
cachedResult := SqrtPriceX96ToPriceCached(sqrtPriceX96Big)
|
|
|
|
// Optimized calculation
|
|
optimizedResult := SqrtPriceX96ToPriceOptimized(sqrtPriceX96)
|
|
|
|
// Compare results (should be identical)
|
|
assert.Equal(t, cachedResult.String(), optimizedResult.String(), "Optimized and cached SqrtPriceX96ToPrice should produce identical results")
|
|
})
|
|
|
|
// Test optimized functions with uint256
|
|
t.Run("PriceToSqrtPriceX96Optimized", func(t *testing.T) {
|
|
// Use a typical price value
|
|
price := new(big.Float).SetFloat64(2000.0)
|
|
|
|
// Cached calculation
|
|
cachedResult := PriceToSqrtPriceX96Cached(price)
|
|
|
|
// Optimized calculation
|
|
optimizedResult := PriceToSqrtPriceX96Optimized(price)
|
|
|
|
// Compare results (should be identical)
|
|
assert.Equal(t, cachedResult.String(), optimizedResult.ToBig().String(), "Optimized and cached PriceToSqrtPriceX96 should produce identical results")
|
|
})
|
|
}
|
|
|
|
// Test that cached constants are working correctly
|
|
func TestCachedConstants(t *testing.T) {
|
|
// Test that Q192 is correctly calculated
|
|
expectedQ192 := new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
|
actualQ192 := GetCachedQ192()
|
|
assert.Equal(t, expectedQ192.String(), actualQ192.String(), "Cached Q192 should equal 2^192")
|
|
|
|
// Test that Q96 is correctly calculated
|
|
expectedQ96 := new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil)
|
|
actualQ96 := GetCachedQ96()
|
|
assert.Equal(t, expectedQ96.String(), actualQ96.String(), "Cached Q96 should equal 2^96")
|
|
|
|
// Test that Q384 is correctly calculated
|
|
expectedQ384 := new(big.Int).Exp(big.NewInt(2), big.NewInt(384), nil)
|
|
actualQ384 := GetCachedQ384()
|
|
assert.Equal(t, expectedQ384.String(), actualQ384.String(), "Cached Q384 should equal 2^384")
|
|
}
|
|
|
|
// Test edge cases
|
|
func TestEdgeCases(t *testing.T) {
|
|
// Test with zero values
|
|
zero := big.NewInt(0)
|
|
zeroFloat := new(big.Float).SetInt64(0)
|
|
|
|
// SqrtPriceX96ToPrice with zero
|
|
result := SqrtPriceX96ToPriceCached(zero)
|
|
assert.Equal(t, "0", result.String(), "SqrtPriceX96ToPriceCached with zero should return zero")
|
|
|
|
// PriceToSqrtPriceX96 with zero
|
|
result2 := PriceToSqrtPriceX96Cached(zeroFloat)
|
|
assert.Equal(t, "0", result2.String(), "PriceToSqrtPriceX96Cached with zero should return zero")
|
|
|
|
// Test with small values
|
|
one := big.NewInt(1)
|
|
oneFloat := new(big.Float).SetInt64(1)
|
|
|
|
// SqrtPriceX96ToPrice with one
|
|
result3 := SqrtPriceX96ToPriceCached(one)
|
|
assert.NotEmpty(t, result3.String(), "SqrtPriceX96ToPriceCached with one should return a value")
|
|
|
|
// PriceToSqrtPriceX96 with one
|
|
result4 := PriceToSqrtPriceX96Cached(oneFloat)
|
|
assert.NotEmpty(t, result4.String(), "PriceToSqrtPriceX96Cached with one should return a value")
|
|
}
|