TWAMM (Time-Weighted Automated Market Maker)
Time-Weighted automated market maker (TWAMM) is an idea proposed by Paradigm that brings together the ideas in AMMs (Automated Market Maker) and TWAP (Time-Weighted Average Price).
The TWAMM protocol contains an embedded AMM and allows traders to place block orders that execute over a set time. As time passes and the trades in the embedded AMM cause its price to deviate from the market, arbitragers will trade against the pool to bring the price back in line with the market. This allows trades with TWAMM to track TWAP of an asset.
An automated market maker (AMM) is a type of DeFi protocol that allows users to trade assets. This works by creating a pool of two or more assets and opening users to trade against these liquidity providers.
As we’ve looked at in previous posts, traditional AMMs run with different constraints. While they allow for permissionless trading and market making, there are a host of downsides for traders and LPs.
- Traders encounter high price impact when trading in large amounts on chain.
- Liquidity providers are subject to impermanent loss and adverse selection, often greater than what they gain in income from fees.
Time-weighted average price (TWAP) is a strategy used by traders that aims to track the price of an asset over a set time. A TWAP can better reflect the true market price of an asset by averaging its price over time. It is a more sophisticated strategy often used in traditional financial markets to execute large orders.
When trading with size in traditional financial markets, brokers will often act as intermediaries to execute individual trades. For example, a broker looking to buy $10m of ETH over the course of a day might execute 1 trade of $35,000 every five minutes and complete buying in just under 24 hours. Trading this way with small orders can minimize the price impact of the overall trade and the final execution price will be a weighted average of the day’s price.
Time-Weighted Automated Market Maker (TWAMM) aims is to help traders execute large orders with minimal slippage and low gas fees without negatively affecting the price.
The Time-Weighted Average Market Maker (TWAMM) provides the on-chain equivalent of the TWAP order. The TWAMM has specialized logic for order splitting and a direct connection to an embedded exchange, providing smooth execution for low gas cost. Arbitrageurs keep prices on the TWAMM's embedded exchange in line with market prices, ensuring execution near the time-weighted average price of the asset.
Conducting large trades on a traditional AMM like Uniswap can have a bad price impact and is vulnerable to MEV frontrunning or sandwich attacks. Splitting the trade into smaller orders means paying gas fees for each transaction. The TWAMM solves this problem by representing these orders in the smart contracts state. MEV (Maximal Extractable Value) refers to the maximum value that can be extracted permissionlessly through the inclusion, censoring or re-ordering of transactions within a single block.
The TWAMM opens itself up for arbitrage opportunities. Yet it ticks along, continuing to calculate the price impact of the infinitely-spaced long-term orders from the book. This makes the final execution price of the long-term orders traded in to a TWAMM roughly track the TWAP over the specified period.
The TWAMM breaks these long-term orders into infinitely many infinitely small virtual sub-orders, which trade against the embedded AMM at an even rate over time. Processing transactions for each of these virtual sub-orders individually would cost infinite gas, but a closed-form mathematical formula allows us to calculate their cumulative effect only when needed.
The execution of long-term orders will push the embedded AMM's price away from prices on other markets over time. When this happens, arbitrageurs will trade against the embedded AMM's price to bring it back in line, ensuring good execution for long-term orders.
For example, if long-term sells have made ETH cheaper on the embedded AMM than it is on a particular centralized exchange, arbitrageurs will buy ETH from the embedded AMM, bringing its price back up, and sell it on the centralized exchange for a profit.
The TWAMM treats virtual sub-orders as if they take place in the space between blocks, which is important to avoid sandwich attacks.
To achieve this in a gas-efficient manner, the TWAMM uses lazy evaluation, computing the effect of virtual trades only when it is necessary to determine the outcome of an interaction.
Every time a user interacts with the TWAMM (for example, by trading with the embedded AMM or by adding a new long-term order), the TWAMM retroactively calculates the effect of all the virtual trades that took place since the last interaction.
Because these virtual trades are interacting only with the TWAMM's embedded AMM, the behavior of the TWAMM is completely deterministic between external interactions. Even if the TWAMM goes one million blocks between external interactions, the next time somebody interacts with it, it will be able to accurately calculate the results of all of the intervening virtual trades.
Limitation of TWAMM
Information Leakage by Sandwich attack
A sandwich attack is an act of pocketing a profit by executing front- and back- running — a trader looks at a pending transaction on the network and places an order right before and after the trade. For instance, when a trader finds a transaction that purchases 10 ETH at an AMM pool on the Ethereum network, an attacker spends a gas fee higher than the transaction itself, buys the ETH right before the trade takes place, sells the ETH right after the trade to gain profits from the price differences, and on the opposite, a transaction submitter suffers a property damage and loss.
In the case of transactions using TWAMM, it seems that the transaction is also vulnerable to sandwich attacks because the contents of the transaction are open to the public. However, orders using TWAMM run on multiple blocks and increase in size sequentially over time, making it difficult for sandwich attackers to identify targets and make solid profits because multiple arbitrage traders participate. 
Therefore, there is little risk of sandwich attacks running on a single block, but TWAMM may no longer be safe from that risk if sandwich attack techniques targeting multiple blocks are developed.
Paradigm’s paper also recognizes the potential for these risks and states:
At present, there is no way for an attacker to guarantee that they will only trade at the end of a given block if they also get to trade at the beginning of the next block. When such multi-block MEV becomes more common, allowing traders to sandwich across multiple blocks, this may become more of an issue. — Paradigm, TWAMM
The biggest tradeoff long-term traders are likely to encounter with the TWAMM is the information leakage they are exposed to when placing publicly visible orders, which are necessary due to the nature of Ethereum.
If a trader places a sufficiently large long-term order, other traders may be tempted to front-run it, buying up the asset on the TWAMM's embedded AMM and elsewhere in order to sell it back to the trader later as the long-term order pushes prices up.
Because users can cancel their long-term orders at any time, we expect overly aggressive front-runners to be exploited by other traders, keeping the overall impact of information leakage in check.
Drawbacks of TWAMM for Large-Scale Transactions
The disadvantages of using TWAMM compared to other large-scale transactions include the details of transactions such as the size of the fund and the transaction period disclosed, and taking a relatively long time. These disadvantages make large traders shy away from using TWAMM, and make them prefer alternatives such as P2P transactions using the OTC market.
Fraxswap, the first TWAMM
In early April, 2022, Fraxswap, DEX of Frax Finance, was launched as the first TWAMM exchange. Regarding the launch, Sam Kazemian, the founder of Frax Finance, said it had introduced TWAMM to provide services for fund rebalancing of POL (Protocol-owned Liquidity) DAO protocols (source: Do Dive, ‘Fraxswap, the last piece of the puzzle for ‘Next Curve’). POL DAOs have a continuous demand for funding rebalancing and have a strong anti-price incentive for their governance token transactions, which meets the major user requirements of the TWAMM mentioned earlier, so they are presumed to have served the stated purpose.
TWAMM (Time-Weighted Automated Market Maker)
Did you find this article interesting?