Files
mev-beta/pkg/uniswap/cached.go
2025-10-04 09:31:02 -05:00

45 lines
1.1 KiB
Go

// Package uniswap provides mathematical functions for Uniswap V3 calculations.
package uniswap
import "math/big"
// SqrtPriceX96ToPriceCached converts sqrtPriceX96 to a price using cached constants.
func SqrtPriceX96ToPriceCached(sqrtPriceX96 *big.Int) *big.Float {
// Initialize cached constants
initConstants()
// price = (sqrtPriceX96 / 2^96)^2
// price = sqrtPriceX96^2 / 2^192
// Convert to big.Float for precision
sqrtPrice := new(big.Float).SetInt(sqrtPriceX96)
// Calculate sqrtPrice^2
price := new(big.Float).Mul(sqrtPrice, sqrtPrice)
// Divide by 2^192 using cached constant
price.Quo(price, GetQ192Float())
return price
}
// PriceToSqrtPriceX96Cached converts a price to sqrtPriceX96 using cached constants.
func PriceToSqrtPriceX96Cached(price *big.Float) *big.Int {
// Initialize cached constants
initConstants()
// sqrtPriceX96 = sqrt(price) * 2^96
// Calculate sqrt(price)
sqrtPrice := new(big.Float).Sqrt(price)
// Multiply by 2^96 using cached constant
sqrtPrice.Mul(sqrtPrice, GetQ96Float())
// Convert to big.Int
sqrtPriceX96 := new(big.Int)
sqrtPrice.Int(sqrtPriceX96)
return sqrtPriceX96
}