use super::{
IcrcCommand,
account::{AccountCommand, TransactionCacheCommand, TransactionCommand},
analytics::{
AccountAnalyticsCommand, AnalyticsCommand, HolderAnalyticsCommand,
TransactionAnalyticsCommand,
},
ledger::LedgerCommand,
options::{
IcrcAccountOptions, IcrcAllowanceOptions, IcrcArchivesOptions, IcrcLedgerOptions,
IcrcTransactionListOptions, IcrcTransactionPageOptions, IcrcTransactionRefreshOptions,
IcrcTransactionsOptions, TokenValueOptions, TotalSupplyOptions,
},
};
use crate::cli::clap::parsed_options;
use crate::presentation::ic::icrc_account_info_report_text;
use crate::presentation::ic::icrc_account_list_report_text;
use crate::presentation::ic::icrc_holder_list_report_text;
use crate::presentation::ic::icrc_indexed_count_report_text;
use crate::presentation::ic::icrc_token_value_report_text;
use crate::presentation::ic::icrc_total_supply_report_text;
use crate::presentation::icrc::icrc_account_transaction_cache_status_report_text;
use crate::presentation::icrc::icrc_account_transaction_list_report_text;
use crate::presentation::icrc::icrc_account_transaction_page_report_text;
use crate::presentation::icrc::icrc_account_transaction_refresh_report_text;
use crate::presentation::icrc::icrc_allowance_report_text;
use crate::presentation::icrc::icrc_archives_report_text;
use crate::presentation::icrc::icrc_balance_report_text;
use crate::presentation::icrc::icrc_block_types_report_text;
use crate::presentation::icrc::icrc_capabilities_report_text;
use crate::presentation::icrc::icrc_index_report_text;
use crate::presentation::icrc::icrc_tip_certificate_report_text;
use crate::presentation::icrc::icrc_token_report_text;
use crate::presentation::icrc::icrc_transactions_report_text;
use crate::{
cli::common::{current_unix_secs, write_text_or_json},
icrc::IcrcCommandError,
progress::StderrQueryProgress,
storage::cache_root,
};
use clap::ArgMatches;
use ic_query::ic::IcIcrcIndexedCountKind;
use ic_query::ic::IcIcrcIndexedCountRequest;
use ic_query::ic::LiveIcSource;
use ic_query::ic::build_icrc_account_info_report_with_source;
use ic_query::ic::build_icrc_account_list_report_with_source;
use ic_query::ic::build_icrc_holder_list_report_with_source;
use ic_query::ic::build_icrc_indexed_count_report_with_source;
use ic_query::ic::build_icrc_token_value_report_with_source;
use ic_query::ic::build_icrc_total_supply_report_with_source;
use ic_query::icrc::IcrcBalanceRequest;
use ic_query::icrc::IcrcError;
use ic_query::icrc::IcrcLedgerRequest;
use ic_query::icrc::LiveIcrcSource;
use ic_query::icrc::build_icrc_account_transaction_cache_status_report;
use ic_query::icrc::build_icrc_account_transaction_list_report;
use ic_query::icrc::build_icrc_account_transaction_page_report_with_source;
use ic_query::icrc::build_icrc_allowance_report_with_source;
use ic_query::icrc::build_icrc_archives_report_with_source;
use ic_query::icrc::build_icrc_balance_report_with_source;
use ic_query::icrc::build_icrc_block_types_report_with_source;
use ic_query::icrc::build_icrc_capabilities_report_with_source;
use ic_query::icrc::build_icrc_index_report_with_source;
use ic_query::icrc::build_icrc_tip_certificate_report_with_source;
use ic_query::icrc::build_icrc_token_report_with_source;
use ic_query::icrc::build_icrc_transactions_report_with_source;
use ic_query::icrc::refresh_icrc_account_transaction_cache_with_source_and_progress;
use serde::Serialize;
pub async fn run_matches(matches: &ArgMatches) -> Result<(), IcrcCommandError> {
match parsed_options::<IcrcCommand>(matches) {
IcrcCommand::Ledger(command) => run_icrc_ledger(command).await,
IcrcCommand::Account(command) => run_icrc_account(command).await,
IcrcCommand::Analytics(command) => run_icrc_analytics(command).await,
}
}
async fn run_icrc_analytics(command: AnalyticsCommand) -> Result<(), IcrcCommandError> {
match command {
AnalyticsCommand::Account(options) => run_icrc_analytics_account(options).await,
AnalyticsCommand::Holder(options) => run_icrc_analytics_holder(options).await,
AnalyticsCommand::TokenValues(options) => run_icrc_analytics_token_values(options).await,
AnalyticsCommand::TotalSupply(options) => run_icrc_analytics_total_supply(options).await,
AnalyticsCommand::Transaction(TransactionAnalyticsCommand::Count(options)) => {
run_icrc_analytics_indexed_count_leaf(options, IcIcrcIndexedCountKind::Transaction)
.await
}
}
}
async fn run_icrc_analytics_account(
command: AccountAnalyticsCommand,
) -> Result<(), IcrcCommandError> {
match command {
AccountAnalyticsCommand::Count(options) => {
run_icrc_analytics_indexed_count_leaf(options, IcIcrcIndexedCountKind::Account).await
}
AccountAnalyticsCommand::Info(options) => {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_account_info_report_with_source(&request, &LiveIcSource::default())
.await?;
write_text_or_json(format, &report, icrc_account_info_report_text)
}
AccountAnalyticsCommand::List(options) => {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_account_list_report_with_source(&request, &LiveIcSource::default())
.await?;
write_text_or_json(format, &report, icrc_account_list_report_text)
}
}
}
async fn run_icrc_analytics_holder(
command: HolderAnalyticsCommand,
) -> Result<(), IcrcCommandError> {
match command {
HolderAnalyticsCommand::Count(options) => {
run_icrc_analytics_indexed_count_leaf(options, IcIcrcIndexedCountKind::Holder).await
}
HolderAnalyticsCommand::List(options) => {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_holder_list_report_with_source(&request, &LiveIcSource::default())
.await?;
write_text_or_json(format, &report, icrc_holder_list_report_text)
}
}
}
async fn run_icrc_analytics_token_values(
options: TokenValueOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_token_value_report_with_source(&request, &LiveIcSource::default()).await?;
write_text_or_json(format, &report, icrc_token_value_report_text)
}
async fn run_icrc_analytics_indexed_count_leaf(
options: IcrcLedgerOptions,
kind: IcIcrcIndexedCountKind,
) -> Result<(), IcrcCommandError> {
let (format, dashboard, ledger_canister_id) =
options.into_dashboard_request(current_unix_secs()?);
let request = IcIcrcIndexedCountRequest::new(dashboard, ledger_canister_id, kind);
let report =
build_icrc_indexed_count_report_with_source(&request, &LiveIcSource::default()).await?;
write_text_or_json(format, &report, icrc_indexed_count_report_text)
}
async fn run_icrc_analytics_total_supply(
options: TotalSupplyOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_total_supply_report_with_source(&request, &LiveIcSource::default()).await?;
write_text_or_json(format, &report, icrc_total_supply_report_text)
}
async fn run_icrc_ledger(command: LedgerCommand) -> Result<(), IcrcCommandError> {
match command {
LedgerCommand::Token(options) => {
run_simple_ledger_report(
options,
async |request, source| build_icrc_token_report_with_source(request, source).await,
icrc_token_report_text,
)
.await
}
LedgerCommand::Index(options) => {
run_simple_ledger_report(
options,
async |request, source| build_icrc_index_report_with_source(request, source).await,
icrc_index_report_text,
)
.await
}
LedgerCommand::Transactions(options) => run_icrc_transactions(options).await,
LedgerCommand::BlockTypes(options) => {
run_simple_ledger_report(
options,
async |request, source| {
build_icrc_block_types_report_with_source(request, source).await
},
icrc_block_types_report_text,
)
.await
}
LedgerCommand::Archives(options) => run_icrc_archives(options).await,
LedgerCommand::TipCertificate(options) => {
run_simple_ledger_report(
options,
async |request, source| {
build_icrc_tip_certificate_report_with_source(request, source).await
},
icrc_tip_certificate_report_text,
)
.await
}
LedgerCommand::Capabilities(options) => {
run_simple_ledger_report(
options,
async |request, source| {
build_icrc_capabilities_report_with_source(request, source).await
},
icrc_capabilities_report_text,
)
.await
}
}
}
async fn run_simple_ledger_report<Report>(
options: IcrcLedgerOptions,
build: impl AsyncFn(&IcrcLedgerRequest, &LiveIcrcSource) -> Result<Report, IcrcError>,
render_text: fn(&Report) -> String,
) -> Result<(), IcrcCommandError>
where
Report: Serialize,
{
let (format, request) = options.into_ledger_request(current_unix_secs()?);
let report = build(&request, &LiveIcrcSource::default()).await?;
write_text_or_json(format, &report, render_text)
}
async fn run_icrc_account(command: AccountCommand) -> Result<(), IcrcCommandError> {
match command {
AccountCommand::Balance(options) => run_icrc_balance(options).await,
AccountCommand::Allowance(options) => run_icrc_allowance(options).await,
AccountCommand::Transaction(options) => run_icrc_account_transaction(options).await,
}
}
async fn run_icrc_balance(options: IcrcAccountOptions) -> Result<(), IcrcCommandError> {
let (format, ledger) = options.ledger.into_ledger_request(current_unix_secs()?);
let request = IcrcBalanceRequest {
ledger,
account_owner: options.account_owner,
subaccount_hex: options.subaccount_hex,
};
let report =
build_icrc_balance_report_with_source(&request, &LiveIcrcSource::default()).await?;
write_text_or_json(format, &report, icrc_balance_report_text)
}
async fn run_icrc_allowance(options: IcrcAllowanceOptions) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_allowance_report_with_source(&request, &LiveIcrcSource::default()).await?;
write_text_or_json(format, &report, icrc_allowance_report_text)
}
async fn run_icrc_account_transaction(command: TransactionCommand) -> Result<(), IcrcCommandError> {
match command {
TransactionCommand::Page(options) => run_icrc_account_transaction_page(options).await,
TransactionCommand::List(options) => run_icrc_account_transaction_list(options),
TransactionCommand::Refresh(options) => run_icrc_account_transaction_refresh(options).await,
TransactionCommand::Cache(options) => run_icrc_account_transaction_cache(options),
}
}
async fn run_icrc_account_transaction_page(
options: IcrcTransactionPageOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report = build_icrc_account_transaction_page_report_with_source(
&request,
&LiveIcrcSource::default(),
)
.await?;
write_text_or_json(format, &report, icrc_account_transaction_page_report_text)
}
fn run_icrc_account_transaction_list(
options: IcrcTransactionListOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(account_transaction_cache_root()?);
let report = build_icrc_account_transaction_list_report(&request)?;
write_text_or_json(format, &report, icrc_account_transaction_list_report_text)
}
async fn run_icrc_account_transaction_refresh(
options: IcrcTransactionRefreshOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) =
options.into_request(account_transaction_cache_root()?, current_unix_secs()?);
let mut progress = StderrQueryProgress::new();
let report = refresh_icrc_account_transaction_cache_with_source_and_progress(
&request,
&LiveIcrcSource::default(),
&mut progress,
)
.await?;
write_text_or_json(
format,
&report,
icrc_account_transaction_refresh_report_text,
)
}
fn run_icrc_account_transaction_cache(
command: TransactionCacheCommand,
) -> Result<(), IcrcCommandError> {
match command {
TransactionCacheCommand::Status(options) => {
run_icrc_account_transaction_cache_status(options)
}
}
}
fn run_icrc_account_transaction_cache_status(
options: IcrcAccountOptions,
) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_cache_request(account_transaction_cache_root()?);
let report = build_icrc_account_transaction_cache_status_report(&request)?;
write_text_or_json(
format,
&report,
icrc_account_transaction_cache_status_report_text,
)
}
fn account_transaction_cache_root() -> Result<std::path::PathBuf, IcrcCommandError> {
cache_root().map_err(|error| IcrcCommandError::Usage(error.to_string()))
}
async fn run_icrc_transactions(options: IcrcTransactionsOptions) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_transactions_report_with_source(&request, &LiveIcrcSource::default()).await?;
write_text_or_json(format, &report, icrc_transactions_report_text)
}
async fn run_icrc_archives(options: IcrcArchivesOptions) -> Result<(), IcrcCommandError> {
let (format, request) = options.into_request(current_unix_secs()?);
let report =
build_icrc_archives_report_with_source(&request, &LiveIcrcSource::default()).await?;
write_text_or_json(format, &report, icrc_archives_report_text)
}