pcli/command/view/
auction.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
67
68
69
70
71
72
73
74
75
76
77
78
79
use anyhow::Result;
use comfy_table::{presets, Cell, ContentArrangement, Table};
use penumbra_auction::auction::dutch::DutchAuction;
use penumbra_keys::FullViewingKey;
use penumbra_proto::{core::component::auction::v1 as pb_auction, DomainType, Name};
use penumbra_view::ViewClient;

use crate::command::query::auction::render_dutch_auction;

#[derive(Debug, clap::Args)]
pub struct AuctionCmd {
    #[clap(long)]
    /// If set, includes the inactive auctions as well.
    pub include_inactive: bool,
    /// If set, make the view server query an RPC and pcli render the full auction state
    #[clap(long, default_value_t = true)]
    pub query_latest_state: bool,
}

impl AuctionCmd {
    pub fn offline(&self) -> bool {
        false
    }

    pub async fn exec(
        &self,
        view_client: &mut impl ViewClient,
        _fvk: &FullViewingKey,
    ) -> Result<()> {
        let auctions: Vec<(
            penumbra_auction::auction::AuctionId,
            penumbra_view::SpendableNoteRecord,
            u64,
            Option<pbjson_types::Any>,
            Vec<penumbra_dex::lp::position::Position>,
        )> = view_client
            .auctions(None, self.include_inactive, self.query_latest_state)
            .await?;

        for (auction_id, _, local_seq, maybe_auction_state, positions) in auctions.into_iter() {
            if let Some(pb_auction_state) = maybe_auction_state {
                if pb_auction_state.type_url == pb_auction::DutchAuction::type_url() {
                    let dutch_auction = DutchAuction::decode(pb_auction_state.value)
                        .expect("no deserialization error");
                    let asset_cache = view_client.assets().await?;
                    render_dutch_auction(
                        &asset_cache,
                        &dutch_auction,
                        Some(local_seq),
                        positions.get(0).cloned(),
                    )
                    .await
                    .expect("no rendering errors");
                } else {
                    unimplemented!("only supporting dutch auctions at the moment, come back later");
                }
            } else {
                let position_ids: Vec<String> = positions
                    .into_iter()
                    .map(|lp: penumbra_dex::lp::position::Position| format!("{}", lp.id()))
                    .collect();

                let mut auction_table = Table::new();
                auction_table.load_preset(presets::ASCII_FULL);
                auction_table
                    .set_header(vec!["Auction id", "LPs"])
                    .set_content_arrangement(ContentArrangement::DynamicFullWidth)
                    .add_row(vec![
                        Cell::new(&auction_id).set_delimiter('.'),
                        Cell::new(format!("{:?}", position_ids))
                            .set_alignment(comfy_table::CellAlignment::Center),
                    ]);

                println!("{auction_table}");
            }
        }
        Ok(())
    }
}