From aabae0728d261ded9f998a96bd4bb68ead32a251 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 12 Apr 2024 01:09:21 +0900 Subject: [PATCH] Fix test --- x/concentrated-liquidity/math/math_test.go | 32 ++++++++++++++++--- .../swaps_tick_cross_test.go | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/x/concentrated-liquidity/math/math_test.go b/x/concentrated-liquidity/math/math_test.go index 214ca3c33e4..bcf13685ddb 100644 --- a/x/concentrated-liquidity/math/math_test.go +++ b/x/concentrated-liquidity/math/math_test.go @@ -479,6 +479,13 @@ type sqrtRoundingDecTestCase struct { expected osmomath.BigDec } +type sqrtRoundingAmtDecTestCase struct { + sqrtPriceCurrent osmomath.BigDec + liquidity osmomath.BigDec + amountRemaining osmomath.Dec + expected osmomath.BigDec +} + func runSqrtRoundingTestCase( t *testing.T, name string, @@ -509,6 +516,21 @@ func runSqrtRoundingDecTestCase( } } +func runSqrtRoundingAmtDecTestCase( + t *testing.T, + name string, + fn func(osmomath.BigDec, osmomath.BigDec, osmomath.Dec) osmomath.BigDec, + cases map[string]sqrtRoundingAmtDecTestCase, +) { + for name, tc := range cases { + tc := tc + t.Run(name, func(t *testing.T) { + sqrtPriceNext := fn(tc.sqrtPriceCurrent, tc.liquidity, tc.amountRemaining) + require.Equal(t, tc.expected.String(), sqrtPriceNext.String()) + }) + } +} + // Estimates are computed with x/concentrated-liquidity/python/clmath.py func TestGetNextSqrtPriceFromAmount0InRoundingUp(t *testing.T) { tests := map[string]sqrtRoundingTestCase{ @@ -547,31 +569,31 @@ func TestGetNextSqrtPriceFromAmount0InRoundingUp(t *testing.T) { // Estimates are computed with x/concentrated-liquidity/python/clmath.py func TestGetNextSqrtPriceFromAmount0OutRoundingUp(t *testing.T) { - tests := map[string]sqrtRoundingTestCase{ + tests := map[string]sqrtRoundingAmtDecTestCase{ "rounded up at precision end": { sqrtPriceCurrent: sqrt5000BigDec, liquidity: osmomath.MustNewBigDecFromStr("3035764687.503020836176699298"), - amountRemaining: osmomath.MustNewBigDecFromStr("8398"), + amountRemaining: osmomath.MustNewDecFromStr("8398"), // get_next_sqrt_price_from_amount0_out_round_up(liquidity,sqrtPriceCurrent ,amountRemaining) expected: osmomath.MustNewBigDecFromStr("70.724512595179305565323229510645063950"), }, "no round up due zeroes at precision end": { sqrtPriceCurrent: osmomath.MustNewBigDecFromStr("2"), liquidity: osmomath.MustNewBigDecFromStr("10"), - amountRemaining: osmomath.MustNewBigDecFromStr("1"), + amountRemaining: osmomath.MustNewDecFromStr("1"), // liq * sqrt_cur / (liq + token_out * sqrt_cur) = 2.5 expected: osmomath.MustNewBigDecFromStr("2.5"), }, "low price range": { liquidity: smallLiquidity, sqrtPriceCurrent: sqrtANearMin, - amountRemaining: smallValue, + amountRemaining: smallValue.Dec(), // from clmath decimal import * // get_next_sqrt_price_from_amount0_out_round_up(liq, sqrtPriceA, amountRemaining) expected: osmomath.MustNewBigDecFromStr("0.000000000000000023829902587267894423"), }, } - runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount0OutRoundingUp", math.GetNextSqrtPriceFromAmount0OutRoundingUp, tests) + runSqrtRoundingAmtDecTestCase(t, "TestGetNextSqrtPriceFromAmount0OutRoundingUp", math.GetNextSqrtPriceFromAmount0OutRoundingUp, tests) } // Estimates are computed with x/concentrated-liquidity/python/clmath.py diff --git a/x/concentrated-liquidity/swaps_tick_cross_test.go b/x/concentrated-liquidity/swaps_tick_cross_test.go index 1c799bf8521..5613b892bdf 100644 --- a/x/concentrated-liquidity/swaps_tick_cross_test.go +++ b/x/concentrated-liquidity/swaps_tick_cross_test.go @@ -1219,7 +1219,7 @@ func (s *KeeperTestSuite) TestSwaps_Contiguous_Initialized_TickSpacingOne() { // Round up since we want to overestimate the change in sqrt price stemming from the amount out going left-to-right // from the current sqrt price. This overestimated value is then used to calculate amount in charged on the user. // Since amount in is overestimated, this is done in favor of the pool. - updatedNextCurSqrtPrice := math.GetNextSqrtPriceFromAmount0OutRoundingUp(nextSqrtPrice, liq, amountOutDifference) + updatedNextCurSqrtPrice := math.GetNextSqrtPriceFromAmount0OutRoundingUp(nextSqrtPrice, liq, amountOutDifference.Dec()) // Round up since we want to overestimate the amount in in favor of the pool. return math.CalcAmount1Delta(liq.Dec(), updatedNextCurSqrtPrice, nextSqrtPrice, true).DecRoundUp(), updatedNextCurSqrtPrice }