linera_service/
wallet.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use comfy_table::{
5    modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, Color, ContentArrangement,
6    Table,
7};
8use linera_base::identifiers::ChainId;
9pub use linera_client::wallet::*;
10
11pub async fn pretty_print(wallet: &Wallet, chain_ids: impl IntoIterator<Item = ChainId>) {
12    let mut table = Table::new();
13    table
14        .load_preset(UTF8_FULL)
15        .apply_modifier(UTF8_ROUND_CORNERS)
16        .set_content_arrangement(ContentArrangement::Dynamic)
17        .set_header(vec![
18            Cell::new("Chain ID").add_attribute(Attribute::Bold),
19            Cell::new("Latest Block").add_attribute(Attribute::Bold),
20        ]);
21
22    for chain_id in chain_ids {
23        let Some(user_chain) = wallet.chains.get(&chain_id) else {
24            panic!("Chain {} not found.", chain_id);
25        };
26        update_table_with_chain(
27            &mut table,
28            chain_id,
29            user_chain,
30            Some(chain_id) == wallet.default,
31        )
32        .await;
33    }
34    println!("{}", table);
35}
36
37async fn update_table_with_chain(
38    table: &mut Table,
39    chain_id: ChainId,
40    user_chain: &UserChain,
41    is_default_chain: bool,
42) {
43    let epoch = user_chain.epoch;
44    let chain_id_cell = if is_default_chain {
45        Cell::new(format!("{}", chain_id)).fg(Color::Green)
46    } else {
47        Cell::new(format!("{}", chain_id))
48    };
49    let epoch_str = match epoch {
50        None => "-".to_string(),
51        Some(epoch) => format!("{}", epoch),
52    };
53    let account_owner = user_chain.owner;
54    table.add_row(vec![
55        chain_id_cell,
56        Cell::new(format!(
57            r#"AccountOwner:       {}
58Block Hash:         {}
59Timestamp:          {}
60Next Block Height:  {}
61Epoch:              {}"#,
62            account_owner
63                .as_ref()
64                .map(|o| o.to_string())
65                .unwrap_or_else(|| "-".to_string()),
66            user_chain
67                .block_hash
68                .map(|bh| bh.to_string())
69                .unwrap_or_else(|| "-".to_string()),
70            user_chain.timestamp,
71            user_chain.next_block_height,
72            epoch_str
73        )),
74    ]);
75}