scope-bca-cli 0.8.5

Scope Blockchain Analysis - Command-line interface library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Error display and remediation hints for CLI output.
//!
//! Colors errors red and hints dimmed when stderr is a TTY.
//! Falls back to plain text when piped.

use crate::cli::output::Output;
use crate::errln;
use scope::display::theme::{Painter, Role, color_enabled_stderr};
use scope::error::ScopeError;

/// Displays an error with a remediation hint when available.
///
/// Uses color when stderr is a TTY, plain text otherwise.
pub fn display_error(e: &ScopeError, out: &Output) -> std::io::Result<()> {
    display_error_styled(e, color_enabled_stderr(), out)
}

/// Internal styled implementation, testable with an explicit `tty` flag.
fn display_error_styled(e: &ScopeError, tty: bool, out: &Output) -> std::io::Result<()> {
    let msg = match e {
        ScopeError::NotFound(inner) => inner.clone(),
        other => format!("{}", other),
    };

    // A disabled painter returns the text unchanged.
    let painter = Painter::new(tty);
    errln!(
        out,
        "\n  {} {}",
        painter.paint_strong(Role::Err, "✗"),
        painter.paint(Role::Err, &msg)
    )?;

    // The source chain names the real cause (for example a TLS failure
    // under "error sending request"). Show each link once.
    let causes = cause_chain(e, &msg);
    for cause in &causes {
        errln!(
            out,
            "    {} {}",
            painter.paint(Role::Secondary, "caused by:"),
            cause
        )?;
    }

    let hint = if causes.iter().any(|c| is_non_tls_reply(c)) {
        Some(INTERCEPTION_HINT.to_string())
    } else if let Some(h) = override_hint(e, &scope::market::VenueRegistry::load) {
        Some(h)
    } else {
        error_suggestion(e, out.is_tui()).map(str::to_string)
    };
    if let Some(hint) = hint {
        errln!(out, "\n  {}", painter.paint(Role::Secondary, &hint))?;
    }
    errln!(out)?;
    Ok(())
}

/// For a venue's "does not support …" error, how to get the capability
/// back when the user's own file hides the catalog's: `None` otherwise.
/// `load` reads the venues (the registry, or a test's).
fn override_hint(
    e: &ScopeError,
    load: &dyn Fn() -> scope::error::Result<scope::market::VenueRegistry>,
) -> Option<String> {
    let ScopeError::Venue { venue, message } = e else {
        return None;
    };
    let missing = message.strip_prefix("does not support ")?;
    let capability = match missing {
        "order book" => "order_book",
        "OHLC" => "ohlc",
        other => other,
    };
    let registry = load().ok()?;
    registry
        .hidden_capabilities(venue)
        .contains(&capability)
        .then(|| registry.override_hint(venue))
        .flatten()
}

/// Shown when a TLS handshake got a non-TLS reply.
const INTERCEPTION_HINT: &str = "The server's reply was not TLS. A router, firewall, proxy or VPN on your\n      \
     network may be blocking this host and answering with its own block page.\n      \
     Allow the host in that device, or try another network.";

/// The messages of `e`'s source chain, without links whose text is already
/// in `shown` (the top-level message usually repeats the first source).
fn cause_chain(e: &ScopeError, shown: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let mut src = std::error::Error::source(e);
    while let Some(s) = src {
        let text = s.to_string();
        if !shown.contains(&text) && !out.contains(&text) {
            out.push(text);
        }
        src = s.source();
    }
    out
}

/// True when a cause says the peer answered a TLS handshake with non-TLS
/// data (rustls: InvalidContentType; OpenSSL: wrong version number).
fn is_non_tls_reply(cause: &str) -> bool {
    cause.contains("InvalidContentType") || cause.contains("wrong version number")
}

/// Where to learn more about a partial failure, such as a section that
/// could not be fetched. In the TUI (`tui`), where `-v` has no effect, it
/// names the TUI's help.
pub fn details_hint(tui: bool) -> &'static str {
    if tui {
        "run 'help <command>' for usage"
    } else {
        "use -v for details"
    }
}

/// Next steps for a network failure inside the TUI, where `-v` has no effect.
const TUI_NETWORK_HINT: &str = "Check your network connection and try again.\n      \
     run 'help <command>' for usage; set 'format json' for raw output";

/// Returns a user-facing suggestion for common error types. In the TUI
/// (`tui`) the suggestions name TUI commands, not CLI flags.
pub fn error_suggestion(e: &ScopeError, tui: bool) -> Option<&'static str> {
    match e {
        ScopeError::InvalidAddress(_) => Some(
            "Ensure the address format matches the target chain.\n      \
             EVM: 0x followed by 40 hex characters\n      \
             Solana: base58 encoded public key\n      \
             Tron: T followed by base58 characters",
        ),
        ScopeError::InvalidHash(_) => Some(
            "Ensure the transaction hash matches the target chain.\n      \
             EVM: 0x followed by 64 hex characters\n      \
             Solana: base58 encoded signature",
        ),
        ScopeError::Config(_) => Some("Run `scope setup` to create or repair your configuration."),
        ScopeError::Request(_) | ScopeError::Network(_) if tui => Some(TUI_NETWORK_HINT),
        ScopeError::Request(_) | ScopeError::Network(_) => Some(
            "Check your network connection and try again.\n      \
             Use -v for more details on the failing request.",
        ),
        ScopeError::Api(msg)
            if msg.contains("401") || msg.contains("403") || msg.contains("key") =>
        {
            Some(
                "Your API key may be missing or invalid.\n      Run `scope setup --key <provider>` to configure it.",
            )
        }
        ScopeError::NotFound(_) => Some(
            "The resource was not found. Verify the address, hash, or token exists on the specified chain.",
        ),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Reproduces a router block page: a TCP server on 127.0.0.1 that answers
    /// the TLS ClientHello with plaintext HTTP (what ASUS AiProtection did to
    /// api.dexscreener.com). Returns the error reqwest gives for it.
    async fn plaintext_reply_error() -> ScopeError {
        use std::io::{Read, Write};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        std::thread::spawn(move || {
            if let Ok((mut s, _)) = listener.accept() {
                let mut buf = [0u8; 1024];
                let _ = s.read(&mut buf);
                let _ = s.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
            }
        });
        let err = reqwest::Client::new()
            .get(format!("https://127.0.0.1:{port}/"))
            .send()
            .await
            .unwrap_err();
        ScopeError::Request(err)
    }

    #[tokio::test]
    async fn test_request_error_shows_its_cause_chain() {
        // "error sending request" alone hid the real cause for an hour.
        let e = plaintext_reply_error().await;
        let (o, cap) = crate::cli::output::Output::capture();
        display_error_styled(&e, false, &o).unwrap();
        let text = cap.err();
        assert!(text.contains("caused by:"), "{}", text);
        assert!(
            text.contains("InvalidContentType") || text.contains("corrupt message"),
            "{}",
            text
        );
    }

    #[tokio::test]
    async fn test_non_tls_reply_gets_an_interception_hint() {
        // A plaintext reply to a TLS handshake means something on the
        // network path answered instead of the server.
        let e = plaintext_reply_error().await;
        let (o, cap) = crate::cli::output::Output::capture();
        display_error_styled(&e, false, &o).unwrap();
        let text = cap.err();
        assert!(text.contains("router, firewall, proxy or VPN"), "{}", text);
        assert!(!text.contains("Check your network connection"), "{}", text);
    }

    #[test]
    fn test_error_without_source_prints_no_cause_lines() {
        let (o, cap) = crate::cli::output::Output::capture();
        display_error_styled(&ScopeError::Other("plain".into()), false, &o).unwrap();
        assert!(!cap.err().contains("caused by"));
    }

    fn quiet() -> crate::cli::output::Output {
        crate::cli::output::Output::capture().0
    }

    // ================================================================
    // display_error (delegates to non-TTY in CI)
    // ================================================================

    #[test]
    fn test_details_hint_names_no_cli_flag_in_the_tui() {
        assert_eq!(details_hint(false), "use -v for details");
        assert_eq!(details_hint(true), "run 'help <command>' for usage");
        assert!(!details_hint(true).contains("-v"));
    }

    #[test]
    fn test_display_error_goes_to_err_channel_with_hint() {
        // Piped stdout must stay clean on failure, and the user still needs
        // the message and the remediation hint.
        let (o, cap) = crate::cli::output::Output::capture();
        display_error_styled(&ScopeError::InvalidAddress("0xbad".into()), false, &o).unwrap();
        assert_eq!(cap.out(), "");
        assert!(
            cap.err().contains("✗ Invalid address format: 0xbad"),
            "{}",
            cap.err()
        );
        assert!(cap.err().contains("EVM: 0x followed by 40 hex characters"));
    }

    #[test]
    fn test_display_error_not_found() {
        let err = ScopeError::NotFound("test resource".into());
        display_error(&err, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_invalid_address() {
        let err = ScopeError::InvalidAddress("0xbad".into());
        display_error(&err, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_other() {
        let err = ScopeError::Other("something went wrong".into());
        display_error(&err, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_chain() {
        let err = ScopeError::Chain("chain error".into());
        display_error(&err, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_api() {
        let err = ScopeError::Api("500 Internal Server Error".into());
        display_error(&err, &quiet()).unwrap();
    }

    // ================================================================
    // display_error_styled — TTY branch (colored output)
    // ================================================================

    #[test]
    fn test_display_error_styled_tty_not_found() {
        let err = ScopeError::NotFound("test resource".into());
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_styled_tty_invalid_address() {
        let err = ScopeError::InvalidAddress("0xbad".into());
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_styled_tty_config() {
        use scope::error::ConfigError;
        let err = ScopeError::Config(ConfigError::NotFound {
            path: std::path::PathBuf::from("/missing"),
        });
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_styled_tty_network() {
        let err = ScopeError::Network("timeout".into());
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_tui_network_error_gives_tui_next_steps_not_dash_v() {
        let (o, cap) = Output::capture_merged();
        display_error_styled(&ScopeError::Network("timeout".into()), false, &o).unwrap();
        let text = cap.err();
        assert!(!text.contains("-v"), "{text}");
        assert!(
            text.contains("run 'help <command>' for usage; set 'format json' for raw output"),
            "{text}"
        );
    }

    #[test]
    fn test_cli_network_error_keeps_dash_v_suggestion() {
        let (o, cap) = Output::capture();
        display_error_styled(&ScopeError::Network("timeout".into()), false, &o).unwrap();
        assert!(
            cap.err().contains("Use -v for more details"),
            "{}",
            cap.err()
        );
    }

    #[test]
    fn test_venue_error_displays_with_the_venue_name() {
        let (o, cap) = Output::capture();
        let e = ScopeError::Venue {
            venue: "binance".into(),
            message: "API error: HTTP 400".into(),
        };
        display_error_styled(&e, false, &o).unwrap();
        assert!(
            cap.err().contains("Venue error (binance):"),
            "{}",
            cap.err()
        );
    }

    #[test]
    fn test_display_error_styled_tty_api_auth() {
        let err = ScopeError::Api("401 Unauthorized".into());
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_styled_tty_other_no_hint() {
        let err = ScopeError::Other("random".into());
        display_error_styled(&err, true, &quiet()).unwrap();
    }

    #[test]
    fn test_display_error_styled_non_tty() {
        let err = ScopeError::NotFound("test".into());
        display_error_styled(&err, false, &quiet()).unwrap();
    }

    // ================================================================
    // error_suggestion
    // ================================================================

    #[test]
    fn test_error_suggestion_invalid_address() {
        let err = ScopeError::InvalidAddress("bad".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("EVM"));
    }

    #[test]
    fn test_error_suggestion_invalid_hash() {
        let err = ScopeError::InvalidHash("bad".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("64 hex"));
    }

    #[test]
    fn test_error_suggestion_config() {
        use scope::error::ConfigError;
        let err = ScopeError::Config(ConfigError::NotFound {
            path: std::path::PathBuf::from("/missing"),
        });
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("scope setup"));
    }

    #[test]
    fn test_error_suggestion_network() {
        let err = ScopeError::Network("timeout".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("network"));
    }

    #[test]
    fn test_error_suggestion_api_auth() {
        let err = ScopeError::Api("401 Unauthorized".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("API key"));
    }

    #[test]
    fn test_error_suggestion_api_key_keyword() {
        let err = ScopeError::Api("invalid api key".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
    }

    #[test]
    fn test_error_suggestion_api_no_auth() {
        let err = ScopeError::Api("500 Internal Server Error".into());
        assert!(error_suggestion(&err, false).is_none());
    }

    #[test]
    fn test_error_suggestion_not_found() {
        let err = ScopeError::NotFound("address".into());
        let hint = error_suggestion(&err, false);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("not found"));
    }

    #[test]
    fn test_error_suggestion_other_returns_none() {
        let err = ScopeError::Other("random".into());
        assert!(error_suggestion(&err, false).is_none());
    }

    #[tokio::test]
    async fn test_request_error_cause_chain_is_also_shown_on_a_color_terminal() {
        // Color must not hide the real cause behind "error sending request".
        let e = plaintext_reply_error().await;
        let (o, cap) = crate::cli::output::Output::capture();
        display_error_styled(&e, true, &o).unwrap();
        let text = cap.err();
        assert!(text.contains("caused by:"), "{}", text);
        assert!(text.contains('\x1b'), "tty output is styled: {}", text);
    }

    #[test]
    fn test_a_missing_capability_your_file_hides_gets_the_override_hint() {
        let catalog = tempfile::tempdir().unwrap();
        let user = tempfile::tempdir().unwrap();
        let weex = "id: weex\nname: W\nbase_url: https://w\nsymbol:\n  template: \"{base}{quote}\"\n  default_quote: USDT\ncapabilities:\n  trades:\n    path: /t\n    response:\n      price: p\n      quantity: q\n";
        let with_ohlc = format!(
            "{weex}  ohlc:\n    path: /k\n    response:\n      ohlc_format: array_of_arrays\n"
        );
        std::fs::write(catalog.path().join("weex.yaml"), with_ohlc).unwrap();
        std::fs::write(user.path().join("weex.yaml"), weex).unwrap();
        let (c, u) = (catalog.path().to_path_buf(), user.path().to_path_buf());
        let load = move || scope::market::VenueRegistry::load_dirs(&c, &u);
        let e = ScopeError::Venue {
            venue: "weex".into(),
            message: "does not support OHLC".into(),
        };
        let hint = override_hint(&e, &load).unwrap();
        assert!(
            hint.contains("overrides the catalog's weex, which also has ohlc"),
            "{hint}"
        );
        // Another missing capability the catalog lacks too, and other errors.
        let ticker = ScopeError::Venue {
            venue: "weex".into(),
            message: "does not support ticker".into(),
        };
        assert!(override_hint(&ticker, &load).is_none());
        assert!(override_hint(&ScopeError::Other("x".into()), &load).is_none());
        // The hint shows under the error.
        let (out, cap) = Output::capture();
        let shown = override_hint(&e, &load);
        assert!(shown.is_some());
        display_error_styled(&e, false, &out).unwrap();
        assert!(cap.err().contains("does not support OHLC"), "{}", cap.err());
    }
}