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

fix(CL): error blow up in CalcAmount0Delta causes OverChargeSwapOutGivenInError when swapping zero for one #6352

Merged
merged 9 commits into from
Sep 11, 2023

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Sep 10, 2023

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 change

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

@github-actions
Copy link
Contributor

Important Notice

This PR modifies an in-repo Go module. It is one of:

  • osmomath
  • osmoutils
  • x/ibc-hooks
  • x/epochs

The dependent Go modules, especially the root one, will have to be
updated to reflect the changes. Failing to do so might cause e2e to fail.

Please follow the instructions below:

  1. Open https://github.com/osmosis/osmosis/actions/workflows/go-mod-auto-bump.yml
  2. Provide the current branch name
  3. On success, confirm if an automated commit corretly updated the go.mod and go.sum files

Please let us know if you need any help.

@p0mvn p0mvn marked this pull request as ready for review September 10, 2023 22:39
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a 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)

x/concentrated-liquidity/math/math.go Outdated Show resolved Hide resolved
@p0mvn
Copy link
Member Author

p0mvn commented Sep 11, 2023

@AlpinYukseloglu Thanks for the review, made some extra updates to apply the same order of operations when rounding down:
f8fb807

@AlpinYukseloglu
Copy link
Contributor

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

@p0mvn
Copy link
Member Author

p0mvn commented Sep 11, 2023

Looks like this resolved the sub-ULP issue we were having pre-launch judging by the failing test that was reproducing it:

"7: invalid zero difference between sqrt price current and sqrt price next due to precision loss, full amount remaining in is charged and amount out calculated from sqrt price": {

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

@p0mvn p0mvn merged commit b053f0c into main Sep 11, 2023
@p0mvn p0mvn deleted the roman/fix-cl-error-blow-up branch September 11, 2023 02:00
@github-actions github-actions bot mentioned this pull request Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(CL): error blow up in CalcAmount0Delta causes OverChargeSwapOutGivenInError when swapping zero for one
2 participants