Files
mev-beta/pkg/uniswap/advanced_cached_test.go
2025-10-04 09:31:02 -05: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")
}