Skip to content

Presentation compare continuous time swaps

Miao, ZhiCheng edited this page Apr 5, 2024 · 4 revisions

Comparisons of Continuous-Time Swap Designs

⚠ This file is automatically generated from this org file using this script.

The exported presentation can be accessed via this githack link.

REVISIONS:

Date Notes
2024-04-04 EthereumZuri.ch 2024 presentation

What You Will Hear About

  • Why you should trade time-continously.
  • What the different designs of decentralized exchange are.
  • Beneath the surface: functional reactive programming.

Nightmares for a Trader

Most of Us Suck at Market Timing

Buy High Sell Low

Invested All Days vs. Missed Best Days

  • Reference: https://advisor.visualcapitalist.com/cost-of-trying-to-time-the-market/

Best Days in the Market

  • Reference: https://advisor.visualcapitalist.com/cost-of-trying-to-time-the-market/

Front-run by MEV Bots

MEV Terminology

  • 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)

(courtesy to https://flashbots.mirror.xyz/)

Sandwich Attack

This is possible because the transaction is transparent through out the supply chain (with PBS, where 95% of validators are utilizing it.)

The Solution: Model Swaps In Continuous-Time

Let swaps happen (virtually) every block, modeled with continuous-time:

  • avoid market timing.
  • minimize single-block MEV.

Design 1 - “Flowswap”: Adding Continuous-Time to Uniswap V2

CFMM without Time

CFMM with Time

Search Solutions Using SageMath

SageMath is a free open-source mathematics software system licensed under the GPL.

Define CLP:

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

Validate Instant-Swap Solution

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")

Constant (Liquidity) Product Formula: $$L = \frac{L_b * a_Δ}{L_a + a_Δ}$$

Search Continuous-Time Solution Using a Helper Variable q

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")

“Reactified” Constant (Liquidity) Product Formula: $$Lflowswap_a = \frac{-(r_b * t_Δ - L_b) * r_a * t_Δ}{r_a * t_Δ + L_a}$$

Properties

  • Liquidity bootstrap required (TVL == funds at risk): Yes
  • LP Complexity: almost same as Uniswap V2
  • Single-block MEV: safe

Design 2 - ZILMM (Zero-Intermediate-Liquidity Market Maker)

Providing Liquidity

Earning Fees as LP

Properties

Like an “Aqueduct” that routes money streams.

  • Liquidity bootstrap required (TVL == funds at risk): No
  • LP Complexity: high complexity with an unconventional model
  • Single-block MEV: safe

Design 3 - TOREX (Twap-ORacle EXange)

Uniswap V3 TWAP

TWAP In Brief

  • 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

Note: Uniswap V3 differs in using geometric mean instead.

TWAP Observer In Action

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

Properties

  • Liquidity bootstrap required (TVL == funds at risk): No
  • LP Complexity: simple (one benchmark price), flexible (agnostic liquidity source) and competitive
  • Single-block MEV: safe for traders, leaving the challenges to competitive 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.”

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

Comparisons

Design Liquidity Bootstrap Required LP Complexity MEV Safety
“FlowSwap” Yes Same as Uniswap V2 Safe
Zero-intermediate-liquidity Market Maker No High and unconventional Safe
Twap-ORacle EXange (TOREX) No simple, flexiby and competitive Safe*

(*) Liquidity movers must mitigate the challenge themselves to compete.

Some Additional Theory: Functional Reactive Programming

  • 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

Following the progress of TOREX implementation on:

  • https://warpcast.com/superboring
  • https://warpcast.com/superfluid

The source of this slide is available from https://github.com/superfluid-finance/protocol-monorepo/wiki by searching “continuous time swaps” Presentation-compare-continuous-time-swaps/source-from-github-wiki.png

Clone this wiki locally