use super::sort::{IcrcAccountSortArg, IcrcAccountTransactionSortArg, IcrcHolderSortArg};
use super::{
ACCOUNT_ID_ARG, AFTER_ARG, BEFORE_ARG, END_ARG, FOLLOW_ARCHIVES_ARG, FROM_CANISTER_ID_ARG,
INDEX_CANISTER_ID_ARG, LEDGER_CANISTER_ID_ARG, LIMIT_ARG, MAX_PAGES_ARG, OWNER_ARG,
OWNER_PRINCIPAL_ARG, OWNER_SUBACCOUNT_ARG, PAGE_SIZE_ARG, PRINCIPAL_ARG, SORT_ARG, SORT_BY_ARG,
SPENDER_PRINCIPAL_ARG, SPENDER_SUBACCOUNT_ARG, START_ARG, STEP_ARG, SUBACCOUNT_ARG,
};
use crate::cli::{
clap::{required_string, required_typed, string_option, typed_option},
common::{OutputFormat, SourceOutputOptions},
};
use clap::ArgMatches;
use ic_query::{
ic::{IcIcrcAccountSort, IcIcrcHolderSort},
icrc::IcrcAccountTransactionSort,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsAccountListOptions {
pub(in crate::icrc) target: IcrcLedgerOptions,
pub(in crate::icrc) owner: Option<String>,
pub(in crate::icrc) after: Option<String>,
pub(in crate::icrc) before: Option<String>,
pub(in crate::icrc) limit: u16,
pub(in crate::icrc) sort_by: IcIcrcAccountSort,
}
impl IcrcAnalyticsAccountListOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcLedgerOptions::from_matches(matches),
owner: string_option(matches, OWNER_ARG),
after: string_option(matches, AFTER_ARG),
before: string_option(matches, BEFORE_ARG),
limit: required_typed(matches, LIMIT_ARG),
sort_by: required_typed::<IcrcAccountSortArg>(matches, SORT_BY_ARG).into(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsAccountInfoOptions {
pub(in crate::icrc) target: IcrcLedgerOptions,
pub(in crate::icrc) account_id: String,
}
impl IcrcAnalyticsAccountInfoOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcLedgerOptions::from_matches(matches),
account_id: required_string(matches, ACCOUNT_ID_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsHolderListOptions {
pub(in crate::icrc) target: IcrcLedgerOptions,
pub(in crate::icrc) after: Option<String>,
pub(in crate::icrc) before: Option<String>,
pub(in crate::icrc) limit: u16,
pub(in crate::icrc) sort_by: IcIcrcHolderSort,
}
impl IcrcAnalyticsHolderListOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcLedgerOptions::from_matches(matches),
after: string_option(matches, AFTER_ARG),
before: string_option(matches, BEFORE_ARG),
limit: required_typed(matches, LIMIT_ARG),
sort_by: required_typed::<IcrcHolderSortArg>(matches, SORT_BY_ARG).into(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsWindowOptions {
pub(in crate::icrc) target: IcrcLedgerOptions,
pub(in crate::icrc) start_unix_secs: Option<u64>,
pub(in crate::icrc) end_unix_secs: Option<u64>,
}
impl IcrcAnalyticsWindowOptions {
fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcLedgerOptions::from_matches(matches),
start_unix_secs: typed_option(matches, START_ARG),
end_unix_secs: typed_option(matches, END_ARG),
}
}
pub(in crate::icrc) fn resolve_bounds(
&self,
now_unix_secs: u64,
default_window_secs: u64,
minimum_default_start: u64,
) -> (u64, u64) {
let end = self.end_unix_secs.unwrap_or(now_unix_secs);
let start = self.start_unix_secs.unwrap_or_else(|| {
end.saturating_sub(default_window_secs)
.max(minimum_default_start)
});
(start, end)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsTotalSupplyOptions {
pub(in crate::icrc) window: IcrcAnalyticsWindowOptions,
pub(in crate::icrc) step_secs: u32,
}
impl IcrcAnalyticsTotalSupplyOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
let step_secs = required_typed(matches, STEP_ARG);
Self {
window: IcrcAnalyticsWindowOptions::from_matches(matches),
step_secs,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAnalyticsTokenValueOptions {
pub(in crate::icrc) window: IcrcAnalyticsWindowOptions,
pub(in crate::icrc) limit: u16,
}
impl IcrcAnalyticsTokenValueOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
window: IcrcAnalyticsWindowOptions::from_matches(matches),
limit: required_typed(matches, LIMIT_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcLedgerOptions {
pub(in crate::icrc) ledger_canister_id: String,
pub(in crate::icrc) format: OutputFormat,
pub(in crate::icrc) source_endpoint: String,
}
impl IcrcLedgerOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
let ledger_canister_id = required_string(matches, LEDGER_CANISTER_ID_ARG);
let source = SourceOutputOptions::from_matches(matches);
Self {
ledger_canister_id,
format: source.format,
source_endpoint: source.source_endpoint,
}
}
pub(in crate::icrc) fn into_dashboard_request(
self,
now_unix_secs: u64,
) -> (OutputFormat, ic_query::ic::IcDashboardRequest, String) {
let (format, request) = SourceOutputOptions {
format: self.format,
source_endpoint: self.source_endpoint,
}
.into_dashboard_request(now_unix_secs);
(format, request, self.ledger_canister_id)
}
pub(in crate::icrc) fn into_ledger_request(
self,
now_unix_secs: u64,
) -> (OutputFormat, ic_query::icrc::IcrcLedgerRequest) {
(
self.format,
ic_query::icrc::IcrcLedgerRequest::new(
self.source_endpoint,
now_unix_secs,
self.ledger_canister_id,
),
)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAccountOptions {
pub(in crate::icrc) ledger: IcrcLedgerOptions,
pub(in crate::icrc) account_owner: String,
pub(in crate::icrc) subaccount_hex: Option<String>,
}
impl IcrcAccountOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
ledger: IcrcLedgerOptions::from_matches(matches),
account_owner: required_string(matches, PRINCIPAL_ARG),
subaccount_hex: string_option(matches, SUBACCOUNT_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAllowanceOptions {
pub(in crate::icrc) ledger: IcrcLedgerOptions,
pub(in crate::icrc) account_owner: String,
pub(in crate::icrc) account_subaccount_hex: Option<String>,
pub(in crate::icrc) spender_owner: String,
pub(in crate::icrc) spender_subaccount_hex: Option<String>,
}
impl IcrcAllowanceOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
ledger: IcrcLedgerOptions::from_matches(matches),
account_owner: required_string(matches, OWNER_PRINCIPAL_ARG),
account_subaccount_hex: string_option(matches, OWNER_SUBACCOUNT_ARG),
spender_owner: required_string(matches, SPENDER_PRINCIPAL_ARG),
spender_subaccount_hex: string_option(matches, SPENDER_SUBACCOUNT_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAccountTransactionPageOptions {
pub(in crate::icrc) target: IcrcAccountOptions,
pub(in crate::icrc) index_canister_id: Option<String>,
pub(in crate::icrc) start: Option<String>,
pub(in crate::icrc) limit: u32,
}
impl IcrcAccountTransactionPageOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcAccountOptions::from_matches(matches),
index_canister_id: string_option(matches, INDEX_CANISTER_ID_ARG),
start: string_option(matches, START_ARG),
limit: required_typed(matches, LIMIT_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAccountTransactionListOptions {
pub(in crate::icrc) target: IcrcAccountOptions,
pub(in crate::icrc) limit: u32,
pub(in crate::icrc) sort: IcrcAccountTransactionSort,
}
impl IcrcAccountTransactionListOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcAccountOptions::from_matches(matches),
limit: required_typed(matches, LIMIT_ARG),
sort: required_typed::<IcrcAccountTransactionSortArg>(matches, SORT_ARG).into(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcAccountTransactionRefreshOptions {
pub(in crate::icrc) target: IcrcAccountOptions,
pub(in crate::icrc) index_canister_id: Option<String>,
pub(in crate::icrc) page_size: u32,
pub(in crate::icrc) max_pages: Option<u32>,
}
impl IcrcAccountTransactionRefreshOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
target: IcrcAccountOptions::from_matches(matches),
index_canister_id: string_option(matches, INDEX_CANISTER_ID_ARG),
page_size: required_typed(matches, PAGE_SIZE_ARG),
max_pages: typed_option(matches, MAX_PAGES_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcTransactionsOptions {
pub(in crate::icrc) ledger: IcrcLedgerOptions,
pub(in crate::icrc) start: u64,
pub(in crate::icrc) limit: u32,
pub(in crate::icrc) follow_archives: bool,
}
impl IcrcTransactionsOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
ledger: IcrcLedgerOptions::from_matches(matches),
start: required_typed(matches, START_ARG),
limit: required_typed(matches, LIMIT_ARG),
follow_archives: matches.get_flag(FOLLOW_ARCHIVES_ARG),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::icrc) struct IcrcArchivesOptions {
pub(in crate::icrc) ledger: IcrcLedgerOptions,
pub(in crate::icrc) from_canister_id: Option<String>,
}
impl IcrcArchivesOptions {
pub(in crate::icrc) fn from_matches(matches: &ArgMatches) -> Self {
Self {
ledger: IcrcLedgerOptions::from_matches(matches),
from_canister_id: string_option(matches, FROM_CANISTER_ID_ARG),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn target() -> IcrcLedgerOptions {
IcrcLedgerOptions {
ledger_canister_id: "aaaaa-aa".to_string(),
source_endpoint: "https://example.com/api".to_string(),
format: OutputFormat::Json,
}
}
#[test]
fn native_ledger_request_retains_explicit_target_time_and_output() {
let (format, request) = target().into_ledger_request(123);
assert_eq!(format, OutputFormat::Json);
assert_eq!(request.source_endpoint, "https://example.com/api");
assert_eq!(request.ledger_canister_id, "aaaaa-aa");
assert_eq!(request.now_unix_secs, 123);
}
#[test]
fn analytics_defaults_clamp_only_the_implicit_start() {
let mut window = IcrcAnalyticsWindowOptions {
target: target(),
start_unix_secs: None,
end_unix_secs: None,
};
assert_eq!(window.resolve_bounds(100, 30, 0), (70, 100));
assert_eq!(window.resolve_bounds(10, 30, 0), (0, 10));
assert_eq!(window.resolve_bounds(100, 30, 80), (80, 100));
window.end_unix_secs = Some(90);
assert_eq!(window.resolve_bounds(100, 30, 0), (60, 90));
window.start_unix_secs = Some(5);
assert_eq!(window.resolve_bounds(100, 30, 80), (5, 90));
window.start_unix_secs = Some(110);
assert_eq!(window.resolve_bounds(100, 30, 80), (110, 90));
}
}