Add enhanced concurrency patterns, rate limiting, market management, and pipeline processing

This commit is contained in:
Krypto Kajun
2025-09-12 01:35:50 -05:00
parent 300976219a
commit fbb85e529a
17 changed files with 1440 additions and 190 deletions

View File

@@ -1,6 +1,7 @@
package uniswap
import (
"math"
"math/big"
"github.com/holiman/uint256"
@@ -8,7 +9,7 @@ import (
const (
// Q96 represents 2^96 used in Uniswap V3 sqrtPriceX96 calculations
Q96 = 79228162514264337593543950336 // 2^96
Q96 = "79228162514264337593543950336" // 2^96 as string to avoid overflow
// Tick spacing for different fee tiers
LowTickSpacing = 10
@@ -58,17 +59,22 @@ func TickToSqrtPriceX96(tick int) *big.Int {
// sqrtPriceX96 = 1.0001^(tick/2) * 2^96
// Calculate 1.0001^(tick/2)
base := new(big.Float).SetFloat64(1.0001)
tickF := new(big.Float).SetFloat64(float64(tick) / 2.0)
power := new(big.Float).Pow(base, tickF)
base := 1.0001
power := float64(tick) / 2.0
result := math.Pow(base, power)
// Convert to big.Float
price := new(big.Float).SetFloat64(result)
// Multiply by 2^96
q96 := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil))
sqrtPrice := new(big.Float).Mul(power, q96)
q96Int := new(big.Int)
q96Int.SetString(Q96, 10)
q96 := new(big.Float).SetInt(q96Int)
price.Mul(price, q96)
// Convert to big.Int
sqrtPriceX96 := new(big.Int)
sqrtPrice.Int(sqrtPriceX96)
price.Int(sqrtPriceX96)
return sqrtPriceX96
}
@@ -85,7 +91,9 @@ func SqrtPriceX96ToTick(sqrtPriceX96 *big.Int) int {
// Convert to big.Float
sqrtPrice := new(big.Float).SetInt(sqrtPriceX96)
q96 := new(big.Float).SetInt(new(big.Int).SetInt64(Q96))
q96Int := new(big.Int)
q96Int.SetString(Q96, 10)
q96 := new(big.Float).SetInt(q96Int)
// Calculate sqrtPriceX96 / 2^96
ratio := new(big.Float).Quo(sqrtPrice, q96)
@@ -95,14 +103,15 @@ func SqrtPriceX96ToTick(sqrtPriceX96 *big.Int) int {
// Calculate log_1.0001(price)
// log_1.0001(x) = ln(x) / ln(1.0001)
lnPrice := new(big.Float).Log(price)
lnBase := new(big.Float).Log(new(big.Float).SetFloat64(1.0001))
logRatio := new(big.Float).Quo(lnPrice, lnBase)
priceFloat, _ := price.Float64()
lnPrice := math.Log(priceFloat)
lnBase := math.Log(1.0001)
logRatio := lnPrice / lnBase
// Convert to int
tick, _ := logRatio.Int64()
tick := int(logRatio)
return int(tick)
return tick
}
// GetTickAtSqrtPrice calculates the tick for a given sqrtPriceX96 using uint256