mostro_client/cli/
orders_info.rs

1use crate::cli::Context;
2use crate::parser::common::{print_key_value, print_section_header};
3use crate::parser::dms::print_commands_results;
4use crate::util::{send_dm, wait_for_dm};
5use anyhow::Result;
6use mostro_core::prelude::*;
7use uuid::Uuid;
8
9pub async fn execute_orders_info(order_ids: &[Uuid], ctx: &Context) -> Result<()> {
10    if order_ids.is_empty() {
11        return Err(anyhow::anyhow!("At least one order ID is required"));
12    }
13
14    print_section_header("📋 Orders Information Request");
15    print_key_value("📊", "Number of Orders", &order_ids.len().to_string());
16    print_key_value("🆔", "Order IDs", &format!("{} order(s)", order_ids.len()));
17    for (i, order_id) in order_ids.iter().enumerate() {
18        print_key_value("  ", &format!("{}.", i + 1), &order_id.to_string());
19    }
20    print_key_value("🎯", "Mostro PubKey", &ctx.mostro_pubkey.to_string());
21    print_key_value("💡", "Action", "Requesting order information...");
22    println!();
23
24    // Create request id
25    let request_id = Uuid::new_v4().as_u128() as u64;
26
27    // Create payload with the order IDs
28    let payload = Payload::Ids(order_ids.to_vec());
29
30    // Create message using the proper Message structure
31    let message = Message::new_order(
32        None,
33        Some(request_id),
34        Some(ctx.trade_index),
35        Action::Orders,
36        Some(payload),
37    );
38
39    // Serialize the message
40    let message_json = message
41        .as_json()
42        .map_err(|_| anyhow::anyhow!("Failed to serialize message"))?;
43
44    // Send the DM
45    let sent_message = send_dm(
46        &ctx.client,
47        Some(&ctx.identity_keys),
48        &ctx.trade_keys,
49        &ctx.mostro_pubkey,
50        message_json,
51        None,
52        false,
53    );
54
55    // Wait for the DM response from mostro
56    let recv_event = wait_for_dm(ctx, None, sent_message).await?;
57
58    // Parse the incoming DM and handle the response
59    let messages = crate::parser::dms::parse_dm_events(recv_event, &ctx.trade_keys, None).await;
60    if let Some((message, _, _)) = messages.first() {
61        let message_kind = message.get_inner_message_kind();
62
63        // Check if this is the expected response
64        if message_kind.request_id == Some(request_id) {
65            print_commands_results(message_kind, ctx).await?;
66        } else {
67            return Err(anyhow::anyhow!(
68                "Received response with mismatched action. Expected: Orders, Got: {:?}",
69                message_kind.action
70            ));
71        }
72    } else {
73        return Err(anyhow::anyhow!("No response received from Mostro"));
74    }
75
76    Ok(())
77}