ic-query-cli 0.100.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: icrc::commands
//!
//! Responsibility: compose generic ICRC CLI command families and shared arguments.
//! Does not own: live ledger calls, report DTOs, or text rendering.
//! Boundary: keeps ledger and account grammar separate behind one ICRC facade.

mod account;
mod analytics;
mod dispatch;
mod ledger;
pub(in crate::icrc) mod options;
#[cfg(test)]
mod tests;
use account::AccountCommand;
use analytics::AnalyticsCommand;
pub use dispatch::run_matches;
use ledger::LedgerCommand;

use crate::cli::clap::principal_text_value_parser;
use clap::{Command as ClapCommand, Subcommand, builder::ValueParser};
use ic_query::icrc::normalize_subaccount_hex;

const DEFAULT_ICRC_TRANSACTIONS_LIMIT: &str = "25";
const MAX_ICRC_TRANSACTIONS_LIMIT: u64 = 100;
const DEFAULT_ICRC_ACCOUNT_TRANSACTION_PAGE_SIZE: &str = "100";
const LEDGER_CANISTER_ID_ARG: &str = "ledger-canister-id";
const INDEX_CANISTER_ID_ARG: &str = "index-canister-id";
const PRINCIPAL_ARG: &str = "principal";
const OWNER_PRINCIPAL_ARG: &str = "owner-principal";
const SPENDER_PRINCIPAL_ARG: &str = "spender-principal";
const SUBACCOUNT_ARG: &str = "subaccount";
const OWNER_SUBACCOUNT_ARG: &str = "owner-subaccount";
const SPENDER_SUBACCOUNT_ARG: &str = "spender-subaccount";
const START_ARG: &str = "start";
const LIMIT_ARG: &str = "limit";
const ACCOUNT_ID_ARG: &str = "account-id";
const PAGE_SIZE_ARG: &str = "page-size";
const MAX_PAGES_ARG: &str = "max-pages";
const SORT_ARG: &str = "sort";
const FOLLOW_ARCHIVES_ARG: &str = "follow-archives";
const FROM_CANISTER_ID_ARG: &str = "from";

///
/// IcrcCommand
///
/// Parsed ICRC family and its complete typed operation tree.
///

#[derive(Subcommand)]
#[command(about = "Inspect generic ICRC ledgers and official analytics", long_about = None)]
enum IcrcCommand {
    #[command(subcommand)]
    Ledger(LedgerCommand),
    #[command(subcommand)]
    Account(AccountCommand),
    #[command(subcommand)]
    Analytics(AnalyticsCommand),
}

pub fn command() -> ClapCommand {
    IcrcCommand::augment_subcommands(ClapCommand::new("icrc"))
        .mut_subcommand("ledger", |command| {
            command.mut_subcommands(|command| {
                command
                    .mut_arg("source-endpoint", |arg| {
                        arg.help("IC API endpoint used for ICRC ledger queries")
                            .display_order(20)
                    })
                    .mut_arg("json", |arg| arg.display_order(22))
            })
        })
        .mut_subcommand("account", with_account_endpoint)
        .mut_subcommand("analytics", analytics::configure)
}

fn with_account_endpoint(command: ClapCommand) -> ClapCommand {
    if command.has_subcommands() {
        command.mut_subcommands(with_account_endpoint)
    } else {
        command
            .mut_arg("source-endpoint", |arg| {
                arg.help("IC API endpoint used for ICRC ledger queries")
                    .display_order(1000)
            })
            .mut_arg("json", |arg| arg.display_order(1001))
    }
}

fn subaccount_hex_value_parser() -> ValueParser {
    ValueParser::new(|value: &str| normalize_subaccount_hex(value).map_err(|err| err.to_string()))
}