-
Notifications
You must be signed in to change notification settings - Fork 592
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
Move spot price safety checks to keeper layer #2508
Conversation
Co-authored-by: Nicolas Lara <nicolaslara@gmail.com>
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.
Left some questions and comments, otherwise lgtm!
// spot_price = (Base_supply / Weight_base) / (Quote_supply / Weight_quote) | ||
// spot_price = (weight_quote / weight_base) * (base_supply / quote_supply) | ||
invWeightRatio := quote.Weight.ToDec().Quo(base.Weight.ToDec()) | ||
supplyRatio := base.Token.Amount.ToDec().Quo(quote.Token.Amount.ToDec()) | ||
fullRatio := supplyRatio.Mul(invWeightRatio) |
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.
confirmed this logic has been correctly moved
@@ -10,6 +10,8 @@ import ( | |||
"github.com/osmosis-labs/osmosis/v11/osmoutils" | |||
) | |||
|
|||
var MaxSpotPrice = sdk.NewDec(2).Power(128).Sub(sdk.OneDec()) |
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.
what's the reason we don't import it / use it directly from gamm module but declare it twice both in twap module and gamm module?
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.
imports basically, introduces a dependency on its types package. (Maybe this doesn't make sense though)
I think it is ok for the two to diverge with time
@@ -53,7 +53,12 @@ func getSpotPrices(ctx sdk.Context, k types.AmmInterface, poolId uint64, denom0, | |||
sp1 = sdk.ZeroDec() | |||
} | |||
} | |||
// if sp0.GT(gammtypes.MaxSpotPrice) | |||
if sp0.GT(types.MaxSpotPrice) { |
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.
this is just a safety check since CalculateSpotPrice
shouldnt return values over types.MaxSpotPrice right?
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.
yup! In case a different spot price backend gets put in
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
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.
LGTM!
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.
Great work!
Before merging, I would like to understand whether we need to update the latestErrTime
when getting spot prices in TWAP and assuming a different backend.
Also, to discuss and plan increasing test coverage for the keeper's CalculateSpotPrice
@@ -6,6 +6,9 @@ var pointOne = sdk.OneDec().QuoInt64(10) | |||
|
|||
// SigFigRound rounds to a specified significant figure. | |||
func SigFigRound(d sdk.Dec, tenToSigFig sdk.Int) sdk.Dec { | |||
if d.IsZero() { | |||
return d |
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.
We are completely missing tests for this function. We should add them
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.
Agreed, should be done in a separate PR, and is not v12 blocking
if spotPrice.GT(types.MaxSpotPrice) { | ||
return types.MaxSpotPrice, types.ErrSpotPriceOverflow | ||
} else if !spotPrice.IsPositive() { | ||
return sdk.Dec{}, types.ErrSpotPriceInternal |
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.
It seems that the only time we are hitting this branch during the tests is when we attempt to set up a pool. Can we have a test case directly covering this branch?
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.
I don't think this matters that much to directly test, as a blocker for this PR. Its a safety check to give a clear pre-condition for successful results.
// defer to catch panics, in case something internal overflows. | ||
defer func() { | ||
if r := recover(); r != nil { | ||
spotPrice = sdk.Dec{} |
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.
We don't have a test case covering this branch. Is it possible to add it?
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.
Not easily, I tried.
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.
Actually I tried harder, it is!
Thanks for pointing this out :)
Closes: #2507
What is the purpose of the change
This is currently blocked on #2405 , as create pool panics right nowTesting and Verifying
This change should be moving + adding new test cases in total, each more modularly guaranteeing a component.
Documentation and Release Note
Unreleased
section inCHANGELOG.md
? yes (TODO)x/<module>/spec/
) (TODO)