The OrderBook contract essentially contains and manages a particular market. It adds limit orders, matches market orders and works in close conjunction with the Kuru AMM vault.

Structures Used:

  1. Order → (address owner, uint96 size, uint40 prev, uint40 next, uint40 flippedId, uint32 price, uint32 flippedPrice, bool isBuy)
  2. PricePoint → (uint40 head, uint40 tail)
  3. TreeUint24 → Tree to store price points at which order exists. Contains functions to add, remove price points and find best buy and sell prices.

The order not only stores the order data but also stores a reference to the previous and next order in its price DLL. Refer to the bellow doc for further details regarding the tree and the DLL.

OrderBook Structure

It is recommended to read the short doc below to understand flip orders if you understand order books. However, the technical read on flip orders requires some knowledge about the order book contract.

Flip Orders

Flip Orders (Technical)

OrderBook Initialization Flow :

  1. Initial OrderBook Contract is deployed. Must disable initializers so that this contract can only be delegated to.
  2. Proxy is deployed with target address as implementation contract. The Router passes all required parameters to the OrderBook through the initialize function.

Order Types :

For security testing reasons, it would be wise to categorize orders into these following categories:

Head of a PricePoint :

Tail of a PricePoint :

Middle of an order :

Note: In a PP with a single order, both head and tail are the same order ID. In this case, next will never be NON NULL.