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:
36
orig/pkg/uniswap/lookup/lookup_bench_test.go
Normal file
36
orig/pkg/uniswap/lookup/lookup_bench_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package lookup
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkSqrtPriceX96ToPriceWithLookup(b *testing.B) {
|
||||
// Create a test sqrtPriceX96 value
|
||||
sqrtPriceX96 := new(big.Int)
|
||||
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = SqrtPriceX96ToPriceWithLookup(sqrtPriceX96)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPriceToSqrtPriceX96WithLookup(b *testing.B) {
|
||||
// Create a test price value
|
||||
price := new(big.Float).SetFloat64(1.0)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = PriceToSqrtPriceX96WithLookup(price)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTickToSqrtPriceX96WithLookup(b *testing.B) {
|
||||
tick := 100000
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = TickToSqrtPriceX96WithLookup(tick)
|
||||
}
|
||||
}
|
||||
69
orig/pkg/uniswap/lookup/optimized.go
Normal file
69
orig/pkg/uniswap/lookup/optimized.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package lookup
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// SqrtPriceX96ToPriceWithLookup converts sqrtPriceX96 to a price using lookup tables
|
||||
func SqrtPriceX96ToPriceWithLookup(sqrtPriceX96 *big.Int) *big.Float {
|
||||
// price = (sqrtPriceX96 / 2^96)^2
|
||||
// price = sqrtPriceX96^2 / 2^192
|
||||
|
||||
// Calculate sqrtPriceX96^2
|
||||
sqrtPriceSquared := new(big.Int).Mul(sqrtPriceX96, sqrtPriceX96)
|
||||
|
||||
// Convert to big.Float for division
|
||||
price := new(big.Float).SetInt(sqrtPriceSquared)
|
||||
|
||||
// Divide by 2^192 using lookup table
|
||||
q192 := GetQ192()
|
||||
q192Float := new(big.Float).SetInt(q192)
|
||||
price.Quo(price, q192Float)
|
||||
|
||||
return price
|
||||
}
|
||||
|
||||
// PriceToSqrtPriceX96WithLookup converts a price to sqrtPriceX96 using lookup tables
|
||||
func PriceToSqrtPriceX96WithLookup(price *big.Float) *big.Int {
|
||||
// sqrtPriceX96 = sqrt(price) * 2^96
|
||||
|
||||
// Calculate sqrt(price)
|
||||
sqrtPrice := new(big.Float).Sqrt(price)
|
||||
|
||||
// Multiply by 2^96 using lookup table
|
||||
q96Int := GetQ96()
|
||||
q96 := new(big.Float).SetInt(q96Int)
|
||||
sqrtPrice.Mul(sqrtPrice, q96)
|
||||
|
||||
// Convert to big.Int
|
||||
sqrtPriceX96 := new(big.Int)
|
||||
sqrtPrice.Int(sqrtPriceX96)
|
||||
|
||||
return sqrtPriceX96
|
||||
}
|
||||
|
||||
// TickToSqrtPriceX96WithLookup converts a tick to sqrtPriceX96 using lookup tables
|
||||
func TickToSqrtPriceX96WithLookup(tick int) *big.Int {
|
||||
// sqrtPriceX96 = 1.0001^(tick/2) * 2^96
|
||||
|
||||
// For better performance with large tick values, we'll use logarithms
|
||||
// but with cached base values
|
||||
lnBase := math.Log(1.0001) // ln(1.0001) ≈ 9.999500016666e-05
|
||||
logResult := lnBase * float64(tick) / 2.0
|
||||
result := math.Exp(logResult)
|
||||
|
||||
// Convert to big.Float
|
||||
price := new(big.Float).SetFloat64(result)
|
||||
|
||||
// Multiply by 2^96 using lookup table
|
||||
q96Int := GetQ96()
|
||||
q96 := new(big.Float).SetInt(q96Int)
|
||||
price.Mul(price, q96)
|
||||
|
||||
// Convert to big.Int
|
||||
sqrtPriceX96 := new(big.Int)
|
||||
price.Int(sqrtPriceX96)
|
||||
|
||||
return sqrtPriceX96
|
||||
}
|
||||
120
orig/pkg/uniswap/lookup/tables.go
Normal file
120
orig/pkg/uniswap/lookup/tables.go
Normal file
@@ -0,0 +1,120 @@
|
||||
// Package lookup provides lookup tables for frequently used Uniswap V3 calculations.
|
||||
package lookup
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// Lookup tables for frequently used values
|
||||
sqrt10001Table map[int]*big.Float
|
||||
q96Table *big.Int
|
||||
q192Table *big.Int
|
||||
|
||||
// Once variables for initializing lookup tables
|
||||
sqrt10001Once sync.Once
|
||||
q96Once sync.Once
|
||||
)
|
||||
|
||||
// initSqrt10001Table initializes the lookup table for sqrt(1.0001^n)
|
||||
func initSqrt10001Table() {
|
||||
sqrt10001Once.Do(func() {
|
||||
sqrt10001Table = make(map[int]*big.Float)
|
||||
|
||||
// Use a more practical range for ticks
|
||||
// This covers the range most commonly encountered in Uniswap V3
|
||||
// Most Uniswap V3 pools have ticks in the range of approx. -887272 to 887272
|
||||
// For performance, we'll precompute a more reasonable range
|
||||
// and compute on-demand for values outside this range
|
||||
for i := -100000; i <= 100000; i += 2500 { // Only precompute every 2500th value
|
||||
// Calculate sqrt(1.0001^(i/2))
|
||||
base := 1.0001
|
||||
power := float64(i) / 2.0
|
||||
result := pow(base, power)
|
||||
|
||||
// Store in lookup table
|
||||
sqrt10001Table[i] = new(big.Float).SetFloat64(result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// initQTables initializes the lookup tables for Q96 and Q192
|
||||
func initQTables() {
|
||||
q96Once.Do(func() {
|
||||
// Q96 = 2^96
|
||||
q96Table = new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil)
|
||||
|
||||
// Q192 = 2^192 = (2^96)^2
|
||||
q192Table = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
|
||||
})
|
||||
}
|
||||
|
||||
// GetSqrt10001 retrieves the precomputed sqrt(1.0001^n) value
|
||||
func GetSqrt10001(n int) *big.Float {
|
||||
initSqrt10001Table()
|
||||
|
||||
// Check if value is in lookup table
|
||||
if val, ok := sqrt10001Table[n]; ok {
|
||||
return val
|
||||
}
|
||||
|
||||
// For values not in the lookup table, find the closest precomputed value
|
||||
// and calculate the difference to reduce computation
|
||||
base := 1.0001
|
||||
power := float64(n) / 2.0
|
||||
result := pow(base, power)
|
||||
|
||||
// Add to lookup table for future use if it's within a reasonable range
|
||||
// to prevent memory overflow
|
||||
if n >= -500000 && n <= 500000 {
|
||||
sqrt10001Table[n] = new(big.Float).SetFloat64(result)
|
||||
}
|
||||
|
||||
return new(big.Float).SetFloat64(result)
|
||||
}
|
||||
|
||||
// GetQ96 retrieves the precomputed Q96 value (2^96)
|
||||
func GetQ96() *big.Int {
|
||||
initQTables()
|
||||
return q96Table
|
||||
}
|
||||
|
||||
// GetQ192 retrieves the precomputed Q192 value (2^192)
|
||||
func GetQ192() *big.Int {
|
||||
initQTables()
|
||||
return q192Table
|
||||
}
|
||||
|
||||
// Helper function for computing powers efficiently
|
||||
func pow(base, exp float64) float64 {
|
||||
if exp == 0 {
|
||||
return 1
|
||||
}
|
||||
if exp == 1 {
|
||||
return base
|
||||
}
|
||||
if exp == 2 {
|
||||
return base * base
|
||||
}
|
||||
|
||||
// For other values, use exponentiation by squaring
|
||||
return powInt(base, int(exp))
|
||||
}
|
||||
|
||||
// Integer power function using exponentiation by squaring
|
||||
func powInt(base float64, exp int) float64 {
|
||||
if exp < 0 {
|
||||
return 1.0 / powInt(base, -exp)
|
||||
}
|
||||
|
||||
result := 1.0
|
||||
for exp > 0 {
|
||||
if exp&1 == 1 {
|
||||
result *= base
|
||||
}
|
||||
base *= base
|
||||
exp >>= 1
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user