Skip to content

Commit

Permalink
feat: Add extrinsic to cancel buy orders when payments fail
Browse files Browse the repository at this point in the history
  • Loading branch information
abhath-labs committed Aug 21, 2024
1 parent 9101f4a commit 08165e6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions pallets/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ pub mod pallet {
BankWireInProcessSet {
order_id: BuyOrderId,
},
/// A buy order was cancelled
BuyOrderCancelled {
order_id: BuyOrderId,
sell_order_id: OrderId,
},
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -1148,6 +1153,43 @@ pub mod pallet {
Ok(())
})
}

#[pallet::call_index(15)]
#[pallet::weight(T::WeightInfo::force_set_purchase_fee())]
pub fn cancel_buy_order(origin: OriginFor<T>, order_id: BuyOrderId) -> DispatchResult {
let sender = ensure_signed(origin)?;
Self::check_validator_account(&sender)?;

let buy_order = BuyOrders::<T>::take(order_id).ok_or(Error::<T>::InvalidOrderId)?;

// add the credits to the sell order
Orders::<T>::try_mutate(buy_order.order_id, |maybe_order| -> DispatchResult {
let order = maybe_order.as_mut().ok_or(Error::<T>::InvalidOrderId)?;
order.units = order
.units
.checked_add(&buy_order.units)
.ok_or(Error::<T>::OrderUnitsOverflow)?;
Ok(())
})?;

// remove buy orders by user record
BuyOrdersByUser::<T>::try_mutate(
buy_order.buyer.clone(),
|open_orders| -> DispatchResult {
let open_orders = open_orders.get_or_insert_with(Default::default);
open_orders.retain(|&x| x != (order_id, buy_order.units));
Ok(())
},
)?;

// emit an event that expired order was removed
Self::deposit_event(Event::BuyOrderCancelled {
order_id,
sell_order_id: buy_order.order_id,
});

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion runtime/bitgreen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("bitgreen-parachain"),
impl_name: create_runtime_str!("bitgreen-parachain"),
authoring_version: 1,
spec_version: 1402, // v1.4.2
spec_version: 1403, // v1.4.3
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down
2 changes: 1 addition & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("bitgreen-rococo"),
impl_name: create_runtime_str!("bitgreen-rococo"),
authoring_version: 1,
spec_version: 1402, // v1.4.2
spec_version: 1403, // v1.4.3
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down

0 comments on commit 08165e6

Please sign in to comment.