/galois

Fusotao proving client.

Primary LanguageRustApache License 2.0Apache-2.0

Galois

Granted by
NEAR Grants Program

License GitHub Workflow Status (branch)

Introduction

Galois is an extremely high performance matching engine written in Rust.

Galois uses Event Sourcing pattern to handle tens of thousands of orders per second or even better, depending on the performance of persistence. Basic architecture is shown below.

                   core dump(disk)
                        ^
                        ^
                   +----------+
events(mysql)  >>  |  galois  |  >> match results(mysql)/best n price(redis)
                   +----------+
                        ^
                        ^
                 query requests(TCP) 
                       

Galois works as a component of Fusotao which is being WIP. The version without fusotao feature enabled can be used for building a high performance exchange with broker(refer to Guide to implement a broker). Buttttt, we strongly recommend you shall hire an expert before you do the business, seriously.

Getting Started

Dependencies

  • MySQL: persist the events and output the match result
  • Redis: output the best n price of the orderbook

Quick Start

Compile galois with nightly version. Then cp galois.toml.example galois.toml and modify the galois.toml the mysql and redis configurations, as well as the snapshot directory.

# init mysql
mysql -u {user_name} -p {database} < init.sql

# start redis
redis-server

galois -c galois.toml

Galois is now waiting for the incoming events and execute. Before you can execute orders, you need to issue a new pair and create a mysql table to receive the matching result outputs. The UserId is hex format of H256 with a mandatory 0x prefix or ss58check when fusotao enabled.

# create a table to receive outputs from galois, 100 and 101 represent the base currency code and the quote currency code.
create table t_clearing_result_100_101 like t_clearing_result;

# tell galois to create a new trading pair 101/100 with base_scale=4, quote_scale=4 and other parameters.
insert into t_sequence(f_cmd) values('{"base":101,"quote":100,"base_scale":4,"quote_scale":4,"taker_fee":"0.002","maker_fee":"0.002","min_amount":"0.1","min_vol":"10","enable_market_order":false,"open":true,"cmd":13}');

# account 1 tranfer 100.0 100 currency into galois
insert into t_sequence(f_cmd) values('{"currency":100,"amount":"100.0","cmd":11,"user_id":"0x0000000000000000000000000000000000000000000000000000000000000001"}');

# account 1 place an order(bid,symbol=101100,price=10.0,amount=0.5,user_id=1,order_id=1), order_id is generated by broker
insert into t_sequence(f_cmd) values('{"quote":100,"base":101,"cmd":1,"price":"10.0","amount":"0.5","order_id":1,"user_id":"0x0000000000000000000000000000000000000000000000000000000000000001"}');

If everything is ok, you could see a record in t_clearing_result_100_101 which means the order has been accepted. At the same time, a key V2_DEPTH_L32_101_100 would be available in redis to render the markets depth of 101100.

Instructions

ASK_LIMIT = 0;
BID_LIMIT = 1;
CANCEL = 4;
CANCEL_ALL = 5; # only available when fusotao disabled
TRANSFER_OUT = 10;
TRANSFER_IN = 11;
UPDATE_SYMBOL = 13;
QUERY_ORDER = 14;
QUERY_BALANCE = 15;
QUERY_ACCOUNTS = 16;
DUMP = 17;

License

Galois is licensed under Apache 2.0