Skip to content

Commit

Permalink
Convert sdk.Int to BigDec (#6409)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
(cherry picked from commit e2b521d)

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	go.sum
#	osmomath/decimal_test.go
#	x/concentrated-liquidity/math/math.go
  • Loading branch information
hieuvubk authored and mergify[bot] committed Sep 20, 2023
1 parent 7bd815e commit e48b94b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions x/concentrated-liquidity/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit e48b94b

Please sign in to comment.