-
Notifications
You must be signed in to change notification settings - Fork 606
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
Changes from 9 commits
ad759f8
3cc21a8
cb6f73d
938436d
341f982
ef9785c
1f21fc3
4225d5f
39f676a
a16034f
503af84
a574e39
03a878d
88e48d5
9e18a01
507daf3
79f03e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
Comment on lines
+587
to
+589
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this is not mutative. See that 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I see, currently it's hard to access There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created an issue for tracking #17772 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
product := sqrtPriceA.Mul(sqrtPriceB) | ||
diff := sqrtPriceB.Sub(sqrtPriceA) | ||
|
@@ -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)) | ||
|
There was a problem hiding this comment.
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