Matching Engine

The CVEX Protocol's Matching Engine is designed to overcome challenges inherent in implementing a traditional order book within a smart contract environment. A key issue arises when a taker order matches with a significantly large number of maker orders, disproportionately increasing execution costs for the taker. To address this, CVEX divides the order matching process into two distinct phases:

  1. Order Sampling: In this phase, matched limit orders are efficiently sampled from the order book and transferred into a clearance queue as a collective group. This process is managed by the entity initiating the taker order.

  2. Order Clearance: This phase involves the execution of queued orders and is carried out by Clearance Bots.

For efficient order sampling, the CVEX Protocol utilises a specialised data structure similar to an Order Statistic Tree. This binary tree, which separates buy and sell orders for each contract, exhibits the following characteristics:

  • Each node in the tree represents a limit order, without intermediate nodes.

  • Orders are organised based on traditional order book rules, sorted by price and then by time of placement.

  • The AVL algorithm, tailored for smart contract environments to minimise read operations during rebalancing, ensures effective depth maintenance.

  • Every node cumulatively tracks the sum of quote amounts (in contract value) and net amounts (in USDC) for all encompassed orders within their respective subtrees.

This order organisation enables efficient aggregated requests, such as grouping orders by price intervals or their cumulative amounts. In the matching process, the protocol first locates a path from the root to the final order matched a taker order. This is achieved by navigating from the root, ensuring the cumulative quote amount on the left side of the path equals the taker order's size.

Last updated