Bitcoin is 20W, you still don’t know how to play the matchmaking engine

Laravel Package for Matching Engine

Matching Engine For Laravel (matching engine based on redis), PHP high-performance matching engine

Quick start

  • github address
  • installation: composer require sting_bo/mengine
  • Copy the configuration file: php artisan vendor:publish

rely

  • predis

Extra

Instructions for use

  • If a system with existing data uses this library, you can write an initialization script yourself, and run the data into the queue first

  • User orders

  • After placing an order, save it to the database first, and then start the following steps to instantiate the document object

use StingBo\Mengine\Core\Order;

$uuid = 3; // 用户唯一标识
$oid = 4; // 订单唯一标识
$symbol = 'abc2usdt'; // 交易对
$transaction = 'buy'; // 交易方向,buy/sale
$price = 0.4; // 交易价格,会根据设置精度转化为整数
$volume = 15; // 交易数量,会根据设置精度转化为整数

$order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);

交易方向And 交易精度can be flexibly set in the configuration file

return [
    'mengine' => [
        // 交易类型,不可更改
        'transaction' => [
            'buy',
            'sale',
        ],
        // 精度,可更改
        'accuracy' => 8, //default        
        'test2usdt_accuracy' => 4, //设置交易对精度则使用,没有则取accuracy
    ],
];

  • Push to the queue, the queue task needs to be started manually
use StingBo\Mengine\Services\MengineService;

$ms = new MengineService();
$ms->pushQueue($order);

Open the queue task:
php artisan queue:work --queue=abc2usdt
You can also use horizonand supervisorto assist, get twice the result with half the effort!

When the queue is consumed, it will enter the matching process. The approximate steps are as follows:

  1. Get matching orders
  2. If there is no matching order, enter the commission pool and trigger the commission pool change event, see point 5 for details
  3. If there is a matching order, the program is matched and the order pool data is updated
  4. A successful transaction will trigger an event. Developers need to process orders with transactions in the listener, such as updating database data, WebSocket notifications, etc.
    Register a listener for successful matching events in EventServiceProvider:
// 撮合成功通知,参数分别是:当前订单,被匹配的单据,交易数量
event(new MatchEvent($order, $match_order, $match_volume));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\MatchEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器,应该也使用异步来实现
    ],
];
  1. If it is only a partial transaction, the remaining part will enter the commission pool, trigger the commission pool change event, K-line or depth list change notification, etc. The
    registered listeners are as follows:
// 委托池数据变更事件
event(new PushQueueEvent($order));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\PushQueueEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器,应该也使用异步来实现
    ],
];
  • User cancels an order

The order cancellation process should first query the database to confirm whether it can be cancelled, then delete the data from redis successfully, and finally update back to the database

$order = new Order($uuid, $oid, $symbol, $transaction, $volume, $price);
$ms = new MengineService();
$ms->deleteOrder($order);

This matching engine does not implement a lock mechanism like a database. In order to prevent the order to be cancelled when the order is matched, the order and the cancellation are in the same queue to ensure the order. Each transaction pair is The efficiency of the isolated queue is also guaranteed to a certain extent, but development needs to implement the function of asynchronously notifying users. The registered listener is as follows:

// 撤单成功通知
event(new DeleteOrderSuccEvent($order));

// 注册监听器
protected $listen = [
    'StingBo\Mengine\Events\DeleteOrderSuccEvent' => [
        'App\Listeners\YourListener', // 你自己的监听器,应该也使用异步来实现
    ],
];
  • Get a list of buying/selling depths of a certain trading pair

$symbol = 'abc2cny';
$transaction = 'buy';
$ms = new MengineService();
$ms->getDepth($symbol, $transaction);

to sum up

Tested on a local junk notebook, the matching speed of transaction pairs averaged 200 transactions/s, and the matching speed will continue to be optimized in the future

Insert picture description here

Guess you like

Origin blog.csdn.net/sting_bo/article/details/112440210