arshamalh / cryptocurrency-exchange

An Implementation of cryptocurrency exchange includes a Matching Engine for matching buy and sell orders and a Market Maker for providing liquidity to the exchange.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of content

Code

If you want to start reading code, start from the matchingengine directory. For execute program you will need Ganache for private ETH environment.

Explanations

Matching Engine, The Limit and The Orders

What is Matching Engine

What is it? how does it work? and how can it help?

A matching engine is a software component that sits at the core of a crypto exchange, responsible for matching buy and sell orders from traders. It works by continuously monitoring the order book, which is a list of all the buy and sell orders that traders have placed on the exchange.

What is The Order

Imagine there are two people, Sara and John, and each one has their own order request like this:

Each order contains the price, amount of crypto or stock, and type of order, which can be a buy or sell order.

What is The Limit

A limit is a group of orders at a certain price level. It's like a bucket of Orders with the same price but different amount that is for different people.

As I said, each limit can contain multiple orders like this:

What is the matching engine job?

In theory, what the matching engine does is simple. The matching engine tries to sort the orders correctly in the first stage. Buyers' orders are sorted in descending order, and sellers' orders are sorted in ascending order based on price.

When a new order is placed, the matching engine will look for the best available match among the existing orders in the order book. If a match is found, the trade is executed and the orders are removed from the order book. If no match is found, the new order is added to the order book.

Market Maker

What is the idea of MM

A market maker is a software program that automatically places buy and sell orders on an exchange to provide liquidity and stability for traders. This market maker, which is separate from the exchange, aims to enhance the trading experience by providing more consistent bid-ask spreads and increased trading activity.A market maker is a software program that automatically places buy and sell orders on an exchange in an effort to maintain liquidity and stability in the market.

APIs

Books

Get market orderbook

GET /books/{market}  (Currently we have only ETH market) 

Response:

  "TotalAsksVolume": 13000,
  "TotalBidsVolume": 13000,
  "Asks": [
    {
      "UserID": 7,
      "ID": 122540,
      "Amount": 1000,
      "IsBid": false,
      "Price": 9700,
      "Timestamp": 1674833074926966443
    },
    {
      "UserID": 7,
      "ID": 941318,
      "Amount": 1000,
      "IsBid": false,
      "Price": 9800,
      "Timestamp": 1674833074926969800
    },
    {
      "UserID": 7,
      "ID": 984059,
      "Amount": 1000,
      "IsBid": false,
      "Price": 9900,
      "Timestamp": 1674833074926971287
    },
    {
      "UserID": 8,
      "ID": 498081,
      "Amount": 10000,
      "IsBid": false,
      "Price": 10000,
      "Timestamp": 1674833074926974852
    }
  ],
  "Bids": [
    {
      "UserID": 7,
      "ID": 954425,
      "Amount": 1000,
      "IsBid": true,
      "Price": 9300,
      "Timestamp": 1674833074926977231
    },
    {
      "UserID": 7,
      "ID": 902081,
      "Amount": 1000,
      "IsBid": true,
      "Price": 9200,
      "Timestamp": 1674833074926977976
    },
    {
      "UserID": 7,
      "ID": 131847,
      "Amount": 1000,
      "IsBid": true,
      "Price": 9100,
      "Timestamp": 1674833074926978939
    },
    {
      "UserID": 8,
      "ID": 727887,
      "Amount": 10000,
      "IsBid": true,
      "Price": 9000,
      "Timestamp": 1674833074926982393
    }
  ]
}

Get best ask

GET /books/{market}/best/ask  

Response:

{
  "Price": 9700
}

Get best bid

GET /books/{market}/best/bid 

Response:

{
  "Price": 9400
}

Order

Get user orders

GET /orders/{userID} 

Response:

{
  "Asks": [
    {
      "UserID": 8,
      "ID": 498081,
      "Amount": 10000,
      "IsBid": false,
      "Price": 10000,
      "Timestamp": 1674833061829744659
    }
  ],
  "Bids": [
    {
      "UserID": 8,
      "ID": 727887,
      "Amount": 10000,
      "IsBid": true,
      "Price": 9000,
      "Timestamp": 1674833061830209674
    }
  ]
}

Post user orders

POST /orders

Parameters:

{
  "userID": 1,
  "Type": "LIMIT",
  "IsBid": true,
  "Amount": 1000,
  "Price": 90,
  "Market": "ETH"
}

Response:

{
  "OrderID": 203300
}

Delete user orders

DELETE /orders/{orderID}

Response:

{
  "msg": "Canceled"
}

About

An Implementation of cryptocurrency exchange includes a Matching Engine for matching buy and sell orders and a Market Maker for providing liquidity to the exchange.


Languages

Language:Go 99.6%Language:Makefile 0.2%Language:Shell 0.1%