simulator_client/reroute_report/
render.rs1use super::{Direction, Flow, Report, fold, short_mint};
4
5const MAX_DIRECTIONS: usize = 12;
8
9impl Report {
10 pub fn render(&self, subtitle: Option<&str>) -> String {
15 let target = self.target();
16 let mut out = String::new();
17 let name = target
18 .label()
19 .map(str::to_string)
20 .or_else(|| target.program().map(|program| program.to_string()))
21 .unwrap_or_else(|| "venue".to_string());
22 out.push_str(&format!("\n {name}"));
23 if let Some(subtitle) = subtitle {
24 out.push_str(&format!(" {subtitle}"));
25 }
26 out.push_str("\n\n");
27
28 let priced = self.sol_usd().is_some();
29 out.push_str(&row("", "on L1", "re-quoted", "delta"));
30 let total = self.total();
31 out.push_str(&rows_for(&total, "", priced));
32 out.push_str(&share_row(&total, ""));
33
34 let mut ranked: Vec<_> = self
35 .by_direction()
36 .filter(|(_, direction)| direction.present())
37 .collect();
38 ranked.sort_unstable_by_key(|(mints, direction)| {
39 (std::cmp::Reverse(direction.rank()), *mints)
40 });
41 for ((input, output), direction) in ranked.iter().take(MAX_DIRECTIONS) {
42 out.push_str(&format!(
43 "\n {} → {}\n",
44 short_mint(input),
45 short_mint(output)
46 ));
47 out.push_str(&rows_for(direction, " ", priced));
48 out.push_str(&share_row(direction, " "));
49 }
50 if let Some(rest) = ranked.get(MAX_DIRECTIONS..).filter(|rest| !rest.is_empty()) {
51 let tail = fold(rest.iter().map(|(_, direction)| **direction));
52 out.push_str(&format!("\n + {} more directions\n", rest.len()));
53 out.push_str(&rows_for(&tail, " ", priced));
54 out.push_str(&share_row(&tail, " "));
55 }
56
57 out
58 }
59
60 pub fn to_json(&self) -> serde_json::Value {
62 let target = self.target();
63 let total = self.total();
64 let directions: Vec<_> = self
65 .by_direction()
66 .map(|((input, output), direction)| {
67 serde_json::json!({
68 "inputMint": input,
69 "outputMint": output,
70 "l1": flow_json(&direction.l1),
71 "requoted": flow_json(&direction.requoted),
72 "sharePct": direction.share().map(|(l1, _)| l1),
73 "requotedSharePct": direction.share().map(|(_, requoted)| requoted),
74 })
75 })
76 .collect();
77 serde_json::json!({
78 "label": target.label(),
79 "program": target.program().map(|program| program.to_string()),
80 "total": {
81 "l1": flow_json(&total.l1),
82 "requoted": flow_json(&total.requoted),
83 },
84 "share": {
86 "l1Pct": total.share().map(|(l1, _)| l1),
87 "requotedPct": total.share().map(|(_, requoted)| requoted),
88 },
89 "byDirection": directions,
90 })
91 }
92}
93
94fn flow_json(flow: &Flow) -> serde_json::Value {
95 serde_json::json!({
96 "swaps": flow.legs,
97 "usd": flow.usd,
98 "swapsWithoutAmount": flow.without_amount,
99 "swapsWithoutPrice": flow.without_price,
100 })
101}
102
103fn share_row(direction: &Direction, indent: &str) -> String {
104 let Some((l1, requoted)) = direction.share() else {
105 return String::new();
106 };
107 row(
108 &format!("{indent}share"),
109 &format!("{l1:.1}%"),
110 &format!("{requoted:.1}%"),
111 &format!("{:+.1}pp", requoted - l1),
112 )
113}
114
115fn row(label: &str, l1: &str, requoted: &str, delta: &str) -> String {
116 format!(" {label:<10} {l1:>14} {requoted:>14} {delta:>14}\n")
117}
118
119fn rows_for(direction: &Direction, indent: &str, priced: bool) -> String {
122 let mut out = totals("swaps", indent, direction.l1.legs, direction.requoted.legs);
123 if priced && !direction.fully_unpriced() {
124 out.push_str(&usd_row(direction, indent));
125 }
126 if direction.unpriced() {
127 out.push_str(&format!(
128 "{indent} ({} swaps in mints this range could not price)\n",
129 direction.l1.without_price + direction.requoted.without_price
130 ));
131 }
132 out
133}
134
135fn totals(label: &str, indent: &str, l1: u64, requoted: u64) -> String {
136 row(
137 &format!("{indent}{label}"),
138 &l1.to_string(),
139 &requoted.to_string(),
140 &signed(i128::from(requoted) - i128::from(l1)),
141 )
142}
143
144fn usd_row(direction: &Direction, indent: &str) -> String {
145 row(
146 &format!("{indent}volume"),
147 &money(direction.l1.usd),
148 &money(direction.requoted.usd),
149 &format!(
150 "{}{}",
151 if direction.requoted.usd < direction.l1.usd {
152 "-"
153 } else {
154 "+"
155 },
156 money((direction.requoted.usd - direction.l1.usd).abs())
157 ),
158 )
159}
160
161fn money(usd: f64) -> String {
162 match usd {
163 usd if usd >= 1e6 => format!("${:.2}M", usd / 1e6),
164 usd if usd >= 1e3 => format!("${:.1}k", usd / 1e3),
165 usd => format!("${usd:.0}"),
166 }
167}
168
169fn signed(delta: i128) -> String {
170 match delta < 0 {
171 true => delta.to_string(),
172 false => format!("+{delta}"),
173 }
174}