HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    Write
    Owners
    • Owners
    • Signed-in users
    • Everyone
    Owners Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Sifchain: AMM Specification ## Summary In this analysis we focus on the economics of the automated market maker with special attention to fees. The attached notes from the [Thorchain documentation](https://docs.thorchain.org/how-it-works/continuous-liquidity-pools#slip-based-fee-model-clp) provide notation for and rudimentary analysis of uniswap style, constant product automated market makers. These notes address the constant fee model which exists in Uniswap and an alternative fee model in use by Thorchain. It goes on to identify the canonical form of all contemporary KYC AMM DEXes. The note addresses the concept of virtual depth but provides no analysis thereof. We focus our attention on the composition between the swapping mechanisms and the add/remove liquidity mechanisms. More complex fee structures may introduce unintended consequences in the form of "path planning" attacks that involve carefully sequencing (eg sandwiching) swap actions with add/remove liquidity actions to save on fees. ## Definitions In this section, we will rely on the definitions as represented in the Thorchain documentation for ease of collaboration, although in future documents we use slightly different (more signal processing and control theory oriented) notation in our work on the subject. ### State Space Consider a positive orthant with coordinants $(X,Y)$ where $X,Y>0$. We will let $X$ denote the prior quanitity of the token to be swapped into the AMM and $Y$ to be the prior quantity of the token to be swapped out. The action taken by a trader is denoted $x=\Delta X$ and the results number of tokens to be issued to the trader in the output of this transaction is $y=\Delta Y$. ![](https://i.imgur.com/AYTYVwc.png) ### Actions available While the focus of this note is on the swapping mechanism, that mechanism is dependent on the existence of an add/remove liquidity mechanism. For simplicity we will assume that the add/remove liquidity mechanism is a simple linear price preserving mechanism. #### Adding and removing liquidity Denote the price with units $Y$ per $X$ as $P = \frac{Y}{X}$, then adding liquidity can be modeled as simply adding $(x,y)$ such that $\frac{Y+y}{X+x} = \frac{Y}{X} = P$. Noting that in practice adding liquidity in only one token is supported but its a functional composition of this primitive with the swap primitive. Adding liquidity issues liquidity tokens (such that supply expands proportional to the increase in liquidity). Additionally they may be burned to withdraw $(x,y)$ such that $\frac{Y+y}{X+x} = \frac{Y}{X} = P$ with the $x=\alpha X<0$, and $y=\alpha Y<0$ where theta is the share of the total supply of liquidity tokens beeing burned. ![](https://i.imgur.com/eIxzMJt.png) #### Swapping The swap primitive is explored in detail in this note but may be summarized in its general form as: Given a prior state $(X,Y)$ and the deposit action $x=\Delta X$, the swap mechanism will return a quantity of tokens $y = f(x; X,Y)$. In order for such a policy to be meaningful it is prudent to derive it as an invariant preserving map. For simplicity in this note, we assume $x$ is added to the pool and $y$ is removed, but this can be reversed without loss of generality. Noting that the generality is lost when assymmmetry is introduced, eg. when synthetic depth is introduced. In order to design practical invariants that preserve properties of interest to Sifchain, we rely on a range of metrics which are computable over the state-space and the set of valid actions conditioned on a point in that statespace. ### Some Metrics of Interest Even with such a simple state space there are a range of metrics of interest which can be used to construct analytic invariants. Such invariant choices should not be arbitrary but rather determined based on the desired trader and/or liquidity provider user experience. **(Prior) Spot Price** $$ P = \frac{Y}{X}$$ **(Posterior) Spot Price (for a swap)** $$ P^+ = \frac{Y-y}{X+x}$$ **Realized Price (for a swap x for y)** $$\bar P = \frac{y}{x} $$ **Price Slippage (normalized)** $$\frac{P-\bar P }{P} = 1-\frac{\bar P}{P}= 1-\frac{Xy}{xY}$$ **Liquidity Level** $$ K = X\cdot Y $$ **Pool Value (local, in X units)** $$ 2X= X+\frac{Y}{P} = X + \frac{X}{Y}Y$$ **Pool Value (3rd party $\hat P$)** $$X+ \frac{Y}{\hat P} $$ **Arbitrage Equality (3rd party $\hat P$)** $$x\hat{P}= y+\epsilon = x\bar P +\epsilon $$ **Arbitrage Profit (in Y units)** $$\epsilon = \frac{\hat P - \bar P}{\hat P}x$$ Note that when designing a fee structure it is important to look at KPIs associated with the user's experience, eg. slippage, impermanent loss, and arbitrage opportunities. ### Existing Designs A brief review of existing designs with visuals #### Constant Product Market Maker: No Fees ![](https://i.imgur.com/VnC5hof.png) This is the base design that most people reason about because it is the most intuitive. This is an idealized uniswap model. #### Constant Product Market Maker: Flat Fee ![](https://i.imgur.com/5fLtNhv.png) There is an [implementation of uniswap in cadCAD](https://github.com/cadCAD-org/demos/blob/master/demos/uniswap/simplified-uniswap.ipynb) and an [overview with video of the model](https://community.cadcad.org/t/modeling-uniswap-in-cadcad/35). #### Thorchain "Slip fee" (CLP) ![](https://i.imgur.com/s1JiH2R.png) This can be viewed as setting the fee $\phi$ according a regularizing rule that penalizes slippage and thus larger transactions, incentivizing smaller transactions, but also introduces angles for gaming the system to minimize fees. The derivation can be found in the [Thorchain docs](https://docs.thorchain.org/how-it-works/continuous-liquidity-pools#clp-derivation). Comparing: $$y_{ff} = \frac{xY}{x+X}(1-\phi)$$ with $$y_{clp} = \frac{xYX}{(x+X)^2} =\frac{xY}{x+X} \frac{X}{x+X}$$ so $$(1-\phi)=\frac{X}{x+X} = 1 - \frac{x}{x+X}$$ this is simply choosing a functional fee, as $$ \phi = \frac{x}{x+X}$$ #### "Slip fee" with virtual depth ![](https://i.imgur.com/7KfmOE6.png) Has analogies to connector weights in [Bancor](https://medium.com/@billyrennekamp/converting-between-bancor-and-bonding-curve-price-formulas-9c11309062f5#) and to non-uniform pools in [Balancer](https://medium.com/balancer-protocol/bonding-surfaces-balancer-protocol-ff6d3d05d577). #### Generalized Fees ![](https://i.imgur.com/KiXMZ8J.png) For choice invariant $V(X,Y)$ determine $f(x;X,Y)$ such that for all $x>0$ we have $$V(X+x, Y-f(x;X,Y))=V(X, Y)$$ In order to call it a fee relative to a constant product market maker, there needs to be value accrued to the liquidity proviers which is accomplied by $$(X+x)\cdot (Y- f(x; X,Y))>X\cdot Y $$ It is also prudent to satisfy the boundary condition that the non-action yields no payout $f(0;X,Y)=0$ for all $(X,Y)$ as well as for the limit of the realized price to be the spot price: $$\lim_{x\rightarrow 0^+} \frac{f(x; X,Y)}{x}= \frac{Y}{X} =P$$ that latter denoting that the realized price should approach the spot price as the transaction size get arbitrarily small. It is also prudent to consider whether the fee mechanism damages the liquidity pool's capacity to function as an estimator. The mathematics of AMMs as estimators are minor variations on the mathematics for bonding curves as estimators as presented in this academic paper: [economic games as estimators](https://pdfs.semanticscholar.org/c1c2/2e06ac3fab0bfdf08bfc85a837da13fe16c2.pdf).

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in via Google

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully