1use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5
6pub const CAP_SCOPED_INDICATIVE: &str = "scoped_indicative";
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct ScopeId(pub [u8; 16]);
23
24impl ScopeId {
25 pub fn to_hex(self) -> String {
27 let mut out = String::with_capacity(32);
28 for b in self.0 {
29 use std::fmt::Write;
30 write!(out, "{b:02x}").expect("writing hex to String cannot fail");
31 }
32 out
33 }
34
35 pub fn from_label(label: &str) -> Option<Self> {
40 let bytes = label.as_bytes();
41 if bytes.is_empty() || bytes.len() > 16 {
42 return None;
43 }
44 let mut buf = [0u8; 16];
45 buf[..bytes.len()].copy_from_slice(bytes);
46 Some(Self(buf))
47 }
48}
49
50impl std::fmt::Display for ScopeId {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.write_str(&self.to_hex())
53 }
54}
55
56impl TryFrom<&str> for ScopeId {
57 type Error = ScopeIdParseError;
58
59 fn try_from(s: &str) -> Result<Self, Self::Error> {
60 let bytes = s.as_bytes();
61 if bytes.len() != 32 {
62 return Err(ScopeIdParseError::Length(bytes.len()));
63 }
64 let mut buf = [0u8; 16];
65 for (i, chunk) in bytes.chunks_exact(2).enumerate() {
66 let hi = hex_nibble(chunk[0]).ok_or(ScopeIdParseError::NotLowercaseHex)?;
67 let lo = hex_nibble(chunk[1]).ok_or(ScopeIdParseError::NotLowercaseHex)?;
68 buf[i] = (hi << 4) | lo;
69 }
70 Ok(Self(buf))
71 }
72}
73
74fn hex_nibble(c: u8) -> Option<u8> {
77 match c {
78 b'0'..=b'9' => Some(c - b'0'),
79 b'a'..=b'f' => Some(c - b'a' + 10),
80 _ => None,
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
85#[displaystr::display]
86pub enum ScopeIdParseError {
87 Length(usize) = "scope must be 32 lowercase hex chars, got length {_0}",
89 NotLowercaseHex = "scope must be canonical lowercase hex",
91}
92
93impl Serialize for ScopeId {
94 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
95 serializer.serialize_str(&self.to_hex())
96 }
97}
98
99impl<'de> Deserialize<'de> for ScopeId {
100 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
101 let s = std::borrow::Cow::<str>::deserialize(deserializer)?;
104 Self::try_from(s.as_ref()).map_err(serde::de::Error::custom)
105 }
106}
107
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113pub struct GatewayResumeQuoteProvider {
114 pub wallet: String,
115 pub timestamp: String,
116 pub nonce: u64,
117 pub signature: String,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122#[serde(tag = "type", rename_all = "snake_case")]
123pub enum QpClientMessage {
124 ConnectQuoteProvider {
126 wallet: String,
127 timestamp: String,
128 nonce: u64,
129 signature: String,
130 },
131 GatewayResumeQuoteProvider {
135 wallet: String,
136 timestamp: String,
137 nonce: u64,
138 signature: String,
139 },
140 IndicativeQuoteUpdate { quotes: Vec<IndicativeQuote> },
144 ScopedIndicativeQuoteUpdate {
154 scope: ScopeId,
155 quotes: Vec<IndicativeQuote>,
156 },
157 RfqResponse {
159 rfq_id: String,
160 action: String,
162 #[serde(skip_serializing_if = "Option::is_none")]
163 legs: Option<Vec<QpResponseLeg>>,
164 #[serde(skip_serializing_if = "Option::is_none")]
165 net_premium: Option<String>,
166 #[serde(skip_serializing_if = "Option::is_none")]
167 valid_for_ms: Option<u64>,
168 #[serde(skip_serializing_if = "Option::is_none")]
169 signature: Option<String>,
170 #[serde(skip_serializing_if = "Option::is_none")]
171 nonce: Option<u64>,
172 },
173}
174
175#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
177#[serde(tag = "type", rename_all = "snake_case")]
178pub enum QpServerMessage {
179 Authenticated {
180 wallet: String,
181 #[serde(default, skip_serializing_if = "Vec::is_empty")]
187 capabilities: Vec<String>,
188 },
189 AuthFailed {
190 reason: String,
191 },
192 RfqRequest {
193 rfq_id: String,
194 legs: Vec<QpRfqLeg>,
195 taker_wallet: String,
196 request_timestamp: u64,
197 response_deadline_ms: u64,
198 #[serde(default, skip_serializing_if = "Option::is_none")]
199 auto_accept_limit: Option<String>,
200 #[serde(default)]
201 auto_execute: bool,
202 #[serde(default, skip_serializing_if = "Option::is_none")]
203 taker_limit_price: Option<String>,
204 #[serde(default, skip_serializing_if = "Option::is_none")]
205 reference_price: Option<String>,
206 #[serde(default, skip_serializing_if = "Option::is_none")]
207 min_improvement_tick: Option<String>,
208 #[serde(default, skip_serializing_if = "Option::is_none")]
209 auction_deadline_ms: Option<u64>,
210 #[serde(default)]
211 requires_price_improvement: bool,
212 },
213 QpMarginRejection {
214 rfq_id: String,
215 quote_id: String,
216 reason: String,
217 },
218 RfqAlreadyFilled {
219 rfq_id: String,
220 filled_by_quote_id: String,
221 },
222}
223
224pub type QpInboundMessage = QpClientMessage;
226pub type QpOutboundMessage = QpServerMessage;
227
228#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
230pub struct IndicativeQuote {
231 pub instrument: String,
232 pub bid_price: String,
233 pub ask_price: String,
234 pub max_bid_size: String,
235 pub max_ask_size: String,
236}
237
238#[derive(Debug, PartialEq, Eq, Deserialize)]
244pub struct BorrowedIndicativeQuoteUpdate<'a> {
245 #[serde(rename = "type")]
246 _message_type: IndicativeQuoteUpdateType,
247 #[serde(borrow)]
248 pub quotes: Vec<BorrowedIndicativeQuote<'a>>,
249}
250
251#[derive(Debug, PartialEq, Eq, Deserialize)]
252#[serde(rename_all = "snake_case")]
253enum IndicativeQuoteUpdateType {
254 IndicativeQuoteUpdate,
255}
256
257#[derive(Debug, PartialEq, Eq, Deserialize)]
259pub struct BorrowedIndicativeQuote<'a> {
260 #[serde(borrow)]
261 pub instrument: Cow<'a, str>,
262 #[serde(borrow)]
263 pub bid_price: Cow<'a, str>,
264 #[serde(borrow)]
265 pub ask_price: Cow<'a, str>,
266 #[serde(borrow)]
267 pub max_bid_size: Cow<'a, str>,
268 #[serde(borrow)]
269 pub max_ask_size: Cow<'a, str>,
270}
271
272#[derive(Debug, PartialEq, Eq, Deserialize)]
276pub struct BorrowedScopedIndicativeQuoteUpdate<'a> {
277 #[serde(rename = "type")]
278 _message_type: ScopedIndicativeQuoteUpdateType,
279 pub scope: ScopeId,
280 #[serde(borrow)]
281 pub quotes: Vec<BorrowedIndicativeQuote<'a>>,
282}
283
284#[derive(Debug, PartialEq, Eq, Deserialize)]
285#[serde(rename_all = "snake_case")]
286enum ScopedIndicativeQuoteUpdateType {
287 ScopedIndicativeQuoteUpdate,
288}
289
290#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
292pub struct QpResponseLeg {
293 pub instrument: String,
294 pub side: String,
295 pub price: String,
296 pub size: String,
297}
298
299#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
304pub struct QpRfqLeg {
305 pub instrument: String,
306 pub side: String,
307 pub size: String,
308 #[serde(default, skip_serializing_if = "Option::is_none")]
312 pub physical: Option<QpPhysicalLeg>,
313}
314
315#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
319pub struct QpPhysicalLeg {
320 pub settles_as: String,
323 pub deliver_token: u32,
326 pub deliver_token_symbol: String,
328 pub basis: String,
331}
332
333#[cfg(test)]
334#[path = "qp_test.rs"]
335mod tests;