Published on

TWAMM: Price Optimizer for DeFi Users

Authors

TWAMM brings important implications in trading, making DeFi more attractive and opening up more growth potential in the long term.

What does TWAMM mean?

TWAMM (Time-weighted Automated Market Maker) is an on-chain trading mechanism, designed to break down orders with large trading volumes and execute them gradually over a period of time.

This minimizes common problems such as sharp price fluctuations or liquidity depletion, and ensures the trading price is close to the average price over time (TWAP).

Unlike you. AMM Traditionally like Curve Finance's Uniswap or Stableswap, TWAMM is still relatively new to the DeFi sector. However, with Uniswap v4 introducing TWAMM through custom “hooks”, this mechanism has become more popular.

Some protocols also call this feature DCA (Dollar Cost Averaging)due to the ability to execute buy and sell orders over time effectively.

 

Identifying the Price Impact Problem

Market making plays an important role in balancing supply and demand, ensuring stable market operations. Traditional market makers profit from the difference between the bid and ask prices.

For example, in South Korea, securities firms participate in the Market Making Scheme, which provides bid and ask prices for illiquid assets, thereby profiting from trading fees and price spreads.

However, in DeFi, there are no intermediaries, so traditional market makers cannot function. To solve this problem, Uniswap introduced AMM to become the basic tenet of decentralized exchanges (DEXs).

AMM works based on the Constant Product formula: x*y=k

In this case, x and y are the number of two types of tokens in the pool, and k is a constant. Token prices are determined in proportion to the number of tokens in the pool, which maintains liquidity and automatically adjusts prices without third-party intervention.

Invite your friends
Price impact on CPAMM models

Example of price impact in CPAMM. Suppose an ETH/USDC liquidity pool has:

  • Before trading: x = 1 ETH, y = 2,000 USDC, k=2,000, ETH price is 2,000 USDC.
  • New price: 1 ETH = 4,000 USDC.

This creates a large price impact: the buyer receives only 0.5 ETH for twice as expensive (4,000 USDC compared to the original 2,000 USDC). Unlike traditional markets, where market makers absorb price shocks, in AMM, the entire burden from price changes is on the trader.

Thus, it can be seen that what is happening in the DeFi world is radically different from the real market, where the burden from the price shock will rest on the shoulders of market makers.

The issue of price impact causes large order traders to avoid making trades, which negatively impacts the market as a whole in terms of capital inflows and trading volatility. While large orders can be broken down, the inconvenience of extended trading time and high gas fees make users hesitant to execute large orders.

To solve these problems, TWAMM (Time-Weighted Automated Market Maker) was born, inspired by the TWAP (Time-Weighted Average Price) mechanism.

Solution: Understanding TWAMM

Theoretical basis for the formation of TWAMM

TWAMM is based on the principle of TWAP, a traditional trading algorithm used to execute long-term orders at a time-weighted average price. TWAP ensures that large orders are executed gradually, minimizing sudden price impacts.

Let's say there is an order to buy $100 million worth of Apple stock in 8 hours. TWAP will break this command into small parts. That's equivalent to buying about 350 Apple shares every 100 milliseconds over an 8-hour period. This helps to reduce the impact on the market price due to the execution of too large an order in one go.

TWAMM applies this principle to the blockchain environment by dividing large orders into a multitude of small virtual commands and processing them sequentially on an embedded AMM (embedded AMM). This approach both leverages efficient computation and optimizes on-chain transaction costs.

TWAMM mechanism

Theoretically, TWAMM involves making large transactions by dividing them into small units. However, performing countless transactions containing details on-chain (especially on the Ethereum network) results in large network costs and is not feasible.

Invite your friends
General model in the design of TWAMM

Therefore, the possible TWAMM mechanism on the actual blockchain is implemented as follows:

TWAMM operates separately from an embedded AMM, so sending orders via TWAMM does not affect liquidity in the AMM. That is, orders are sent to an independent AMM and create a single Order Pool.

At the same time, a multitude of small virtual orders are executed over time based on the initial market price of the asset, the size of the trader's capital and the established trading period.

There is no change to the actual liquidity since this is a virtual order, but over time it causes changes in the exchange rate of the asset, or in other words the price, just as when a real order is executed.

Trading in the Order Pool created on TWAMM is independent of market liquidity, so there is a difference between the market price of the asset and its price on TWAMM.

Invite your friends
TWAMM in the design of Uniswap. Source: Uniswap

Furthermore, the Order Pool is open to casual traders, so there is a demand for price spread trading as price differentials exist. As a result, participants make a price difference trade on the Order Pool, and as a result, the transaction is executed at a price close to the weighted average price (TWAP).

Until the time period set by the trader expires, spreads trading continues through the above mechanism, minimizing the price impact and making large-scale asset trades.

The limitations of TWAMM

TWAMM, despite its benefits in reducing price impact, still has limitations that make it less popular with large-scale traders. Transaction details such as capital size and execution time are all made public on the chain, creating the risk of being hacked or analyzed by third parties.

In addition, long trading times across multiple blocks can be inconvenient for investors who need speed, making them prefer solutions such as P2P trading over OTC markets for greater security.

In addition, TWAMM is also vulnerable to sandwich attacks, a front-running and back-running strategy aimed at profiting from price swings. The attacker tracks pending transactions on the blockchain network, then places a buy order immediately before and a sell order immediately after that transaction to profit from the price change.

Invite your friends
General model of sandwich attack

For example, if a trader wants to buy 10 ETH in an AMM pool on the Ethereum network, the attacker will pay a higher gas fee to buy ETH before the initial transaction. When the price of ETH rises, they quickly sell ETH and profit from the price difference, while the person who made the initial trade suffered losses.

In TWAMM, transactions are also public on-chain, which creates the risk of sandwich attacks. However, the difference with TWAMM is that large commands are broken down and executed across multiple blocks, making it difficult to execute attacks in a single block. In addition, the involvement of many arbitrageurs helps to adjust the price during trading, reducing the likelihood of attackers making a solid profit.

Although the risk of attacking a sandwich on a single block is very low, if multi-block attack techniques are developed, TWAMM will become vulnerable.

The report from Paradigm also warns that if multi-block MEV (Miner Extractable Value) tools become popular, the risk of sandwich attacks targeting TWAMM could increase. This poses challenges for DeFi protocols in upgrading security mechanisms and mitigating risks from future multi-block mining attacks.

Paradigm also acknowledges the potential risks of these risks:

Currently, there is no way for an attacker to guarantee that they will only trade at the end of a certain block if they can also trade at the top of the next block. As MEVs across multiple blocks become more popular, allowing traders to perform sandwich attacks on multiple blocks, this could become a bigger problem.
Paradigm