Skip to content

Commit

Permalink
pcli(tx): close/withdraw multiple LPs at once (#4490)
Browse files Browse the repository at this point in the history
## Describe your changes

This PR makes the pcli tx lp close/withdraw to take multiple position
IDs.

## Issue ticket number and link

Closes #4217

## Checklist before requesting a review

- [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:

  > Client change
  • Loading branch information
kerber0x authored May 30, 2024
1 parent e70b7a6 commit 58b389b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
78 changes: 46 additions & 32 deletions crates/bin/pcli/src/command/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use ibc_types::lightclients::tendermint::client_state::ClientState as Tendermint
use rand_core::OsRng;
use regex::Regex;

use crate::command::tx::auction::AuctionCmd;
use liquidity_position::PositionCmd;
use penumbra_asset::{asset, asset::Metadata, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_dex::{lp::position, swap_claim::SwapClaimPlan};
Expand Down Expand Up @@ -61,6 +60,7 @@ use penumbra_view::{SpendableNoteRecord, ViewClient};
use penumbra_wallet::plan::{self, Planner};
use proposal::ProposalCmd;

use crate::command::tx::auction::AuctionCmd;
use crate::App;

mod auction;
Expand Down Expand Up @@ -480,12 +480,12 @@ impl TxCmd {
"You will receive outputs of {} and {}. Claiming now...",
Value {
amount: pro_rata_outputs.0,
asset_id: swap_record.output_data.trading_pair.asset_1()
asset_id: swap_record.output_data.trading_pair.asset_1(),
}
.format(&asset_cache),
Value {
amount: pro_rata_outputs.1,
asset_id: swap_record.output_data.trading_pair.asset_2()
asset_id: swap_record.output_data.trading_pair.asset_2(),
}
.format(&asset_cache),
);
Expand Down Expand Up @@ -1104,21 +1104,28 @@ impl TxCmd {
app.build_and_submit_transaction(plan).await?;
}
TxCmd::Position(PositionCmd::Close {
position_id,
position_ids,
source,
fee_tier,
}) => {
let plan = Planner::new(OsRng)
let mut planner = Planner::new(OsRng);
planner
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into())
.position_close(*position_id)
.set_fee_tier((*fee_tier).into());

position_ids.iter().for_each(|position_id| {
planner.position_close(*position_id);
});

let plan = planner
.plan(
app.view
.as_mut()
.context("view service must be initialized")?,
AddressIndex::new(*source),
)
.await?;

app.build_and_submit_transaction(plan).await?;
}
TxCmd::Position(PositionCmd::CloseAll {
Expand Down Expand Up @@ -1229,44 +1236,51 @@ impl TxCmd {
}
TxCmd::Position(PositionCmd::Withdraw {
source,
position_id,
position_ids,
fee_tier,
}) => {
let mut client = DexQueryServiceClient::new(app.pd_channel().await?);

// Fetch the information regarding the position from the view service.
let position = client
.liquidity_position_by_id(LiquidityPositionByIdRequest {
position_id: Some(PositionId::from(*position_id)),
})
.await?
.into_inner();
let mut planner = Planner::new(OsRng);
planner
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into());

let reserves = position
.data
.clone()
.expect("missing position metadata")
.reserves
.expect("missing position reserves");
let pair = position
.data
.expect("missing position")
.phi
.expect("missing position trading function")
.pair
.expect("missing trading function pair");
for position_id in position_ids {
// Fetch the information regarding the position from the view service.
let position = client
.liquidity_position_by_id(LiquidityPositionByIdRequest {
position_id: Some(PositionId::from(*position_id)),
})
.await?
.into_inner();

let plan = Planner::new(OsRng)
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into())
.position_withdraw(*position_id, reserves.try_into()?, pair.try_into()?)
let reserves = position
.data
.clone()
.expect("missing position metadata")
.reserves
.expect("missing position reserves");
let pair = position
.data
.expect("missing position")
.phi
.expect("missing position trading function")
.pair
.expect("missing trading function pair");

planner.position_withdraw(*position_id, reserves.try_into()?, pair.try_into()?);
}

let plan = planner
.plan(
app.view
.as_mut()
.context("view service must be initialized")?,
AddressIndex::new(*source),
)
.await?;

app.build_and_submit_transaction(plan).await?;
}
TxCmd::Position(PositionCmd::RewardClaim {}) => {
Expand Down
14 changes: 7 additions & 7 deletions crates/bin/pcli/src/command/tx/liquidity_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum PositionCmd {
Order(OrderCmd),
/// Debits an all opened position NFTs associated with a specific source and credits closed position NFTs.
CloseAll {
/// Only spend funds originally received by the given address index.
/// Only spend fugdnds originally received by the given address index.
#[clap(long, default_value = "0")]
source: u32,
/// Only close positions for the given trading pair.
Expand All @@ -29,13 +29,13 @@ pub enum PositionCmd {
#[clap(short, long, value_enum, default_value_t)]
fee_tier: FeeTier,
},
/// Debits an opened position NFT and credits a closed position NFT.
/// Debits opened position NFTs and credits closed position NFTs.
Close {
/// Only spend funds originally received by the given address index.
#[clap(long, default_value = "0")]
source: u32,
/// The [`position::Id`] of the position to close.
position_id: position::Id,
/// The list of [`position::Id`] of the positions to close.
position_ids: Vec<position::Id>,
/// The selected fee tier to multiply the fee amount by.
#[clap(short, long, value_enum, default_value_t)]
fee_tier: FeeTier,
Expand All @@ -52,13 +52,13 @@ pub enum PositionCmd {
#[clap(short, long, value_enum, default_value_t)]
fee_tier: FeeTier,
},
/// Debits a closed position NFT and credits a withdrawn position NFT and the final reserves.
/// Debits closed position NFTs and credits withdrawn position NFTs and the final reserves.
Withdraw {
/// Only spend funds originally received by the given address index.
#[clap(long, default_value = "0")]
source: u32,
/// The [`position::Id`] of the position to withdraw.
position_id: position::Id,
/// The list of [`position::Id`] of the positions to withdraw.
position_ids: Vec<position::Id>,
/// The selected fee tier to multiply the fee amount by.
#[clap(short, long, value_enum, default_value_t)]
fee_tier: FeeTier,
Expand Down

0 comments on commit 58b389b

Please sign in to comment.