Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a command to query all open positions in a PCLI wallet #4904

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/bin/pcli/src/command/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;

use address::AddressCmd;
use balance::BalanceCmd;
use lps::LiquidityPositionsCmd;
use noble_address::NobleAddressCmd;
use staked::StakedCmd;
use transaction_hashes::TransactionHashesCmd;
Expand All @@ -15,6 +16,7 @@ use self::auction::AuctionCmd;
mod address;
mod auction;
mod balance;
mod lps;
mod noble_address;
mod staked;
mod wallet_id;
Expand Down Expand Up @@ -48,6 +50,9 @@ pub enum ViewCmd {
ListTransactionHashes(TransactionHashesCmd),
/// Displays a transaction's details by hash.
Tx(TxCmd),
/// View information about the liquidity positions you control.
#[clap(visible_alias = "lps")]
LiquidityPositions(LiquidityPositionsCmd),
}

impl ViewCmd {
Expand All @@ -63,6 +68,7 @@ impl ViewCmd {
ViewCmd::Sync => false,
ViewCmd::ListTransactionHashes(transactions_cmd) => transactions_cmd.offline(),
ViewCmd::Tx(tx_cmd) => tx_cmd.offline(),
ViewCmd::LiquidityPositions(lps_cmd) => lps_cmd.offline(),
}
}

Expand Down Expand Up @@ -110,6 +116,7 @@ impl ViewCmd {
.exec(&full_viewing_key, view_client, channel)
.await?;
}
ViewCmd::LiquidityPositions(cmd) => cmd.exec(app).await?,
}

Ok(())
Expand Down
48 changes: 48 additions & 0 deletions crates/bin/pcli/src/command/view/lps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anyhow::{anyhow, Result};
use futures::stream::TryStreamExt;
use penumbra_dex::lp::position::{Position, State};
use penumbra_proto::core::component::dex::v1::{
query_service_client::QueryServiceClient as DexQueryServiceClient,
LiquidityPositionsByIdRequest,
};
use penumbra_view::ViewClient;

use crate::{command::utils, App};

#[derive(Debug, clap::Args)]
pub struct LiquidityPositionsCmd {}

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

pub async fn exec(&self, app: &mut App) -> Result<()> {
let my_position_ids = app
.view()
.owned_position_ids(Some(State::Opened), None)
.await?;
let mut dex_client = DexQueryServiceClient::new(app.pd_channel().await?);

let positions_stream = dex_client
.liquidity_positions_by_id(LiquidityPositionsByIdRequest {
position_id: my_position_ids.into_iter().map(Into::into).collect(),
})
.await?
.into_inner()
.map_err(|e| anyhow!("eror fetching liquidity positions: {}", e))
.and_then(|msg| async move {
msg.data
.ok_or_else(|| anyhow!("missing liquidity position in response"))
.map(Position::try_from)?
});

let asset_cache = app.view().assets().await?;

let positions = positions_stream.try_collect::<Vec<_>>().await?;

println!("{}", utils::render_positions(&asset_cache, &positions));

Ok(())
}
}
Loading