Skip to content

Commit

Permalink
feat: add support for depth parameter in orderbook query
Browse files Browse the repository at this point in the history
This commit adds new method to DLOBClient, which allows to request
orderbook with a required depth. To handle cases when depth is not
provided, default constant can be used, which is set to value of 10.

Motivation: currently DLOBClient requests the entirety of orderbook,
which contains around 50 levels. Sometimes this can be unecessary, for
example if we are only interested in best bid/ask, which is why it can
be useful to have an option to control the depth of requested orderbook.
  • Loading branch information
bmuddha committed Feb 28, 2024
1 parent 379cc59 commit 6866359
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::types::Context;

static STATE_ACCOUNT: OnceLock<Pubkey> = OnceLock::new();

pub const DEFAULT_ORDERBOOK_DEPTH: u8 = 10;

pub const TOKEN_PROGRAM_ID: Pubkey =
solana_sdk::pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");

Expand Down
15 changes: 13 additions & 2 deletions src/dlob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tokio::{
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};

use crate::{
constants::DEFAULT_ORDERBOOK_DEPTH,
types::{MarketId, SdkError, SdkResult},
utils::dlob_subscribe_ws_json,
};
Expand All @@ -43,14 +44,23 @@ impl DLOBClient {
}
/// Query L2 Orderbook for given `market`
pub async fn get_l2(&self, market: MarketId) -> Result<L2Orderbook, SdkError> {
self.get_l2_with_depth(market, DEFAULT_ORDERBOOK_DEPTH)
.await
}

pub async fn get_l2_with_depth(
&self,
market: MarketId,
depth: u8,
) -> Result<L2Orderbook, SdkError> {
let market_type = match market.kind {
MarketType::Perp => "perp",
MarketType::Spot => "spot",
};
let response = self
.client
.get(format!(
"{}/l2?marketType={}&marketIndex={}",
"{}/l2?marketType={}&marketIndex={}&depth={depth}",
&self.url, market_type, market.index
))
.send()
Expand Down Expand Up @@ -89,7 +99,8 @@ impl DLOBClient {
async move {
loop {
let _ = interval.tick().await;
if tx.try_send(client.get_l2(market).await).is_err() {
let book = client.get_l2(market).await;
if tx.try_send(book).is_err() {
// capacity reached or receiver closed, end the subscription task
break;
}
Expand Down

0 comments on commit 6866359

Please sign in to comment.