From ad759f8d8d3ed161300dbd88d8155717ac18be71 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 11 Sep 2023 14:59:03 -0400 Subject: [PATCH 01/11] refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level --- CHANGELOG.md | 1 + x/concentrated-liquidity/math/math.go | 18 +++- x/concentrated-liquidity/math/math_test.go | 98 ++++++++++++++++++++++ x/concentrated-liquidity/python/clmath.py | 6 +- 4 files changed, 116 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56ab25d6d15..8ce59efdbf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#6334](https://github.com/osmosis-labs/osmosis/pull/6334) fix: enable taker fee cli * [#6352](https://github.com/osmosis-labs/osmosis/pull/6352) Reduce error blow-up in CalcAmount0Delta by changing the order of math operations. +* [#6368](https://github.com/osmosis-labs/osmosis/pull/6368) Stricter rounding behavior in CL math's CalcAmount0Delta and GetNextSqrtPriceFromAmount0InRoundingUp ### API Breaks diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index d24ab58b110..c3d3a0b4b2b 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -18,6 +18,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. + // TODO (perf): consider better conversion helpers to minimize reallocations. amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) product := sqrtPriceA.Mul(sqrtPriceB) @@ -26,6 +27,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm panic(fmt.Sprintf("liquidity0 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB)) } + // TODO (perf): consider Dec() function that does not reallocate return amountBigDec.MulMut(product).QuoMut(diff).Dec() } @@ -40,12 +42,14 @@ func Liquidity1(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. + // TODO (perf): consider better conversion helpers to minimize reallocations. amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) diff := sqrtPriceB.Sub(sqrtPriceA) if diff.IsZero() { panic(fmt.Sprintf("liquidity1 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB)) } + // TODO (perf): consider Dec() function that does not reallocate return amountBigDec.QuoMut(diff).Dec() } @@ -73,13 +77,19 @@ func CalcAmount0Delta(liq, sqrtPriceA, sqrtPriceB osmomath.BigDec, roundUp bool) // - calculating amountIn during swap // - adding liquidity (request user to provide more tokens in in favor of the pool) // The denominator is truncated to get a higher final amount. - return liq.MulRoundUp(diff).QuoRoundUp(sqrtPriceA).QuoRoundUp(sqrtPriceB).Ceil() + // Note that the order of divisions is important here. First, we divide by a larger number (sqrtPriceB) and then by a smaller number (sqrtPriceA). + // This leads to a smaller error amplification. + // TODO (perf): QuoRoundUpMut with no reallocation. + return liq.MulRoundUp(diff).QuoRoundUp(sqrtPriceB).QuoRoundUp(sqrtPriceA).Ceil() } // These are truncated at precision end to round in favor of the pool when: // - calculating amount out during swap // - withdrawing liquidity // Each intermediary step is truncated at precision end to get a smaller final amount. - return liq.MulTruncate(diff).QuoTruncate(sqrtPriceA).QuoTruncate(sqrtPriceB) + // Note that the order of divisions is important here. First, we divide by a larger number (sqrtPriceB) and then by a smaller number (sqrtPriceA). + // This leads to a smaller error amplification. + // TODO (perf): QuoTruncate with no reallocation. + return liq.MulTruncate(diff).QuoTruncate(sqrtPriceB).QuoTruncate(sqrtPriceA) } // CalcAmount1Delta takes the asset with the smaller liquidity in the pool as well as the sqrtpCur and the nextPrice and calculates the amount of asset 1 @@ -124,11 +134,11 @@ func GetNextSqrtPriceFromAmount0InRoundingUp(sqrtPriceCurrent, liquidity, amount return sqrtPriceCurrent } - product := amountZeroRemainingIn.Mul(sqrtPriceCurrent) + product := amountZeroRemainingIn.MulTruncate(sqrtPriceCurrent) // denominator = product + liquidity denominator := product denominator.AddMut(liquidity) - return liquidity.Mul(sqrtPriceCurrent).QuoRoundUp(denominator) + return liquidity.MulRoundUp(sqrtPriceCurrent).QuoRoundUp(denominator) } // GetNextSqrtPriceFromAmount0OutRoundingUp utilizes sqrtPriceCurrent, liquidity, and amount of denom0 that still needs diff --git a/x/concentrated-liquidity/math/math_test.go b/x/concentrated-liquidity/math/math_test.go index 0c2c943bcfa..6877a9fc9a7 100644 --- a/x/concentrated-liquidity/math/math_test.go +++ b/x/concentrated-liquidity/math/math_test.go @@ -19,6 +19,17 @@ var ( sqrt4545BigDec = osmomath.BigDecFromDec(sqrt4545) sqrt5000BigDec = osmomath.BigDecFromDec(sqrt5000) sqrt5500BigDec = osmomath.BigDecFromDec(sqrt5500) + + // sqrt(10 ^-36 * 567) 36 decimals + // chosen arbitrarily for testing the extended price range + sqrtANearMin = osmomath.MustNewBigDecFromStr("0.000000000000000023811761799581315315") + // sqrt(10 ^-36 * 123567) 36 decimals + // chosen arbitrarily for testing the extended price range + sqrtBNearMin = osmomath.MustNewBigDecFromStr("0.000000000000000351520980881653776714") + // This value is estimated using liquidity1 function in clmath.py between sqrtANearMin and sqrtBNearMin. + smallLiquidity = osmomath.MustNewBigDecFromStr("0.000000000000316705045072312223884779") + // Arbitrary small value, exists to test small movement over the low price range. + smallValue = osmomath.MustNewBigDecFromStr("10.12345678912345671234567891234567") ) // liquidity1 takes an amount of asset1 in the pool as well as the sqrtpCur and the nextPrice @@ -39,6 +50,15 @@ func TestLiquidity1(t *testing.T) { expectedLiquidity: "1517882343.751510418088349649", // https://www.wolframalpha.com/input?i=5000000000+%2F+%2870.710678118654752440+-+67.416615162732695594%29 }, + "low price range": { + currentSqrtP: sqrtANearMin, + sqrtPLow: sqrtBNearMin, + amount1Desired: osmomath.NewInt(5000000000), + // from math import * + // from decimal import * + // amount1 / (sqrtPriceB - sqrtPriceA) + expectedLiquidity: "15257428564277849269508363.222206252646611708", + }, } for name, tc := range testCases { @@ -76,6 +96,18 @@ func TestLiquidity0(t *testing.T) { expectedLiquidity: "1519437308.014768571720923239", // https://www.wolframalpha.com/input?i=1000000+*+%2870.710678118654752440*+74.161984870956629487%29+%2F+%2874.161984870956629487+-+70.710678118654752440%29 }, + "low price range": { + currentSqrtP: sqrtANearMin, + sqrtPHigh: sqrtBNearMin, + amount0Desired: osmomath.NewInt(123999), + // from clmath import * + // from math import * + // product1 = round_osmo_prec_down(sqrtPriceA * sqrtPriceB) + // product2 = round_osmo_prec_down(amount0 * product1) + // diff = round_osmo_prec_down(sqrtPriceB - sqrtPriceA) + // round_sdk_prec_down(product2 / diff) + expectedLiquidity: "0.000000000003167050", + }, } for name, tc := range testCases { @@ -191,6 +223,16 @@ func TestCalcAmount0Delta(t *testing.T) { isWithTolerance: true, amount0Expected: osmomath.MustNewBigDecFromStr("3546676037185128488234786333758360815266.999539026068480181194797910898392880"), }, + "low price range": { + liquidity: smallLiquidity, + sqrtPA: sqrtANearMin, + sqrtPB: sqrtBNearMin, + roundUp: false, + // from clmath decimal import * + // from math import * + // calc_amount_zero_delta(liq, sqrtPriceA, sqrtPriceB, False) + amount0Expected: osmomath.MustNewBigDecFromStr("12399.405290456300691064448232516066947340"), + }, } for name, tc := range testCases { @@ -277,6 +319,27 @@ func TestCalcAmount1Delta(t *testing.T) { roundUp: true, amount1Expected: osmomath.MustNewBigDecFromStr("28742157707995443393876876754535992.801567623738751734").Ceil(), // round up at precision end. }, + "low price range (no round up)": { + liquidity: smallLiquidity, + sqrtPA: sqrtANearMin, + sqrtPB: sqrtBNearMin, + roundUp: false, + // from clmath decimal import * + // from math import * + // calc_amount_one_delta(liq, sqrtPriceA, sqrtPriceB, False) + amount1Expected: osmomath.MustNewBigDecFromStr("0.000000000000000000000000000103787162"), + }, + "low price range (with round up)": { + liquidity: smallLiquidity, + sqrtPA: sqrtANearMin, + sqrtPB: sqrtBNearMin, + roundUp: true, + // from clmath decimal import * + // calc_amount_one_delta(liq, sqrtPriceA, sqrtPriceB, False) + // Actual result: 0.000000000000000000000000000103787163 + // Gets rounded up to 1. Is this acceptable when the multiplicative difference is so large? + amount1Expected: osmomath.MustNewBigDecFromStr("0.000000000000000000000000000103787163").Ceil(), + }, } for name, tc := range testCases { @@ -449,6 +512,14 @@ func TestGetNextSqrtPriceFromAmount0InRoundingUp(t *testing.T) { // round_osmo_prec_up(liquidity / (round_osmo_prec_down(liquidity / sqrtPriceCurrent) + amountRemaining)) expected: osmomath.MustNewBigDecFromStr("70.666663910857144331148691821263626767"), }, + "low price range": { + liquidity: smallLiquidity, + sqrtPriceCurrent: sqrtANearMin, + amountRemaining: smallValue, + // from clmath decimal import * + // get_next_sqrt_price_from_amount0_in_round_up(liq, sqrtPriceA, amountRemaining) + expected: osmomath.MustNewBigDecFromStr("0.000000000000000023793654323441728435"), + }, } runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount0InRoundingUp", math.GetNextSqrtPriceFromAmount0InRoundingUp, tests) } @@ -470,6 +541,14 @@ func TestGetNextSqrtPriceFromAmount0OutRoundingUp(t *testing.T) { // liq * sqrt_cur / (liq + token_out * sqrt_cur) = 2.5 expected: osmomath.MustNewBigDecFromStr("2.5"), }, + "low price range": { + liquidity: smallLiquidity, + sqrtPriceCurrent: sqrtANearMin, + amountRemaining: smallValue, + // 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) } @@ -499,6 +578,14 @@ func TestGetNextSqrtPriceFromAmount1InRoundingDown(t *testing.T) { // calculated with x/concentrated-liquidity/python/clmath.py round_decimal(sqrt_next, 36, ROUND_FLOOR) expected: osmomath.MustNewBigDecFromStr("70.738319930382329008049494613660784220"), }, + "low price range": { + liquidity: smallLiquidity, + sqrtPriceCurrent: sqrtANearMin, + amountRemaining: smallValue, + // from clmath decimal import * + // get_next_sqrt_price_from_amount1_in_round_down(liq, sqrtPriceA, amountRemaining) + expected: osmomath.MustNewBigDecFromStr("31964936923603.477920799226065501544948016880497639"), + }, } runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount1InRoundingDown", math.GetNextSqrtPriceFromAmount1InRoundingDown, tests) } @@ -519,6 +606,17 @@ func TestGetNextSqrtPriceFromAmount1OutRoundingDown(t *testing.T) { // round_osmo_prec_down(sqrtPriceCurrent - round_osmo_prec_up(tokenOut / liquidity)) expected: osmomath.MustNewBigDecFromStr("2.5"), }, + "low price range": { + liquidity: smallLiquidity, + sqrtPriceCurrent: sqrtANearMin, + amountRemaining: smallValue, + // from clmath decimal import * + // get_next_sqrt_price_from_amount1_out_round_down(liq, sqrtPriceA, amountRemaining) + // While a negative sqrt price value is invalid and should be caught by the caller, + // we mostly focus on testing rounding behavior and math correctness at low spot prices. + // For the purposes of our test, this result is acceptable. + expected: osmomath.MustNewBigDecFromStr("-31964936923603.477920799226065453921424417717867010"), + }, } runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount1OutRoundingDown", math.GetNextSqrtPriceFromAmount1OutRoundingDown, tests) } diff --git a/x/concentrated-liquidity/python/clmath.py b/x/concentrated-liquidity/python/clmath.py index 849500d6082..313efa8a8cb 100644 --- a/x/concentrated-liquidity/python/clmath.py +++ b/x/concentrated-liquidity/python/clmath.py @@ -1,4 +1,5 @@ from decimal import Decimal, ROUND_FLOOR, ROUND_CEILING, getcontext +from math import * class Coin: # Define this class based on what fields sdk.Coin has. @@ -114,12 +115,11 @@ def calc_amount_zero_delta(liquidity, sqrtPriceA, sqrtPriceB, roundUp): diff = sqrtPriceA - sqrtPriceB product_num = liquidity * diff - product_denom = sqrtPriceA * sqrtPriceB if roundUp: - return round_osmo_prec_up(round_osmo_prec_up(product_num) / round_osmo_prec_down(product_denom)) + return round_osmo_prec_up(round_osmo_prec_up(round_osmo_prec_up(product_num) / sqrtPriceA) / sqrtPriceB) - return round_osmo_prec_down(round_osmo_prec_down(product_num) / round_osmo_prec_up(product_denom)) + return round_osmo_prec_down(round_osmo_prec_down(round_osmo_prec_down(product_num) / sqrtPriceA)/ sqrtPriceB) def calc_amount_one_delta(liquidity, sqrtPriceA, sqrtPriceB, roundUp): if sqrtPriceB > sqrtPriceA: From 3cc21a8528a9c0363f163a8cbdfdfdabb05aa724 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 11 Sep 2023 15:37:55 -0400 Subject: [PATCH 02/11] comment updates --- x/concentrated-liquidity/math/math.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index c3d3a0b4b2b..6429182bb41 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -134,10 +134,12 @@ func GetNextSqrtPriceFromAmount0InRoundingUp(sqrtPriceCurrent, liquidity, amount return sqrtPriceCurrent } + // Truncate at precision end to make denominator smaller so that the final result is larger. product := amountZeroRemainingIn.MulTruncate(sqrtPriceCurrent) // denominator = product + liquidity denominator := product denominator.AddMut(liquidity) + // MulRoundUp and QuoRoundUp to make the final result larger by rounding up at precision end. return liquidity.MulRoundUp(sqrtPriceCurrent).QuoRoundUp(denominator) } From cb6f73dc31271d13bf95685973eb0e25a8819c17 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Thu, 14 Sep 2023 18:42:10 +0700 Subject: [PATCH 03/11] convert int => bigdec instead of int => dec => bigdec --- osmomath/decimal.go | 6 ++++++ x/concentrated-liquidity/math/math.go | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 72dcb42c55b..c2ba6622ae6 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -582,6 +582,12 @@ func BigDecFromDec(d Dec) BigDec { return NewBigDecFromBigIntWithPrec(d.BigInt(), PrecisionDec) } +// BigDecFromSDKInt returns the BigDec representation of an sdkInt. +// Values in any additional decimal places are truncated. +func BigDecFromSDKInt(i Int) BigDec { + return NewBigDecFromBigIntWithPrec(i.BigInt(), 0) +} + // BigDecFromDecSlice returns the []BigDec representation of an []Dec. // Values in any additional decimal places are truncated. func BigDecFromDecSlice(ds []Dec) []BigDec { diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index 6429182bb41..fb64884b756 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -18,8 +18,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. - // TODO (perf): consider better conversion helpers to minimize reallocations. - amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) + amountBigDec := osmomath.BigDecFromSDKInt(amount) product := sqrtPriceA.Mul(sqrtPriceB) diff := sqrtPriceB.Sub(sqrtPriceA) From 938436da375d1e0ec0c17beca4a3cb66f339ddd6 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Thu, 14 Sep 2023 18:42:24 +0700 Subject: [PATCH 04/11] tests --- osmomath/decimal_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index 6f354d86a1b..f2de861998c 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -295,6 +295,27 @@ func (s *decimalTestSuite) TestBigDecFromSdkDec() { } } +func (s *decimalTestSuite) TestBigDecFromSdkInt() { + tests := []struct { + i osmomath.Int + want osmomath.BigDec + expPanic bool + }{ + {osmomath.ZeroInt(), osmomath.NewBigDec(0), false}, + {osmomath.OneInt(), osmomath.NewBigDec(1), false}, + {osmomath.NewInt(10), osmomath.NewBigDec(10), false}, + {osmomath.NewInt(10090090090090090), osmomath.NewBigDecWithPrec(10090090090090090, 0), false}, + } + for tcIndex, tc := range tests { + if tc.expPanic { + s.Require().Panics(func() { osmomath.BigDecFromSDKInt(tc.i) }) + } else { + value := osmomath.BigDecFromSDKInt(tc.i) + s.Require().Equal(tc.want, value, "bad osmomath.BigDecFromDec(), index: %v", tcIndex) + } + } +} + func (s *decimalTestSuite) TestBigDecFromSdkDecSlice() { tests := []struct { d []osmomath.Dec From 341f982793d833a5c1a85d04287b99461d80d1e4 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Thu, 14 Sep 2023 18:46:20 +0700 Subject: [PATCH 05/11] Liquidity1 lack --- x/concentrated-liquidity/math/math.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index fb64884b756..7256fafd87e 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -41,8 +41,7 @@ func Liquidity1(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. - // TODO (perf): consider better conversion helpers to minimize reallocations. - amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) + amountBigDec := osmomath.BigDecFromSDKInt(amount) diff := sqrtPriceB.Sub(sqrtPriceA) if diff.IsZero() { panic(fmt.Sprintf("liquidity1 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB)) From 1f21fc37269b8ea14719dfd90bfaa0bb923b2ac9 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 16 Sep 2023 15:41:41 +0700 Subject: [PATCH 06/11] add changelog endpoint --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27b8bf92337..5325cf504a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#6334](https://github.com/osmosis-labs/osmosis/pull/6334) fix: enable taker fee cli * [#6352](https://github.com/osmosis-labs/osmosis/pull/6352) Reduce error blow-up in CalcAmount0Delta by changing the order of math operations. * [#6368](https://github.com/osmosis-labs/osmosis/pull/6368) Stricter rounding behavior in CL math's CalcAmount0Delta and GetNextSqrtPriceFromAmount0InRoundingUp +* [#6409](https://github.com/osmosis-labs/osmosis/pull/6409) CL math: Convert Int to BigDec ### API Breaks From 39f676ab3b07e188c46a4c121a435059ac33591d Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 19 Sep 2023 01:01:50 +0700 Subject: [PATCH 07/11] update go mod --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 625b62cf99e..58848d0ce39 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.17 github.com/ory/dockertest/v3 v3.10.0 github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 - github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918125210-4225d5fcc9f0 github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf diff --git a/go.sum b/go.sum index b857615f419..8d6ef3d00f9 100644 --- a/go.sum +++ b/go.sum @@ -959,6 +959,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf h1:sXSC/RG9sxQCB5QJ5JB+M9Bok7Xbpv2zx9ee6287glw= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918125210-4225d5fcc9f0 h1:kQWGBnQk5I1piplnN+mP0qv6gi0eC09/DLgWCIyI8Jk= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918125210-4225d5fcc9f0/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf h1:r5R/L3tzH+vGPahAdvnVB2Vo0KPhZR0oMNyX4+G2FEo= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:7VoXHwrSSx8Sii0UFc9YIixF6C/9XfV1pdU2Dliu4WA= github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf h1:8lkIsAj3L7zxvOZbqVLNJRpSdDxaYhYfAIG7XjPaJiU= From 503af84b17204fb01074cf94c02e607042020d57 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 19 Sep 2023 03:05:26 +0700 Subject: [PATCH 08/11] keep comments --- x/concentrated-liquidity/math/math.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index c766d1cd1ca..cb2b2479d7b 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -18,6 +18,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. + // TODO (perf): consider better conversion helpers to minimize reallocations. amountBigDec := osmomath.BigDecFromSDKInt(amount) product := sqrtPriceA.Mul(sqrtPriceB) @@ -41,6 +42,7 @@ func Liquidity1(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm // We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, // our liquidity calculations will be off from our theoretical calculations within our tests. + // TODO (perf): consider better conversion helpers to minimize reallocations. amountBigDec := osmomath.BigDecFromSDKInt(amount) diff := sqrtPriceB.Sub(sqrtPriceA) if diff.IsZero() { From 88e48d5e33ee226b28406165190b0ca81d9cb7e9 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 19 Sep 2023 14:02:15 +0700 Subject: [PATCH 09/11] go mod --- go.mod | 2 +- go.sum | 2 ++ x/concentrated-liquidity/math/math_test.go | 16 ---------------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 786d0caf78e..d7d473c0e21 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.17 github.com/ory/dockertest/v3 v3.10.0 github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 - github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918184012-da92c9cdf6bd + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf diff --git a/go.sum b/go.sum index b542b00b1cc..b4c77ac75b2 100644 --- a/go.sum +++ b/go.sum @@ -961,6 +961,8 @@ github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf h1 github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918184012-da92c9cdf6bd h1:U7r0uBLTWeLrgGOu1re0aTl10yreX1j3dNDu12KqBpE= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918184012-da92c9cdf6bd/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad h1:I4m0TxfAAovSmxI0rYvijAVX6JhoYe12VmjM5vcABxU= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf h1:r5R/L3tzH+vGPahAdvnVB2Vo0KPhZR0oMNyX4+G2FEo= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:7VoXHwrSSx8Sii0UFc9YIixF6C/9XfV1pdU2Dliu4WA= github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf h1:8lkIsAj3L7zxvOZbqVLNJRpSdDxaYhYfAIG7XjPaJiU= diff --git a/x/concentrated-liquidity/math/math_test.go b/x/concentrated-liquidity/math/math_test.go index b85f15b8a4c..51a6005657e 100644 --- a/x/concentrated-liquidity/math/math_test.go +++ b/x/concentrated-liquidity/math/math_test.go @@ -620,19 +620,3 @@ func TestGetNextSqrtPriceFromAmount1OutRoundingDown(t *testing.T) { runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount1OutRoundingDown", math.GetNextSqrtPriceFromAmount1OutRoundingDown, tests) } -var sink interface{} - -func BenchmarkQuoTruncate(b *testing.B) { - b1 := osmomath.NewBigDec(1000000000) - b2 := osmomath.NewBigDec(4371) - b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { - sink = b1.QuoTruncateMut(b2) - } - - if sink == nil { - b.Fatal("Benchmark did not run") - } - sink = (interface{})(nil) -} From 9e18a011711adbaba4c4272e97adedb9867135f9 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 19 Sep 2023 14:03:00 +0700 Subject: [PATCH 10/11] fix --- x/concentrated-liquidity/math/math_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/concentrated-liquidity/math/math_test.go b/x/concentrated-liquidity/math/math_test.go index 51a6005657e..affd657841e 100644 --- a/x/concentrated-liquidity/math/math_test.go +++ b/x/concentrated-liquidity/math/math_test.go @@ -619,4 +619,3 @@ func TestGetNextSqrtPriceFromAmount1OutRoundingDown(t *testing.T) { } runSqrtRoundingTestCase(t, "TestGetNextSqrtPriceFromAmount1OutRoundingDown", math.GetNextSqrtPriceFromAmount1OutRoundingDown, tests) } - From 79f03e2187c8295a7859b56af561cb77984bf14f Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 19 Sep 2023 20:44:24 -0400 Subject: [PATCH 11/11] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ab5edd10c3..42b37b343fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,7 +65,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#6352](https://github.com/osmosis-labs/osmosis/pull/6352) Reduce error blow-up in CalcAmount0Delta by changing the order of math operations. * [#6368](https://github.com/osmosis-labs/osmosis/pull/6368) Stricter rounding behavior in CL math's CalcAmount0Delta and GetNextSqrtPriceFromAmount0InRoundingUp * [#6409](https://github.com/osmosis-labs/osmosis/pull/6409) CL math: Convert Int to BigDec -* [#6426](https://github.com/osmosis-labs/osmosis/pull/6426) bug: send non osmo txfees to community pool instead of stakers ### API Breaks