use serde::{Deserialize, Serialize};
use std::borrow::Cow;
pub const CAP_SCOPED_INDICATIVE: &str = "scoped_indicative";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScopeId(pub [u8; 16]);
impl ScopeId {
pub fn to_hex(self) -> String {
let mut out = String::with_capacity(32);
for b in self.0 {
use std::fmt::Write;
write!(out, "{b:02x}").expect("writing hex to String cannot fail");
}
out
}
pub fn from_label(label: &str) -> Option<Self> {
let bytes = label.as_bytes();
if bytes.is_empty() || bytes.len() > 16 {
return None;
}
let mut buf = [0u8; 16];
buf[..bytes.len()].copy_from_slice(bytes);
Some(Self(buf))
}
}
impl std::fmt::Display for ScopeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.to_hex())
}
}
impl TryFrom<&str> for ScopeId {
type Error = ScopeIdParseError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let bytes = s.as_bytes();
if bytes.len() != 32 {
return Err(ScopeIdParseError::Length(bytes.len()));
}
let mut buf = [0u8; 16];
for (i, chunk) in bytes.chunks_exact(2).enumerate() {
let hi = hex_nibble(chunk[0]).ok_or(ScopeIdParseError::NotLowercaseHex)?;
let lo = hex_nibble(chunk[1]).ok_or(ScopeIdParseError::NotLowercaseHex)?;
buf[i] = (hi << 4) | lo;
}
Ok(Self(buf))
}
}
fn hex_nibble(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[displaystr::display]
pub enum ScopeIdParseError {
Length(usize) = "scope must be 32 lowercase hex chars, got length {_0}",
NotLowercaseHex = "scope must be canonical lowercase hex",
}
impl Serialize for ScopeId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.to_hex())
}
}
impl<'de> Deserialize<'de> for ScopeId {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = std::borrow::Cow::<str>::deserialize(deserializer)?;
Self::try_from(s.as_ref()).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GatewayResumeQuoteProvider {
pub wallet: String,
pub timestamp: String,
pub nonce: u64,
pub signature: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum QpClientMessage {
ConnectQuoteProvider {
wallet: String,
timestamp: String,
nonce: u64,
signature: String,
},
GatewayResumeQuoteProvider {
wallet: String,
timestamp: String,
nonce: u64,
signature: String,
},
IndicativeQuoteUpdate { quotes: Vec<IndicativeQuote> },
ScopedIndicativeQuoteUpdate {
scope: ScopeId,
quotes: Vec<IndicativeQuote>,
},
RfqResponse {
rfq_id: String,
action: String,
#[serde(skip_serializing_if = "Option::is_none")]
legs: Option<Vec<QpResponseLeg>>,
#[serde(skip_serializing_if = "Option::is_none")]
net_premium: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
valid_for_ms: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
signature: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<u64>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum QpServerMessage {
Authenticated {
wallet: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
capabilities: Vec<String>,
},
AuthFailed {
reason: String,
},
RfqRequest {
rfq_id: String,
legs: Vec<QpRfqLeg>,
taker_wallet: String,
request_timestamp: u64,
response_deadline_ms: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
auto_accept_limit: Option<String>,
#[serde(default)]
auto_execute: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
taker_limit_price: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
reference_price: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
min_improvement_tick: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
auction_deadline_ms: Option<u64>,
#[serde(default)]
requires_price_improvement: bool,
},
QpMarginRejection {
rfq_id: String,
quote_id: String,
reason: String,
},
RfqAlreadyFilled {
rfq_id: String,
filled_by_quote_id: String,
},
}
pub type QpInboundMessage = QpClientMessage;
pub type QpOutboundMessage = QpServerMessage;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndicativeQuote {
pub instrument: String,
pub bid_price: String,
pub ask_price: String,
pub max_bid_size: String,
pub max_ask_size: String,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct BorrowedIndicativeQuoteUpdate<'a> {
#[serde(rename = "type")]
_message_type: IndicativeQuoteUpdateType,
#[serde(borrow)]
pub quotes: Vec<BorrowedIndicativeQuote<'a>>,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
enum IndicativeQuoteUpdateType {
IndicativeQuoteUpdate,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct BorrowedIndicativeQuote<'a> {
#[serde(borrow)]
pub instrument: Cow<'a, str>,
#[serde(borrow)]
pub bid_price: Cow<'a, str>,
#[serde(borrow)]
pub ask_price: Cow<'a, str>,
#[serde(borrow)]
pub max_bid_size: Cow<'a, str>,
#[serde(borrow)]
pub max_ask_size: Cow<'a, str>,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct BorrowedScopedIndicativeQuoteUpdate<'a> {
#[serde(rename = "type")]
_message_type: ScopedIndicativeQuoteUpdateType,
pub scope: ScopeId,
#[serde(borrow)]
pub quotes: Vec<BorrowedIndicativeQuote<'a>>,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
enum ScopedIndicativeQuoteUpdateType {
ScopedIndicativeQuoteUpdate,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QpResponseLeg {
pub instrument: String,
pub side: String,
pub price: String,
pub size: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QpRfqLeg {
pub instrument: String,
pub side: String,
pub size: String,
}
#[cfg(test)]
#[path = "qp_test.rs"]
mod tests;