Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Apr 11, 2024
1 parent 8fce257 commit aabae07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 27 additions & 5 deletions x/concentrated-liquidity/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/swaps_tick_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aabae07

Please sign in to comment.