From cc610a78ac87ef88f5f8fd98ffa24d271448211d Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 8 Feb 2024 15:33:42 +0900 Subject: [PATCH 1/4] fix missing package --- pool/_TEST_math_logic_test.gnoa | 4 ++-- pool/_TEST_pool_native_swap_test.gnoa | 2 +- pool/_TEST_pool_single_lp_test.gnoa | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pool/_TEST_math_logic_test.gnoa b/pool/_TEST_math_logic_test.gnoa index edf60730..449072e4 100644 --- a/pool/_TEST_math_logic_test.gnoa +++ b/pool/_TEST_math_logic_test.gnoa @@ -95,7 +95,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Positive_16000(t *testing.T) { "_", // recipient false, // zeroForOne 16000, // amountSpecified - MAX_PRICE, // sqrtPriceLimitX96 + consts.MAX_PRICE, // sqrtPriceLimitX96 ) shouldEQ(t, poolOut, bigint(-43468)) @@ -112,7 +112,7 @@ func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) { "_", // recipient false, // zeroForOne -16000, // amountSpecified - MAX_PRICE, // sqrtPriceLimitX96 + consts.MAX_PRICE, // sqrtPriceLimitX96 ) shouldEQ(t, poolOut, bigint(-16000)) shouldEQ(t, poolIn, bigint(5888)) diff --git a/pool/_TEST_pool_native_swap_test.gnoa b/pool/_TEST_pool_native_swap_test.gnoa index edbde6ff..dc679a92 100644 --- a/pool/_TEST_pool_native_swap_test.gnoa +++ b/pool/_TEST_pool_native_swap_test.gnoa @@ -110,7 +110,7 @@ func TestSwapSellNative(t *testing.T) { test1, false, bigint(10000), - MAX_PRICE, + consts.MAX_PRICE, std.GetOrigCaller(), ) shouldEQ(t, amount0, bigint(-3685)) diff --git a/pool/_TEST_pool_single_lp_test.gnoa b/pool/_TEST_pool_single_lp_test.gnoa index 64f76964..d0785ace 100644 --- a/pool/_TEST_pool_single_lp_test.gnoa +++ b/pool/_TEST_pool_single_lp_test.gnoa @@ -277,7 +277,7 @@ func TestSwap(t *testing.T) { userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - amount0, amount1 := Swap(barPath, fooPath, fee500, test1, false, 16000, MAX_PRICE, std.GetOrigCaller()) // give enough amount to take fees away + amount0, amount1 := Swap(barPath, fooPath, fee500, test1, false, 16000, consts.MAX_PRICE, std.GetOrigCaller()) // give enough amount to take fees away poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, poolAddr) poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, poolAddr) @@ -341,7 +341,7 @@ func TestCollectProtocol(t *testing.T) { std.TestSetPrevRealm("gno.land/r/demo/router") std.TestSetOrigCaller(test1) - Swap(barPath, fooPath, fee500, test1, false, 200000, MAX_SQRT_RATIO-1, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(barPath, fooPath, fee500, test1, false, 200000, consts.MAX_SQRT_RATIO-1, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 test1OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) test1OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) From 478046bc96728c33d3dfb5324be251159d98e4ea Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 8 Feb 2024 15:33:47 +0900 Subject: [PATCH 2/4] refactor --- pool/sqrt_price_math.gno | 274 ++++++++++++++++++++++++++++----------- 1 file changed, 201 insertions(+), 73 deletions(-) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index 1c721d93..c1d023f4 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -6,46 +6,61 @@ import ( ) func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( - sqrtPX96 bigint, - liquidity bigint, - amount bigint, - add bool, + sqrtPX96 bigint, + liquidity bigint, + amount bigint, + add bool, ) bigint { - requireUnsigned(sqrtPX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || sqrtPX96(%d) >= 0", sqrtPX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || liquidity(%d) >= 0", liquidity)) - requireUnsigned(amount, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || amount(%d) >= 0", amount)) + requireUnsigned( + sqrtPX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || sqrtPX96(%d) >= 0", + sqrtPX96, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || liquidity(%d) >= 0", + liquidity, + )) + + requireUnsigned( + amount, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || amount(%d) >= 0", + amount, + )) + if amount == 0 { return sqrtPX96 } numerator1 := liquidity << 96 - requireUnsigned(numerator1, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || numerator1(%d) >= 0", numerator1)) - product := amount * sqrtPX96 - requireUnsigned(product, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || product(%d) >= 0", product)) - - var denominator bigint if add { - if product/amount == sqrtPX96 { - denominator = numerator1 + product - requireUnsigned(denominator, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || denominator(%d) >= 0", denominator)) - - if denominator >= numerator1 { - requireUnsigned(numerator1*sqrtPX96/denominator, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || numerator1 * sqrtPX96 / denominator(%d) >= 0", numerator1*sqrtPX96/denominator)) - return numerator1 * sqrtPX96 / denominator - } - } - - requireUnsigned(numerator1/((numerator1/sqrtPX96)+amount), ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || numerator1 / ( (numerator1 / sqrtPX96) + amount(%d) >= 0", (numerator1/((numerator1/sqrtPX96)+amount)))) - return numerator1 / ((numerator1 / sqrtPX96) + amount) + denominator := numerator1 + product + + require( + denominator != 0, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator != 0, got: %d", + denominator, + )) + + return numerator1 * sqrtPX96 / denominator } - require(product/amount == sqrtPX96 && numerator1 > product, "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || condition must return true") - denominator = numerator1 - product - requireUnsigned(denominator, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || denominator(%d) >= 0", denominator)) + denominator := numerator1 - product + + requirePositive( + denominator, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator > 0, got: %d", + denominator, + )) - requireUnsigned(numerator1*sqrtPX96/denominator, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || numerator1 * sqrtPX96 / denominator(%d) >= 0", numerator1*sqrtPX96/denominator)) return numerator1 * sqrtPX96 / denominator } @@ -55,31 +70,63 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( amount bigint, add bool, ) bigint { - requireUnsigned(sqrtPX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || sqrtPX96(%d) >= 0", sqrtPX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || liquidity(%d) >= 0", liquidity)) - requireUnsigned(amount, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || amount(%d) >= 0", amount)) + requireUnsigned( + sqrtPX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || sqrtPX96(%d) >= 0", + sqrtPX96, + )) + + requireUnsigned( + amount, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || amount(%d) >= 0", + amount, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || liquidity(%d) >= 0", + liquidity, + )) var quotient bigint - if add { - if amount <= consts.MAX_UINT160 { - quotient = (amount << 96) / liquidity - } else { - quotient = amount * consts.Q96 / liquidity - } - requireUnsigned(quotient, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || quotient(%d) >= 0", quotient)) - return sqrtPX96 + quotient + if amount <= consts.MAX_UINT160 { + quotient = (amount << 96) / liquidity } else { - if amount <= consts.MAX_UINT160 { - quotient = (amount << 96) / liquidity - } else { - quotient = amount * consts.Q96 / liquidity - } - requireUnsigned(quotient, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || quotient(%d) >= 0", quotient)) - - require(sqrtPX96 > quotient, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || sqrtPX96(%d) must be greater than quotient(%d)", sqrtPX96, quotient)) - return sqrtPX96 - quotient + quotient = amount * (consts.Q96 / liquidity) } -} + + // quotient mutst be positive when amount and liquidity are positive + + if add { + result := sqrtPX96 + quotient + + require( + result >= sqrtPX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected result >= sqrtPX96, got: %d", + result, + ), + ) + + return result + } + + result := sqrtPX96 - quotient + + require( + result >= 0, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected sqrtPX96 >= quotient, got: %d, %d", + sqrtPX96, + quotient, + ), + ) + + return result +} func sqrtPriceMathGetNextSqrtPriceFromInput( sqrtPX96 bigint, @@ -87,17 +134,45 @@ func sqrtPriceMathGetNextSqrtPriceFromInput( amountIn bigint, zeroForOne bool, ) (sqrtQ bigint) { - requireUnsigned(sqrtPX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || sqrtPX96(%d) >= 0", sqrtPX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || liquidity(%d) >= 0", liquidity)) - requireUnsigned(amountIn, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || amountIn(%d) >= 0", amountIn)) + requireUnsigned( + sqrtPX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected sqrtPX96 >= 0. got: %d", + sqrtPX96, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected liquidity >= 0. got: %d", + liquidity, + )) + + requireUnsigned( + amountIn, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected amountIn >= 0. got: %d", + amountIn, + )) + if zeroForOne { - requireUnsigned(sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true), "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || condition must return `v` >= 0__#1") - return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) + amount0Result := sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) + requireUnsigned( + amount0Result, + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || condition must return `v` >= 0__#1", + ) + + return amount0Result } - requireUnsigned(sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true), "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || condition must return `v` >= 0__#2") - return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true) + amount1Result := sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true) + requireUnsigned( + amount1Result, + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || condition must return `v` >= 0__#2", + ) + + return amount1Result } func sqrtPriceMathGetNextSqrtPriceFromOutput( @@ -106,16 +181,35 @@ func sqrtPriceMathGetNextSqrtPriceFromOutput( amountOut bigint, zeroForOne bool, ) (sqrtQ bigint) { - requireUnsigned(sqrtPX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || sqrtPX96(%d) >= 0", sqrtPX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || liquidity(%d) >= 0", liquidity)) + requireUnsigned( + sqrtPX96, + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected sqrtPX96 >= 0. got: %s", + sqrtPX96, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected liquidity >= 0. got: %d", + liquidity, + )) if zeroForOne { - requireUnsigned(sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false), "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || condition must return `v` >= 0__#1") - return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false) + amount1Result := sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false) + requireUnsigned( + amount1Result, + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || condition must return `v` >= 0__#1", + ) + + return amount1Result } - requireUnsigned(sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false), "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || condition must return `v` >= 0__#2") - return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false) + amount0Result := sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false) + requireUnsigned( + amount0Result, + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || condition must return `v` >= 0__#2", + ) + + return amount0Result } func sqrtPriceMathGetAmount0DeltaHelper( @@ -123,20 +217,33 @@ func sqrtPriceMathGetAmount0DeltaHelper( sqrtRatioBX96 bigint, liquidity bigint, ) bigint { - requireUnsigned(sqrtRatioAX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || sqrtRatioAX96(%d) >= 0", sqrtRatioAX96)) - requireUnsigned(sqrtRatioBX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || sqrtRatioBX96(%d) >= 0", sqrtRatioBX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || liquidity(%d) >= 0", liquidity)) + requireUnsigned( + sqrtRatioAX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioAX96 >= 0. got: %d", + sqrtRatioAX96, + )) + + requireUnsigned( + sqrtRatioBX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioBX96 >= 0. got: %d", + sqrtRatioBX96, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected liquidity >= 0. got: %d", + liquidity, + )) if sqrtRatioAX96 > sqrtRatioBX96 { sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } - requireUnsigned(sqrtRatioAX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || sqrtRatioAX96(%d) >= 0", sqrtRatioAX96)) numerator1 := absBigint(liquidity) << 96 - requireUnsigned(numerator1, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || numerator1(%d) >= 0", numerator1)) - numerator2 := sqrtRatioBX96 - sqrtRatioAX96 - requireUnsigned(numerator2, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || numerator2(%d) >= 0", numerator2)) return ((numerator1 * numerator2) / sqrtRatioBX96) / sqrtRatioAX96 } @@ -162,8 +269,19 @@ func sqrtPriceMathGetAmount0Delta( sqrtRatioBX96 bigint, liquidity bigint, ) bigint { - requireUnsigned(sqrtRatioAX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || sqrtRatioAX96(%d) >= 0", sqrtRatioAX96)) - requireUnsigned(sqrtRatioBX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || sqrtRatioBX96(%d) >= 0", sqrtRatioBX96)) + requireUnsigned( + sqrtRatioAX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioAX96 must be unsigned. got: %d", + sqrtRatioAX96, + )) + + requireUnsigned( + sqrtRatioBX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioBX96 must be unsigned. got: %d", + sqrtRatioBX96, + )) if liquidity < 0 { return -sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, absBigint(-liquidity)) @@ -177,8 +295,18 @@ func sqrtPriceMathGetAmount1Delta( sqrtRatioBX96 bigint, liquidity bigint, ) bigint { - requireUnsigned(sqrtRatioAX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || sqrtRatioAX96(%d) >= 0", sqrtRatioAX96)) - requireUnsigned(sqrtRatioBX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || sqrtRatioBX96(%d) >= 0", sqrtRatioBX96)) + requireUnsigned( + sqrtRatioAX96, + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioAX96 must be unsigned. got: %d", + sqrtRatioAX96, + )) + + requireUnsigned( + sqrtRatioBX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioBX96 must be unsigned. got: %d", + sqrtRatioBX96, + )) if liquidity < 0 { return -sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, absBigint(-liquidity)) From 4954f4e79305cbcfa463fb152d07a054c373dae6 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 8 Feb 2024 16:27:17 +0900 Subject: [PATCH 3/4] fixup --- pool/sqrt_price_math.gno | 69 +++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index c1d023f4..529c24fc 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -14,21 +14,21 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( requireUnsigned( sqrtPX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || sqrtPX96(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected sqrtPX96 must be unsigned, got: %d", sqrtPX96, )) requireUnsigned( liquidity, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || liquidity(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected liquidity must be unsigned, got: %d", liquidity, )) requireUnsigned( amount, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || amount(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected amount must be unsigned, got: %d", amount, )) @@ -54,14 +54,14 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( denominator := numerator1 - product - requirePositive( + requireUnsigned( denominator, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator > 0, got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator >= 0, got: %d", denominator, )) - return numerator1 * sqrtPX96 / denominator + return numerator1 / ((sqrtPX96 / denominator) + amount) } func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( @@ -73,21 +73,21 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( requireUnsigned( sqrtPX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || sqrtPX96(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected sqrtPX96 must be unsigned, got: %d", sqrtPX96, )) requireUnsigned( amount, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || amount(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected amount must be unsigned, got: %d", amount, )) requireUnsigned( liquidity, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || liquidity(%d) >= 0", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected liquidity must be unsigned, got: %d", liquidity, )) @@ -137,21 +137,21 @@ func sqrtPriceMathGetNextSqrtPriceFromInput( requireUnsigned( sqrtPX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected sqrtPX96 >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected sqrtPX96 must be unsigned, got: %d", sqrtPX96, )) requireUnsigned( liquidity, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected liquidity >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected liquidity must be unsigned, got: %d", liquidity, )) requireUnsigned( amountIn, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected amountIn >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected amountIn must be unsigned, got: %d", amountIn, )) @@ -160,7 +160,10 @@ func sqrtPriceMathGetNextSqrtPriceFromInput( amount0Result := sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) requireUnsigned( amount0Result, - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || condition must return `v` >= 0__#1", + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() #1 || expected unsigned result, got: %d", + amount0Result, + ), ) return amount0Result @@ -183,13 +186,13 @@ func sqrtPriceMathGetNextSqrtPriceFromOutput( ) (sqrtQ bigint) { requireUnsigned( sqrtPX96, - ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected sqrtPX96 >= 0. got: %s", + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected sqrtPX96 >= 0, got: %s", sqrtPX96, )) requireUnsigned( liquidity, - ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected liquidity >= 0. got: %d", + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected liquidity >= 0, got: %d", liquidity, )) @@ -220,21 +223,21 @@ func sqrtPriceMathGetAmount0DeltaHelper( requireUnsigned( sqrtRatioAX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioAX96 >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioAX96 must be unsigned, got: %d", sqrtRatioAX96, )) requireUnsigned( sqrtRatioBX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioBX96 >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioBX96 must be unsigned, got: %d", sqrtRatioBX96, )) requireUnsigned( liquidity, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected liquidity >= 0. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected liquidity must be unsigned, got: %d", liquidity, )) @@ -253,9 +256,25 @@ func sqrtPriceMathGetAmount1DeltaHelper( sqrtRatioBX96 bigint, liquidity bigint, ) bigint { - requireUnsigned(sqrtRatioAX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || sqrtRatioAX96(%d) >= 0", sqrtRatioAX96)) - requireUnsigned(sqrtRatioBX96, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || sqrtRatioBX96(%d) >= 0", sqrtRatioBX96)) - requireUnsigned(liquidity, ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || liquidity(%d) >= 0", liquidity)) + requireUnsigned( + sqrtRatioAX96, + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected sqrtRatioAX96 must be unsigned, got: %d", + sqrtRatioAX96, + )) + + requireUnsigned( + sqrtRatioBX96, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected sqrtRatioBX96 must be unsigned, got: %d", + sqrtRatioBX96, + )) + + requireUnsigned( + liquidity, + ufmt.Sprintf( + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected liquidity must be unsigned, got: %d", + liquidity, + )) if sqrtRatioAX96 > sqrtRatioBX96 { sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 @@ -272,14 +291,14 @@ func sqrtPriceMathGetAmount0Delta( requireUnsigned( sqrtRatioAX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioAX96 must be unsigned. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioAX96 must be unsigned, got: %d", sqrtRatioAX96, )) requireUnsigned( sqrtRatioBX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioBX96 must be unsigned. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioBX96 must be unsigned, got: %d", sqrtRatioBX96, )) @@ -297,14 +316,14 @@ func sqrtPriceMathGetAmount1Delta( ) bigint { requireUnsigned( sqrtRatioAX96, - ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioAX96 must be unsigned. got: %d", + ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioAX96 must be unsigned, got: %d", sqrtRatioAX96, )) requireUnsigned( sqrtRatioBX96, ufmt.Sprintf( - "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioBX96 must be unsigned. got: %d", + "[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioBX96 must be unsigned, got: %d", sqrtRatioBX96, )) From e1269a799f0b432c5fccd1d01177af6429eb05c0 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 8 Feb 2024 16:47:50 +0900 Subject: [PATCH 4/4] fixup-fixup --- pool/sqrt_price_math.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index 529c24fc..30e15565 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -61,7 +61,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( denominator, )) - return numerator1 / ((sqrtPX96 / denominator) + amount) + return numerator1 / ((numerator1 / sqrtPX96) + amount) } func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(