ic-query-cli 0.100.1

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Typed bounded official ICRC analytics operations and endpoint policy.

use super::options::{
    AccountListOptions, AnalyticsAccountInfoOptions, HolderListOptions, IcrcLedgerOptions,
    TokenValueOptions, TotalSupplyOptions,
};
use crate::cli::common::{COLLECTION_MODE_LIVE, collection_help};
use clap::{Command, Subcommand};
use ic_query::ic::{
    DEFAULT_ICRC_ACCOUNT_INFO_SOURCE_ENDPOINT, DEFAULT_ICRC_ANALYTICS_SOURCE_ENDPOINT,
};

///
/// AnalyticsCommand
///
/// Parsed official analytics operation and its arguments.
///

#[derive(Subcommand)]
#[command(about = "Inspect bounded official ICRC analytics", long_about = None)]
pub(super) enum AnalyticsCommand {
    #[command(subcommand)]
    Account(AccountAnalyticsCommand),
    #[command(subcommand)]
    Holder(HolderAnalyticsCommand),
    #[command(about = "Show bounded external token value observations for one ICRC ledger", long_about = "Show a bounded token price and 24-hour volume series aggregated by the official IC Dashboard ICRC analytics API. The command makes exactly one live request, performs no pagination or follow-up calls, and does not use a cache. Rows retain their external provider name and URL; values are off-chain and non-certified.", after_help = collection_help(COLLECTION_MODE_LIVE, "Examples:\n  icq icrc analytics token-values mxzaz-hqaaa-aaaar-qaada-cai\n  icq icrc analytics token-values mxzaz-hqaaa-aaaar-qaada-cai --start 1785542400 --end 1785628800 --limit 100 --json"))]
    TokenValues(TokenValueOptions),
    #[command(about = "Show bounded historical total supply for one indexed ICRC ledger", long_about = "Show bounded historical total supply for one ledger indexed by the official IC Dashboard ICRC analytics API. The command makes exactly one live request, performs no enumeration or follow-up calls, and does not use a cache. Values are off-chain, non-certified analytics in raw ledger base units.", after_help = collection_help(COLLECTION_MODE_LIVE, "Examples:\n  icq icrc analytics total-supply mxzaz-hqaaa-aaaar-qaada-cai\n  icq icrc analytics total-supply mxzaz-hqaaa-aaaar-qaada-cai --start 1785542400 --end 1785801600 --step 86400 --json"))]
    TotalSupply(TotalSupplyOptions),
    #[command(subcommand)]
    Transaction(TransactionAnalyticsCommand),
}

pub(super) fn configure(command: Command) -> Command {
    command.mut_subcommands(|command| {
        if command.has_subcommands() {
            let entity = command.get_name().to_string();
            command.mut_subcommands(|command| {
                let command = if command.get_name() == "count" {
                    count_help(command, &entity)
                } else {
                    command
                };
                with_endpoint(command)
            })
        } else {
            with_endpoint(command)
        }
    })
}

fn with_endpoint(command: Command) -> Command {
    let endpoint = if command.get_name() == "info" {
        DEFAULT_ICRC_ACCOUNT_INFO_SOURCE_ENDPOINT
    } else {
        DEFAULT_ICRC_ANALYTICS_SOURCE_ENDPOINT
    };
    command
        .mut_arg("source-endpoint", |arg| {
            arg.default_value(endpoint)
                .help("Official IC Dashboard ICRC analytics API base endpoint")
                .display_order(20)
        })
        .mut_arg("ledger-canister-id", |arg| arg.index(1))
        .mut_arg("json", |arg| arg.display_order(21))
}

fn count_help(command: Command, entity: &str) -> Command {
    let plural = format!("{entity}s");
    command
        .about(format!("Show the current indexed {entity} count for one ICRC ledger"))
        .long_about(format!("Show the current {entity} count for one ledger indexed by the official IC Dashboard ICRC analytics API. The command makes exactly one live request, requests no {plural} rows, performs no enumeration or follow-up calls, and does not use a cache. The count is off-chain and non-certified."))
        .after_help(collection_help(COLLECTION_MODE_LIVE, &format!("Examples:\n  icq icrc analytics {entity} count mxzaz-hqaaa-aaaar-qaada-cai\n  icq icrc analytics {entity} count mxzaz-hqaaa-aaaar-qaada-cai --json")))
}

///
/// AccountAnalyticsCommand
///
/// Parsed account analytics operation.
///

#[derive(Subcommand)]
#[command(about = "Inspect indexed account analytics for one ICRC ledger", long_about = None)]
pub(super) enum AccountAnalyticsCommand {
    Count(IcrcLedgerOptions),
    #[command(about = "Show one exact off-chain account record; not point-in-time guaranteed", long_about = "Show one exact account record maintained by the official IC Dashboard ICRC analytics API. The account id is the opaque stable id returned by account list. The command makes exactly one live request, performs no native ledger follow-up, and does not use a cache. Values are off-chain, non-certified, and not point-in-time guaranteed.", after_help = collection_help(COLLECTION_MODE_LIVE, "Examples:\n  icq icrc analytics account info mxzaz-hqaaa-aaaar-qaada-cai 222nw-nqiei-h4uy6-fqm54-d3slu-jveav-vqrn6-yojxi-4eug3-2ejie-vae\n  icq icrc analytics account info mxzaz-hqaaa-aaaar-qaada-cai 222nw-nqiei-h4uy6-fqm54-d3slu-jveav-vqrn6-yojxi-4eug3-2ejie-vae --json"))]
    Info(AnalyticsAccountInfoOptions),
    #[command(about = "Show one bounded off-chain account page; not a complete snapshot", long_about = "Show one explicitly bounded account page maintained by the official IC Dashboard ICRC analytics API. The command makes exactly one live request, never follows cursors automatically, and does not use a cache. Returned cursors can be supplied unchanged to --after or --before. Rows are off-chain, non-certified, and not a complete or point-in-time ledger snapshot.", after_help = collection_help(COLLECTION_MODE_LIVE, "Examples:\n  icq icrc analytics account list mxzaz-hqaaa-aaaar-qaada-cai --limit 25\n  icq icrc analytics account list mxzaz-hqaaa-aaaar-qaada-cai --sort-by=-balance --json"))]
    List(AccountListOptions),
}

///
/// HolderAnalyticsCommand
///
/// Parsed holder analytics operation.
///

#[derive(Subcommand)]
#[command(about = "Inspect indexed holder analytics for one ICRC ledger", long_about = None)]
pub(super) enum HolderAnalyticsCommand {
    Count(IcrcLedgerOptions),
    #[command(about = "Show one bounded off-chain holder page; not a complete snapshot", long_about = "Show one explicitly bounded principal-level holder page maintained by the official IC Dashboard ICRC analytics API. The command makes exactly one live request, never follows cursors automatically, and does not use a cache. Returned cursors can be supplied unchanged to --after or --before. Rows are off-chain, non-certified aggregates and not a complete or point-in-time ledger snapshot.", after_help = collection_help(COLLECTION_MODE_LIVE, "Examples:\n  icq icrc analytics holder list mxzaz-hqaaa-aaaar-qaada-cai --limit 25\n  icq icrc analytics holder list mxzaz-hqaaa-aaaar-qaada-cai --sort-by=-balance --json"))]
    List(HolderListOptions),
}

///
/// TransactionAnalyticsCommand
///
/// Parsed transaction analytics operation.
///

#[derive(Subcommand)]
#[command(about = "Inspect indexed transaction analytics for one ICRC ledger", long_about = None)]
pub(super) enum TransactionAnalyticsCommand {
    Count(IcrcLedgerOptions),
}