TWAP
A simple time-weighted average price (TWAP) trade may be thought of as n
smaller trades happening every t
time interval, commencing at time t0
. Additionally, it is possible to limit a part's validity of the order to a certain span
of time interval t
.
Data Structure
struct Data {
IERC20 sellToken;
IERC20 buyToken;
address receiver; // address(0) if the safe
uint256 partSellAmount; // amount to sell in each part
uint256 minPartLimit; // minimum buy amount in each part (limit)
uint256 t0;
uint256 n;
uint256 t;
uint256 span;
bytes32 appData;
}
No direction of trade is specified, as for TWAP it is assumed to be a sell order
Example: Alice wants to sell 12,000,000 DAI for at least 7500 WETH. She wants to do this using a TWAP, executing a part each day over a period of 30 days.
sellToken
= DAIbuytoken
= WETHreceiver
=address(0)
partSellAmount
= 12000000 / 30 = 400000 DAIminPartLimit
= 7500 / 30 = 250 WETHt0
= Nominated start time (unix epoch seconds)n
= 30 (number of parts)t
= 86400 (duration of each part, in seconds)span
= 0 (duration ofspan
, in seconds, or0
for entire interval)
If Alice also wanted to restrict the duration in which each part traded in each day, she may set span
to a non-zero duration. For example, if Alice wanted to execute the TWAP, each day for 30 days, however only wanted to trade for the first 12 hours of each day, she would set span
to 43200
(ie. 60 * 60 * 12
).
Using span
allows for use cases such as weekend or week-day only trading.
Methodology
To create a TWAP order:
- ABI-Encode the
IConditionalOrder.ConditionalOrderParams
struct with:handler
: set to theTWAP
smart contract deployment.salt
: set to a unique value.staticInput
: the ABI-encodedTWAP.Data
struct.
- Use the
struct
from (1) as either a Merkle leaf, or withComposableCoW.create
to create a single conditional order. - Approve
GPv2VaultRelayer
to traden x partSellAmount
of the safe'ssellToken
tokens (in the example above,GPv2VaultRelayer
would receive approval for spending 12,000,000 DAI tokens).
When calling ComposableCoW.create
, setting dispatch = true
will cause ComposableCoW
to emit event logs that are indexed by the watch tower automatically. If you wish to maintain a private order (and will submit to the CoW Protocol API through your own infrastructure, you may set dispatch
to false
).
Fortunately, when using Safe, it is possible to batch together all the above calls to perform this step atomically, and optimise gas consumption / UX.
For cancelling a TWAP order, follow the instructions at Conditional order cancellation.