Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert sdk.Int to BigDec #6409

Merged
merged 17 commits into from
Sep 20, 2023
Merged
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we do not reference sdk anymore, Int is defined in osmomath package

Suggested change
// BigDecFromSDKInt returns the BigDec representation of an sdkInt.
// BigDecFromSDKInt returns the BigDec representation of an Int.

// Values in any additional decimal places are truncated.
func BigDecFromSDKInt(i Int) BigDec {
return NewBigDecFromBigIntWithPrec(i.BigInt(), 0)
}
Comment on lines +587 to +589
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is not mutative. See that BigInt() reinitializes the underlying buffer.

Let's still keep this, please. However, I'm also wondering if we can make a mutative version that would reuse the input's buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I see, currently it's hard to access Int.i directly without BigInt()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make an issue to consider this in the future and potentially talk to the SDK team about exposing such API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue for tracking #17772

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried returning Int.i in my local, performance is improved but the potential risks are unclear


// BigDecFromDecSlice returns the []BigDec representation of an []Dec.
// Values in any additional decimal places are truncated.
func BigDecFromDecSlice(ds []Dec) []BigDec {
Expand Down
21 changes: 21 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions x/concentrated-liquidity/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep these comments please.

Do you mint creating an issue for this osmosis side and linking it to cosmos/cosmos-sdk#17772?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


product := sqrtPriceA.Mul(sqrtPriceB)
diff := sqrtPriceB.Sub(sqrtPriceA)
Expand All @@ -42,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))
Expand Down