ic-query-cli 0.104.1

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: icrc::history
//!
//! Responsibility: render ledger-wide ICRC transaction and archive reports as text.
//! Does not own: account history, token metadata, live reads, or JSON output.
//! Boundary: formats bounded ledger blocks and archive evidence for humans.

use crate::presentation::text_value::table;
use ic_host_text::table::Alignment;

use crate::presentation::icrc::LEFT_2_ALIGNMENTS;
use crate::presentation::icrc::push_table_section;
use crate::presentation::text_value::optional_text;
use crate::presentation::text_value::truncate_text;
use ic_query::icrc::IcrcArchiveFollowErrorRow;
use ic_query::icrc::IcrcArchivedBlocksRow;
use ic_query::icrc::IcrcArchivedRangeRow;
use ic_query::icrc::IcrcArchivesReport;
use ic_query::icrc::IcrcBlockTypesReport;
use ic_query::icrc::IcrcFollowedArchiveBlockRow;
use ic_query::icrc::IcrcTransactionBlockRow;
use ic_query::icrc::IcrcTransactionsReport;

const ICRC_DETAIL_TEXT_LIMIT: usize = 160;
const ICRC3_BLOCK_TABLE_HEADERS: [&str; 5] =
    ["INDEX", "TYPE", "KIND", "TIMESTAMP_NS", "AMOUNT_BASE_UNITS"];
const ICRC3_BLOCK_TABLE_ALIGNMENTS: [Alignment; 5] = [
    Alignment::Right,
    Alignment::Left,
    Alignment::Left,
    Alignment::Right,
    Alignment::Right,
];
const ARCHIVE_RANGE_TABLE_ALIGNMENTS: [Alignment; 4] = [
    Alignment::Left,
    Alignment::Left,
    Alignment::Right,
    Alignment::Right,
];

#[must_use]
/// Render the ICRC transactions report as human-facing text.
pub fn icrc_transactions_report_text(report: &IcrcTransactionsReport) -> String {
    let mut lines = Vec::from(crate::presentation::text_value::report_fields!(report;
        ledger_canister_id = report.provenance.ledger_canister_id,
        requested_start,
        requested_limit,
        follow_archives,
        log_length = optional_text(report.transactions.log_length.as_ref()),
        returned_blocks = report.transactions.blocks.len(),
        archived_callbacks = report.transactions.archived_blocks.len(),
        followed_archive_blocks = report.transactions.followed_archive_blocks.len(),
        archive_follow_errors = report.transactions.archive_follow_errors.len(),
    ));
    lines.extend(crate::presentation::icrc::acquisition_lines(
        &report.provenance,
    ));
    push_table_section(
        &mut lines,
        &report.transactions.blocks,
        render_transaction_blocks_table,
    );
    push_table_section(
        &mut lines,
        &report.transactions.followed_archive_blocks,
        render_followed_archive_blocks_table,
    );
    push_table_section(
        &mut lines,
        &report.transactions.archived_blocks,
        render_archive_callbacks_table,
    );
    push_table_section(
        &mut lines,
        &report.transactions.archive_follow_errors,
        render_archive_follow_errors_table,
    );
    lines.join("\n")
}

fn render_transaction_blocks_table(blocks: &[IcrcTransactionBlockRow]) -> String {
    table(
        &ICRC3_BLOCK_TABLE_HEADERS,
        &blocks
            .iter()
            .map(transaction_block_cells)
            .collect::<Vec<_>>(),
        &ICRC3_BLOCK_TABLE_ALIGNMENTS,
    )
}

fn render_followed_archive_blocks_table(blocks: &[IcrcFollowedArchiveBlockRow]) -> String {
    table(
        &[
            "ARCHIVE_CANISTER",
            "METHOD",
            "INDEX",
            "TYPE",
            "KIND",
            "TIMESTAMP_NS",
            "AMOUNT_BASE_UNITS",
        ],
        &blocks
            .iter()
            .map(|block| {
                let [index, block_type, kind, timestamp, amount] =
                    followed_archive_block_cells(block);
                [
                    block.archive_canister_id.clone(),
                    block.callback_method.clone(),
                    index,
                    block_type,
                    kind,
                    timestamp,
                    amount,
                ]
            })
            .collect::<Vec<_>>(),
        &[
            Alignment::Left,
            Alignment::Left,
            Alignment::Right,
            Alignment::Left,
            Alignment::Left,
            Alignment::Right,
            Alignment::Right,
        ],
    )
}

fn render_archive_callbacks_table(archives: &[IcrcArchivedBlocksRow]) -> String {
    table(
        &["ARCHIVE_CANISTER", "METHOD", "START", "LENGTH"],
        &archives
            .iter()
            .flat_map(|archive| {
                archive.ranges.iter().map(|range| {
                    archive_range_cells(
                        &archive.callback_canister_id,
                        &archive.callback_method,
                        range,
                    )
                })
            })
            .collect::<Vec<_>>(),
        &ARCHIVE_RANGE_TABLE_ALIGNMENTS,
    )
}

fn render_archive_follow_errors_table(errors: &[IcrcArchiveFollowErrorRow]) -> String {
    table(
        &["ARCHIVE_CANISTER", "METHOD", "START", "LENGTH", "ERROR"],
        &errors
            .iter()
            .flat_map(|error| {
                error.ranges.iter().map(|range| {
                    let [canister_id, method, start, length] = archive_range_cells(
                        &error.callback_canister_id,
                        &error.callback_method,
                        range,
                    );
                    [
                        canister_id,
                        method,
                        start,
                        length,
                        truncate_text(&error.error, ICRC_DETAIL_TEXT_LIMIT),
                    ]
                })
            })
            .collect::<Vec<_>>(),
        &[
            Alignment::Left,
            Alignment::Left,
            Alignment::Right,
            Alignment::Right,
            Alignment::Left,
        ],
    )
}

#[must_use]
/// Render the ICRC block types report as human-facing text.
pub fn icrc_block_types_report_text(report: &IcrcBlockTypesReport) -> String {
    let mut lines = Vec::from(crate::presentation::text_value::report_fields!(report;
        ledger_canister_id = report.provenance.ledger_canister_id,
        block_type_count = report.block_types.len(),
    ));
    lines.extend(crate::presentation::icrc::acquisition_lines(
        &report.provenance,
    ));
    push_table_section(&mut lines, &report.block_types, |rows| {
        table(
            &["BLOCK_TYPE", "URL"],
            &rows
                .iter()
                .map(|block_type| [block_type.block_type.clone(), block_type.url.clone()])
                .collect::<Vec<_>>(),
            &LEFT_2_ALIGNMENTS,
        )
    });
    lines.join("\n")
}

#[must_use]
/// Render the ICRC archives report as human-facing text.
pub fn icrc_archives_report_text(report: &IcrcArchivesReport) -> String {
    let mut lines = Vec::from(crate::presentation::text_value::report_fields!(report;
        ledger_canister_id = report.provenance.ledger_canister_id,
        from_canister_id = optional_text(report.from_canister_id.as_ref()),
        archive_count = report.archives.len(),
    ));
    lines.extend(crate::presentation::icrc::acquisition_lines(
        &report.provenance,
    ));
    push_table_section(&mut lines, &report.archives, |rows| {
        table(
            &["ARCHIVE_CANISTER", "START", "END"],
            &rows
                .iter()
                .map(|archive| {
                    [
                        archive.canister_id.clone(),
                        archive.start.clone(),
                        archive.end.clone(),
                    ]
                })
                .collect::<Vec<_>>(),
            &[Alignment::Left, Alignment::Right, Alignment::Right],
        )
    });
    lines.join("\n")
}

fn transaction_block_cells(block: &IcrcTransactionBlockRow) -> [String; 5] {
    block_summary_cells(
        &block.index,
        block.block_type.as_ref(),
        block.transaction_kind.as_ref(),
        block.timestamp_unix_nanos.as_ref(),
        block.amount_base_units.as_ref(),
    )
}

fn followed_archive_block_cells(block: &IcrcFollowedArchiveBlockRow) -> [String; 5] {
    block_summary_cells(
        &block.block.index,
        block.block.block_type.as_ref(),
        block.block.transaction_kind.as_ref(),
        block.block.timestamp_unix_nanos.as_ref(),
        block.block.amount_base_units.as_ref(),
    )
}

fn block_summary_cells(
    index: &str,
    block_type: Option<&String>,
    transaction_kind: Option<&String>,
    timestamp_unix_nanos: Option<&String>,
    amount_base_units: Option<&String>,
) -> [String; 5] {
    [
        index.to_string(),
        optional_text(block_type),
        optional_text(transaction_kind),
        optional_text(timestamp_unix_nanos),
        optional_text(amount_base_units),
    ]
}

fn archive_range_cells(
    canister_id: &str,
    method: &str,
    range: &IcrcArchivedRangeRow,
) -> [String; 4] {
    [
        canister_id.to_string(),
        method.to_string(),
        range.start.clone(),
        range.length.clone(),
    ]
}