renegade_sdk/renegade_wallet_client/actions/
update_order.rs

1//! Updates an order
2
3use renegade_circuit_types::Amount;
4use renegade_external_api::{
5    http::order::{UPDATE_ORDER_ROUTE, UpdateOrderRequest, UpdateOrderResponse},
6    types::{ApiOrder, ApiOrderCore},
7};
8use uuid::Uuid;
9
10use crate::{RenegadeClientError, actions::construct_http_path, client::RenegadeClient};
11
12impl RenegadeClient {
13    /// Updates an order.
14    ///
15    /// Currently, the only parameters which can updated are the order's
16    /// `min_fill_size`, and whether to `allow_external_matches`.
17    pub async fn update_order(
18        &self,
19        order_update_config: OrderUpdateConfig,
20    ) -> Result<ApiOrder, RenegadeClientError> {
21        let request = self.build_update_order_request(order_update_config).await?;
22        let path = construct_http_path!(UPDATE_ORDER_ROUTE, "account_id" => self.get_account_id(), "order_id" => request.order.id);
23        let UpdateOrderResponse { order } = self.relayer_client.post(&path, request).await?;
24
25        Ok(order)
26    }
27
28    /// Builds the order update request
29    async fn build_update_order_request(
30        &self,
31        order_update_config: OrderUpdateConfig,
32    ) -> Result<UpdateOrderRequest, RenegadeClientError> {
33        let mut order = match order_update_config.initial_order {
34            Some(initial_order) => initial_order,
35            None => self.get_order(order_update_config.order_id).await?.order,
36        };
37
38        if let Some(min_fill_size) = order_update_config.min_fill_size {
39            order.min_fill_size = min_fill_size;
40        }
41
42        if let Some(allow_external_matches) = order_update_config.allow_external_matches {
43            order.allow_external_matches = allow_external_matches;
44        }
45
46        Ok(UpdateOrderRequest { order })
47    }
48}
49
50// -----------------------
51// | Order Update Config |
52// -----------------------
53
54/// Container for order update configuration options
55#[derive(Debug, Default)]
56pub struct OrderUpdateConfig {
57    /// The ID of the order to update.
58    order_id: Uuid,
59    /// The initial order to update. If not provided, the order will be fetched
60    /// from the relayer.
61    initial_order: Option<ApiOrderCore>,
62    /// The updated minimum fill size for the order.
63    min_fill_size: Option<Amount>,
64    /// Whether to allow external matches on the order.
65    allow_external_matches: Option<bool>,
66}
67
68impl OrderUpdateConfig {
69    /// Create a new order update config
70    pub fn new(order_id: Uuid) -> Self {
71        Self { order_id, ..Default::default() }
72    }
73
74    /// Set the initial order to update
75    pub fn with_initial_order(
76        mut self,
77        initial_order: ApiOrderCore,
78    ) -> Result<Self, RenegadeClientError> {
79        if initial_order.id != self.order_id {
80            return Err(RenegadeClientError::invalid_order_update(format!(
81                "Initial order ID does not match the order ID to update: {} != {}",
82                initial_order.id, self.order_id
83            )));
84        }
85
86        self.initial_order = Some(initial_order);
87        Ok(self)
88    }
89
90    /// Set the updated minimum fill size for the order
91    pub fn with_min_fill_size(mut self, min_fill_size: Amount) -> Self {
92        self.min_fill_size = Some(min_fill_size);
93        self
94    }
95
96    /// Set whether to allow external matches on the order
97    pub fn with_allow_external_matches(mut self, allow_external_matches: bool) -> Self {
98        self.allow_external_matches = Some(allow_external_matches);
99        self
100    }
101}