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:
@@ -38,7 +38,7 @@ func FuzzPricingConversions(f *testing.F) {
|
||||
// Convert price to sqrtPriceX96 and back
|
||||
priceBigFloat := new(big.Float).SetFloat64(price)
|
||||
sqrtPriceX96 := uniswap.PriceToSqrtPriceX96(priceBigFloat)
|
||||
|
||||
|
||||
// Verify sqrtPriceX96 is positive
|
||||
require.True(t, sqrtPriceX96.Sign() > 0, "sqrtPriceX96 must be positive")
|
||||
|
||||
@@ -50,7 +50,7 @@ func FuzzPricingConversions(f *testing.F) {
|
||||
require.True(t, convertedPriceFloat > 0, "Converted price must be positive")
|
||||
|
||||
// Check round-trip consistency (allow some tolerance for floating point precision)
|
||||
tolerance := 0.01 // 1% tolerance
|
||||
tolerance := 0.01 // 1% tolerance
|
||||
if price > 0.01 && price < 100000 { // For reasonable price ranges
|
||||
relativeError := abs(price-convertedPriceFloat) / price
|
||||
assert.True(t, relativeError < tolerance,
|
||||
@@ -77,7 +77,7 @@ func FuzzTickConversions(f *testing.F) {
|
||||
|
||||
// Convert tick to sqrtPriceX96 and back
|
||||
sqrtPriceX96 := uniswap.TickToSqrtPriceX96(tick)
|
||||
|
||||
|
||||
// Verify sqrtPriceX96 is positive
|
||||
require.True(t, sqrtPriceX96.Sign() > 0, "sqrtPriceX96 must be positive")
|
||||
|
||||
@@ -94,11 +94,11 @@ func FuzzTickConversions(f *testing.F) {
|
||||
// FuzzSqrtPriceX96Operations performs fuzz testing on sqrtPriceX96 operations
|
||||
func FuzzSqrtPriceX96Operations(f *testing.F) {
|
||||
// Add seed values for fuzzing
|
||||
f.Add("79228162514264337593543950336") // 2^96 (price = 1)
|
||||
f.Add("158456325028528675187087900672") // 2 * 2^96 (price = 4)
|
||||
f.Add("39614081257132168796771975168") // 2^95 (price = 0.25)
|
||||
f.Add("1122334455667788990011223344") // Random value
|
||||
f.Add("999888777666555444333222111") // Another random value
|
||||
f.Add("79228162514264337593543950336") // 2^96 (price = 1)
|
||||
f.Add("158456325028528675187087900672") // 2 * 2^96 (price = 4)
|
||||
f.Add("39614081257132168796771975168") // 2^95 (price = 0.25)
|
||||
f.Add("1122334455667788990011223344") // Random value
|
||||
f.Add("999888777666555444333222111") // Another random value
|
||||
|
||||
f.Fuzz(func(t *testing.T, sqrtPriceX96Str string) {
|
||||
sqrtPriceX96 := new(big.Int)
|
||||
@@ -139,18 +139,18 @@ func FuzzSqrtPriceX96Operations(f *testing.F) {
|
||||
|
||||
// Convert sqrtPriceX96 to tick
|
||||
tick := uniswap.SqrtPriceX96ToTick(sqrtPriceX96)
|
||||
|
||||
|
||||
// Verify tick is in valid range
|
||||
assert.True(t, tick >= -887272 && tick <= 887272,
|
||||
"Tick %d outside valid range", tick)
|
||||
|
||||
// Convert back to sqrtPriceX96
|
||||
backToSqrtPrice := uniswap.TickToSqrtPriceX96(tick)
|
||||
|
||||
|
||||
// Check consistency (allow small difference due to rounding)
|
||||
diff := new(big.Int).Sub(sqrtPriceX96, backToSqrtPrice)
|
||||
diff.Abs(diff)
|
||||
|
||||
|
||||
// Allow difference of up to 0.01% of original value
|
||||
tolerance := new(big.Int).Div(sqrtPriceX96, big.NewInt(10000))
|
||||
assert.True(t, diff.Cmp(tolerance) <= 0,
|
||||
@@ -162,10 +162,10 @@ func FuzzSqrtPriceX96Operations(f *testing.F) {
|
||||
// FuzzPriceImpactCalculations performs fuzz testing on price impact calculations
|
||||
func FuzzPriceImpactCalculations(f *testing.F) {
|
||||
// Add seed values for fuzzing
|
||||
f.Add(int64(1000000), int64(1000000000000000000)) // Small swap, large liquidity
|
||||
f.Add(int64(1000000), int64(1000000000000000000)) // Small swap, large liquidity
|
||||
f.Add(int64(1000000000), int64(1000000000000000000)) // Large swap, large liquidity
|
||||
f.Add(int64(1000000), int64(1000000000)) // Small swap, small liquidity
|
||||
f.Add(int64(100000000), int64(1000000000)) // Large swap, small liquidity
|
||||
f.Add(int64(1000000), int64(1000000000)) // Small swap, small liquidity
|
||||
f.Add(int64(100000000), int64(1000000000)) // Large swap, small liquidity
|
||||
|
||||
f.Fuzz(func(t *testing.T, swapAmount int64, liquidity int64) {
|
||||
// Skip invalid inputs
|
||||
@@ -240,9 +240,9 @@ func FuzzMathematicalProperties(f *testing.F) {
|
||||
if geometricMean > 0 {
|
||||
geometricMeanSqrt := new(big.Float).SetFloat64(geometricMean)
|
||||
geometricMeanSqrt.Sqrt(geometricMeanSqrt)
|
||||
|
||||
|
||||
geometricMeanSqrtX96 := uniswap.PriceToSqrtPriceX96(geometricMeanSqrt)
|
||||
|
||||
|
||||
// Calculate geometric mean of sqrtPrices
|
||||
// This is complex with big integers, so we'll skip this test for extreme values
|
||||
if sqrtPrice1.BitLen() < 200 && sqrtPrice2.BitLen() < 200 {
|
||||
@@ -267,4 +267,4 @@ func abs64(x int64) int64 {
|
||||
return -x
|
||||
}
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user