The Optimization problem
In this section, we describe all the different components of the optimization problem that needs to be solved within each batch.
User orders
Suppose that there are tokens. From a high-level perspective, we can define a user order as an acceptance set specifying the trades a user is willing to accept (where negative entries of a vector represent tokens sold, while positive entries represent tokens bought). So, for example, if and then a user is happy to receive x units of token 1 in exchange for y units of token 2.
This is from the user's perspective, and is therefore net of fees.
We also assume that that is, when submitting an order a user accepts that the order may not be filled. Also, to each order we define surplus_function , measuring "how good" a trade is from the point of view of the user who submitted order S. By definition .
Practically speaking, CoW Protocol allows only some types of orders, which we can think of as constraints on the set S that a user can submit. One such constraint is that only pairwise swaps are allowed, that is, all vectors in have zeros in dimensions. Furthermore, each order must fit within one of the categories we now discuss. To simplify notation, when discussing these categories, we assume that .
Limit Sell Orders
A limit sell order specifies a maximum sell amount of a given token Y > 0, a buy token b, and a limit price , that corresponds to the worst-case exchange rate that the user is willing to settle for. They can be fill-or-kill whenever the executed sell amount must be Y (or nothing). They can be partially fillable if the executed sell amount can be smaller or equal to Y. Formally, if x denotes the (proposed) buy amount and y denotes the (proposed) sell amount of the order, a fill-or-kill limit sell order has the form
and a partially-fillable sell order has the form
In both cases, the surplus function is defined as
,
where is the additional amount of buy tokens received by the user relative to the case in which they trade at the limit price, and is the price of the buy token relative to a numéraire (in our case ETH) and is externally provided (i.e., by an oracle). The function is therefore expressed in units of the numéraire and is always non-negative.
A final observation is that orders can be valid over multiple batches. For a fill-or-kill, this means that an order that is not filled remains valid for a certain period (specified by the user). For a partially-fillable order, this also means that only a fraction of it may be executed in any given batch.
Limit Buy Orders
A limit buy order is specified by a maximum buy amount X > 0 and a limit price corresponding to the worst-case exchange rate the user is willing to settle for. With x denoting the buy amount and y denoting the sell amount of the order, fill-or-kill limit buy orders have the form
while partially-fillable limit buy orders have the form
Again, the surplus function is defined as
,
where is the price of the sell token relative to a numéraire and is externally provided. Also here, orders can be executed over multiple batches.
Fees
Each user order has an associated fee paid to the protocol. At a high level, these fees can be represented by a function that, for a given order maps all possible trades to a positive vector of tokens, that is with .
From the practical viewpoint, for market fill-or-kill orders, the fee is always in the sell token and is pre-specified: it is an estimate of the cost of executing an order and is explicitly shown to the user before the order is submitted. Instead, (long-standing) limit orders are "feeless" from the user's perspective: users are guaranteed a limit price without specifying how fees will be calculated. Solvers are the ones proposing a fee under the expectation that this fee should equal the cost of execution of this trade in isolation. The fee of limit orders is again denominated in the sell token.