Skip to main content

strike_sdk/indexer/
client.rs

1//! HTTP client for the Strike indexer REST API.
2
3use crate::error::{Result, StrikeError};
4use crate::indexer::types::*;
5
6/// Client for the Strike indexer REST API.
7///
8/// Used for bootstrap/snapshot reads. Live data comes from on-chain WSS.
9pub struct IndexerClient {
10    base_url: String,
11    http: reqwest::Client,
12}
13
14impl IndexerClient {
15    pub(crate) fn new(base_url: &str) -> Self {
16        Self {
17            base_url: base_url.trim_end_matches('/').to_string(),
18            http: reqwest::Client::new(),
19        }
20    }
21
22    /// Fetch all markets from the indexer.
23    pub async fn get_markets(&self) -> Result<Vec<Market>> {
24        let url = format!("{}/markets", self.base_url);
25        let resp: MarketsResponse = self
26            .http
27            .get(&url)
28            .send()
29            .await?
30            .json()
31            .await
32            .map_err(|e| StrikeError::Indexer(e.to_string()))?;
33        Ok(resp.markets)
34    }
35
36    /// Fetch only active markets (status == "active").
37    pub async fn get_active_markets(&self) -> Result<Vec<Market>> {
38        let markets = self.get_markets().await?;
39        Ok(markets
40            .into_iter()
41            .filter(|m| m.status == "active")
42            .collect())
43    }
44
45    /// Fetch the orderbook snapshot for a market.
46    pub async fn get_orderbook(&self, market_id: u64) -> Result<OrderbookSnapshot> {
47        let url = format!("{}/markets/{}/orderbook", self.base_url, market_id);
48        let resp: OrderbookSnapshot = self
49            .http
50            .get(&url)
51            .send()
52            .await?
53            .json()
54            .await
55            .map_err(|e| StrikeError::Indexer(e.to_string()))?;
56        Ok(resp)
57    }
58
59    /// Fetch open orders for a given address.
60    pub async fn get_open_orders(&self, address: &str) -> Result<Vec<IndexerOrder>> {
61        let url = format!("{}/positions/{}", self.base_url, address);
62        let resp: PositionsResponse = self
63            .http
64            .get(&url)
65            .send()
66            .await?
67            .json()
68            .await
69            .map_err(|e| StrikeError::Indexer(e.to_string()))?;
70        Ok(resp.open_orders)
71    }
72}