Deep Dive Into the Binance DEX Match Engine

Share
Deep Dive Into the Binance DEX Match Engine
Listen to this article
00:00 / 00:00

How Binance DEX is different from Binance.com


Contents:


What is the purpose of Binance Chain/DEX?

The purpose of the Binance Chain and DEX is to create a decentralized marketplace directly on the blockchain. Assets are not sent to an exchange or wallet that is controlled by a single platform or entity. Instead, the order book and transactions take place on the blockchain. There are no middleman fees but only network cost, and users are fully and solely in control of their assets.


Key features of Binance Chain

Binance Chain is based on the Byzantine Fault Tolerance (BFT) consensus mechanism. This means that the blockchain is secure as long as 2/3 of the nodes are honest. Also, once a transaction is committed in a block, it becomes final. There are zero risks of a chain modification or a forking scenario.

Also, Binance Chain block time is approximately 1 second, with a high transaction throughput. This is one of the fastest commercially deployed blockchains at the time of writing.

Binance DEX keeps its order book on the blockchain, this means that anyone who runs a full node has a full copy of it. 

Here are some ways that Binance DEX is different from binance.com: 

  • Order Type: Binance DEX only accepts limit orders

  • Matching logic: Because of the nature of blockchains, blocks are produced in discrete time intervals, hence Binance DEX does not do continuous matching as most centralized exchanges do. Instead, order matching takes place using periodic auction matching for all open orders received in the past up to the latest block.


How are orders matched on Binance DEX?

If you are in a hurry, you can skip this section and go to the “Match Engine Examples” directly.

There are 2 types of Matching Engines:

  • Continuous - where orders are matched immediately

  • Discrete - where matching is performed at every time interval using auction rules 

Binance DEX uses a discrete Matching Engine. Orders are collected every block and all open orders up to the latest block are matched at the end of that block. 


Main principles of the Matching Engine:

  • Matching is attempted 1 time each block 

  • Matching occurs when the highest bid price is higher than the lowest ask price

  • Only 1 price is selected in each block, determined as the fill price among all the fillable orders

  • Order matching is prioritized first by price aggressiveness, then second by time (block height) that they are accepted

Every matching attempt occurs after a block is recorded on the chain (“mined”). The matching attempt follows these steps: 

  • Determining block fill price from the following factors:

    • Maximum executable volume

    • Minimum surplus

    • Market pressure

    • Reference price

  • Allocating order fills using rules:

  • Orders with the highest bid price will match with the orders with the lowest ask price;

  • If all bids or asks at the block fill price cannot be fully filled by the respective asks or bids, orders from the earliest blocks are selected and filled first

  • If all bids or asks at the block fill price cannot be fully filled by the respective asks or bids, and arrive in the same block, allocated execution will be in proportion to their quantity (floored if the number has a partial lot). If the allocation cannot be divided equally, a deterministic algorithm will guarantee a fair selection using a random order ID.


Matching Engine Examples

Please pay special attention to Scenario 3,4,5, if you are “eager” traders or trade in very volatile or illiquid markets.


Scenario 1: No match

In this example, the bid and ask prices are not 'crossed' (highest bid less than lowest ask). While order matching is attempted, no orders are filled, as illustrated here:

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine

Deep Dive Into the Binance Chain Match Engine

A buy order for 10 Testnet tokens at unit price 5 is placed.Deep Dive Into the Binance Chain Match Engine

No orders are filled. The order book has these open orders now:

Deep Dive Into the Binance Chain Match Engine

Scenario 2: Achieving maximum executable volume

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine


You then place the following orders:

  • Buy order for 30 Testnet tokens at unit price 12

  • Buy order for 10 Testnet tokens at unit price 10

  • Buy order for 20 Testnet tokens at unit price 9

  • Buy order for 30 Testnet tokens at unit price 8


Deep Dive Into the Binance Chain Match Engine

The Price for this round is 6 to achieve max match volume. And a quantity of 90 trades is executed at price 6. 

After the orders are matched, you can see the trade history:

Sell Order:

Deep Dive Into the Binance Chain Match Engine

Buy Order:

Deep Dive Into the Binance Chain Match Engine

Scenario 3: Orders with best bid price would match with order with best ask price

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine

Deep Dive Into the Binance Chain Match Engine

Then, you place the following orders:

  • 25 orders at price 10

Deep Dive Into the Binance Chain Match Engine

The Price for this round is 7 to have max matched volume. And all the trades are executed at 7. Sell orders are partially filled. Since orders with best bid price would match with the orders with best ask price, sell orders of 6 will get filled first. Then the sell order of 7 will get filled after that. In this way, the sell orders of 7 are partially filled.

Deep Dive Into the Binance Chain Match Engine

Deep Dive Into the Binance Chain Match Engine

Open orders after the match:

Deep Dive Into the Binance DEX Match Engine


Scenario 4: Buy a large chunk 

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine

As you can see, there are different sell orders at different price levels. If you place a buy order of large quantity: 150 at price 25. The following will happen:

Deep Dive Into the Binance DEX Match Engine

The price is settled at 10. And all the sell orders are matched. 

Deep Dive Into the Binance Chain Match Engine

Orderbook after match:

Deep Dive Into the Binance Chain Match Engine

Analysis:

This trade price can seem as a disadvantage to the buyer. In the example, you pay 800 in total. You can get a fairer trade buy place different trades of smaller amount: You will pay less if you follow the following steps:

  • Buy 15 orders at 6

  • Buy 25 orders at 7

  • Buy 25 orders at 8

  • Buy 5 orders at 9

  • Buy 10 orders at 10

If you choose to place your orders in this way, the notional is 610, which is less than 800.


Scenario 5: Sell a large chunk

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine

There are the following orders:

Deep Dive Into the Binance Chain Match Engine

You place a large sell order: sell 80 at price 6.

Deep Dive Into the Binance Chain Match Engine

The trades are settled at price 6.

Deep Dive Into the Binance Chain Match Engine

The notional of 450 is not ideal. You can sell more if you follow the following steps:

  • place 5 sell orders of price 10.

  • place 40 sell orders of price 8

  • place 10 sell orders of price 7

  • place 20 sell orders of price 6

In this way, the sell notional can be 560. 


Scenario 6: First come, First match

At the beginning of this block, we have the following open orders.

Deep Dive Into the Binance Chain Match Engine

Then, the following orders come to match engine:

  • 20 buy orders of price 6

  • 10 sell orders of price 6

Deep Dive Into the Binance Chain Match Engine

Deep Dive Into the Binance Chain Match Engine

Since there are already sell trade of 6 in the order, so those trades are matched first. 

Buy orders are fully filled.

Deep Dive Into the Binance Chain Match Engine

Previous 15 sell orders of 6 are fully filled. Deep Dive Into the Binance Chain Match Engine

Those 10 sell orders of 6 are only half filled.

Deep Dive Into the Binance Chain Match Engine

Orderbook after match:

Deep Dive Into the Binance Chain Match Engine

Loading