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    for chain_id in chain_ids {
22        let Some(user_chain) = wallet.chains.get(&chain_id) else {
23            panic!("Chain {} not found.", chain_id);
24        };
25        update_table_with_chain(
26            &mut table,
27            chain_id,
28            user_chain,
29            Some(chain_id) == wallet.default,
30        )
31        .await;
32    }
33    println!("{}", table);
34}
35
36async fn update_table_with_chain(
37    table: &mut Table,
38    chain_id: ChainId,
39    user_chain: &UserChain,
40    is_default_chain: bool,
41) {
42    let chain_id_cell = if is_default_chain {
43        Cell::new(format!("{}", chain_id)).fg(Color::Green)
44    } else {
45        Cell::new(format!("{}", chain_id))
46    };
47    let account_owner = user_chain.owner;
48    table.add_row(vec![
49        chain_id_cell,
50        Cell::new(format!(
51            r#"AccountOwner:       {}
52Block Hash:         {}
53Timestamp:          {}
54Next Block Height:  {}"#,
55            account_owner
56                .as_ref()
57                .map(|o| o.to_string())
58                .unwrap_or_else(|| "-".to_string()),
59            user_chain
60                .block_hash
61                .map(|bh| bh.to_string())
62                .unwrap_or_else(|| "-".to_string()),
63            user_chain.timestamp,
64            user_chain.next_block_height
65        )),
66    ]);
67}