From 5ad778f98d43360adc536148aaaca9e1eee4c2af Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Tue, 28 May 2024 17:07:15 +0100 Subject: [PATCH] refactor: make lp withdraw action to take multiple position IDs --- crates/bin/pcli/src/command/tx.rs | 56 ++++++++++--------- .../pcli/src/command/tx/liquidity_position.rs | 6 +- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/crates/bin/pcli/src/command/tx.rs b/crates/bin/pcli/src/command/tx.rs index 31cee71660..a93fbb5bb4 100644 --- a/crates/bin/pcli/src/command/tx.rs +++ b/crates/bin/pcli/src/command/tx.rs @@ -1236,37 +1236,42 @@ 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 planner = Planner::new(OsRng) + .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() @@ -1274,6 +1279,7 @@ impl TxCmd { AddressIndex::new(*source), ) .await?; + app.build_and_submit_transaction(plan).await?; } TxCmd::Position(PositionCmd::RewardClaim {}) => { diff --git a/crates/bin/pcli/src/command/tx/liquidity_position.rs b/crates/bin/pcli/src/command/tx/liquidity_position.rs index 742588877b..9cf2d2e4be 100644 --- a/crates/bin/pcli/src/command/tx/liquidity_position.rs +++ b/crates/bin/pcli/src/command/tx/liquidity_position.rs @@ -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, /// The selected fee tier to multiply the fee amount by. #[clap(short, long, value_enum, default_value_t)] fee_tier: FeeTier,