Inspiration

Most of the traditional financial market exchanges use the order book model, which has a good user experience. Can a Dex developed on the ic network achieve similar trading functionality and user experience as a Cex?

This is what we think about, and what we want to do.

To develop orderbook Dex on IC, an order matching engine is necessary for order management and order matching. Order matching engine module is the core of Dex, it is reusable and lowers the barrier for Dex developers.

What it does

DeSwap Orderbook is an order matching engine module for the development of Dex, in Motoko language.

DeSwap Orderbook

The Matching Engine takes orders as an input, matches them and produces trades as an output. DeSwap Orderbook implements a price-time priority matching algorithm (FIFO): orders are first ranked according to their price; orders of the same price are then ranked depending on when they were entered.

DeSwap Orderbook supports various order types like limit orders (LMT), market orders (MKT), Fill-And-Kill orders (FAK), Fill-Or-Kill orders (FOK).

How we built it

The matching engine makes a difference between incoming orders and book orders. Strictly speaking, an incoming order is an order that is in the process of being entered, and a book order is an order that is in the order book already.

Matching engine

To get a general idea of a matching engine, you can consider it as a function that takes an order (1) and an “order book” (2) as input parameters, and gives back a list of trades (3) plus all the remaining orders (4). The remaining orders will become the “order book” for the next order received by the matching engine.

  • Fully filled order

An order will match fully if its entire open quantity is executed. Since there is nothing left to match, a fully matched book order is removed from the order book, and a fully matched order that is in the process of being entered, is not written to the book.

  • Partially filled order

Or an order matches partially, if not all its open quantity is executed. In this case an order that was already on the order book remains on the order book, and an order that is in the process of being entered and is not an FOK order, is written to the order book. The quantity that was executed is removed from the open quantity and added to the accumulated executed quantity.

  • No match

Where there is no match, the order becomes a resting order and is directly included in the order book.

  • A order involved in multiple executions

It is possible for a single order to get involved in multiple executions at different points in time. For example, an order may be partially executed upon entry, while the remaining open order remains in the order book. The open portion may then be executed a minute later, an hour later, or even days later.

  • Matching principles

When orders are entered into the central order book, they are sorted by price, time. There are different algorithms available for matching orders, and DeSwap Orderbook has chosen the First-In-First-Out (FIFO) algorithm.
FIFO is also known as a price-time algorithm. According to the FIFO algorithm, buy orders take priority in the order of price and time. Then, buy orders with the same maximum price are prioritized based on the time of bid, and priority is given to the first buy order. It is automatically prioritized over the buy orders at lower prices.

Challenges we ran into

Implementing all of the classic order book trading rules on ic smartcontract has faced a number of challenges. We are gradually working through them.

Accomplishments that we're proud of

DeSwap OrderBook has been implemented to support 4 order types such as Limit Order (LMT), Market Order (MKT), Fill and Kill Order (FAK) and Fill or Kill Order (FOK).

We have implemented a Demo of orderbook Dex that supports trading pairs of DIP20, DRC20 standard tokens with native ICP. https://pk6zh-iiaaa-aaaaj-ainda-cai.raw.ic0.app/

What we learned

There is great potential for developing Dex based on IC networks.

What's next for DeSwap Orderbook

We plan to open source the DeSwap Orderbook module, which can be used by any Dex for development.

Built With

  • motoko
+ 5 more
Share this project:

Updates