From e2b521d0eee859f3345e42b3b411e1c8653ac2df Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Wed, 20 Sep 2023 08:32:30 +0700 Subject: [PATCH] Convert sdk.Int to BigDec (#6409) * refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level * comment updates * convert int => bigdec instead of int => dec => bigdec * tests * Liquidity1 lack * add changelog endpoint * update go mod * keep comments * go mod * fix * Update CHANGELOG.md --------- Co-authored-by: Roman --- CHANGELOG.md | 1 + go.mod | 2 +- go.sum | 2 ++ osmomath/decimal.go | 6 ++++++ osmomath/decimal_test.go | 21 +++++++++++++++++++++ x/concentrated-liquidity/math/math.go | 4 ++-- 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b6f81ac91f..42b37b343fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,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 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/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/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 diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index 210f43d430d..cb2b2479d7b 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -19,7 +19,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) @@ -43,7 +43,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))