#+Title: Comparisons of Continuous-Time Swap Designs #+Subtitle: A ETHTallinn 2024 Talk #+Author: Miao, ZhiCheng (Superfluid Protocol) #+Email: miao@superfluid.finance #+OPTIONS: num:nil toc:nil timestamp:nil #+REVEAL_INIT_OPTIONS: navigationMode: "linear", transitionSpeed: "fast" #+REVEAL_THEME: night #+REVEAL_TRANS: Concave #+REVEAL_EXTRA_CSS: ../css/sf-slide-dark2022.css #+REVEAL_TITLE_SLIDE_BACKGROUND: ../images/sf-slide-dark2022-bg1.png #+REVEAL_DEFAULT_SLIDE_BACKGROUND: ../images/sf-slide-dark2022-bg1.png #+STARTUP: overview #+begin_notes :revisions | Date | Notes | | 2024-04-04 | EthereumZuri.ch 2024 presentation | | 2024-04-19 | ETHTallinn 2024 presentation | #+end_notes * What You Will Hear About #+ATTR_REVEAL: :frag (roll-in) - Why you should trade time-continously. - What different designs of decentralized exchange there are. - A Comparison of these designs. * Nightmares for a Trader ** Most of Us Suck at Market Timing *** Buy High Sell Low #+ATTR_HTML: :width 70% [[file:bitcoin-buy-high-sell-low.png]] *** Invested All Days vs. Missed Best Days #+ATTR_HTML: :width 70% [[file:dca-1.png]] - Reference: https://advisor.visualcapitalist.com/cost-of-trying-to-time-the-market/ *** Best Days in the Market #+ATTR_HTML: :width 70% [[file:dca-2.png]] - Reference: https://advisor.visualcapitalist.com/cost-of-trying-to-time-the-market/ ** Front-run by MEV Bots *** MEV Terminology #+ATTR_REVEAL: :frag (roll-in) - *user*: a normal ethereum user who sends transactions. - *searcher*: advanced ethereum user specialized in finding MEV opportunities and sending advanced transaction types like bundles. - *builder*: party specialized in the construction of ethereum execution payloads using transactions received from users and searchers (trusted by searchers and users for fair inclusion). - *validator*: party which signs and submits a beacon block to the network. It is also called proposer, hence the term PBS (Proposer Builder Separation). *** MEV Supply Chain (Without Proposer Builder Separation) #+ATTR_HTML: :width 70% [[file:mev-supply-chain.png]] /(courtesy to [[https://flashbots.mirror.xyz/]])/ *** The Nightmware: Sandwich Attack #+ATTR_HTML: :width 55% [[file:mev-sandwich.png]] This is possible because the transaction is transparent through out the supply chain (even with PBS, where 95% of validators are utilizing it using MEV-Boost.) #+ATTR_HTML: :width 55% [[file:pbs-architecture.png]] * The Solution: Model Swaps In Continuous-Time Let swaps happen (virtually) every block, modeled with continuous-time: - avoid market timing. - minimize the single-block MEV risk. * Design 1 - "Flowswap": Adding Continuous-Time to Uniswap V2 ** CFMM (Constant Function Market Maker) without Time #+ATTR_HTML: :width 50% [[file:CLP-without-time.png]] - The curve represent the constant function used: constant (liquidity) product. - A swap is instant, and it moves the product from blue point to red point instantly. ** CFMM with Time #+ATTR_HTML: :width 70% [[file:CLP-reactified.png]] - With /some magic/, to be explained shortly, we make the "point" moves smoothly along the curve over time. ** COMMENT Search Solutions Using SageMath /SageMath is a free open-source mathematics software system licensed under the GPL./ *** Define CLP: #+begin_src python from sage.all import var, assume, function, solve def CLP(x, y, x_prime, y_prime): """Constant Liquidity Product Swap""" return x * y == x_prime * y_prime #+end_src *** Validate Instant-Swap Solution #+begin_src python def CLP(x, y, x_prime, y_prime): """Constant Liquidity Product Swap""" return x * y == x_prime * y_prime def solve_clp_a4b(): print("# Solve CLP equation for selling A for B instantly\n") v_a = var("a_Δ") v_b = var("b_Δ") clp = CLP( L_a, L_b, L_a + v_a, L_b + v_b ) sols = solve(clp, v_b) assert(len(sols) == 1) print(sols[0]) print("\n") #+end_src Constant (Liquidity) Product Formula: $$L_{b\Delta} = \frac{L_b * a_\Delta}{L_a + a_\Delta}$$ *** Search Continuous-Time Solution Using a Helper Variable ~q~ #+begin_src python def CLP(x, y, x_prime, y_prime): """Constant Liquidity Product Swap""" return x * y == x_prime * y_prime def solve_rclp_rtb_bidir(): print("# Solve Reactive CLP rtb_bidir equation\n") cf_a = r_a * (t - t_0) cf_b = r_b * (t - t_0) q = var("q") clp = CLP( L_a, L_b, L_a + cf_a + q * cf_b, L_b + cf_b + 1/q * cf_a ) sols = solve(clp, q) print("L_{flowswap_a} =", (1/q * cf_a).subs(sols[0])) print("L_{flowswap_b} =", (q * cf_b).subs(sols[0])) print("\n") #+end_src "Reactified" Constant (Liquidity) Product Formula: $$L_{flowswap_a} = \frac{-(r_b * t_\Delta - L_b) * r_a * t_\Delta}{r_a * t_\Delta + L_a}$$ ** Properties #+ATTR_REVEAL: :frag (roll-in) - Liquidity bootstrap required (TVL == funds at risk): *YES* - Complexity to provide liquidity: *SIMPLE*, almost same as Uniswap V2 - MEV protection: *SAFE* * Design 2 - ZILMM (Zero-Intermediate-Liquidity Market Maker) ** Setting Up Liquidity #+ATTR_HTML: :width 70% [[file:aqueduct-1.png]] - A LP sends two money streams, one for token A, one for token B, and receives them back simultaneously. ** Earning Fees From Traders #+ATTR_HTML: :width 70% [[file:aqueduct-2.png]] - A trader sends one money stream for token A, in exchange for another money stream for token B. - LPs provide the difference, and take fees from the streams. ** Properties /Like an "Aqueduct" that routes money streams./ #+ATTR_REVEAL: :frag (roll-in) - Liquidity bootstrap required (TVL == funds at risk): *NO* - Complexity to provide liquidity: *HIGH*, due to its unconventional model - MEV protection: *SAFE* * Design 3 - TOREX (Twap-ORacle EXange) ** Uniswap V3 TWAP *** TWAP In Brief #+ATTR_REVEAL: :frag (roll-in) - TWAP stands for “Time Weighted Average Price”. TWAP was added in Uniswap v2 and improved in Uniswap v3. - A /price cumulative/ is re-calibrated with each swap, its value changes per each block. - One can observe price cumulatives of two time points, and calculate the average price of that time window. *** Calculate Price From TWAPs #+ATTR_HTML: :width 70% [[file:uniswap-v2-twap.png]] Note: Uniswap V3 differs in using geometric mean instead. *** TWAP Observer In Action #+ATTR_HTML: :width 70% [[file:twap-observations.png]] Comparing two weeks data of OP-USDC spot prices (from Yahoo Finance) and the average prices sinces the last observation (green dots): ** TOREX In Motion #+ATTR_HTML: :width 70% [[file:torex-in-motion.png]] ** Properties #+ATTR_REVEAL: :frag (roll-in) - Liquidity bootstrap required (TVL == funds at risk): *NO* - Complexity to provide liquidity: *SIMPLE*. - One benchmark price, - flexible, agnostic liquidity source, - and competitive. - MEV protection: *SAFE* for traders; a competitive *challenge* to be solved by the liquidity movers. * Design 4 - TWAMM (Time-Weighted Average Market Maker) - Reference: https://www.paradigm.xyz/2021/07/twamm - "The Time-Weighted Average Market Maker (TWAMM) provides the on-chain equivalent of the TWAP order." - "Because it processes trades in between blocks, it is also less susceptible to sandwich attacks." * Comparisons | Design | Liquidity Bootstrap (*) | Complexity to Provide Liquidity | MEV Protection | |------------+-------------------------+---------------------------------+----------------| | "FlowSwap" | Required | Same as Uniswap V2 | Safe | | ZILMM | Not Required | High and unconventional | Safe | | TOREX | Not Required | simple, flexiby and competitive | Safe (**) | - (*) Liquidity bootstrap requires higher TVL, and TVL == funds at risk. - (**) Liquidity movers must mitigate the MEV risks themselves to compete. * The Enabler: Composable Money Payments Without it, these designs cannot be implemented effectively. ** Superfluid Super Tokens - ERC20 compatible. - Wrapping existing ERC20 tokens (upgrade) or pure super tokens. - Providing programmable money streams. - Composable, hence the "just-in-time" liquidity that minimizes idle liquidity. ** Superfluid Payment Primitives #+ATTR_HTML: :width 70% [[file:payment-primitives.png]] * COMMENT Some Additional Theory: Functional Reactive Programming #+ATTR_REVEAL: :frag (roll-in) - Modeling a problem domain explicitly with time is also called functional reactive programming (FRP). - The original formulation of functional reactive programming can be found in the ICFP 97 paper Functional Reactive Animation by Conal Elliott and Paul Hudak. - Hence, we also call exchanges that do continuous-time swaps “reactive exchanges.” * Thank You For Listening! Following the progress of TOREX implementation on: - https://warpcast.com/superboring - https://warpcast.com/superfluid Follow me on: - https://warpcast.com/hellwolf #+REVEAL_HTML: