Caspian, an L2-Powered AMM

A DeFi-friendly L2 AMM

StarkWare
StarkWare
Apr 4 · 5 min read

TL;DR

  • Capital efficiency is a major source of concern when considering L2 solutions for DeFi.
  • The reason: it is assumed that liquidity will fragment between L1 and multiple L2 solutions.
  • Presenting Caspian: We propose a new L2-powered AMM design. This AMM leaves assets on L1, resulting in defragmented liquidity, and better capital efficiency.
  • StarkEx 3.0 (Coming in June 2021) will support Caspian.
    It will include two new primitives required for implementing Caspian: L1 Limit Orders, and Batch-Long Flash Loans.

Background

AMMs are an important DeFi tool. Its core qualities — autonomy, composability & liquidity bootstrapping — make AMMs the first building block of the DeFi lego. Since its start with Bancor and Uniswap, the AMM design space has been developed in many directions: from specialized markets (e.g. Curve), through generalized markets (e.g. Balancer) to impermanent-loss-aware markets (e.g. Dodo or Bancor V2).

How do AMMs fit in with the fast-growing L2 scaling landscape?

Up till now, L2 AMM designs entailed moving the AMM in its entirety to L2, including liquidity, resulting in undesirable fragmentation of L1 liquidity.

We propose an L2-powered AMM design that allows liquidity to remain on L1, and users to trade on L2.

Beyond allowing L1-based Liquidity Providers (LPs) to participate in the AMM, this design also allows:

  • Liquidity to be deposited in other L1 DeFi protocols (work initiated by BentoBox and Balancer V2).
  • Cross-L2 AMMs (described here).

What is Caspian?

Caspian is an L2-powered AMM. Just like the Caspian Sea — the world’s largest lake — the Caspian AMM allows liquidity to be bridged on L2 while remaining unfragmented, on L1.

Caspian has an off-chain Operator, which processes batches of L2 trades. The Operator is the sole entity trading against an L2 powered AMM contract on L1. It mimics the contract’s logic, and offers trade quotes based on the state of the AMM at the beginning of the batch, and the subsequent L2 trades included in the batch.

At the end of the batch, the Operator settles all trades by executing the net difference against the L2 powered AMM contract. The Operator must mimic the contract’s logic, and apply it to the correct state of accounts, in order to ensure that the L1 contract will be able to serve as a counterparty to the L2 trades.

Off-chain

  • Operator: a risk-less market maker existing on L1 and L2s.
    The Operator functions as a coordinator. Its sole purpose is to match traders within each batch with one another and match the net difference against the L1 contract. It settles batches of trades using STARK proofs.
    Note the Operator is only trusted to fairly order all trades. To remove this point of centralization, the Operator itself could be decentralized.
  • Traders: Alice and Bob are traders with funds on L2.
  • Liquidity Providers: Carol is an LP on L1.

On-chain

L2 Powered AMM Smart Contract
This contract presents a standard AMM interface with two major modifications:

  • Only the Operator can trade against this contract on L1.
  • Withdrawal is subject to the AMM Cooldown Period (ACP).
    Why is an ACP needed?
    Recall that off-chain, the Operator offers Alice a price quote. Following this quote, if the L1 AMM’s liquidity increases, the price improves; conversely, if the AMM’s liquidity decreases, the price worsens. For that reason, deposits can be instant but withdrawals must not be serviced before the end of an open batch.

StarkEx: The smart contract connecting the AMM’s logic with the off-chain StarkEx scalability engine.

As we said previously, StarkEx will soon add two new features: L1 Limit Orders and Batch-Long Flash Loans. We already described L1 Limit Order in our DeFi Pooling post. Let us introduce Batch-Long Flash Loans.

Batch-Long Flash Loan is a flash loan that is not limited to a single transaction; instead, it extends for the duration of a batch. It offers the Operator the ability to mint tokens on L2, as long as these tokens are burned by the end of the batch.

This improves the Operator’s efficiency to serve as a middle man, simplifying the merger of multiple Limit Orders into a single one, or routing trades within the L2.

Basic Operations

  1. Alice/Bob requests/approves a quote from the Operator
  2. Operator matches the quotes with itself.
    To avoid holding liquidity, the Operator can take a batch-long flash loan (which, by definition, is paid back by the end of the batch).

Because the Operator abides by the AMM formula, they can:

  1. Generate an L1 Limit Order from the AMM
  2. Match the net difference (which equals the batch-long flash loan) with the L1 Limit Order
  3. Prove batch/Send proof
  4. Update the state

For LPs, there is no difference whatsoever between L1 and L2. They interface with the AMM via its L1 smart contract, without having to know anything about its inner workings off-chain. Let’s look at the two basic operations for an LP - deposit and withdraw:

Deposit is an atomic operation: Carol deposits her funds on L1 and receives her LP token. The Operator monitors the chain to include new deposits to their quote.

Withdraw is a three-step operation

  1. Carol locks her LP tokens, thus triggering the AMM Cooldown Period (ACP)
  2. The Operator must serve this request by the end of the ACP
  3. If the Operator denies Carol the service, after the ACP she can withdraw at the latest known AMM ratio, directly from the AMM smart contract

Conclusion

To bring back DeFi’s primary goal of financial inclusion, we need to reduce transaction costs and avoid liquidity fragmentation at all costs.
Caspian, an L2-powered AMM, is a step in this direction. It minimizes the L2 logic while providing better capital efficiency, enabling L2 liquidity to be used as superfluid collateral, i.e. to both serve as collateral and produce returns. We continue to actively explore and research the design space around Caspian.

Louis Guthmann

StarkWare

Developing the Full Proof Stack for STARK

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store