feat(core): implement core MEV bot functionality with market scanning and Uniswap V3 pricing
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -25,57 +25,57 @@ func SqrtPriceX96ToPrice(sqrtPriceX96 *big.Int) *big.Float {
|
||||
|
||||
// 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 (which is (2^96)^2)
|
||||
q192 := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil))
|
||||
price.Quo(price, q192)
|
||||
|
||||
|
||||
return price
|
||||
}
|
||||
|
||||
// PriceToSqrtPriceX96 converts a price to sqrtPriceX96
|
||||
func PriceToSqrtPriceX96(price *big.Float) *big.Int {
|
||||
// sqrtPriceX96 = sqrt(price) * 2^96
|
||||
|
||||
|
||||
// Calculate sqrt(price)
|
||||
sqrtPrice := new(big.Float).Sqrt(price)
|
||||
|
||||
|
||||
// Multiply by 2^96
|
||||
q96 := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil))
|
||||
sqrtPrice.Mul(sqrtPrice, q96)
|
||||
|
||||
|
||||
// Convert to big.Int
|
||||
sqrtPriceX96 := new(big.Int)
|
||||
sqrtPrice.Int(sqrtPriceX96)
|
||||
|
||||
|
||||
return sqrtPriceX96
|
||||
}
|
||||
|
||||
// TickToSqrtPriceX96 converts a tick to sqrtPriceX96
|
||||
func TickToSqrtPriceX96(tick int) *big.Int {
|
||||
// sqrtPriceX96 = 1.0001^(tick/2) * 2^96
|
||||
|
||||
|
||||
// Calculate 1.0001^(tick/2)
|
||||
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
|
||||
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)
|
||||
price.Int(sqrtPriceX96)
|
||||
|
||||
|
||||
return sqrtPriceX96
|
||||
}
|
||||
|
||||
@@ -84,33 +84,33 @@ func SqrtPriceX96ToTick(sqrtPriceX96 *big.Int) int {
|
||||
// tick = log_1.0001(sqrtPriceX96 / 2^96)^2
|
||||
// tick = log_1.0001(price)
|
||||
// tick = 2 * log_1.0001(sqrtPriceX96 / 2^96)
|
||||
|
||||
|
||||
if sqrtPriceX96.Cmp(big.NewInt(0)) <= 0 {
|
||||
return 0 // Invalid input
|
||||
}
|
||||
|
||||
|
||||
// Convert to big.Float
|
||||
sqrtPrice := new(big.Float).SetInt(sqrtPriceX96)
|
||||
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)
|
||||
|
||||
|
||||
// Calculate (sqrtPriceX96 / 2^96)^2 to get price
|
||||
price := new(big.Float).Mul(ratio, ratio)
|
||||
|
||||
|
||||
// Calculate log_1.0001(price)
|
||||
// log_1.0001(x) = ln(x) / ln(1.0001)
|
||||
priceFloat, _ := price.Float64()
|
||||
lnPrice := math.Log(priceFloat)
|
||||
lnBase := math.Log(1.0001)
|
||||
logRatio := lnPrice / lnBase
|
||||
|
||||
|
||||
// Convert to int
|
||||
tick := int(logRatio)
|
||||
|
||||
|
||||
return tick
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ func SqrtPriceX96ToTick(sqrtPriceX96 *big.Int) int {
|
||||
func GetTickAtSqrtPrice(sqrtPriceX96 *uint256.Int) int {
|
||||
// This is a simplified implementation
|
||||
// In practice, you would use a more precise logarithmic calculation
|
||||
|
||||
|
||||
// Convert to big.Int for calculation
|
||||
sqrtPriceBig := sqrtPriceX96.ToBig()
|
||||
return SqrtPriceX96ToTick(sqrtPriceBig)
|
||||
@@ -136,4 +136,4 @@ func GetPreviousTick(currentTick int, tickSpacing int) int {
|
||||
// Round down to nearest tick spacing
|
||||
tick := (currentTick / tickSpacing) * tickSpacing
|
||||
return tick
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,16 +14,16 @@ func TestSqrtPriceX96ToPrice(t *testing.T) {
|
||||
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
|
||||
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")
|
||||
}
|
||||
@@ -34,17 +34,17 @@ func TestPriceToSqrtPriceX96(t *testing.T) {
|
||||
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
|
||||
@@ -61,7 +61,7 @@ func TestTickToSqrtPriceX96(t *testing.T) {
|
||||
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")
|
||||
@@ -73,6 +73,6 @@ func TestSqrtPriceX96ToTick(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user