---
# System prepended metadata

title: Does the TWAMM Track the TWAP?

---

# Does the TWAMM Track the TWAP?

Some notes on the [TWAMM](https://www.paradigm.xyz/2021/07/twamm).

These expressions might be somewhere out there, but I viewed this as an exercise to learn more about the properties of the TWAMM.

## TL;DR

- Yes, the TWAMM delivers an average purchase price of the TWAP over the future deployment period $[0, T]$.
- However, this assumes capital is continuously deployed at a constant rate.
- The TWAMM won't necessarily track the TWAP if the capital deployment rate is a non-constant function of time.

## Infinitesimal Deployment


There are two reserves $(R_x, R_y)$ to trade against with the TWAMM.

Suppose a trader looks to sell $X$ amount of `token0` over $[t_0, t_n]$ to the $R_x$ reserve in exchange for some amount $Y$ out of `token1` from the $R_y$ reserve.

At each time $t_i$, an amount $\Delta x(t_i)$ gets sold to the TWAMM pool. The aggregate amount of `token1` the trader has received up to time $t_j$ is given by

$$
y(t_j) = \sum_{i=1}^{j} \bar{P}(t_i) \cdot \Delta x(t_i)
$$

where $\bar{P}(t_i) = \Delta y (t_i) / \Delta x (t_i)$ is the average price received for the swap at each time $t_i \in [t_0, t_j]$. $y(t_0) = 0$ and $y(t_n) = Y$.

Our goal is to determine what the trader's average price is for the *entire trade*:

$$
P_{x \to y} = \frac{Y}{X}
$$

and then compare this to what the TWAP for the pool was over the same time period. Take each swap to happen in the infinitesimal limit such that the number of time steps $n \to \infty$: $[t_0, t_n] \to [0, T]$. The discrete sum above reduces to a continuous integral

$$
\sum_{i=1}^{n} \bar{P}(t_i) \cdot \Delta x(t_i) \to \int_0^T P(t) \cdot dx(t)
$$

not necessarily assuming evenly distributed deployment of the $x$ funds at each time $t$. For infinitesimal deployment, the average price at each time step will be the marginal price $dy/dx$ for the pool, so I have dropped the bar.

Express the infinitesimal amount sold of `token0` funds at time $t$ to be a function of time

$$
dx(t) = \dot{x}(t) \cdot dt
$$

where $\dot{x}(t) = dx/dt$ is the rate of capital deployment into the TWAMM pool. The total amount of `token1` the trader has received up to time $t$ will then be

$$
y(t) = \int_0^t dt' \;  P(t') \dot{x}(t')
$$

and the associated average price received by the trader for the entire trade through $T$

$$
P_{x \to y} = \frac{y(T)}{x(T)} \\
= \frac{\int_0^T dt \;  P(t) \dot{x}(t)}{\int_0^T dt \; \dot{x}(t)}
$$


## TWAP for the Pool

What's the TWAP of the pool over this same time period?

By definition, the TWAP is the time-weighted average price of the pool over a given time period. In the discrete case, this is

$$
\mathrm{TWAP}(t_0, t_j) = \frac{\sum^j_{i=1} P(t_i) \cdot (t_i - t_{i-1})}{t_j - t_0}
$$

As $n \to \infty$, the continuous version

$$
\frac{\sum^n_{i=1} P(t_i) \cdot (t_i - t_{i-1})}{t_n - t_0} \to \frac{1}{T} \int_0^T dt \; P(t)
$$

changes the TWAP expression over the entire deployment period $[0, T]$ to the simpler

$$
\mathrm{TWAP}(0, T) = \frac{1}{T} \int_0^T dt \; P(t)
$$


## When Average Price is the TWAP

Comparing the average price $P_{x \to y}$ received for the swap through the TWAMM and the TWAP of the TWAMM pool over the same time, shows that when the rate of capital deployment is constant over time

$$
\dot{x}(t) = \dot{x}_0
$$

the average price received by the trader will be the TWAP of the pool over the time of deployment as

$$
P_{x \to y}(\dot{x}=\dot{x}_0) = \frac{\dot{x}_0 \int_0^T dt P(t)}{\dot{x}_0 \int_0^T dt} \\
= \frac{1}{T} \int_0^T dt \; P(t) = \mathrm{TWAP}(0, T)
$$

The condition of a constant rate of selling over the deployment period guarantees the trader receives as their average price the TWAMM pool's TWAP over that same timespan.

If the rate of capital deployment is not constant over the deployment period, the average price of their trade won't necessarily coincide with the pool's time-weighted average price.

Note that for small values of $T$ and large sizes $X \sim \mathcal{O}(R_x)$, the TWAP of the TWAMM pool will diverge significantly from the TWAP of another spot pool for the same asset, similar to oracle manipulation calculations for Uni V3 TWAP observations. However, the larger the time period $T$ is, the closer the trade average price should be to another AMM's spot TWAP for the same asset due to arbitrage arguments.


## References

- [TWAMM (White, Robinson, Adams)](https://www.paradigm.xyz/2021/07/twamm)
- [Mathematical Principle of TWAMM (Peng)](https://hackmd.io/@luffy/SJxSsOH1Y)