Try โ€‚โ€‰HackMD

Does the TWAMM Track the TWAP?

Some notes on the 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

(Rx,Ry) to trade against with the TWAMM.

Suppose a trader looks to sell

X amount of token0 over
[t0,tn]
to the
Rx
reserve in exchange for some amount
Y
out of token1 from the
Ry
reserve.

At each time

ti, an amount
ฮ”x(ti)
gets sold to the TWAMM pool. The aggregate amount of token1 the trader has received up to time
tj
is given by

y(tj)=โˆ‘i=1jPยฏ(ti)โ‹…ฮ”x(ti)

where

Pยฏ(ti)=ฮ”y(ti)/ฮ”x(ti) is the average price received for the swap at each time
tiโˆˆ[t0,tj]
.
y(t0)=0
and
y(tn)=Y
.

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

Pxโ†’y=YX

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โ†’โˆž:
[t0,tn]โ†’[0,T]
. The discrete sum above reduces to a continuous integral

โˆ‘i=1nPยฏ(ti)โ‹…ฮ”x(ti)โ†’โˆซ0TP(t)โ‹…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)=xห™(t)โ‹…dt

where

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)=โˆซ0tdtโ€ฒP(tโ€ฒ)xห™(tโ€ฒ)

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

T

Pxโ†’y=y(T)x(T)=โˆซ0TdtP(t)xห™(t)โˆซ0Tdtxห™(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

TWAP(t0,tj)=โˆ‘i=1jP(ti)โ‹…(tiโˆ’tiโˆ’1)tjโˆ’t0

As

nโ†’โˆž, the continuous version

โˆ‘i=1nP(ti)โ‹…(tiโˆ’tiโˆ’1)tnโˆ’t0โ†’1Tโˆซ0TdtP(t)

changes the TWAP expression over the entire deployment period

[0,T] to the simpler

TWAP(0,T)=1Tโˆซ0TdtP(t)

When Average Price is the TWAP

Comparing the average price

Pxโ†’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

xห™(t)=xห™0

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

Pxโ†’y(xห™=xห™0)=xห™0โˆซ0TdtP(t)xห™0โˆซ0Tdt=1Tโˆซ0TdtP(t)=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โˆผO(Rx)
, 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