renegade_sdk/renegade_wallet_client/actions/
admin_get_open_orders.rs

1//! Fetches all open orders managed by the relayer
2
3use renegade_external_api::http::admin::ADMIN_GET_ORDERS_ROUTE;
4use renegade_external_api::types::{ApiAdminOrder, GetOrdersAdminResponse};
5
6use crate::{
7    RenegadeClientError,
8    actions::{MATCHING_POOL_PARAM, PAGE_TOKEN_PARAM},
9    client::RenegadeClient,
10};
11
12// --- Public Actions --- //
13impl RenegadeClient {
14    /// Fetches all open orders managed by the relayer.
15    ///
16    /// This method will paginate through all of the orders across multiple
17    /// requests, returning them all.
18    pub async fn admin_get_open_orders(&self) -> Result<Vec<ApiAdminOrder>, RenegadeClientError> {
19        let admin_relayer_client = self.get_admin_client()?;
20
21        let path = build_admin_get_orders_path(None, None)?;
22
23        let GetOrdersAdminResponse { mut orders, mut next_page_token } =
24            admin_relayer_client.get(&path).await?;
25
26        while let Some(page_token) = next_page_token {
27            let path = build_admin_get_orders_path(None, Some(page_token))?;
28
29            let response: GetOrdersAdminResponse = admin_relayer_client.get(&path).await?;
30
31            orders.extend(response.orders);
32            next_page_token = response.next_page_token;
33        }
34
35        Ok(orders)
36    }
37
38    /// Fetches all open orders managed by the relayer in the given matching
39    /// pool.
40    ///
41    /// This method will paginate through all of the orders across multiple
42    /// requests, returning them all.
43    pub async fn admin_get_open_orders_in_matching_pool(
44        &self,
45        matching_pool: String,
46    ) -> Result<Vec<ApiAdminOrder>, RenegadeClientError> {
47        let admin_relayer_client = self.get_admin_client()?;
48
49        let path = build_admin_get_orders_path(Some(&matching_pool), None)?;
50
51        let GetOrdersAdminResponse { mut orders, mut next_page_token } =
52            admin_relayer_client.get(&path).await?;
53
54        while let Some(page_token) = next_page_token {
55            let path = build_admin_get_orders_path(Some(&matching_pool), Some(page_token))?;
56
57            let response: GetOrdersAdminResponse = admin_relayer_client.get(&path).await?;
58
59            orders.extend(response.orders);
60            next_page_token = response.next_page_token;
61        }
62
63        Ok(orders)
64    }
65}
66
67// --- Helpers --- //
68
69/// Builds the request path for the admin get orders endpoint
70fn build_admin_get_orders_path(
71    matching_pool: Option<&str>,
72    page_token: Option<i64>,
73) -> Result<String, RenegadeClientError> {
74    let mut params: Vec<(&str, String)> = Vec::new();
75    if let Some(pool) = matching_pool {
76        params.push((MATCHING_POOL_PARAM, pool.to_string()));
77    }
78    if let Some(token) = page_token {
79        params.push((PAGE_TOKEN_PARAM, token.to_string()));
80    }
81
82    if params.is_empty() {
83        Ok(ADMIN_GET_ORDERS_ROUTE.to_string())
84    } else {
85        let query_string =
86            serde_urlencoded::to_string(&params).map_err(RenegadeClientError::serde)?;
87        Ok(format!("{ADMIN_GET_ORDERS_ROUTE}?{query_string}"))
88    }
89}