Skip to content

Commit

Permalink
Add unfilled field to SimulateTradeResponse (#3917)
Browse files Browse the repository at this point in the history
See: penumbra-zone/web#566

Would be nice if clients did not have to calculate this on their own by
doing the math manually.
  • Loading branch information
grod220 authored Mar 1, 2024
1 parent c7654e2 commit 6f8f2cb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
32 changes: 24 additions & 8 deletions crates/core/component/dex/src/component/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{pin::Pin, sync::Arc};

use crate::ExecutionCircuitBreaker;
use async_stream::try_stream;
use cnidarium::{StateDelta, Storage};
use futures::{StreamExt, TryStreamExt};
use tonic::Status;
use tracing::instrument;

use cnidarium::{StateDelta, Storage};
use penumbra_asset::{asset, Value};
use penumbra_proto::{
core::component::dex::v1::{
Expand All @@ -20,18 +22,18 @@ use penumbra_proto::{
},
DomainType, StateReadProto,
};
use tonic::Status;
use tracing::instrument;

use super::{
router::{RouteAndFill, RoutingParams},
PositionRead, StateReadExt,
};
use crate::ExecutionCircuitBreaker;
use crate::{
lp::position::{self, Position},
state_key, DirectedTradingPair, SwapExecution, TradingPair,
};

use super::{
router::{RouteAndFill, RoutingParams},
PositionRead, StateReadExt,
};

// TODO: Hide this and only expose a Router?
pub struct Server {
storage: Storage,
Expand Down Expand Up @@ -541,7 +543,21 @@ impl SimulationService for Server {
.await
.map_err(|e| tonic::Status::internal(format!("error simulating trade: {:#}", e)))?;

let unfilled = Value {
amount: input
.amount
.checked_sub(&swap_execution.input.amount)
.ok_or_else(|| {
tonic::Status::failed_precondition(
"swap execution input amount is larger than request input amount"
.to_string(),
)
})?,
asset_id: input.asset_id,
};

Ok(tonic::Response::new(SimulateTradeResponse {
unfilled: Some(unfilled.into()),
output: Some(swap_execution.into()),
}))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/proto/src/gen/penumbra.core.component.dex.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,9 @@ impl ::prost::Name for SimulateTradeRequest {
pub struct SimulateTradeResponse {
#[prost(message, optional, tag = "1")]
pub output: ::core::option::Option<SwapExecution>,
/// Estimated input amount that will not be swapped due to liquidity
#[prost(message, optional, tag = "2")]
pub unfilled: ::core::option::Option<super::super::super::asset::v1::Value>,
}
impl ::prost::Name for SimulateTradeResponse {
const NAME: &'static str = "SimulateTradeResponse";
Expand Down
17 changes: 17 additions & 0 deletions crates/proto/src/gen/penumbra.core.component.dex.v1.serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4868,10 +4868,16 @@ impl serde::Serialize for SimulateTradeResponse {
if self.output.is_some() {
len += 1;
}
if self.unfilled.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.core.component.dex.v1.SimulateTradeResponse", len)?;
if let Some(v) = self.output.as_ref() {
struct_ser.serialize_field("output", v)?;
}
if let Some(v) = self.unfilled.as_ref() {
struct_ser.serialize_field("unfilled", v)?;
}
struct_ser.end()
}
}
Expand All @@ -4883,11 +4889,13 @@ impl<'de> serde::Deserialize<'de> for SimulateTradeResponse {
{
const FIELDS: &[&str] = &[
"output",
"unfilled",
];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {
Output,
Unfilled,
__SkipField__,
}
impl<'de> serde::Deserialize<'de> for GeneratedField {
Expand All @@ -4911,6 +4919,7 @@ impl<'de> serde::Deserialize<'de> for SimulateTradeResponse {
{
match value {
"output" => Ok(GeneratedField::Output),
"unfilled" => Ok(GeneratedField::Unfilled),
_ => Ok(GeneratedField::__SkipField__),
}
}
Expand All @@ -4931,6 +4940,7 @@ impl<'de> serde::Deserialize<'de> for SimulateTradeResponse {
V: serde::de::MapAccess<'de>,
{
let mut output__ = None;
let mut unfilled__ = None;
while let Some(k) = map_.next_key()? {
match k {
GeneratedField::Output => {
Expand All @@ -4939,13 +4949,20 @@ impl<'de> serde::Deserialize<'de> for SimulateTradeResponse {
}
output__ = map_.next_value()?;
}
GeneratedField::Unfilled => {
if unfilled__.is_some() {
return Err(serde::de::Error::duplicate_field("unfilled"));
}
unfilled__ = map_.next_value()?;
}
GeneratedField::__SkipField__ => {
let _ = map_.next_value::<serde::de::IgnoredAny>()?;
}
}
}
Ok(SimulateTradeResponse {
output: output__,
unfilled: unfilled__,
})
}
}
Expand Down
Binary file modified crates/proto/src/gen/proto_descriptor.bin.no_lfs
Binary file not shown.
2 changes: 2 additions & 0 deletions proto/penumbra/penumbra/core/component/dex/v1/dex.proto
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ message SimulateTradeRequest {

message SimulateTradeResponse {
core.component.dex.v1.SwapExecution output = 1;
// Estimated input amount that will not be swapped due to liquidity
asset.v1.Value unfilled = 2;
}

message EventSwap {
Expand Down

0 comments on commit 6f8f2cb

Please sign in to comment.