Skip to content

Commit e48b94b

Browse files
hieuvubkmergify[bot]
authored andcommitted
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 <roman@osmosis.team> (cherry picked from commit e2b521d) # Conflicts: # CHANGELOG.md # go.mod # go.sum # osmomath/decimal_test.go # x/concentrated-liquidity/math/math.go
1 parent 7bd815e commit e48b94b

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4949
### Bug Fixes
5050

5151
* [#6334](https://github.com/osmosis-labs/osmosis/pull/6334) fix: enable taker fee cli
52+
<<<<<<< HEAD
53+
=======
54+
* [#6352](https://github.com/osmosis-labs/osmosis/pull/6352) Reduce error blow-up in CalcAmount0Delta by changing the order of math operations.
55+
* [#6368](https://github.com/osmosis-labs/osmosis/pull/6368) Stricter rounding behavior in CL math's CalcAmount0Delta and GetNextSqrtPriceFromAmount0InRoundingUp
56+
* [#6409](https://github.com/osmosis-labs/osmosis/pull/6409) CL math: Convert Int to BigDec
57+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
5258
5359
### API Breaks
5460

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ require (
2222
github.com/mattn/go-sqlite3 v1.14.17
2323
github.com/ory/dockertest/v3 v3.10.0
2424
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
25+
<<<<<<< HEAD
2526
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d
2627
github.com/osmosis-labs/osmosis/osmoutils v0.0.6
2728
github.com/osmosis-labs/osmosis/x/epochs v0.0.2
2829
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8
30+
=======
31+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad
32+
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf
33+
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf
34+
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf
35+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
2936
github.com/pkg/errors v0.9.1
3037
github.com/rakyll/statik v0.1.7
3138
github.com/spf13/cast v1.5.1

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3 h1:
964964
github.com/osmosis-labs/cosmos-sdk v0.45.0-rc1.0.20230913192254-a2075590d5a3/go.mod h1:mFGH2RJhuZa6vBuFMNe4JN85fGtIhROtdeQIyIw9isA=
965965
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY=
966966
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
967+
<<<<<<< HEAD
967968
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d h1:b8Bz31kbzTbcSNsBOcvc2CRVJz5C3cGOQB7JqWDzdfA=
968969
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230907220438-2f6dd779bc6d/go.mod h1:Jj4zQ/IA8iPuHF+Hc/dTFJqAis5WYX3EcO7Xg70fAd8=
969970
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
972973
github.com/osmosis-labs/osmosis/x/epochs v0.0.2/go.mod h1:8dvJFHTpu6SIxmOaSaEw0tHnQ/Z9blf5qsF/ZJnMVHo=
973974
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8 h1:BUGowctYQT0vdPgULrvwraEsW+sS6DnbzndTLKLmWVY=
974975
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.8/go.mod h1:sR0lpev9mcm9/9RY50T1og3UC3WpZAsINh/OmgrmFlg=
976+
=======
977+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf h1:sXSC/RG9sxQCB5QJ5JB+M9Bok7Xbpv2zx9ee6287glw=
978+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
979+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918184012-da92c9cdf6bd h1:U7r0uBLTWeLrgGOu1re0aTl10yreX1j3dNDu12KqBpE=
980+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230918184012-da92c9cdf6bd/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
981+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad h1:I4m0TxfAAovSmxI0rYvijAVX6JhoYe12VmjM5vcABxU=
982+
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20230919070012-03a878db9dad/go.mod h1:pIItelRYvonB+H8A6KqucOi7xFnJxzDHaWJqOdFNLI4=
983+
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf h1:r5R/L3tzH+vGPahAdvnVB2Vo0KPhZR0oMNyX4+G2FEo=
984+
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20230911120014-b14342e08daf/go.mod h1:7VoXHwrSSx8Sii0UFc9YIixF6C/9XfV1pdU2Dliu4WA=
985+
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf h1:8lkIsAj3L7zxvOZbqVLNJRpSdDxaYhYfAIG7XjPaJiU=
986+
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20230911120014-b14342e08daf/go.mod h1:C0Uqe6X4N5ASA+1xZ6guaaJyUVKLcaVJIQa4Q4LG9Vk=
987+
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf h1:ZEi+yJJPgpYtmNwZ1bMiP5cMBDQ83FK/YGgmTnWmoAI=
988+
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20230911120014-b14342e08daf/go.mod h1:pqkAsS04Er1kFM41gg3aWTSaYHyLe8C9Dw3Sj8KMXk8=
989+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
975990
github.com/osmosis-labs/wasmd v0.31.0-osmo-v16 h1:X747cZYdnqc/+RV48iPVeGprpVb/fUWSaKGsZUWrdbg=
976991
github.com/osmosis-labs/wasmd v0.31.0-osmo-v16/go.mod h1:Rf8zW/GgBQyFRRB4s62VQHWA6sTlMFSjoDQQpoq64iI=
977992
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=

osmomath/decimal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ func BigDecFromDec(d Dec) BigDec {
582582
return NewBigDecFromBigIntWithPrec(d.BigInt(), PrecisionDec)
583583
}
584584

585+
// BigDecFromSDKInt returns the BigDec representation of an sdkInt.
586+
// Values in any additional decimal places are truncated.
587+
func BigDecFromSDKInt(i Int) BigDec {
588+
return NewBigDecFromBigIntWithPrec(i.BigInt(), 0)
589+
}
590+
585591
// BigDecFromDecSlice returns the []BigDec representation of an []Dec.
586592
// Values in any additional decimal places are truncated.
587593
func BigDecFromDecSlice(ds []Dec) []BigDec {

osmomath/decimal_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,32 @@ func (s *decimalTestSuite) TestBigDecFromDec() {
294294
}
295295
}
296296

297+
<<<<<<< HEAD
297298
func (s *decimalTestSuite) TestBigDecFromDecSlice() {
299+
=======
300+
func (s *decimalTestSuite) TestBigDecFromSdkInt() {
301+
tests := []struct {
302+
i osmomath.Int
303+
want osmomath.BigDec
304+
expPanic bool
305+
}{
306+
{osmomath.ZeroInt(), osmomath.NewBigDec(0), false},
307+
{osmomath.OneInt(), osmomath.NewBigDec(1), false},
308+
{osmomath.NewInt(10), osmomath.NewBigDec(10), false},
309+
{osmomath.NewInt(10090090090090090), osmomath.NewBigDecWithPrec(10090090090090090, 0), false},
310+
}
311+
for tcIndex, tc := range tests {
312+
if tc.expPanic {
313+
s.Require().Panics(func() { osmomath.BigDecFromSDKInt(tc.i) })
314+
} else {
315+
value := osmomath.BigDecFromSDKInt(tc.i)
316+
s.Require().Equal(tc.want, value, "bad osmomath.BigDecFromDec(), index: %v", tcIndex)
317+
}
318+
}
319+
}
320+
321+
func (s *decimalTestSuite) TestBigDecFromSdkDecSlice() {
322+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
298323
tests := []struct {
299324
d []osmomath.Dec
300325
want []osmomath.BigDec

x/concentrated-liquidity/math/math.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm
1818

1919
// We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this,
2020
// our liquidity calculations will be off from our theoretical calculations within our tests.
21+
<<<<<<< HEAD
2122
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec())
23+
=======
24+
// TODO (perf): consider better conversion helpers to minimize reallocations.
25+
amountBigDec := osmomath.BigDecFromSDKInt(amount)
26+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
2227

2328
product := sqrtPriceA.Mul(sqrtPriceB)
2429
diff := sqrtPriceB.Sub(sqrtPriceA)
@@ -40,7 +45,12 @@ func Liquidity1(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm
4045

4146
// We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this,
4247
// our liquidity calculations will be off from our theoretical calculations within our tests.
48+
<<<<<<< HEAD
4349
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec())
50+
=======
51+
// TODO (perf): consider better conversion helpers to minimize reallocations.
52+
amountBigDec := osmomath.BigDecFromSDKInt(amount)
53+
>>>>>>> e2b521d0 (Convert sdk.Int to BigDec (#6409))
4454
diff := sqrtPriceB.Sub(sqrtPriceA)
4555
if diff.IsZero() {
4656
panic(fmt.Sprintf("liquidity1 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB))

0 commit comments

Comments
 (0)