Deploy decentralized exchanges
The matching engine allows decentralized exchanges that are building on top of United Protocol to deploy their own orderbooks. The matching engine maintains their respective orderbooks at a chain level, and provides functionality to create markets and allow users to trade.
Lifecycle Of An Order
All matching engine related transactions will be executed atomically in the scope of a block.
Transactions related to the matching engine will be sent to the dex module, as shown in figure 5.
One transaction may be composed of one or more orders. Upon submission, the transaction handler processes the transaction by adding the orders included from each transaction into the dex module’s internal MemState (Figure 5 action 1).
While processing each block, the dex module has an EndBlocker hook that processes orders recorded in the MemState in bulk (Figure 5 action 2). Specifically, when dex module EndBlocker hook is invoked, orders across transactions will be aggregated by market (i.e. all orders for a BTC perpetual), and combined into one smart contract call for that particular market .
The chain will then call the smart contract associated with that market (i.e. calling a perpetual exchange smart contract), which has all of the logic defined for how to interact with the matching engine (Figure 5 action 3). The smart contract will implement its own custom logic, and then call the matching engine (Figure 5 action 4).
Hook Support
United Protocol allows contracts to register ”hooks” with the network. The registered hooks will be invoked every block and allow operations like flashloan payback to happen in the same block as any associated trade settlements. Specifically, a contract can define two hooks. The first one is called at the beginning of a block to give contracts an opportunity to prepare for any potential trade that may happen in the same block. The second is called at the very end of a block, after order matching and settlement, allowing contracts to perform any post-trade logic if needed.
Last updated