Blockchain use-case for the real stock market — a blockchain-based OTC ATS decentralized application

There are plenty of crypto trading platforms out there, but those are only serving a tiny piece of the total financial markets today. The natural next step is to develop trading platforms that are far bigger and serves more people with a greater purpose. eFiat is currently developing a blockchain-based OTC Trading Platform (aka eFiat’s OTC ATS blockchain). The system is based on private Ethereum Blockchain, while the execution of the trading orders is done by smart contracts written in Solidity and Web3.js. This article describes the methodology and interface protocol of eFiat’s OTC ATS blockchain, the OTC trading platform for securities. eFiat’s OTC ATS blockchain provides a decentralized, highly resilient, low latency, flexible and scalable platform for financial-institution participants to access eFiat’s OTC ATS blockchain for order, quote and various trade management.

Challenges:

There are many different challenges in developing a decentralized application (DApp), though the most complex part has been handled by the Web3.js library. Our developers have to abandon their favorite relational database tool and instead master EVM data management and storage. The speed of execution, order matching mechanism and storage of various status are also the major challenges we have to address through this project.

Our OTC Trading Platform Characteristics:

  • Ethereum base private blockchain — PoA

Application of struct array with key-value mapping:

Order Storage:

In a relational database, we can easily store trading orders in tables with status as a column and when these an order status changes, for example, due to a full execution, we can just update the status field of a particular order record. However, the story is totally different in blockchain. Practically speaking, each transaction is recorded as a block and once the block confirmed by signers, it cannot be changed. Although we can record every order changes as new blocks, this would exponentially increase the size of the blockchain and a lot of computation power will be wasted.

To resolve this problem, we are trying to store a trading order in struct array — which when a transaction occurs, an order’s outstanding quantity can be updated:

1: {
Symbol: IBM,
BuySell: Buy,
Price: 5,
Qty: 20,
OutstandingQty: 20,
OrderType: Limit Order
},
2: {
Symbol: IBM,
BuySell: Buy,
Price: 5,
Qty: 30,
OutstandingQty: 30,
OrderType: Limit Order
},…

Outstanding Order handlings:

We also use struct array to record outstanding orders (for example in Bid/Ask queues) and quantity for symbol and price:

IBM: {
Price: 5
Outstanding_Qty: 100,
Bid_Order_No: [1,2,3]
Ask_Order_No: []
},
GOOG: {
Price: 15
Outstanding_Qty: 10,
Bid_Order_No: []
Ask_Order_No: [4]
}

When orders are transacted (for example order no 1 & 2), our blockchain system will remove that order in Bid/Ask queue to improve the performance for handling other orders afterward.

IBM: {
Price: 5
Outstanding_Qty: 40,
Bid_Order_No: [3]
Ask_Order_No: []
}

Use of event log to store immutable transaction records:

Events and logs allow our developers to log transaction data as proof of transaction that happened in the blockchain. But apart from logging, events and logs in Ethereum can be used for communication between smart contracts and their user interfaces, which facilitate the real-time communications between the blockchain and our DApp. This is very important as this can be used for monitoring order status changes and display of real time-stocks quotes (this is because stock quotes are the aggregation results of orders in the ATS. In Ethereum, when a transaction is mined, smart contracts can broadcast events and write logs to the blockchain.

When an order status change (includes orders are not fully transacted), our system will record into blockchain event log. This is to ensure no one can modify the transaction records.

Demo implementations:

Ownership Assurance:

  • Only the order owner can update / cancel their own orders

Push notifications:

  • When the status of an order changes, our ATS system will automatically push the updated information to the owner through Ethereum event logs. (There would be no polling or Ajax concept!)

OTC Order matching methodology:

  • First-In-First-Out, orders are created early will be processed firstly,
  • Unmatched — if a new order is unable to match any existing orders in the Bid/Ask queues, our OTC system will insert it into Bid/Ask queues according to price and order time, and they will be pending to the next incoming order to match.