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 1a9f135
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/dlob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,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 +94,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 1a9f135

Please sign in to comment.