Skip to content

Commit

Permalink
Add position to OwnedPositionIdsResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Aug 27, 2024
1 parent b858016 commit e970d58
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
10 changes: 5 additions & 5 deletions crates/bin/pcli/src/command/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@ impl TxCmd {
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into());

for position_id in positions_to_close_now {
for (id, _) in positions_to_close_now {
// Close the position
planner.position_close(*position_id);
planner.position_close(*id);
}

let final_plan = planner
Expand Down Expand Up @@ -1229,13 +1229,13 @@ impl TxCmd {
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into());

for position_id in positions_to_withdraw_now {
for (id, _) in positions_to_withdraw_now {
// Withdraw the position

// Fetch the information regarding the position from the view service.
let position = client
.liquidity_position_by_id(LiquidityPositionByIdRequest {
position_id: Some((*position_id).into()),
position_id: Some((*id).into()),
})
.await?
.into_inner();
Expand All @@ -1254,7 +1254,7 @@ impl TxCmd {
.pair
.expect("missing trading function pair");
planner.position_withdraw(
*position_id,
*id,
reserves.try_into().expect("invalid reserves"),
pair.try_into().expect("invalid pair"),
);
Expand Down
Binary file modified crates/cnidarium/src/gen/proto_descriptor.bin.no_lfs
Binary file not shown.
4 changes: 4 additions & 0 deletions crates/proto/src/gen/penumbra.view.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,10 @@ pub struct OwnedPositionIdsResponse {
pub position_id: ::core::option::Option<
super::super::core::component::dex::v1::PositionId,
>,
#[prost(message, optional, tag = "2")]
pub position: ::core::option::Option<
super::super::core::component::dex::v1::Position,
>,
}
impl ::prost::Name for OwnedPositionIdsResponse {
const NAME: &'static str = "OwnedPositionIdsResponse";
Expand Down
17 changes: 17 additions & 0 deletions crates/proto/src/gen/penumbra.view.v1.serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4307,10 +4307,16 @@ impl serde::Serialize for OwnedPositionIdsResponse {
if self.position_id.is_some() {
len += 1;
}
if self.position.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1.OwnedPositionIdsResponse", len)?;
if let Some(v) = self.position_id.as_ref() {
struct_ser.serialize_field("positionId", v)?;
}
if let Some(v) = self.position.as_ref() {
struct_ser.serialize_field("position", v)?;
}
struct_ser.end()
}
}
Expand All @@ -4323,11 +4329,13 @@ impl<'de> serde::Deserialize<'de> for OwnedPositionIdsResponse {
const FIELDS: &[&str] = &[
"position_id",
"positionId",
"position",
];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {
PositionId,
Position,
__SkipField__,
}
impl<'de> serde::Deserialize<'de> for GeneratedField {
Expand All @@ -4351,6 +4359,7 @@ impl<'de> serde::Deserialize<'de> for OwnedPositionIdsResponse {
{
match value {
"positionId" | "position_id" => Ok(GeneratedField::PositionId),
"position" => Ok(GeneratedField::Position),
_ => Ok(GeneratedField::__SkipField__),
}
}
Expand All @@ -4371,6 +4380,7 @@ impl<'de> serde::Deserialize<'de> for OwnedPositionIdsResponse {
V: serde::de::MapAccess<'de>,
{
let mut position_id__ = None;
let mut position__ = None;
while let Some(k) = map_.next_key()? {
match k {
GeneratedField::PositionId => {
Expand All @@ -4379,13 +4389,20 @@ impl<'de> serde::Deserialize<'de> for OwnedPositionIdsResponse {
}
position_id__ = map_.next_value()?;
}
GeneratedField::Position => {
if position__.is_some() {
return Err(serde::de::Error::duplicate_field("position"));
}
position__ = map_.next_value()?;
}
GeneratedField::__SkipField__ => {
let _ = map_.next_value::<serde::de::IgnoredAny>()?;
}
}
}
Ok(OwnedPositionIdsResponse {
position_id: position_id__,
position: position__,
})
}
}
Expand Down
Binary file modified crates/proto/src/gen/proto_descriptor.bin.no_lfs
Binary file not shown.
18 changes: 11 additions & 7 deletions crates/view/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub trait ViewClient {
&mut self,
position_state: Option<position::State>,
trading_pair: Option<TradingPair>,
) -> Pin<Box<dyn Future<Output = Result<Vec<position::Id>>> + Send + 'static>>;
) -> Pin<Box<dyn Future<Output = Result<Vec<(position::Id, Position)>>> + Send + 'static>>;

/// Generates a full perspective for a selected transaction using a full viewing key
fn transaction_info_by_hash(
Expand Down Expand Up @@ -697,7 +697,7 @@ where
&mut self,
position_state: Option<position::State>,
trading_pair: Option<TradingPair>,
) -> Pin<Box<dyn Future<Output = Result<Vec<position::Id>>> + Send + 'static>> {
) -> Pin<Box<dyn Future<Output = Result<Vec<(position::Id, Position)>>> + Send + 'static>> {
// should the return be streamed here? none of the other viewclient responses are, probably fine for now
// but might be an issue eventually
let mut self2 = self.clone();
Expand All @@ -717,11 +717,15 @@ where
let position_ids = pb_position_ids
.into_iter()
.map(|p| {
position::Id::try_from(p.position_id.ok_or_else(|| {
anyhow::anyhow!("empty OwnedPositionsIdsResponse message")
})?)
let id = position::Id::try_from(p.position_id.ok_or_else(|| {
anyhow::anyhow!("OwnedPositionsIdsResponse missing position_id")
})?)?;
let position = Position::try_from(p.position.ok_or_else(|| {
anyhow::anyhow!("OwnedPositionsIdsResponse missing position")
})?)?;
Ok((id, position))
})
.collect::<anyhow::Result<Vec<position::Id>>>()?;
.collect::<anyhow::Result<Vec<(position::Id, Position)>>>()?;

Ok(position_ids)
}
Expand Down Expand Up @@ -1048,6 +1052,6 @@ where

Ok(resp)
}
.boxed()
.boxed()
}
}
9 changes: 5 additions & 4 deletions crates/view/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,16 +1725,17 @@ impl ViewService for ViewServer {
.map_err(|e: anyhow::Error| e.context("could not decode trading pair"))
.map_err(|e| tonic::Status::invalid_argument(format!("{:#}", e)))?;

let ids = self
let positions = self
.storage
.owned_position_ids(position_state, trading_pair)
.owned_positions(position_state, trading_pair)
.await
.map_err(|e| tonic::Status::unavailable(format!("error getting position ids: {e}")))?;
.map_err(|e| tonic::Status::unavailable(format!("error getting positions: {e}")))?;

let stream = try_stream! {
for id in ids {
for (id, position) in positions {
yield pb::OwnedPositionIdsResponse{
position_id: Some(id.into()),
position: Some(position.into())
}
}
};
Expand Down
32 changes: 27 additions & 5 deletions crates/view/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use url::Url;

use penumbra_app::params::AppParameters;
use penumbra_asset::{asset, asset::Id, asset::Metadata, Value};
use penumbra_dex::lp::{BareTradingFunction, TradingFunction};
use penumbra_dex::{
lp::position::{self, Position, State},
TradingPair,
Expand Down Expand Up @@ -1097,7 +1098,7 @@ impl Storage {

tx.commit()?;
Ok::<(), anyhow::Error>(())
})
})
.await??;

Ok(())
Expand Down Expand Up @@ -1679,11 +1680,11 @@ impl Storage {
Ok(())
}

pub async fn owned_position_ids(
pub async fn owned_positions(
&self,
position_state: Option<State>,
trading_pair: Option<TradingPair>,
) -> anyhow::Result<Vec<position::Id>> {
) -> anyhow::Result<Vec<(position::Id, Position)>> {
let pool = self.pool.clone();

let state_clause = match position_state {
Expand Down Expand Up @@ -1714,8 +1715,29 @@ impl Storage {
pool.get()?
.prepare_cached(&q)?
.query_and_then([], |row| {
let position_id: Vec<u8> = row.get("position_id")?;
Ok(position::Id(position_id.as_slice().try_into()?))
let position_id_bytes: Vec<u8> = row.get("position_id")?;
let id = position::Id(position_id_bytes.as_slice().try_into()?);

let position_state_str: String = row.get("position_state")?;
let trading_pair_str: String = row.get("trading_pair")?;

// TODO: DB currently does not save full position. Hence, default backups are used below.
// We should save the full position in the db to be able to provide the full correct data.
let position = Position {
state: State::from_str(&position_state_str)?,
phi: TradingFunction {
component: BareTradingFunction {
fee: 0,
p: Default::default(),
q: Default::default(),
},
pair: TradingPair::from_str(&trading_pair_str)?,
},
reserves: Default::default(),
nonce: [0u8; 32],
close_on_fill: false,
};
Ok((id, position))
})?
.collect()
})
Expand Down
1 change: 1 addition & 0 deletions proto/penumbra/penumbra/view/v1/view.proto
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ message OwnedPositionIdsRequest {

message OwnedPositionIdsResponse {
core.component.dex.v1.PositionId position_id = 1;
core.component.dex.v1.Position position = 2;
}

// Requests information on an asset by asset id
Expand Down

0 comments on commit e970d58

Please sign in to comment.