Skip to content

Commit

Permalink
fix: lp_cancel_orders_batch to accepts OrderId as string/hex (#5504)
Browse files Browse the repository at this point in the history
* introduce CloseOrderJson struct

* use closeOrderJson in the API

* add serde(untagged) to CloseOrderJson
  • Loading branch information
marcellorigotti authored Dec 16, 2024
1 parent 2c41714 commit e149a50
Showing 1 changed file with 33 additions and 3 deletions.
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 @@ -44,6 +45,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 @@ -92,6 +94,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 @@ -208,7 +230,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 @@ -585,13 +607,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

0 comments on commit e149a50

Please sign in to comment.