forc_wallet/
list.rs

1use crate::{
2    account::{UnverifiedOpt, print_balance, print_balance_empty},
3    balance::{get_derived_accounts, list_account_balances, print_account_balances},
4};
5use anyhow::Result;
6use clap::Args;
7use std::collections::BTreeMap;
8
9#[derive(Debug, Args)]
10pub struct List {
11    /// Contains optional flag for displaying all accounts as hex / bytes values.
12    ///
13    /// pass in --as-hex for this alternative display.
14    #[clap(flatten)]
15    unverified: UnverifiedOpt,
16
17    /// The minimum amount of derived accounts to display their balances from.
18    /// If there are not enough accounts in the cache, the wallet will be unlocked (requesting the
19    /// user's password) and will derive more accounts.
20    #[clap(short, long)]
21    target_accounts: Option<usize>,
22}
23
24pub async fn list_wallet_cli(ctx: &crate::CliContext, opts: List) -> Result<()> {
25    let addresses = get_derived_accounts(ctx, opts.unverified.unverified, opts.target_accounts)
26        .await?
27        .range(0..opts.target_accounts.unwrap_or(1))
28        .map(|(a, b)| (*a, *b))
29        .collect::<BTreeMap<_, _>>();
30
31    let (account_balances, total_balance) =
32        list_account_balances(&ctx.node_url, &addresses).await?;
33    print_account_balances(&addresses, &account_balances)?;
34    println!("\nTotal:");
35    if total_balance.is_empty() {
36        print_balance_empty(&ctx.node_url);
37    } else {
38        print_balance(&total_balance);
39    }
40    Ok(())
41}