Design document for the Batch Clearing DEX POC

The aim of the batch clearing dex is to enable users to deposit tokens with the aim of being swapped at a fair price with bounded slippage and almost no impermanent loss. To enable this users will deposit tokens during a deposit window; all deposits during this window will be a 'batch'. Once the deposit window is over the 'batch' will be 'locked'. Deposits to the dex will not specify a price for the swap, they will specify an offset to whichever oracle price is received after the 'batch' is 'locked'; those offsets being 0, +10bps, or -10bps. Once the batch is locked there will be a waiting window before the process starts to await an oracle price. Upon receipt of the oracle price, the batch is terminated and the orders are cleared.

For V1, the deposit window will be 10 mins and then a wait time of 2 minutes before awaiting the oracle price. Only the XTZ/USDT pair will be supported for V1

After the termination of the batch, the users that placed orders can retrieve their funds. Either the user's order did not clear and they retrieve their original funds or the order cleared (totally or partially) and the get an execution of their order.

Deposit

The deposit window is open for a finite time period. During the time period users can deposit their tokens for a BUY or SELL order on the token pair along with an offset to the future received oracle price.

For V1, deposit windows won't run sequentially, in that as soon as a deposit window closes another will open straight away. Once the deposit window closes, users will need to wait for that batch to be cleared before another deposit window opens.

Waiting

Once the deposit window has closed there will be a period of 2 minutes prior to awaiting an oracle price. Once that period has elapsed the first received oracle price will close the batch and clearing will start.

Clearing

The clearing process is the process of matching the orders to ensure that all users can trade at the fairest possible price. Upon deposit there will be six categories of order.

Side

Depending on whether you are buying or selling the pair you will either be on the BUY side or the SELL side. For the pair XTZ/USDT, XTZ is the base and USDT is the quote. So if the XTZ/USDT rate was 1.9 you would get 1 unit of XTZ for 1.9 of USDT if I am buying the pair, i.e. on the BUY side. If I am selling the pair, the inverse would be true. The side of the trade is important to understand which token needs to be deposited in a given swap order.

Tolerance

For any deposit, the user can specify the tolerance to the oracle price that they are willing to trade at. This means that each side can further be segregated into their tolerance levels; $Price_{oracle}-10bps$, $Price_{oracle}$ , $Price_{oracle}+10bps$.

Clearing Level

Given the three tolerance levels, we need to find the level at which we will clear the most orders; the clearing level.

Considering that the amount of deposits for each category is different then we have six categories with a differing amount of tokens deposited for each tolerance.

DepositsP-10bpsPP+10bps
BUYX of USDTY of USDTZ of USDT
SELLR of XTZS of XTZT of XTZ

An added complexity is that if I am will to buy at $Price_{oracle}+10bps$ then I will also be implicitly interested in buying at $Price_{oracle}$ and $Price_{oracle}-10bps$ as they are both cheaper levels if I am on the BUY side. The converse is true for the sell side in that if I sell for $Price_{oracle}-10bps$, then I would be willing to sell for the higher prices of $Price_{oracle}$ and $Price_{oracle}+10bps$.

Determining the clearing level

PricesP-10bpsPP+10bps
BUYP / 1.0001PP * 1.0001
SELL1.0001 / P1/P1/ (1.0001 * P)
P-10bps level

Lets take the P-10bps sell level first. All of the buy levels would be interested in buying at that price, so the clearing volume at that level would be:

$$ CP_{P-10bps} = \min(X + Y + Z, R * \dfrac{ 1.0001 }{P}) $$

P level

Lets take the P sell level first. Only the upper 2 buy levels would be interested in buying at that price, but the lower two SELL levels would be interested in selling so the clearing volume at that level would be:

$$ CP_{P} = \min(Y + Z, (R+S) * \dfrac{1}{P}) $$

P+10bps level

Lets take the P+10bps sell level first. All of the sell levels would be interested in selling at that price, but only the upper BUY level would be interested in buying so the clearing volume at that level would be:

$$ CP_{P+10bps} = \min(Z, (R+S+T) * \dfrac{1}{(P * 1.0001)}) $$

Illustrative Examples

If the Oracle price for XTZ/USDT is 1.9 and the tolerance is +/- 10 basis points, then the six price levels are:

Price LevelsBUYSELL
Price + 10bps1.900190.52626
Price1.90.52631
Price - 10bps1.899810.52636

Assuming these levels we can determine some very basic illustrative examples of different market scenarios:

MARKETAMOUNTS SKEWBUY X @ (P-)BUY Y @ (P)BUY Z @ (P+)SELL R @ (P-)SELL @ S (P)SELL T @ (P+)Orders cleared @ P-10bpsOrders cleared @ POrders cleared @ P+10bpsClearance Level
SELL PRESSUECENTERED55100451000190090020015555P-10bps
SELL PRESSUENEG100554519001000900200155100P-10bps
SELL PRESSUEPOS45551009001000190020010045P-10bps
BUY PRESSURECENTERED250100250951909550150200P+10bps
BUY PRESSURENEG2501002501909595100150200P+10bps
BUY PRESSUREPOS250100250959519050100200P+10bps
BALANCEDCENTERED501015095190955015050P
BALANCEDNEG10150501909595100150101P
BALANCEDPOS505010195951905010050P
BALANCEDOPPOSING (NEG)5050101190959510010050P-10bps
BALANCEDOPPOSING (POS)1015050959519050100101P+10bps

Once we know the clearing level and the volume that can be cleared at that level, we will know how many can be matched (some partially) and those will receive pro-rata execution of their orders. For those that bid outside of the clearing level they will receive their deposits back when they claim.

A Google Sheet with these calculations in is available.

Claiming

After clearing, users can claim their 'results', whether that be their original deposits, a partially matched order result or a fully filled order for the opposing token.

Timelines

gantt
   dateFormat YYYY-MM-DD
   title Timelines for Batcher going to mainnet
   excludes weekends

   section Development
   End of internal audit                                          :ia, 2023-01-09, 4d
   Fee Burning                                                    :fb, 2023-01-09, 4d
   Oracle                                                         :or, 2023-01-09, 10d
   Multiple Token Pools (incl. contract management and multisig)  :mtp, after fb, 3w
   Stabilisation / Final Testing                                  :ft, after mtp, 5d
   Mainnet launch                                                 :mn, after ft, 3d

   section Engagement
   Blog post for fee burning                                      :bpfb, 2023-01-20, 5d
   Blog post for token pools                                      :bptp, 2023-01-27, 5d
   Blog post for oracle                                           :bpor, 2023-02-10, 5d
   Blog post for mainnet                                          :bpmn, 2023-02-20, 5d