TWAMM Research: June 2022

What a slow and uneventful month in crypto…🙄. The contagion from Luna's blow-up brought down 3AC, which continued to wreak havoc across the CeFi ecosystem 🎢. Most of the robust DeFi lending platforms seemed to have escaped, but some almost committed the cardinal sin to survive the fallout -- more on that below.

This month we took time to elaborate on some of the not-so-well understood TWAMM concepts & questions we hear in our conversations with customers. Additionally, since we’re in the final stretch of development, we will be pausing technical write-ups until post-launch.

TWAMM Advantages & Concepts

Solend, a DEX on Solana, proposed to hijack control of a whale wallet to liquidate it via OTC instead of DEXs so they can get a better fill and not crash Solana’s price. In the following sections, we show how TWAMMs could have enabled them to get OTC advantages without sacrificing the permissionless and openness of DeFi.

OTC trading is rife with issues like reliance on a centralized party, lack of transparency, arbitrary pricing, KYC/AML red tape, adversarial, and most of all non-composable. However, OTC desks provide three big advantages over DEXs: lower slippage, zero gas fees, and privacy.

Lower Slippage: Arbitrageurs & Liquidity Sink

Traditional DEXs require a large amount of liquidity or concentrated liquidity to avoid causing massive slippage in a single swap. Even with a large amount of liquidity, DEXs cannot process massive trades, i.e. $100s of millions, as was the case in Solend liquidation.

Even when massive trades are separated into smaller trades on DEXs, they still may need to find LPs to add new liquidity to the pools to achieve target slippage rates. Protocols often rely on high liquidity OTC desks to address this and other challenges when executing massive orders.

Arbitrageurs source liquidity from external sources to correct TWAMM prices
Arbitrageurs source liquidity from external sources to correct TWAMM prices

TWAMMs separate trades into smaller pieces, so the liquidity required to lower slippage is orders of magnitude smaller. For example, a $100M trade over 10,000 blocks means each sub-order is roughly $10,000. A TWAMM liquidity pool with ~$2M in assets can process these trades with ~1% slippage due to arbitrageurs correcting the price.

Additionally, TWAMMs act like a liquidity sink because they incentivize market participants to source liquidity from external sources (DEXs, Lenders, OTC desks, CEXs, etc), resulting in arbitrage profits by correcting the pool’s asset prices. Even the most liquid OTC desk or DEX is restricted to assets in their order book.

Cheaper Gas Fees: Virtual Orders

OTC trading can run advanced algorithms and order splitting to get the best fill because they’re not constrained by gas costs. Meanwhile, DeFi’s go-to solution has been to use an aggregator or manually split large orders over multiple blocks into smaller chunks. However, this has multiple drawbacks including gas fees for each transaction, sub-order granularity tradeoffs, a multitude of MEV attacks (sandwich, front running), and human error.

TWAMMs lazy-loads virtual orders to only execute on-chain when participants interact with underlying AMM
TWAMMs lazy-loads virtual orders to only execute on-chain when participants interact with underlying AMM

In TWAMM, each sub-order is happening virtually in the background and is only executed or written to the chain when a new interaction with underlying AMM happens (swap, mint, burn, etc). Therefore, a long-term swap trader only pays the gas cost of starting, canceling, or withdrawing order proceeds.

ZK Privacy Solution: Aztec Connect

Finally, the cherry on top, the pièce de résistance -- private TWAMMs. During our conversations with customers, privacy was a concern that was brought up as a potential drawback of using TWAMMs for large trades, a really useful feature of OTC desks. Although TWAMMs avoid most MEV attack vectors, the current solution to combat copy traders is canceling your long-term swap and re-starting at another time.

Identity preserving trading solution leveraging Aztec ZK Rollups
Identity preserving trading solution leveraging Aztec ZK Rollups

With the help of Aztec Connect’s novel privacy solution, we’re able to offer the same privacy benefits of OTC desks without sacrificing the benefits of DeFi. In the above image, the user’s identity is obfuscated and the on-chain sleuths are unable to detect who’s executing the trade unless the user withdraws the trade proceeds back to the originating wallet.

Product Development

In preparation for mainnet release, we’ve started creating detailed code documentation, diagrams, and testing to help auditors, integrators, and community members understand the nuances of our product.

Part of the documentation involves creating detailed documentation on the complex code flows and code hierarchies to ensure every aspect of TWAMM is being properly analyzed and tested.

Conventions and function analysis spec for Balancer TWAMM integration
Conventions and function analysis spec for Balancer TWAMM integration

Documenting Code Paths

A visual representation of each major function has been made so developers can understand and follow the logic flow of the various libraries & dependencies.

Flow diagram for initialMint with dependencies, data structures, and assertions
Flow diagram for initialMint with dependencies, data structures, and assertions

Initial Gas Measurements

Finally, we started measuring and optimizing gas costs for Balancer’s Vault + TWAMM integration. We will have a detailed post on the optimizations and testing methodologies in a future technical write-up.

Preliminary gas measurements for various functions and improvements in each version
Preliminary gas measurements for various functions and improvements in each version

Upcoming Work

As mentioned earlier, we’re getting closer to the feature freeze and starting the code review process of TWAMM. We will begin open-sourcing our code to a few early partners initially. Please reach out via Twitter if you’d like access and are interested in integrating our product.

Subscribe to 0x70626a
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.