Skip to content

Commit

Permalink
Add a command to query all open positions in a PCLI wallet (#4904)
Browse files Browse the repository at this point in the history
Closes #4825.

## Describe your changes

This adds a command: `pcli v lps` which displays all open liquidity
positions owned by the wallet.

To test, try, e.g:

```
pcli tx position replicate linear --lower
-price 0.4 --upper-price 0.8 penumbra:gm 100gm --current-price 0.5

pcli v lps
```

## Checklist before requesting a review

- [x] I have added guiding text to explain how a reviewer should test
these changes.

- [x] If this code contains consensus-breaking changes, I have added the
"consensus-breaking" label. Otherwise, I declare my belief that there
are not consensus-breaking changes, for the following reason:

  > PCLI only.

---------

Co-authored-by: Conor Schaefer <[email protected]>
  • Loading branch information
cronokirby and conorsch authored Oct 25, 2024
1 parent c23270b commit fcce631
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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(())
}
}

0 comments on commit fcce631

Please sign in to comment.