pcli/command/query/
community_pool.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::App;
use anyhow::{Context, Result};
use futures::TryStreamExt;
use penumbra_asset::Value;
use penumbra_proto::{
    core::component::community_pool::v1::CommunityPoolAssetBalancesRequest,
    penumbra::core::component::community_pool::v1::query_service_client::QueryServiceClient as CommunityPoolQueryServiceClient,
};
use penumbra_view::ViewClient;
use std::io::{stdout, Write};

#[derive(Debug, clap::Subcommand)]
pub enum CommunityPoolCmd {
    /// Get the balance in the Community Pool, or the balance of a specific asset.
    Balance {
        /// Get only the balance of the specified asset.
        asset: Option<String>,
    },
}

impl CommunityPoolCmd {
    pub async fn exec(&self, app: &mut App) -> Result<()> {
        match self {
            CommunityPoolCmd::Balance { asset } => self.print_balance(app, asset).await,
        }
    }

    pub async fn print_balance(&self, app: &mut App, asset: &Option<String>) -> Result<()> {
        let asset_id = asset.as_ref().map(|asset| {
            // Try to parse as an asset ID, then if it's not an asset ID, assume it's a unit name
            if let Ok(asset_id) = asset.parse() {
                asset_id
            } else {
                penumbra_asset::asset::REGISTRY
                    .parse_unit(asset.as_str())
                    .id()
            }
        });

        let mut client = CommunityPoolQueryServiceClient::new(app.pd_channel().await?);
        let balances = client
            .community_pool_asset_balances(CommunityPoolAssetBalancesRequest {
                asset_ids: asset_id.map_or_else(std::vec::Vec::new, |id| vec![id.into()]),
            })
            .await?
            .into_inner()
            .try_collect::<Vec<_>>()
            .await
            .context("cannot process Community Pool balance data")?;

        let asset_cache = app.view().assets().await?;
        let mut writer = stdout();
        for balance_response in balances {
            let balance: Value = balance_response
                .balance
                .expect("balance should always be set")
                .try_into()
                .context("cannot parse balance")?;
            let value_str = balance.format(&asset_cache);

            writeln!(writer, "{value_str}")?;
        }

        Ok(())
    }
}