simulator_api/
route_plan.rs1use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(transparent)]
19pub struct RoutePlan(Vec<RouteHop>);
20
21impl RoutePlan {
22 pub fn new(hops: Vec<RouteHop>) -> Option<Self> {
24 (!hops.is_empty()).then_some(Self(hops))
25 }
26
27 pub fn hops(&self) -> &[RouteHop] {
29 &self.0
30 }
31
32 pub fn is_empty(&self) -> bool {
34 self.0.is_empty()
35 }
36
37 pub fn is_split(&self) -> bool {
39 is_split(&self.0)
40 }
41}
42
43pub fn is_split(hops: &[RouteHop]) -> bool {
46 hops.iter().any(|hop| hop.percent != FULL_PERCENT)
47}
48
49pub fn pool_keys(hops: &[RouteHop]) -> impl Iterator<Item = &str> {
52 hops.iter().filter_map(RouteHop::amm_key)
53}
54
55pub fn labels(hops: &[RouteHop]) -> impl Iterator<Item = &str> {
57 hops.iter().filter_map(RouteHop::label)
58}
59
60pub const FULL_PERCENT: u64 = 100;
62
63const fn full_percent() -> u64 {
64 FULL_PERCENT
65}
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(rename_all = "camelCase")]
70pub struct RouteHop {
71 #[serde(default)]
73 pub leg: Option<u32>,
74 #[serde(default = "full_percent")]
76 pub percent: u64,
77 #[serde(default)]
80 pub program: Option<String>,
81 #[serde(default)]
83 pub bps: Option<u64>,
84 pub swap_info: RouteHopSwapInfo,
86}
87
88impl Default for RouteHop {
90 fn default() -> Self {
91 Self {
92 leg: None,
93 percent: FULL_PERCENT,
94 program: None,
95 bps: None,
96 swap_info: RouteHopSwapInfo::default(),
97 }
98 }
99}
100
101impl RouteHop {
102 pub fn label(&self) -> Option<&str> {
104 self.swap_info.label.as_deref()
105 }
106
107 pub fn amm_key(&self) -> Option<&str> {
109 self.swap_info.amm_key.as_deref()
110 }
111
112 pub fn input_mint(&self) -> Option<&str> {
114 self.swap_info.input_mint.as_deref()
115 }
116
117 pub fn output_mint(&self) -> Option<&str> {
119 self.swap_info.output_mint.as_deref()
120 }
121}
122
123#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126pub struct RouteHopSwapInfo {
127 #[serde(default)]
130 pub label: Option<String>,
131 #[serde(default)]
133 pub amm_key: Option<String>,
134 #[serde(default)]
136 pub input_mint: Option<String>,
137 #[serde(default)]
139 pub in_amount: Option<String>,
140 #[serde(default)]
142 pub output_mint: Option<String>,
143 #[serde(default)]
145 pub out_amount: Option<String>,
146}
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151
152 fn hop(percent: u64) -> RouteHop {
153 RouteHop {
154 percent,
155 ..Default::default()
156 }
157 }
158
159 #[test]
160 fn no_hops_is_absent_rather_than_empty() {
161 assert_eq!(RoutePlan::new(Vec::new()), None);
162 assert_eq!(
163 RoutePlan::new(vec![hop(FULL_PERCENT)]).map(|plan| plan.hops().len()),
164 Some(1)
165 );
166 }
167
168 #[test]
169 fn a_route_is_split_when_any_hop_carries_less_than_the_whole_trade() {
170 assert!(
171 !RoutePlan::new(vec![hop(100), hop(100)])
172 .expect("a plan")
173 .is_split()
174 );
175 assert!(
176 RoutePlan::new(vec![hop(60), hop(40)])
177 .expect("a plan")
178 .is_split()
179 );
180 }
181
182 #[test]
184 fn both_sources_serialize_onto_one_shape() {
185 let quoted = RouteHop {
186 swap_info: RouteHopSwapInfo {
187 label: Some("SolFi".to_string()),
188 amm_key: Some("pool".to_string()),
189 input_mint: Some("sol".to_string()),
190 in_amount: Some("1000".to_string()),
191 output_mint: Some("usdc".to_string()),
192 out_amount: Some("1010".to_string()),
193 },
194 ..Default::default()
195 };
196 let executed = RouteHop {
197 leg: Some(0),
198 program: Some("prog".to_string()),
199 swap_info: RouteHopSwapInfo {
200 amm_key: None,
201 ..quoted.swap_info.clone()
202 },
203 ..quoted.clone()
204 };
205
206 let json = |hop: &RouteHop| serde_json::to_value(hop).expect("json");
207 for value in [json("ed), json(&executed)] {
208 assert_eq!(value["percent"], 100);
209 assert_eq!(value["swapInfo"]["label"], "SolFi");
210 assert_eq!(value["swapInfo"]["inputMint"], "sol");
211 assert_eq!(value["swapInfo"]["inAmount"], "1000");
213 assert_eq!(value["swapInfo"]["outAmount"], "1010");
214 }
215 assert_eq!(json("ed)["swapInfo"]["ammKey"], "pool");
216 assert_eq!(json(&executed)["program"], "prog");
217 }
218
219 #[test]
221 fn a_recorded_plan_reads_back_onto_the_paths_it_was_written_with() {
222 let recorded = r#"[{"percent":100,"swapInfo":{"label":"Aquifer","ammKey":"CNC5Tae","inputMint":"EPjFWdd","outputMint":"So11111","inAmount":"385638781","outAmount":"4999931316"},"bps":null}]"#;
223
224 let plan: RoutePlan = serde_json::from_str(recorded).expect("a recorded plan decodes");
225 let rewritten = serde_json::to_value(&plan).expect("json");
226
227 assert_eq!(rewritten[0]["percent"], 100);
228 assert_eq!(rewritten[0]["swapInfo"]["label"], "Aquifer");
229 assert_eq!(rewritten[0]["swapInfo"]["ammKey"], "CNC5Tae");
230 assert_eq!(rewritten[0]["swapInfo"]["inputMint"], "EPjFWdd");
231 assert_eq!(rewritten[0]["swapInfo"]["inAmount"], "385638781");
232 assert_eq!(rewritten[0]["swapInfo"]["outputMint"], "So11111");
233 assert_eq!(rewritten[0]["swapInfo"]["outAmount"], "4999931316");
234 assert_eq!(rewritten[0]["bps"], serde_json::Value::Null);
235 }
236
237 #[test]
240 fn an_unset_field_is_written_as_null_rather_than_omitted() {
241 let value = serde_json::to_value(hop(FULL_PERCENT)).expect("json");
242
243 assert!(value.get("program").is_some(), "written, not skipped");
244 assert_eq!(value["program"], serde_json::Value::Null);
245 assert_eq!(value["swapInfo"]["ammKey"], serde_json::Value::Null);
246 assert_eq!(value["program"], value["neverWritten"]);
247 }
248}