SOLUTION ARCHITECT

WORK WITH US

ABOUT

SOLUTION ARCHITECT

SOLUTION ARCHITECT

WORK WITH US

ABOUT

CASE STUDY - FRIEND WITH BENEFITS

$FWB TWAMM Hook

Background

FWB: A New Kind of Social Network

“Made up of creatives and builders who believe in the promise of a better internet. We all hold $FWB tokens as a way to collectively fund and govern our community endeavour and co-own what we create together”

On September 27th 2023, the Friends with Benefits DAO voted on a proposal to allocate a percentage of projected revenue to replenish their treasury with their native $FWB token.

Replenishing the DAOs’ treasury on a quarterly basis would ensure a sustainable flow of tokens for ongoing community engagement, experimentation, and long-term value creation.

Problem

A month before the FWB proposal, Uniswap Labs introduced the Time-Weighted Average Market Maker (TWAMM) hook that reduces the price impact of larger orders by breaking them into small pieces over time.

One specific use case was DAOs converting their treasury, as discussed in this post:

“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.”

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.

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.

As mentioned, large orders impact price from imbalance in liquidity pool assets and invite MEV when validators manipulate transaction orders to maximize profit. This combination results in poor execution and excessive fees for the DAO. 

Instead, we can create a hook that splits the order over time, letting the market smoothly adjust to thousands of swaps instead of doing it in one go.

Upon seeing this post, FWB reached out to Uniswap Labs who connected the team to Uniswap Foundation and Zaha Studio. Additionally, FWB wanted a turn-key approach to the product. Zaha Studio built the entire product in-house to be able to fulfil that goal.

Approach

We asked Akshat Mittal, Protocol Lead at Zaha Studio, how he approached this project:

We asked Akshat Mittal, Protocol Lead at Zaha Studio, how he approached this project:

We asked Akshat Mittal, Protocol Lead at Zaha Studio, how he approached this project:

What kind of solutions came up?

The implementation of TWAMM logic presents significant challenges, particularly regarding on-chain execution due to computational limitations. The original TWAMM concept, as outlined in Paradigm's foundational paper, involves breaking long-term orders into infinitely small transactions to facilitate smooth execution over time. However, traditional methods struggle to process multiple orders simultaneously on-chain, which can lead to inefficiencies and high gas costs.

To address these issues, we developed a solution that linearizes orders. Instead of treating each order as an independent trade, this approach consolidates the processing into a single transaction at the end. This method not only reduces computational overhead but also enhances efficiency by minimizing the number of transactions that need to be executed.

Furthermore, our implementation needed to accommodate operations across multiple liquidity pools. To achieve this, we opted to track earnings per token rather than per pool. This strategy allows for better management of tokens that may exist in various pools and enables functionalities such as many-to-one token trades. By focusing on earnings per token, we can streamline the process and ensure that the TWAMM can effectively handle a diverse array of tokens while maintaining efficiency in execution.

In summary, our approach not only optimizes the TWAMM's operational capacity but also enhances its adaptability across different trading scenarios and liquidity pools, paving the way for more sophisticated trading strategies in decentralized finance.

Why wasn’t Uniswap Labs’ example hook sufficient?

The Uniswap V4 hook example initially presented a well-structured design that addressed several critical considerations for developers. However, as development on V4 progressed over the past year, the original example became outdated, lacking alignment with the new specifications and designs introduced in V4. The Uniswap Labs team prioritized the overarching development of V4, which resulted in the hook's implementation not being fully realized.

Recognizing the complexity of the example hook, we undertook an initiative to extend its functionality and ensure it operated as intended. This involved enhancing its features and rigorously validating the mathematical computations to guarantee that the resulting trades fell within acceptable parameters. One significant aspect that had been overlooked during the initial development was testing; thus, we committed to thorough testing before proceeding to an audit phase.

By the conclusion of this process, we achieved a substantially improved version of the hook. The final state demonstrated enhanced performance and reliability, reflecting our dedication to refining the initial concept while integrating new insights gained throughout V4's development.

What were the considerations?

The development of the TWAMM hook for Uniswap v4 involved several critical considerations to ensure its effective implementation and operation on-chain.

Operational Requirements

The primary goal was to create a v4 hook that could efficiently process multiple trades simultaneously while functioning seamlessly across various liquidity pools. This required ensuring that the core pool operations remained unaffected, allowing for the smooth execution of TWAMM trades without any disruptions.

Earnings Tracking and User Experience

A significant aspect of the implementation was the ability to track earnings effectively. The design needed to facilitate a centralized claiming mechanism for users, enhancing their interaction with the system. Additionally, a strong emphasis was placed on providing an excellent user experience throughout the order creation, processing, and interaction phases.

Addressing MEV and Information Leakage

The potential risks associated with MEV and information leakage were also paramount. The design needed to incorporate strategies to mitigate these risks, ensuring that users' transactions would not be vulnerable to exploitation or front-running by malicious actors.

In summary, the considerations for developing the TWAMM hook encompassed operational efficiency across multiple pools, effective earnings tracking, user-centric design, and robust measures against MEV and information leakage. These factors collectively contributed to creating a reliable and user-friendly solution within the Uniswap v4 ecosystem.

Results
Results

We delivered a TWAMM hook to FWB that met all requirements by the team, iterated on the code after consultation with multiple auditing firms (Certora & ABDK), and partnered with a frontend dev team (IdeaSoft) to implement the FWB UI design.

The team at Uniswap Labs and Foundation were both impressed by the quality of the implementation and thorough understanding of the math behind TWAMM hooks:

“The new TWAMM hook on Uniswap V4 uses advanced math with concentrated liquidity ticks to enable efficient long-term order execution, optimizing both liquidity and precision in a robust design”
“The new TWAMM hook on Uniswap V4 uses advanced math with concentrated liquidity ticks to enable efficient long-term order execution, optimizing both liquidity and precision in a robust design”

DAVID SCHWARTZ

DEVELOPER EXPERIENCE LEAD, UNISWAP FOUNDATION

The $FWB TWAMM hook will be deployed immediately after Uniswap v4 deploys to mainnet.

“The TWAMM hook elevates how we manage our treasury—efficient, automated, and aligned with our vision for a sustainable token economy. The hook mitigates risk, reduces execution costs, and ensures smoother interactions between our treasury and the market. Collaborating with Zaha Studio and Uniswap turned a complex implementation into a seamless solution and set a precedent for other DAOs and cultural organizations to leverage DeFi tools.”
“The TWAMM hook elevates how we manage our treasury—efficient, automated, and aligned with our vision for a sustainable token economy. The hook mitigates risk, reduces execution costs, and ensures smoother interactions between our treasury and the market. Collaborating with Zaha Studio and Uniswap turned a complex implementation into a seamless solution and set a precedent for other DAOs and cultural organizations to leverage DeFi tools.”

Iz

COO at FWB

We’re thrilled to have supported FWB on their mission. We’re excited to measure and share the impact of the hook after v4 mainnet launch.

If you’re looking for technical guidance or implementation on all things Unichain and Uniswap v4, please fill out this brief form or reach to us at team@zaha.studio