1use super::address::AddressReport;
6use crate::display::report::{report_footer, save_report};
7use crate::error::Result;
8use chrono::{DateTime, Utc};
9use std::path::Path;
10
11pub fn generate_address_report(report: &AddressReport) -> String {
13 generate_address_report_core(report, true, true)
14}
15
16pub fn generate_address_report_section(report: &AddressReport) -> String {
18 generate_address_report_core(report, false, false)
19}
20
21pub fn generate_dossier_report(
24 report: &AddressReport,
25 risk: &crate::compliance::risk::RiskAssessment,
26) -> String {
27 let mut md = String::new();
28 md.push_str("# Wallet Dossier\n\n");
29 md.push_str(&format!(
30 "**Address:** `{}` \n**Chain:** {} \n**Generated:** {} \n\n",
31 report.address,
32 capitalize_chain(&report.chain),
33 chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
34 ));
35 md.push_str("---\n\n");
36 md.push_str(&report_balance(report));
37 md.push_str("\n---\n\n");
38 md.push_str(&report_transactions(report));
39 md.push_str("\n---\n\n");
40 md.push_str(&report_tokens(report));
41 md.push_str("\n---\n\n");
42 md.push_str("## Risk Assessment\n\n");
43 md.push_str(&crate::display::format_risk_report(
44 risk,
45 crate::display::OutputFormat::Markdown,
46 true,
47 ));
48 md.push_str(&report_footer());
49 md
50}
51
52fn generate_address_report_core(
53 report: &AddressReport,
54 include_header: bool,
55 include_footer: bool,
56) -> String {
57 let mut md = String::new();
58
59 if include_header {
60 md.push_str(&report_header(report));
61 md.push_str("\n---\n\n");
62 }
63 md.push_str(&report_balance(report));
64 md.push_str("\n---\n\n");
65 md.push_str(&report_transactions(report));
66 md.push_str("\n---\n\n");
67 md.push_str(&report_tokens(report));
68 if include_footer {
69 md.push_str(&report_footer());
70 }
71
72 md
73}
74
75fn report_header(report: &AddressReport) -> String {
76 format!(
77 "# Address Analysis Report\n\n\
78 **Address:** `{}` \n\
79 **Chain:** {} \n\
80 **Generated:** {} \n",
81 report.address,
82 capitalize_chain(&report.chain),
83 Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
84 )
85}
86
87fn report_balance(report: &AddressReport) -> String {
88 let mut s = String::from("## Balance Summary\n\n");
89 s.push_str("| Metric | Value |\n|--------|-------|\n");
90 s.push_str(&format!(
91 "| Native Balance | {} |\n",
92 report.balance.formatted
93 ));
94 if let Some(usd) = report.balance.usd {
95 s.push_str(&format!("| USD Value | ${:.2} |\n", usd));
96 }
97 s.push_str(&format!(
98 "| Transaction Count | {} |\n",
99 report.transaction_count
100 ));
101 s
102}
103
104fn report_transactions(report: &AddressReport) -> String {
105 let mut s = String::from("## Recent Transactions\n\n");
106 match &report.transactions {
107 Some(txs) if !txs.is_empty() => {
108 s.push_str("| Hash | Block | Time | From | To | Value | Status |\n");
109 s.push_str("|------|-------|------|------|-----|-------|--------|\n");
110 for tx in txs.iter().take(20) {
111 let ts = DateTime::<Utc>::from_timestamp(tx.timestamp as i64, 0)
112 .map(|d| d.format("%Y-%m-%d %H:%M").to_string())
113 .unwrap_or_else(|| "-".to_string());
114 let hash_short = if tx.hash.len() > 10 {
115 format!("{}...{}", &tx.hash[..6], &tx.hash[tx.hash.len() - 4..])
116 } else {
117 tx.hash.clone()
118 };
119 let to = tx.to.as_deref().unwrap_or("-");
120 let status = if tx.status { "✓" } else { "✗" };
121 s.push_str(&format!(
122 "| `{}` | {} | {} | `{}` | `{}` | {} | {} |\n",
123 hash_short, tx.block_number, ts, tx.from, to, tx.value, status
124 ));
125 }
126 if txs.len() > 20 {
127 s.push_str(&format!("\n*Showing 20 of {} transactions*\n", txs.len()));
128 }
129 }
130 _ => s.push_str("*No transaction data available*\n"),
131 }
132 s
133}
134
135fn report_tokens(report: &AddressReport) -> String {
136 let mut s = String::from("## Token Balances\n\n");
137 match &report.tokens {
138 Some(tokens) if !tokens.is_empty() => {
139 s.push_str("| Token | Contract | Balance |\n");
140 s.push_str("|-------|----------|--------|\n");
141 for t in tokens {
142 s.push_str(&format!(
143 "| {} ({}) | `{}` | {} |\n",
144 t.name, t.symbol, t.contract_address, t.formatted_balance
145 ));
146 }
147 }
148 _ => s.push_str("*No token balance data available*\n"),
149 }
150 s
151}
152
153fn capitalize_chain(chain: &str) -> String {
154 let mut chars = chain.chars();
155 match chars.next() {
156 None => String::new(),
157 Some(c) => c.to_uppercase().chain(chars).collect(),
158 }
159}
160
161pub fn save_address_report(report: &str, path: impl AsRef<Path>) -> Result<()> {
163 save_report(report, path)
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169 use crate::cli::address::{AddressReport, Balance, TokenBalance, TransactionSummary};
170
171 fn minimal_report() -> AddressReport {
172 AddressReport {
173 address: "0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2".to_string(),
174 chain: "ethereum".to_string(),
175 balance: Balance {
176 raw: "1000000000000000000".to_string(),
177 formatted: "1.0 ETH".to_string(),
178 usd: Some(3500.0),
179 },
180 transaction_count: 10,
181 transactions: None,
182 tokens: None,
183 }
184 }
185
186 #[test]
187 fn test_generate_address_report_section_minimal() {
188 let report = minimal_report();
189 let md = generate_address_report_section(&report);
190 assert!(md.contains("Balance Summary"));
191 assert!(md.contains("1.0 ETH"));
192 assert!(md.contains("$3500.00"));
193 assert!(md.contains("Transaction Count"));
194 assert!(md.contains("No transaction data available"));
195 assert!(md.contains("No token balance data available"));
196 }
197
198 #[test]
199 fn test_generate_address_report_section_with_transactions() {
200 let mut report = minimal_report();
201 report.transactions = Some(vec![TransactionSummary {
202 hash: "0xabc123def456".to_string(),
203 block_number: 12345,
204 timestamp: 1700000000,
205 from: "0xfrom123".to_string(),
206 to: Some("0xto456".to_string()),
207 value: "1 ETH".to_string(),
208 status: true,
209 }]);
210 let md = generate_address_report_section(&report);
211 assert!(md.contains("Recent Transactions"));
212 assert!(md.contains("0xabc1"));
213 assert!(md.contains("12345"));
214 }
215
216 #[test]
217 fn test_generate_address_report_section_with_tokens() {
218 let mut report = minimal_report();
219 report.tokens = Some(vec![TokenBalance {
220 contract_address: "0xusdc".to_string(),
221 symbol: "USDC".to_string(),
222 name: "USD Coin".to_string(),
223 decimals: 6,
224 balance: "1000000".to_string(),
225 formatted_balance: "1.0 USDC".to_string(),
226 }]);
227 let md = generate_address_report_section(&report);
228 assert!(md.contains("Token Balances"));
229 assert!(md.contains("USDC"));
230 assert!(md.contains("USD Coin"));
231 }
232
233 #[test]
234 fn test_generate_address_report_full() {
235 let report = minimal_report();
236 let md = generate_address_report(&report);
237 assert!(md.contains("Address Analysis Report"));
238 assert!(md.contains("0x742d35Cc6634C0532925a3b844Bc9e7595f1b3c2"));
239 assert!(md.contains("Ethereum"));
240 }
241
242 #[test]
243 fn test_capitalize_chain_empty() {
244 assert_eq!(capitalize_chain(""), "");
246 }
247
248 #[test]
249 fn test_save_address_report_to_file() {
250 let dir = tempfile::tempdir().unwrap();
252 let path = dir.path().join("test_addr_report.md");
253 let result = save_address_report("# Test Report\n\nSome content", &path);
254 assert!(result.is_ok());
255 assert!(path.exists());
256 let contents = std::fs::read_to_string(&path).unwrap();
257 assert!(contents.contains("Test Report"));
258 }
259
260 #[test]
261 fn test_report_transactions_more_than_20() {
262 let mut report = minimal_report();
264 let txs: Vec<TransactionSummary> = (0..25)
265 .map(|i| TransactionSummary {
266 hash: format!("0x{:064x}", i),
267 block_number: 12345 + i,
268 timestamp: 1700000000 + i * 60,
269 from: "0xfrom".to_string(),
270 to: Some("0xto".to_string()),
271 value: "0.1 ETH".to_string(),
272 status: true,
273 })
274 .collect();
275 report.transactions = Some(txs);
276 let md = generate_address_report_section(&report);
277 assert!(md.contains("Showing 20 of 25 transactions"));
278 }
279
280 #[test]
281 fn test_generate_dossier_report() {
282 use crate::compliance::risk::{RiskAssessment, RiskCategory, RiskFactor, RiskLevel};
283 use chrono::Utc;
284
285 let report = minimal_report();
286 let risk = RiskAssessment {
287 address: report.address.clone(),
288 chain: report.chain.clone(),
289 overall_score: 3.5,
290 risk_level: RiskLevel::Low,
291 factors: vec![RiskFactor {
292 name: "Test factor".to_string(),
293 category: RiskCategory::Behavioral,
294 score: 3.0,
295 weight: 0.5,
296 description: "A test risk factor".to_string(),
297 evidence: vec!["evidence".to_string()],
298 }],
299 assessed_at: Utc::now(),
300 recommendations: vec!["Be cautious".to_string()],
301 };
302 let md = generate_dossier_report(&report, &risk);
303 assert!(md.contains("Wallet Dossier"));
304 assert!(md.contains("Risk Assessment"));
305 assert!(md.contains("Test factor"));
306 }
307}