Uniswap Labs Blog

Uniswap v4 TWAMM Hook

August 24, 2023

The Uniswap Protocol is a vital crypto infrastructure for millions of swappers, offering deep liquidity to support over $1.6 trillion in trading volume. Uniswap v4 is the latest evolution of the Protocol. Developers can build custom AMM features on top of Uniswap using hooks.

One hook we're excited about is the time-weighted average market maker (TWAMM), a new type of swap that reduces the price impact of larger orders by breaking them into small pieces over time.

What is the TWAMM hook?

TWAMM gets swappers better prices by splitting a large single swap order into smaller ones that steadily execute over time. These smaller swaps are less likely to move the market price.

How it works

In Uniswap v4, each pool can insert hooks that run at various points of a pool action's lifecycle. These hooks are smart contracts distinct from the core v4 liquidity contract. Orders are deposited into the TWAMM hook, and the user sets the duration they want their order to execute over. These two parameters instruct the TWAMM hook to swap at the price of each block until the expiration.

TWAMM orders are always the first pool action in a block, meaning they happen before any swaps or LP positions are adjusted. The first time someone swaps or LPs in a block, the pool will first check whether the TWAMM orders have been fulfilled. If there are any open TWAMM orders, the TWAMM hook will settle those orders before continuing on to complete any swaps or LP transactions before repeating the process with the next block. Executing TWAMM orders as the first pool action in the block protects users from frontrunning MEV.



TWAMM pool use cases

A TWAMM liquidity pool is a major unlock for swappers. Any order that does not need to be executed immediately benefits from the TWAMM hook saving on price impact and MEV.

Whether it’s a swapper moving large volumes or a DAO converting its treasury, large swaps leak a lot of value that swappers miss out on. TWAMM pools can smooth out their price curve by breaking up the swap.

Recurring investments

The TWAMM hook is great for longer-term investors who want "set it and forget it" recurring investments. Also known as dollar-cost average (DCA) is a popular trading strategy for passive buyers to invest the same amount at regular intervals. The steady cadence of investments offsets daily market volatility by averaging the purchases over time.

Say Alice wants to regularly buy $1,000 of ETH per month. She could split that into $1,000 a month, $250 a week, or $33 a day. Or $5,000 over five months. The TWAMM hook is a "set it and forget it" pool that can do exactly that.

With the TWAMM hook, Alice can deposit $1,000 into the pool contract and tell it to swap those funds over the span of a month. The TWAMM pool will execute a proportional swap every block until that month ends, even better than once daily.

A DAO converting its treasury

DAOs buy and sell large quantities of cryptocurrencies to diversify and manage their treasury risk, like when ENS DAO sold $16.5M of ETH for USDC.

Onchain markets can't easily absorb large volumes without impacting price and inviting MEV, which results in poor execution. If Alice's DAO sold $10M of ETH for USDC, it might increase the price by 0.61%, costing her $60,000. To avoid "leaking" value like this through large orders, DAOs might work with trading desks who break big orders into smaller ones.

The TWAMM hook can let DAOs market sell large portions of their treasuries. If Alice's DAO can split their order over time, they could sell smaller quantities of tokens until the date they set, letting the market smoothly adjust to thousands of swaps instead of doing it in one go. Using the TWAMM hooks, the DAO's $10M order can execute at every block for the next 30 days.

The particular hook design we created also protected Alice's DAO from frontrunning. By design, TWAMM executions are guaranteed to be the first pool action in each block. She cannot be frontrun because her order happens before any other pool swaps can settle.

To learn more about the TWAMM hook, you can look at our sample implementation on Github. You can read about v4 in our whitepaper, check out our code, consider contributing, and connect with the Labs team and other devs on Discord. Uniswap v4 and the TWAMM hook are still being developed. The final specs may vary.

Related posts