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

Implement Stableswap for 2 assets #10

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

prism0x
Copy link

@prism0x prism0x commented May 1, 2022

This is a work in progress to implement the functions \varphi, \nabla\varphi and find_arb! for 2 asset Stableswap.

Check out the following write-up for more details.
https://hackmd.io/@prism0x/stableswap-optimal-routing

@angeris angeris self-requested a review May 19, 2022 23:34
@angeris
Copy link
Collaborator

angeris commented May 19, 2022

Thanks for the PR!

Below are some quick comments, first on general style and then on the actual implementation. :)

Example files (and generally in packages) shouldn't include the Pkg.activate() calls since it is assumed that the package is in the active environment (due to it being installed or activated as the current env, for dev purposes). Additionally instantiate() shouldn't be called within a package as it should be handled by the package manager for the end user.

I think there was an auto-formatting tool being used which has added spacing for a number of operations? (e.g., line 178/179 of cfmms.jl.) The usual standard is that more-than-one-level-deep nested operations have no spacing, while other operations are generally allowed to use spacing and is up to discretion. This makes it easier to read which symbols are close together, especially when things are being multiplied/divided, etc.

On the technical side: I think it's possible to compute the no-arbitrage trade analytically in the two-coin case of Stableswap, no? In particular, given that k is fixed for the trade, solving the resulting equation should give you a quadratic, which has a simple solution—unless I'm missing something!

@angeris angeris self-assigned this May 19, 2022
@angeris angeris removed their request for review May 19, 2022 23:52
@prism0x
Copy link
Author

prism0x commented May 21, 2022

Thanks, will fix the style errors. I had done it like that because I wasn't that familiar with Julia at that point, thanks for taking the time to comment on them.

Regarding analytic solution of Stableswap --- it wasn't quadratic as far as could derive. I simplified the objective function that takes the output quantity down to:

$$ \Pi(\Delta_\alpha) = m_p\Delta_\alpha - \frac{\sqrt{\alpha^2 + \beta} - \alpha}{2} + R_\beta $$

with

$$ \begin{aligned} \alpha(r, k) &= r-k + \frac{k}{4A} \\ \beta(r) &= \frac{k^3}{4Ar} \\ r &= R_\alpha - \Delta_\alpha/\gamma \end{aligned} $$

So it's a bit hairy with the square root and all. $\varphi$ in terms of input/output amounts is quadratic---that where the square root comes from, after taking its inverse to plug it into the objective function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants