renegade_sdk/external_match_client/
options.rs

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