From e48b94bfd2be466cc1b45a8d0b104f8d96cb86a3 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 (cherry picked from commit e2b521d0eee859f3345e42b3b411e1c8653ac2df) # Conflicts: # CHANGELOG.md # go.mod # go.sum # osmomath/decimal_test.go # x/concentrated-liquidity/math/math.go --- CHANGELOG.md | 6 ++++++ go.mod | 7 +++++++ go.sum | 15 +++++++++++++++ osmomath/decimal.go | 6 ++++++ osmomath/decimal_test.go | 25 +++++++++++++++++++++++++ x/concentrated-liquidity/math/math.go | 10 ++++++++++ 6 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57b2d022d79..19a79e7374b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug Fixes * [#6334](https://github.com/osmosis-labs/osmosis/pull/6334) fix: enable taker fee cli +<<<<<<< HEAD +======= +* [#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 +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) ### API Breaks diff --git a/go.mod b/go.mod index e003ecbbdcd..15efbaa2abf 100644 --- a/go.mod +++ b/go.mod @@ -22,10 +22,17 @@ 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 +<<<<<<< HEAD github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d github.com/osmosis-labs/osmosis/osmoutils v0.0.6 github.com/osmosis-labs/osmosis/x/epochs v0.0.2 github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8 +======= + 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 +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) github.com/pkg/errors v0.9.1 github.com/rakyll/statik v0.1.7 github.com/spf13/cast v1.5.1 diff --git a/go.sum b/go.sum index b4b955d227b..62b482f8830 100644 --- a/go.sum +++ b/go.sum @@ -964,6 +964,7 @@ github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3 h1: github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3/go.mod h1:mFGH2RJhuZa6vBuFMNe4JN85fGtIhROtdeQIyIw9isA= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI= +<<<<<<< HEAD github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d h1:b8Bz31kbzTbcSNsBOcvc2CRVJz5C3cGOQB7JqWDzdfA= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d/go.mod h1:Jj4zQ/IA8iPuHF+Hc/dTFJqAis5WYX3EcO7Xg70fAd8= github.com/osmosis-labs/osmosis/osmoutils v0.0.6 h1:VI4wPzxXpdg5XEUaBugY9lADwxzw3H6lEJ0D+TZc4oE= @@ -972,6 +973,20 @@ github.com/osmosis-labs/osmosis/x/epochs v0.0.2 h1:aEeXHGCSJMgMtAvCucsD2RSaWZ8lI github.com/osmosis-labs/osmosis/x/epochs v0.0.2/go.mod h1:8dvJFHTpu6SIxmOaSaEw0tHnQ/Z9blf5qsF/ZJnMVHo= github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8 h1:BUGowctYQT0vdPgULrvwraEsW+sS6DnbzndTLKLmWVY= github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8/go.mod h1:sR0lpev9mcm9/9RY50T1og3UC3WpZAsINh/OmgrmFlg= +======= +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.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= +github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf/go.mod h1:C0Uqe6X4N5ASA+1xZ6guaaJyUVKLcaVJIQa4Q4LG9Vk= +github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf h1:ZEi+yJJPgpYtmNwZ1bMiP5cMBDQ83FK/YGgmTnWmoAI= +github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf/go.mod h1:pqkAsS04Er1kFM41gg3aWTSaYHyLe8C9Dw3Sj8KMXk8= +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) github.com/osmosis-labs/wasmd v0.31.0-osmo-v16 h1:X747cZYdnqc/+RV48iPVeGprpVb/fUWSaKGsZUWrdbg= github.com/osmosis-labs/wasmd v0.31.0-osmo-v16/go.mod h1:Rf8zW/GgBQyFRRB4s62VQHWA6sTlMFSjoDQQpoq64iI= github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= 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 84e5ec05054..9309d51af7b 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -294,7 +294,32 @@ func (s *decimalTestSuite) TestBigDecFromDec() { } } +<<<<<<< HEAD func (s *decimalTestSuite) TestBigDecFromDecSlice() { +======= +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() { +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) tests := []struct { d []osmomath.Dec want []osmomath.BigDec diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index b1a0930411d..9c6686fc150 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -18,7 +18,12 @@ 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. +<<<<<<< HEAD amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) +======= + // TODO (perf): consider better conversion helpers to minimize reallocations. + amountBigDec := osmomath.BigDecFromSDKInt(amount) +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) product := sqrtPriceA.Mul(sqrtPriceB) diff := sqrtPriceB.Sub(sqrtPriceA) @@ -40,7 +45,12 @@ 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. +<<<<<<< HEAD amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) +======= + // TODO (perf): consider better conversion helpers to minimize reallocations. + amountBigDec := osmomath.BigDecFromSDKInt(amount) +>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409)) diff := sqrtPriceB.Sub(sqrtPriceA) if diff.IsZero() { panic(fmt.Sprintf("liquidity1 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB))