ic-query-cli 0.104.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Clap schemas and request projection for bounded Dashboard analytics.
use super::IcrcLedgerOptions;
use crate::cli::common::{OutputFormat, resolve_time_bounds};
use clap::{
    Args,
    builder::{PossibleValuesParser, RangedU64ValueParser, TypedValueParser},
};
use ic_query::ic::{
    DEFAULT_ICRC_TOKEN_VALUE_WINDOW_SECS, DEFAULT_ICRC_TOTAL_SUPPLY_STEP_SECS,
    DEFAULT_ICRC_TOTAL_SUPPLY_WINDOW_SECS, IcIcrcAccountInfoRequest, IcIcrcAccountListQuery,
    IcIcrcAccountListRequest, IcIcrcAccountSort, IcIcrcHolderListQuery, IcIcrcHolderListRequest,
    IcIcrcHolderSort, IcIcrcTokenValueQuery, IcIcrcTokenValueRequest, IcIcrcTotalSupplyQuery,
    IcIcrcTotalSupplyRequest, MAX_ICRC_INDEX_PAGE_ROWS, MAX_ICRC_TOKEN_VALUE_ROWS,
    MIN_ICRC_ANALYTICS_TIMESTAMP,
};

///
/// IndexPageOptions
///
/// Shared opaque cursors and bounded row count for one index page.
///

#[derive(Args)]
#[group(skip)]
struct IndexPageOptions {
    #[arg(
        long,
        value_name = "cursor",
        conflicts_with = "before",
        help = "Opaque exclusive forward cursor returned by an earlier page"
    )]
    after: Option<String>,
    #[arg(
        long,
        value_name = "cursor",
        help = "Opaque exclusive backward cursor returned by an earlier page"
    )]
    before: Option<String>,
    #[arg(long, value_name = "rows", default_value = "20", value_parser = RangedU64ValueParser::<u16>::new().range(1..=u64::from(MAX_ICRC_INDEX_PAGE_ROWS)), help = "Maximum rows requested from this one page")]
    limit: u16,
}

///
/// AccountListOptions
///
/// Clap-owned account index page, ordering and owner selection.
///

#[derive(Args)]
#[group(skip)]
pub(in crate::icrc) struct AccountListOptions {
    #[command(flatten)]
    page: IndexPageOptions,
    #[arg(long, value_name = "field", default_value = "id", allow_hyphen_values = true, value_parser = crate::cli::clap::choice_parser(IcIcrcAccountSort::ALL, IcIcrcAccountSort::as_api_value), help = "Stable upstream page sort; a leading '-' selects descending order")]
    sort_by: IcIcrcAccountSort,
    #[arg(long, value_name = "principal", value_parser = super::principal_text_value_parser(), help = "Restrict accounts to one canonical owner principal")]
    owner: Option<String>,
    #[command(flatten)]
    ledger: IcrcLedgerOptions,
}

///
/// HolderListOptions
///
/// Clap-owned principal-level holder page and ordering.
///

#[derive(Args)]
#[group(skip)]
pub(in crate::icrc) struct HolderListOptions {
    #[command(flatten)]
    page: IndexPageOptions,
    #[arg(long, value_name = "field", default_value = "principal", allow_hyphen_values = true, value_parser = crate::cli::clap::choice_parser(IcIcrcHolderSort::ALL, IcIcrcHolderSort::as_api_value), help = "Stable upstream page sort; a leading '-' selects descending order")]
    sort_by: IcIcrcHolderSort,
    #[command(flatten)]
    ledger: IcrcLedgerOptions,
}

///
/// TokenValueOptions
///
/// Clap-owned token-value bounds; omitted times resolve against the caller clock.
///

#[derive(Args)]
#[group(skip)]
pub(in crate::icrc) struct TokenValueOptions {
    #[arg(
        long,
        value_name = "unix-seconds",
        help = "Series start; defaults to 24 hours before --end"
    )]
    start: Option<u64>,
    #[arg(
        long,
        value_name = "unix-seconds",
        help = "Series end; defaults to the current time"
    )]
    end: Option<u64>,
    #[arg(long, value_name = "rows", default_value = "1000", value_parser = RangedU64ValueParser::<u16>::new().range(1..=u64::from(MAX_ICRC_TOKEN_VALUE_ROWS)), help = "Maximum rows requested; reaching it reports possible truncation")]
    limit: u16,
    #[command(flatten)]
    ledger: IcrcLedgerOptions,
}

///
/// TotalSupplyOptions
///
/// Clap-owned total-supply bounds and upstream-supported sampling intervals.
///

#[derive(Args)]
#[group(skip)]
pub(in crate::icrc) struct TotalSupplyOptions {
    #[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_ICRC_ANALYTICS_TIMESTAMP..), help = "Inclusive series start; defaults to 30 days before --end")]
    start: Option<u64>,
    #[arg(long, value_name = "unix-seconds", value_parser = RangedU64ValueParser::<u64>::new().range(MIN_ICRC_ANALYTICS_TIMESTAMP..), help = "Inclusive series end; defaults to the current time")]
    end: Option<u64>,
    #[arg(long, value_name = "seconds", default_value_t = DEFAULT_ICRC_TOTAL_SUPPLY_STEP_SECS, hide_default_value = true, value_parser = PossibleValuesParser::new(["3600", "86400"]).try_map(|value| value.parse::<u32>()), help = "Observation interval accepted by the official API; defaults to 86400")]
    step: u32,
    #[command(flatten)]
    ledger: IcrcLedgerOptions,
}

///
/// AnalyticsAccountInfoOptions
///
/// Ledger and opaque account identity for one exact analytics record.
///

#[derive(Args)]
#[group(skip)]
pub(in crate::icrc) struct AnalyticsAccountInfoOptions {
    #[command(flatten)]
    ledger: IcrcLedgerOptions,
    #[arg(
        id = "account-id",
        value_name = "account-id",
        index = 2,
        help = "Opaque account id returned by account list"
    )]
    account_id: String,
}

impl AccountListOptions {
    pub(in crate::icrc) fn into_request(
        self,
        now: u64,
    ) -> (OutputFormat, IcIcrcAccountListRequest) {
        let (format, dashboard, ledger) = self.ledger.into_dashboard_request(now);
        let mut query = IcIcrcAccountListQuery::new(self.page.limit, self.sort_by);
        query.owner = self.owner;
        query.after = self.page.after;
        query.before = self.page.before;
        (
            format,
            IcIcrcAccountListRequest::new(dashboard, ledger, query),
        )
    }
}

impl AnalyticsAccountInfoOptions {
    pub(in crate::icrc) fn into_request(
        self,
        now: u64,
    ) -> (OutputFormat, IcIcrcAccountInfoRequest) {
        let (format, dashboard, ledger) = self.ledger.into_dashboard_request(now);
        (
            format,
            IcIcrcAccountInfoRequest::new(dashboard, ledger, self.account_id),
        )
    }
}

impl HolderListOptions {
    pub(in crate::icrc) fn into_request(self, now: u64) -> (OutputFormat, IcIcrcHolderListRequest) {
        let (format, dashboard, ledger) = self.ledger.into_dashboard_request(now);
        let mut query = IcIcrcHolderListQuery::new(self.page.limit, self.sort_by);
        query.after = self.page.after;
        query.before = self.page.before;
        (
            format,
            IcIcrcHolderListRequest::new(dashboard, ledger, query),
        )
    }
}

impl TokenValueOptions {
    pub(in crate::icrc) fn into_request(self, now: u64) -> (OutputFormat, IcIcrcTokenValueRequest) {
        let (format, dashboard, ledger) = self.ledger.into_dashboard_request(now);
        let (start, end) = resolve_time_bounds(
            self.start,
            self.end,
            now,
            DEFAULT_ICRC_TOKEN_VALUE_WINDOW_SECS,
            0,
        );
        (
            format,
            IcIcrcTokenValueRequest::new(
                dashboard,
                ledger,
                IcIcrcTokenValueQuery::new(start, end, self.limit),
            ),
        )
    }
}

impl TotalSupplyOptions {
    pub(in crate::icrc) fn into_request(
        self,
        now: u64,
    ) -> (OutputFormat, IcIcrcTotalSupplyRequest) {
        let (format, dashboard, ledger) = self.ledger.into_dashboard_request(now);
        let (start, end) = resolve_time_bounds(
            self.start,
            self.end,
            now,
            DEFAULT_ICRC_TOTAL_SUPPLY_WINDOW_SECS,
            MIN_ICRC_ANALYTICS_TIMESTAMP,
        );
        (
            format,
            IcIcrcTotalSupplyRequest::new(
                dashboard,
                ledger,
                IcIcrcTotalSupplyQuery::new(start, end, self.step),
            ),
        )
    }
}