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 all 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
36 changes: 33 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,26 @@ pub mod rpc_types {
pub bitcoin: Vec<SwapChannelInfo<Bitcoin>>,
pub polkadot: Vec<SwapChannelInfo<Polkadot>>,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
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 +235,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 +623,21 @@ 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>>> {
Ok(self
.api
.lp_api()
.cancel_orders_batch(orders, wait_for.unwrap_or_default())
.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?)
}
}
Expand Down
Loading