mostro_client/cli/
list_orders.rs

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