renegade_sdk/renegade_wallet_client/actions/
get_orders.rs

1//! Fetches all orders in the account
2
3use renegade_external_api::{
4    http::order::{GET_ORDERS_ROUTE, GetOrdersResponse},
5    types::ApiOrder,
6};
7
8use crate::{
9    RenegadeClientError,
10    actions::{INCLUDE_HISTORIC_ORDERS_PARAM, PAGE_TOKEN_PARAM, construct_http_path},
11    client::RenegadeClient,
12};
13
14// --- Public Actions --- //
15impl RenegadeClient {
16    /// Fetches all orders in the account, optionally including historic
17    /// (inactive) orders.
18    ///
19    /// This method will paginate through all of the account's orders across
20    /// multiple requests, returning them all.
21    pub async fn get_orders(
22        &self,
23        include_historic_orders: bool,
24    ) -> Result<Vec<ApiOrder>, RenegadeClientError> {
25        let path = self.build_get_orders_request_path(include_historic_orders, None)?;
26
27        let GetOrdersResponse { mut orders, mut next_page_token } =
28            self.relayer_client.get(&path).await?;
29
30        while let Some(page_token) = next_page_token {
31            let path =
32                self.build_get_orders_request_path(include_historic_orders, Some(page_token))?;
33
34            let response: GetOrdersResponse = self.relayer_client.get(&path).await?;
35
36            orders.extend(response.orders);
37            next_page_token = response.next_page_token;
38        }
39
40        Ok(orders)
41    }
42}
43
44// --- Private Helpers --- //
45impl RenegadeClient {
46    /// Builds the request path for the get orders endpoint
47    fn build_get_orders_request_path(
48        &self,
49        include_historic_orders: bool,
50        page_token: Option<i64>,
51    ) -> Result<String, RenegadeClientError> {
52        let path = construct_http_path!(GET_ORDERS_ROUTE, "account_id" => self.get_account_id());
53
54        let mut params = vec![(INCLUDE_HISTORIC_ORDERS_PARAM, include_historic_orders.to_string())];
55        if let Some(token) = page_token {
56            params.push((PAGE_TOKEN_PARAM, token.to_string()));
57        }
58
59        let query_string =
60            serde_urlencoded::to_string(&params).map_err(RenegadeClientError::serde)?;
61
62        Ok(format!("{path}?{query_string}"))
63    }
64}