Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lp_cancel_orders_batch to accepts OrderId as string/hex #5504

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions api/bin/chainflip-lp-api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rpc_types::CloseOrderJson;
use anyhow::anyhow;
use cf_primitives::{BasisPoints, BlockNumber, EgressId};
use cf_utilities::{
Expand Down Expand Up @@ -49,6 +50,7 @@ use tracing::log;
pub mod rpc_types {
use super::*;
use anyhow::anyhow;
use cf_primitives::chains::assets::any;
use cf_utilities::rpc::NumberOrHex;
use chainflip_api::{lp::PoolPairsMap, queries::SwapChannelInfo};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -97,6 +99,25 @@ pub mod rpc_types {
pub bitcoin: Vec<SwapChannelInfo<Bitcoin>>,
pub polkadot: Vec<SwapChannelInfo<Polkadot>>,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum CloseOrderJson {
Limit { base_asset: any::Asset, quote_asset: any::Asset, side: Side, id: OrderIdJson },
Range { base_asset: any::Asset, quote_asset: any::Asset, id: OrderIdJson },
}

impl TryFrom<CloseOrderJson> for CloseOrder {
type Error = anyhow::Error;

fn try_from(value: CloseOrderJson) -> Result<Self, Self::Error> {
Ok(match value {
CloseOrderJson::Limit { base_asset, quote_asset, side, id } =>
CloseOrder::Limit { base_asset, quote_asset, side, id: id.try_into()? },
CloseOrderJson::Range { base_asset, quote_asset, id } =>
CloseOrder::Range { base_asset, quote_asset, id: id.try_into()? },
})
}
}
}

#[rpc(server, client, namespace = "lp")]
Expand Down Expand Up @@ -213,7 +234,7 @@ pub trait Rpc {
#[method(name = "cancel_orders_batch")]
async fn cancel_orders_batch(
&self,
orders: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>>,
orders: BoundedVec<CloseOrderJson, ConstU32<MAX_ORDERS_DELETE>>,
wait_for: Option<WaitFor>,
) -> RpcResult<ApiWaitForResult<Vec<LimitOrRangeOrder>>>;
}
Expand Down Expand Up @@ -601,13 +622,20 @@ impl RpcServer for RpcServerImpl {

async fn cancel_orders_batch(
&self,
orders: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>>,
orders: BoundedVec<CloseOrderJson, ConstU32<MAX_ORDERS_DELETE>>,
wait_for: Option<WaitFor>,
) -> RpcResult<ApiWaitForResult<Vec<LimitOrRangeOrder>>> {
let mut final_orders: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>> =
BoundedVec::new();
for order in orders {
final_orders
.try_push(order.try_into()?)
.expect("Impossible to fail, given the same MAX_ORDERS_DELETE");
}
Copy link
Contributor

@kylezs kylezs Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove the mut var by collecting

Suggested change
let mut final_orders: BoundedVec<CloseOrder, ConstU32<MAX_ORDERS_DELETE>> =
BoundedVec::new();
for order in orders {
final_orders
.try_push(order.try_into()?)
.expect("Impossible to fail, given the same MAX_ORDERS_DELETE");
}
Ok(self
.api
.lp_api()
.cancel_orders_batch(
orders
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?
.try_into()
.expect("Impossible to fail, given the same MAX_ORDERS_DELETE"),
wait_for.unwrap_or_default(),
)
.await?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this require an extra memory allocation for the Vec<> when collecting?
(or maybe this gets optimized away by the compiler?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe so. But given the performance gain is negligible, I think having without a mut var is a bit simpler and less likely to be mistakenly broken with future changes (definitely isn't a strong opinion which way is preferred here)

Ok(self
.api
.lp_api()
.cancel_orders_batch(orders, wait_for.unwrap_or_default())
.cancel_orders_batch(final_orders, wait_for.unwrap_or_default())
.await?)
}
}
Expand Down
Loading