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 29, 2024
1 parent 379cc59 commit 0045bc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 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
13 changes: 10 additions & 3 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 @@ -42,15 +43,20 @@ impl DLOBClient {
}
}
/// Query L2 Orderbook for given `market`
pub async fn get_l2(&self, market: MarketId) -> Result<L2Orderbook, SdkError> {
pub async fn get_l2(
&self,
market: MarketId,
depth: Option<u8>,
) -> Result<L2Orderbook, SdkError> {
let market_type = match market.kind {
MarketType::Perp => "perp",
MarketType::Spot => "spot",
};
let depth = depth.map(|d| format!("&depth={d}")).unwrap_or_default();
let response = self
.client
.get(format!(
"{}/l2?marketType={}&marketIndex={}",
"{}/l2?marketType={}&marketIndex={}{depth}",
&self.url, market_type, market.index
))
.send()
Expand Down Expand Up @@ -89,7 +95,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, None).await;
if tx.try_send(book).is_err() {
// capacity reached or receiver closed, end the subscription task
break;
}
Expand Down

0 comments on commit 0045bc2

Please sign in to comment.