mostro_client/cli/
list_orders.rs

1use crate::cli::Context;
2use crate::parser::common::{print_key_value, print_section_header};
3use crate::parser::orders::print_orders_table;
4use crate::util::{fetch_events_list, ListKind};
5use anyhow::Result;
6use mostro_core::prelude::*;
7use std::str::FromStr;
8
9#[allow(clippy::too_many_arguments)]
10pub async fn execute_list_orders(
11    kind: &Option<String>,
12    currency: &Option<String>,
13    status: &Option<String>,
14    ctx: &Context,
15) -> Result<()> {
16    // Used to get upper currency string to check against a list of tickers
17    let mut upper_currency: Option<String> = None;
18    // Default status is pending
19    let mut status_checked: Option<Status> = Some(Status::Pending);
20    // Default kind is none
21    let mut kind_checked: Option<mostro_core::order::Kind> = None;
22
23    // New check against strings
24    if let Some(s) = status {
25        status_checked = Some(
26            Status::from_str(s)
27                .map_err(|e| anyhow::anyhow!("Not valid status '{}': {:?}", s, e))?,
28        );
29    }
30
31    print_section_header("📋 List Orders");
32
33    // Print status requested
34    if let Some(status) = &status_checked {
35        print_key_value("📊", "Status Filter", &format!("{:?}", status));
36    }
37    // New check against strings for kind
38    if let Some(k) = kind {
39        kind_checked = Some(
40            mostro_core::order::Kind::from_str(k)
41                .map_err(|e| anyhow::anyhow!("Not valid order kind '{}': {:?}", k, e))?,
42        );
43        if let Some(kind) = &kind_checked {
44            print_key_value("📈", "Order Type", &format!("{} orders", kind));
45        }
46    }
47
48    // Uppercase currency
49    if let Some(curr) = currency {
50        upper_currency = Some(curr.to_uppercase());
51        if let Some(currency) = &upper_currency {
52            print_key_value("💱", "Currency Filter", currency);
53        }
54    }
55
56    print_key_value("🎯", "Mostro PubKey", &ctx.mostro_pubkey.to_string());
57    print_key_value("💡", "Action", "Fetching orders from relays...");
58    println!();
59
60    // Get orders from relays
61    let table_of_orders = fetch_events_list(
62        ListKind::Orders,
63        status_checked,
64        upper_currency,
65        kind_checked,
66        ctx,
67        None,
68    )
69    .await?;
70    let table = print_orders_table(table_of_orders)?;
71    println!("{table}");
72
73    Ok(())
74}