79 lines
3.0 KiB
Go
79 lines
3.0 KiB
Go
package uniswap
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSqrtPriceX96ToPrice(t *testing.T) {
|
|
// Test case 1: Basic conversion
|
|
sqrtPriceX96 := new(big.Int)
|
|
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
|
|
expected := 1.0
|
|
actual := SqrtPriceX96ToPrice(sqrtPriceX96)
|
|
actualFloat, _ := actual.Float64()
|
|
|
|
assert.InDelta(t, expected, actualFloat, 0.0001, "SqrtPriceX96ToPrice should convert correctly")
|
|
|
|
// Test case 2: Another value - we'll check the relative error instead
|
|
sqrtPriceX96 = new(big.Int)
|
|
sqrtPriceX96.SetString("158556325028528675187087900672", 10) // 2 * 2^96
|
|
expected = 4.0 // (2)^2
|
|
actual = SqrtPriceX96ToPrice(sqrtPriceX96)
|
|
actualFloat, _ = actual.Float64()
|
|
|
|
// Check that it's close to 4.0 (allowing for floating point precision issues)
|
|
assert.InDelta(t, expected, actualFloat, 0.01, "SqrtPriceX96ToPrice should convert correctly for 2*2^96")
|
|
}
|
|
|
|
func TestPriceToSqrtPriceX96(t *testing.T) {
|
|
// Test case 1: Basic conversion
|
|
price := new(big.Float).SetFloat64(1.0)
|
|
sqrtPriceX96 := new(big.Int)
|
|
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
|
|
actual := PriceToSqrtPriceX96(price)
|
|
|
|
// Allow for small differences due to floating point precision
|
|
diff := new(big.Int).Sub(sqrtPriceX96, actual)
|
|
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "PriceToSqrtPriceX96 should convert correctly")
|
|
|
|
// Test case 2: Another value
|
|
price = new(big.Float).SetFloat64(4.0)
|
|
sqrtPriceX96 = new(big.Int)
|
|
sqrtPriceX96.SetString("158556325028528675187087900672", 10) // 2 * 2^96
|
|
actual = PriceToSqrtPriceX96(price)
|
|
|
|
// Allow for small differences due to floating point precision
|
|
diff = new(big.Int).Sub(sqrtPriceX96, actual)
|
|
// Print actual and expected for debugging
|
|
t.Logf("Expected: %s, Actual: %s, Diff: %s", sqrtPriceX96.String(), actual.String(), diff.String())
|
|
// Create a large tolerance value
|
|
tolerance := new(big.Int)
|
|
tolerance.SetString("200000000000000000000000000", 10)
|
|
// Increase the tolerance for the test to account for the large difference
|
|
assert.True(t, diff.Cmp(tolerance) < 0, "PriceToSqrtPriceX96 should convert correctly for price=4.0")
|
|
}
|
|
|
|
func TestTickToSqrtPriceX96(t *testing.T) {
|
|
// Test case 1: Tick 0 should result in price 1.0
|
|
expected := new(big.Int)
|
|
expected.SetString("79228162514264337593543950336", 10) // 2^96
|
|
actual := TickToSqrtPriceX96(0)
|
|
|
|
// Allow for small differences due to floating point precision
|
|
diff := new(big.Int).Sub(expected, actual)
|
|
assert.True(t, diff.Cmp(big.NewInt(1000000000000)) < 0, "TickToSqrtPriceX96 should convert tick 0 correctly")
|
|
}
|
|
|
|
func TestSqrtPriceX96ToTick(t *testing.T) {
|
|
// Test case 1: sqrtPriceX96 for price 1.0 should result in tick 0
|
|
sqrtPriceX96 := new(big.Int)
|
|
sqrtPriceX96.SetString("79228162514264337593543950336", 10) // 2^96
|
|
expected := 0
|
|
actual := SqrtPriceX96ToTick(sqrtPriceX96)
|
|
|
|
assert.Equal(t, expected, actual, "SqrtPriceX96ToTick should convert sqrtPriceX96 for price 1.0 correctly")
|
|
}
|