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

solver for multi-asset CFMM #1429

Merged
merged 20 commits into from
May 6, 2022
Merged

solver for multi-asset CFMM #1429

merged 20 commits into from
May 6, 2022

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented May 5, 2022

Closes: #1369

What is the purpose of the change

This PR implements the solver for our multi-asset CFMM (as outlined in the previous PR for issue #1369).

This is an extension of the original two-asset CFMM used in Solidly, which we expand here to accommodate n-asset pools without significantly increasing complexity or runtime. While this type of multi-asset pool was one that we found was perhaps unnecessarily complex for volatile assets, it is critical from a capital efficiency standpoint that we have the option for Curve-like 3pools and 4pools for our stableswap so as to not fragment stablecoin liquidity.

Our final multi-asset CFMM is xyz(x^2 + y^2 + w) = k, where z represents the product of the reserves of all assets that are not being swapped between, and w represents the sum of all of their squares. This PR implements a solver that finds the correct output for of a requested token a given an input of another token b in the pool and accommodates swaps between any two assets x and y in an n-asset stableswap. The polynomial we solve for can be described as:

(x - a)(y + b)z((x - a)^2 + (y +b)^2 + w) = k, where we solve for a. The solution is pasted below, but given how it is quite complex and not easy to visually parse without the abstractions we have laid out in the code comments, please refer to those for more comprehensive and readable implementation details.

a = (-27 b^2 w x y - 27 b^2 x^3 y - 27 b^2 x y^3 + sqrt((-27 b^2 w x y - 27 b^2 x^3 y - 27 b^2 x y^3 - 54 b w x y^2 - 54 b 
x^3 y^2 - 54 b x y^4 - 27 w x y^3 - 27 x^3 y^3 - 27 x y^5)^2 + 4 (3 b^4 + 12 b^3 y + 3 b^2 w + 18 b^2 y^2 + 6 b w y + 12
 b y^3 + 3 w y^2 + 3 y^4)^3) - 54 b w x y^2 - 54 b x^3 y^2 - 54 b x y^4 - 27 w x y^3 - 27 x^3 y^3 - 27 x y^5)^(1/3)/(3 
2^(1/3) (b + y)) - (2^(1/3) (3 b^4 + 12 b^3 y + 3 b^2 w + 18 b^2 y^2 + 6 b w y + 12 b y^3 + 3 w y^2 + 3 y^4))/(3 (b + y) (-27
 b^2 w x y - 27 b^2 x^3 y - 27 b^2 x y^3 + sqrt((-27 b^2 w x y - 27 b^2 x^3 y - 27 b^2 x y^3 - 54 b w x y^2 - 54 b x^3 y^2
 - 54 b x y^4 - 27 w x y^3 - 27 x^3 y^3 - 27 x y^5)^2 + 4 (3 b^4 + 12 b^3 y + 3 b^2 w + 18 b^2 y^2 + 6 b w y + 12 b y^3 +
 -  3 w y^2 + 3 y^4)^3) - 54 b w x y^2 - 54 b x^3 y^2 - 54 b x y^4 - 27 w x y^3 - 27 x^3 y^3 - 27 x y^5)^(1/3)) + (b x + x 
 - y)/(b + y) 

where b + y!=0

Brief change log

  • Implemented solveCfmmMulti function in x/gamm/pool-models/stableswap/amm.go using the derivations set up in code comments
  • Implemented binary search logic for this new multi-asset CFMM
  • Set up tests for multi-asset pools and expanded existing two-asset stableswap tests to also check consistency between our two CFMM implementations
  • Cleaned up/simplified the math for the solver to make it more readable and simpler to compute (e.g. limiting the max bitlen to avoid risking integer overflows)

Testing and Verifying

The tests for this change line up in coverage with those for our two-asset stableswap CFMM and can be found in amm_test.go.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (yes)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (no)
  • How is the feature or change documented? (not documented)

@AlpinYukseloglu AlpinYukseloglu requested a review from a team May 5, 2022 15:24
@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label May 5, 2022
@AlpinYukseloglu AlpinYukseloglu changed the title multi-asset swap / LP equations, and tests solver for multi-asset CFMM May 5, 2022
@codecov-commenter
Copy link

codecov-commenter commented May 6, 2022

Codecov Report

Merging #1429 (faefe39) into main (f56fbe5) will decrease coverage by 0.31%.
The diff coverage is 13.75%.

@@            Coverage Diff             @@
##             main    #1429      +/-   ##
==========================================
- Coverage   19.82%   19.51%   -0.32%     
==========================================
  Files         202      226      +24     
  Lines       27685    30995    +3310     
==========================================
+ Hits         5489     6048     +559     
- Misses      21175    23842    +2667     
- Partials     1021     1105      +84     
Impacted Files Coverage Δ
x/epochs/client/cli/query.go 0.00% <ø> (ø)
x/gamm/pool-models/stableswap/amm.go 25.43% <0.00%> (-30.34%) ⬇️
x/gamm/pool-models/stableswap/msgs.go 0.00% <0.00%> (ø)
x/gamm/pool-models/stableswap/pool.go 0.00% <0.00%> (ø)
x/gamm/pool-models/stableswap/stableswap_pool.go 0.00% <0.00%> (ø)
x/gamm/types/pool.go 0.00% <ø> (ø)
x/incentives/client/cli/query.go 0.00% <ø> (ø)
x/incentives/keeper/distribute.go 61.00% <0.00%> (ø)
x/incentives/keeper/hooks.go 50.00% <0.00%> (ø)
x/incentives/module.go 51.11% <0.00%> (+1.11%) ⬆️
... and 53 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b71507c...faefe39. Read the comment docs.

@AlpinYukseloglu AlpinYukseloglu self-assigned this May 6, 2022
@mergify mergify bot merged commit 72e1c42 into main May 6, 2022
@mergify mergify bot deleted the multi-asset-stable-solver branch May 6, 2022 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge C:x/gamm Changes, features and bugs related to the gamm module.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[x/gamm][StableSwap] multi-asset swap / LP equations, and tests
3 participants