Skip to main content

quorum_sdk/
types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub enum Objective {
5    Fastest,
6    HighestLanding,
7    LowestFee,
8    MevProtected,
9}
10
11impl Objective {
12    pub fn as_str(&self) -> &'static str {
13        match self {
14            Objective::Fastest => "fastest",
15            Objective::HighestLanding => "highest_landing",
16            Objective::LowestFee => "lowest_fee",
17            Objective::MevProtected => "mev_protected",
18        }
19    }
20}
21
22impl std::fmt::Display for Objective {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        f.write_str(self.as_str())
25    }
26}
27
28impl std::str::FromStr for Objective {
29    type Err = crate::QuorumError;
30
31    fn from_str(s: &str) -> Result<Self, Self::Err> {
32        match s.to_lowercase().as_str() {
33            "fastest" => Ok(Objective::Fastest),
34            "highest_landing" | "highestlanding" => Ok(Objective::HighestLanding),
35            "lowest_fee" | "lowestfee" => Ok(Objective::LowestFee),
36            "mev_protected" | "mevprotected" => Ok(Objective::MevProtected),
37            other => Err(crate::QuorumError::InvalidResponse(format!(
38                "unknown objective: {}",
39                other
40            ))),
41        }
42    }
43}
44
45impl Serialize for Objective {
46    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
47    where
48        S: serde::Serializer,
49    {
50        serializer.serialize_str(self.as_str())
51    }
52}
53
54impl<'de> Deserialize<'de> for Objective {
55    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
56    where
57        D: serde::Deserializer<'de>,
58    {
59        let s = String::deserialize(deserializer)?;
60        s.parse().map_err(serde::de::Error::custom)
61    }
62}
63
64#[derive(Debug, Serialize)]
65pub(crate) struct ExecuteRequest {
66    pub wire_transaction_b64: String,
67    pub objective: Objective,
68}
69
70#[derive(Debug, Clone, Deserialize)]
71pub struct ExecutionResult {
72    #[serde(alias = "success", default)]
73    pub landed: bool,
74    pub signature: Option<String>,
75    #[serde(alias = "route_used")]
76    pub route_type: Option<String>,
77    pub latency_ms: Option<u64>,
78    #[serde(default)]
79    pub attempts: usize,
80    pub request_id: Option<String>,
81    pub validator: Option<String>,
82    pub tpu_addr: Option<String>,
83    pub confirmation_ms: Option<u64>,
84    pub fee_paid: Option<u64>,
85    pub predicted_probability: Option<f64>,
86    pub error: Option<String>,
87}
88
89#[derive(Debug, Clone, Deserialize)]
90pub struct Route {
91    #[serde(rename = "type")]
92    pub route_type: Option<String>,
93    pub validator: Option<String>,
94    pub tpu_addr: Option<String>,
95    pub latency_ms: Option<u64>,
96    pub score: Option<f64>,
97}
98
99#[derive(Debug, Clone, Deserialize)]
100pub struct PrimaryRoute {
101    #[serde(rename = "type")]
102    pub mode: Option<String>,
103    #[serde(default)]
104    pub routes: Vec<Route>,
105}
106
107#[derive(Debug, Clone, Deserialize)]
108pub struct FallbackRoute {
109    #[serde(rename = "type")]
110    pub route_type: Option<String>,
111    pub endpoint: Option<String>,
112    pub validator: Option<String>,
113    pub tpu_addr: Option<String>,
114}
115
116#[derive(Debug, Clone, Deserialize)]
117pub struct Strategy {
118    pub objective: String,
119    pub primary: Option<PrimaryRoute>,
120    #[serde(default)]
121    pub fallbacks: Vec<FallbackRoute>,
122    pub landing_probability: f64,
123    pub recommended_fee_microlamports: u64,
124    pub ttl_secs: Option<u64>,
125    pub computed_at: Option<String>,
126    pub validator: Option<String>,
127    pub tpu_addr: Option<String>,
128    pub latency_ms: Option<u64>,
129}
130
131impl Strategy {
132    pub fn primary_leader(&self) -> Option<&Route> {
133        self.primary.as_ref().and_then(|p| p.routes.first())
134    }
135
136    pub fn primary_validator(&self) -> Option<&str> {
137        self.primary_leader()
138            .and_then(|r| r.validator.as_deref())
139            .or(self.validator.as_deref())
140    }
141
142    pub fn primary_tpu_addr(&self) -> Option<&str> {
143        self.primary_leader()
144            .and_then(|r| r.tpu_addr.as_deref())
145            .or(self.tpu_addr.as_deref())
146    }
147
148    pub fn primary_latency_ms(&self) -> Option<u64> {
149        self.primary_leader()
150            .and_then(|r| r.latency_ms)
151            .or(self.latency_ms)
152    }
153}
154
155#[derive(Debug, Clone, Deserialize)]
156pub struct ValidatorInfo {
157    pub pubkey: String,
158    pub latency_ms: Option<u64>,
159    pub score: f64,
160    #[serde(default)]
161    pub reachable: bool,
162    #[serde(alias = "client_type")]
163    pub client: Option<String>,
164    pub tpu_addr: Option<String>,
165    #[serde(default)]
166    pub observer_count: Option<u32>,
167    #[serde(default)]
168    pub supports_bundles: Option<bool>,
169    #[serde(default)]
170    pub computed_at: Option<String>,
171}
172
173#[derive(Debug, Clone, Deserialize)]
174pub struct CongestionSignal {
175    #[serde(alias = "congestion_level", default)]
176    pub congestion_level: f64,
177    #[serde(default)]
178    pub fee_multiplier: Option<f64>,
179    #[serde(default)]
180    pub unreachable_ratio: Option<f64>,
181    #[serde(default)]
182    pub mean_latency_ms: Option<u64>,
183    #[serde(default)]
184    pub timestamp: Option<String>,
185    #[serde(default)]
186    pub level: Option<String>,
187    #[serde(default)]
188    pub recommended_fee_microlamports: Option<u64>,
189    #[serde(default)]
190    pub slot_fill_rate: Option<f64>,
191    #[serde(default)]
192    pub drop_rate: Option<f64>,
193    #[serde(default)]
194    pub updated_at: Option<u64>,
195}
196
197impl CongestionSignal {
198    pub fn level_str(&self) -> &str {
199        if let Some(ref l) = self.level {
200            return l.as_str();
201        }
202        if self.congestion_level < 0.25 {
203            "low"
204        } else if self.congestion_level < 0.60 {
205            "medium"
206        } else if self.congestion_level < 0.85 {
207            "high"
208        } else {
209            "critical"
210        }
211    }
212
213    pub fn fee_recommendation(&self) -> u64 {
214        if let Some(fee) = self.recommended_fee_microlamports {
215            return fee;
216        }
217        if let Some(mult) = self.fee_multiplier {
218            return (mult * 5000.0) as u64;
219        }
220        5000
221    }
222}
223
224#[derive(Debug, Clone, Deserialize)]
225pub struct HealthStatus {
226    pub status: String,
227    pub version: Option<String>,
228    #[serde(default)]
229    pub ready: bool,
230    #[serde(alias = "validators_scored", default)]
231    pub validators_tracked: usize,
232    pub congestion_level: Option<f64>,
233    pub last_updated: Option<String>,
234    pub last_sweep_id: Option<u64>,
235}