mschneider / solcpp

A fast Solana and Mango Markets C++ SDK
Other
40 stars 13 forks source link

Example orderbook subscribe sol. #19

Open papadpickle opened 2 years ago

papadpickle commented 2 years ago

this example subscribes to SOL perp market on-chain events for bids, asks and fill. following info is logged at each update of orderbook as well as fill event:

note: in case no fill event received before an orderbook update, the latest trade will be empty. maybe there is a way to get the last traded price(mango-bowl recent trades maybe?), in that case we can add that in future.

snippet: image

additions:

minor improvements:

mschneider commented 2 years ago
[2022-03-01 20:24:16.874] [info] ============Update============
[2022-03-01 20:24:16.874] [info] Latest trade: 9880
[2022-03-01 20:24:16.874] [info] Bid-Ask 9879-9880
[2022-03-01 20:24:16.874] [info] MidPrice: 9879.5
[2022-03-01 20:24:16.874] [info] Spread: 1.01 bps
[2022-03-01 20:24:16.874] [info] Market depth -2%: 1458616
[2022-03-01 20:24:16.874] [info] Market depth +2%: 950953

output should probably display ui units instead of native units, i'll open a separate ticket for this

mschneider commented 2 years ago

tried it locally and the code works well for a few minutes but then one by one the different sockets disconnect. once a single socket disconnects, aggregated data can be invalid (e.g. mid price, after one side of the book stopped updating)

papadpickle commented 2 years ago

added changes to stop logging and exit in case of problem at any of the wss connections. image

papadpickle commented 2 years ago

re your comment https://github.com/mschneider/solcpp/pull/19#discussion_r817093630:

  1. parsing of the messages is moved to the classes(BookSide and Trade in mango_v3.hpp) out of subscriptions.
  2. handled at point 1
  3. this is first iteration of parsing the incoming messages and extracted only necessary info. We can fill up/edit the parsing and data struct as we go was my idea. You can create an issue here which can be handled later.
  4. I have refactored the code by adding AccountSubscriber class, removed unnecessary subscriber classes and replacing critical sections with shared_ptr.

I have one point about the structure of the code overall: we can refactor the monolithic mango_v3.hpp into individual hpp and cpp files to speed up dev work. Now a single line change at those interfaces forces the recompilation of everything. Since we are anyway creating a static lib, we may as well go with seperate files instead of having one huge hpp. Unless of course you decide to make it a header only lib.

papadpickle commented 2 years ago

updated the PR with following changes: