renegade_sdk/external_match_client/
options.rs

1//! Request options types for the client
2use url::form_urlencoded;
3
4use crate::{
5    api_types::{
6        ASSEMBLE_EXTERNAL_MATCH_ROUTE, REQUEST_EXTERNAL_MATCH_ROUTE, REQUEST_EXTERNAL_QUOTE_ROUTE,
7    },
8    types::ExternalOrder,
9    GAS_REFUND_NATIVE_ETH_QUERY_PARAM,
10};
11
12use super::{GAS_REFUND_ADDRESS_QUERY_PARAM, GAS_SPONSORSHIP_QUERY_PARAM};
13
14/// The options for requesting a quote
15#[derive(Clone, Default)]
16pub struct RequestQuoteOptions {
17    /// Whether to disable gas sponsorship
18    pub disable_gas_sponsorship: bool,
19    /// The address to refund gas to if `sponsor_gas` is true
20    pub gas_refund_address: Option<String>,
21    /// Whether to refund gas in terms of native ETH, as opposed to in-kind
22    pub refund_native_eth: bool,
23}
24
25impl RequestQuoteOptions {
26    /// Create a new options with default values
27    pub fn new() -> Self {
28        Default::default()
29    }
30
31    /// Disable gas sponsorship
32    pub fn disable_gas_sponsorship(mut self) -> Self {
33        self.disable_gas_sponsorship = true;
34        self
35    }
36
37    /// Set the gas refund address
38    pub fn with_gas_refund_address(mut self, gas_refund_address: String) -> Self {
39        self.gas_refund_address = Some(gas_refund_address);
40        self
41    }
42
43    /// Set whether to refund gas in terms of native ETH
44    pub fn with_refund_native_eth(mut self) -> Self {
45        self.refund_native_eth = true;
46        self
47    }
48
49    /// Get the request path given the options
50    pub(crate) fn build_request_path(&self) -> String {
51        let mut query = form_urlencoded::Serializer::new(String::new());
52        query.append_pair(GAS_SPONSORSHIP_QUERY_PARAM, &self.disable_gas_sponsorship.to_string());
53        query.append_pair(GAS_REFUND_NATIVE_ETH_QUERY_PARAM, &self.refund_native_eth.to_string());
54
55        if let Some(addr) = &self.gas_refund_address {
56            query.append_pair(GAS_REFUND_ADDRESS_QUERY_PARAM, addr);
57        }
58
59        format!("{}?{}", REQUEST_EXTERNAL_QUOTE_ROUTE, query.finish())
60    }
61}
62
63/// The options for requesting an external match
64#[deprecated(
65    since = "0.1.0",
66    note = "This endpoint will soon be removed, use `request_quote` and `assemble_quote` instead"
67)]
68#[derive(Clone, Default)]
69pub struct ExternalMatchOptions {
70    /// Whether to perform gas estimation
71    pub do_gas_estimation: bool,
72    /// Whether or not to request gas sponsorship for the match
73    ///
74    /// If granted, the auth server will sign the bundle to indicate that the
75    /// gas paid to settle the match should be refunded to the given address
76    /// (`tx.origin` if not specified). This is subject to a rate limit.
77    pub sponsor_gas: bool,
78    /// The address to refund gas to if `sponsor_gas` is true
79    pub gas_refund_address: Option<String>,
80    /// The receiver address that the darkpool will send funds to
81    ///
82    /// If not provided, the receiver address is the message sender
83    pub receiver_address: Option<String>,
84}
85
86#[allow(deprecated)]
87impl ExternalMatchOptions {
88    /// Create a new options with default values
89    pub fn new() -> Self {
90        Default::default()
91    }
92
93    /// Set the gas estimation flag
94    pub fn with_gas_estimation(mut self, do_gas_estimation: bool) -> Self {
95        self.do_gas_estimation = do_gas_estimation;
96        self
97    }
98
99    /// Set the receiver address
100    pub fn with_receiver_address(mut self, receiver_address: String) -> Self {
101        self.receiver_address = Some(receiver_address);
102        self
103    }
104
105    /// Request gas sponsorship
106    pub fn request_gas_sponsorship(mut self) -> Self {
107        self.sponsor_gas = true;
108        self
109    }
110
111    /// Set the gas refund address
112    pub fn with_gas_refund_address(mut self, gas_refund_address: String) -> Self {
113        self.gas_refund_address = Some(gas_refund_address);
114        self
115    }
116
117    /// Get the request path given the options
118    pub(crate) fn build_request_path(&self) -> String {
119        let mut query = form_urlencoded::Serializer::new(String::new());
120
121        // Add query params for gas sponsorship
122        query.append_pair(GAS_SPONSORSHIP_QUERY_PARAM, &(!self.sponsor_gas).to_string());
123        if let Some(addr) = &self.gas_refund_address {
124            query.append_pair(GAS_REFUND_ADDRESS_QUERY_PARAM, addr);
125        }
126
127        format!("{}?{}", REQUEST_EXTERNAL_MATCH_ROUTE, query.finish())
128    }
129}
130
131/// The options for assembling a quote
132#[derive(Clone, Default)]
133pub struct AssembleQuoteOptions {
134    /// Whether to do gas estimation
135    pub do_gas_estimation: bool,
136    /// Whether or not to allow shared access to the resulting bundle
137    ///
138    /// If true, the bundle may be sent to other clients requesting an external
139    /// match. If false, the bundle will be exclusively held for some time
140    pub allow_shared: bool,
141    /// Whether or not to request gas sponsorship for the match
142    ///
143    /// If granted, the auth server will sign the bundle to indicate that the
144    /// gas paid to settle the match should be refunded to the given address
145    /// (`tx.origin` if not specified). This is subject to a rate limit.
146    #[deprecated(
147        since = "0.1.0",
148        note = "This option will soon be removed, request gas sponsorship when requesting a quote instead"
149    )]
150    pub sponsor_gas: bool,
151    /// The address to refund gas to if `sponsor_gas` is true
152    #[deprecated(
153        since = "0.1.0",
154        note = "This option will soon be removed, request gas sponsorship when requesting a quote instead"
155    )]
156    pub gas_refund_address: Option<String>,
157    /// The receiver address that the darkpool will send funds to
158    ///
159    /// If not provided, the receiver address is the message sender
160    pub receiver_address: Option<String>,
161    /// The updated order to use when assembling the quote
162    ///
163    /// The `base_amount`, `quote_amount`, and `min_fill_size` are allowed to
164    /// change, but the pair and side is not
165    pub updated_order: Option<ExternalOrder>,
166}
167
168impl AssembleQuoteOptions {
169    /// Create a new options with default values
170    pub fn new() -> Self {
171        Default::default()
172    }
173
174    /// Set the gas estimation flag
175    pub fn with_gas_estimation(mut self, do_gas_estimation: bool) -> Self {
176        self.do_gas_estimation = do_gas_estimation;
177        self
178    }
179
180    /// Set the allow shared flag
181    pub fn with_allow_shared(mut self, allow_shared: bool) -> Self {
182        self.allow_shared = allow_shared;
183        self
184    }
185
186    /// Request gas sponsorship
187    #[deprecated(
188        since = "0.1.0",
189        note = "This option will soon be removed, request gas sponsorship when requesting a quote instead"
190    )]
191    #[allow(deprecated)]
192    pub fn request_gas_sponsorship(mut self) -> Self {
193        self.sponsor_gas = true;
194        self
195    }
196
197    /// Set the gas refund address
198    #[deprecated(
199        since = "0.1.0",
200        note = "This option will soon be removed, request gas sponsorship when requesting a quote instead"
201    )]
202    #[allow(deprecated)]
203    pub fn with_gas_refund_address(mut self, gas_refund_address: String) -> Self {
204        self.gas_refund_address = Some(gas_refund_address);
205        self
206    }
207
208    /// Set the receiver address
209    pub fn with_receiver_address(mut self, receiver_address: String) -> Self {
210        self.receiver_address = Some(receiver_address);
211        self
212    }
213
214    /// Set the updated order
215    pub fn with_updated_order(mut self, updated_order: ExternalOrder) -> Self {
216        self.updated_order = Some(updated_order);
217        self
218    }
219
220    /// Get the request path given the options
221    #[allow(deprecated)]
222    pub(crate) fn build_request_path(&self) -> String {
223        let mut query = form_urlencoded::Serializer::new(String::new());
224        if self.sponsor_gas {
225            // We only write this query parameter if it was explicitly set. The
226            // expectation of the auth server is that when gas sponsorship is
227            // requested at the quote stage, there should be no query parameters
228            // at all in the assemble request.
229            query.append_pair(GAS_SPONSORSHIP_QUERY_PARAM, &(!self.sponsor_gas).to_string());
230        }
231
232        if let Some(addr) = &self.gas_refund_address {
233            query.append_pair(GAS_REFUND_ADDRESS_QUERY_PARAM, addr);
234        }
235
236        let query_str = query.finish();
237        if query_str.is_empty() {
238            return ASSEMBLE_EXTERNAL_MATCH_ROUTE.to_string();
239        }
240
241        format!("{ASSEMBLE_EXTERNAL_MATCH_ROUTE}?{query_str}")
242    }
243}