-
Notifications
You must be signed in to change notification settings - Fork 608
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
[x/gamm][stableswap]: Investigate critical precision issue with binary search CFMM solver #2349
Comments
I think the problem even worse than you described, the solver seems to me can only accommodate x and y smaller than 10 ^ 3 or so. Let me prove my point here, so the constant function of stable swap is |
Given what I said, I still think the binary solver is better cause no matter what tool we use, k value will always end up being off by some large value in the case of large x and y. In other words, what I've proven above is also true for the direct solver. Also, the direct solver touches the root function which is not good right now also due to precision so it might even be less accurate than this binary solver. I think this is something we have to accept as the limitation of sdk.Dec. |
Background
Our stableswap implementation has two solver types for our CFMM: a direct solver and a binary search solver. If we can get the latter to work, it would be the better approach for efficiency reasons, but it currently suffers from a precision issue that causes it to loop infinitely in many common scenarios.
The root of the issue is that since binary search solves largest digit -> smallest digit,
10**-18
floating point precision onX
affords us 18 digits of precision onk
. i.e.:Expected
k
:200000000.000000000000000000
Estimated
k
:200000000.000000000887912648
Since our
k
precision threshold requires at least10**-10
precision, this fails at anything abovek
=100,000,000
=10^8
.This means that when we try to solve for the change in asset
X
, we hit the10**-18
precision cap with at most10**-9
precision, and since our binary search implementation only terminates upon reaching 10^-10 precision, this causes the solver to run indefinitely.We should probably make it terminate after 256 iterations as this comment mistakenly claims it does, but I think the issue around precision is an important one to solve regardless so that the solver outputs make sense.
Suggested Design
I believe we have three options to make this work:
k
values greater than10**8
(probably a bad idea & still vulnerable to largek
s)10**18
to accommodate largerk
valuesX
(marginally inefficient at the trader’s expense but at least removes any security risks due to using binary search over direct solver)If we cannot make any of these work and do not have any alternative ways to deal with this precision issue, I think we should consider removing/not using the binary search solver for security reasons.
Acceptance Criteria
The text was updated successfully, but these errors were encountered: