dAMM, an L2-Powered AMM
- 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 dAMM (formerly named Caspian)— decentralized AMM. 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 dAMM.
It will include two new primitives required for implementing dAMM: L1 Limit Orders, and Batch-Long Flash Loans.
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 dAMM?
dAMM is an L2-powered AMM. It allows liquidity to be bridged on L2 while remaining unfragmented, on L1.
dAMM 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.
- 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.
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.
What’s New? Batch-Long Flash Loans
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.
- Alice/Bob requests/approves a quote from the Operator
- 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:
- Generate an L1 Limit Order from the AMM
- Match the net difference (which equals the batch-long flash loan) with the L1 Limit Order
- Prove batch/Send proof
- 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
- Carol locks her LP tokens, thus triggering the AMM Cooldown Period (ACP)
- The Operator must serve this request by the end of the ACP
- 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
To bring back DeFi’s primary goal of financial inclusion, we need to reduce transaction costs and avoid liquidity fragmentation at all costs.
dAMM, 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 dAMM.