-
Notifications
You must be signed in to change notification settings - Fork 607
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
fix(CL): error blow up in CalcAmount0Delta causes OverChargeSwapOutGivenInError when swapping zero for one #6352
Conversation
…venInError when swapping zero for one
Important Notice This PR modifies an in-repo Go module. It is one of:
The dependent Go modules, especially the root one, will have to be Please follow the instructions below:
Please let us know if you need any help. |
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, nice job finding a clean fix for this. I left a non-blocking comment about rounding direction (still approving since rounding direction at precision doesn't matter here as long as we Ceil at the end)
@AlpinYukseloglu Thanks for the review, made some extra updates to apply the same order of operations when rounding down: |
Updates LGTM! Rounding at precision in intermediate calcs might be overkill here (in the original implementation as well) but as long as rounding error doesn't blow up anywhere later I think it's good to keep |
Looks like this resolved the sub-ULP issue we were having pre-launch judging by the failing test that was reproducing it:
Going to look into it now UPDATE: The edge seems to be fixed, the zero sqrt price difference does not occur anymore. Updated the value out. Recalculated it in Python: d4ad039 Added comment that the edge case was fixed: a8e4284 |
Closes: #6351
What is the purpose of the change
See #6351 for detailed context.
Our current order of math operations in
CalcAmount0Delta
was causing error blow-up. This was noticed pre-CL launch but was deemed as acceptable due to occurring infrequently.By lowering our min spot price to 10^-30, the frequency of this error occurring was increased to an unacceptable degree.
The core of the issue is that
a / (b * c)
amplifies the error when b and c are both small. Division by extremely small numbers has such an effect.By changing the order of operations to
a / b / c
, we reduce the error amplification and resolve the problem encountered.The test added on this branch reproduces the edge case (expected results are estimated in Python).
Made changes to the new test helper in
osmoassert
as a drive-by changeDocumentation and Release Note
Unreleased
section ofCHANGELOG.md
?Where is the change documented?
x/{module}/README.md
)