-
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
[stableswap]: simplify CFMM solver #2697
Conversation
Ah great idea, there was no need to keep Nice job, this is a big improvement ;) |
Co-authored-by: Dev Ojha <[email protected]>
…s/osmosis into simplify-cfmm-solver
Recent commit cuts the exponents of the two largest terms in half ( This makes meaningful progress towards #2701, but we still overflow at much smaller asset amounts than we expect to be able to accommodate (current capacity is just under $10m per asset in a 2-asset pool, and we need to be able to support $100m per asset in a 3+ asset pool) Reference to verify new implementation: https://www.desmos.com/calculator/bx5m5wpind The last idea/trick I have in mind is potentially dropping the +1s given the size of pools we're dealing with (maybe have two solvers and switch to the other one when x, y, or k is sufficiently large) Ultimately we'll want to use our binary search solver, but I believe it's important to have a comprehensive and simple reference solver to benchmark against in the meantime |
nice, this is great! Big win! (The largest term is x^6 still tho =p, as Heres an equation switch inspired by how your doing this, basically moving https://www.desmos.com/calculator/lww58igxe8 And actually if you unwrap |
This is awesome! Really good point with In any case, what do you think about this simplification? (no massive denominators and only two unique calculations involving variables): If we're happy with directly computing |
That looks great to me! |
x/gamm/pool-models/stableswap/amm.go
Outdated
// So we want to solve for a given addition of `b` units of y into the pool, | ||
// how many units `a` of x do we get out. | ||
// Let y' = y + b | ||
// we solve k = (x'y')(x'^2 + y^2) for x', using the following equation {wolfram alpha link} |
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.
Needs wolfram alpha link, or equation we test and explanation of correctness
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 put a wolfram alpha link of the original equation, a desmos link for our simplified one, and direct equation + our key abstractions – let me know what you think!
x/gamm/pool-models/stableswap/amm.go
Outdated
large_term := (y4.Quo(k)).Mul(osmomath.NewBigDec(2).Quo(twentySevenRootTwo)) | ||
large_term2 := large_term.Mul(large_term) | ||
|
||
// solve for new xReserve using new yReserve and old k using a solver derived from xy(x^2 + y^2) = k | ||
sqrt_term, err := (osmomath.OneDec().Add(large_term2)).ApproxRoot(2) | ||
if err != nil { | ||
panic(err) | ||
} |
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.
Lets take the equation we want to compute, write out the variable substitutions we do, then show that each of this is very clearly computing the desired variable
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.
Took a crack at this!
…rch (#2816) Progress towards: #2794 ## What is the purpose of the change This PR replaces our stableswap two-asset CFMM solver to use the `BigDec` binary search implementation in #2802 Note: most of the diff is tests merged from #2697 and the core binary search logic from #2802, both of which had to be merged into this branch ## Brief Changelog - Replace binary search solver logic with our `BigDec` binary search implementation ## Testing and Verifying - All two-asset CFMM test cases were set up to test this implementation in `amm_test.go` ## Documentation and Release Note - Does this pull request introduce a new feature or user-facing behavior changes? (no) - Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`? (no) - How is the feature or change documented? (not documented)
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.
Nice!
LGTM to merge once conflicts are fixed
* add checks for invalid cfmm inputs * implement simplified two-asset cfmm solver * add further error and validation checks * reduce largest term's exponent from 12 to 8 * update comments to match new solver method Co-authored-by: Dev Ojha <[email protected]> * reduce largest terms from x^8 to x^4 and k^2 to k respectively * linter * linter * factor out test cases * add overflow and uneven pool tests * add thorough comments and links explaining solver math Co-authored-by: Dev Ojha <[email protected]>
Closes: #2696
What is the purpose of the change
This PR implements a drastically simplified solver for our two-asset stableswap CFMM.
Key differences:
Brief Changelog
I derived this solver manually, but we can confirm on Desmos that it fits our curve exactly: https://www.desmos.com/calculator/wxl9ffxofw
As described in the issue, the key simplifcations leaned on the following assumptions:
Pseudocode for new solver:
Testing and Verifying
Basic tests pass, and the ones that failed on our original solver are around 3x closer to the target with this solver (likely due to fewer terms leading to fewer approximations).
Still working on more comprehensive tests, although we might first need to solve general precision issues around our CFMM.
Documentation and Release Note
Unreleased
section inCHANGELOG.md
? (yes / no)x/<module>/spec/
) / Osmosis docs repo / not documented)