Skip to content

Commit 6bdad96

Browse files
committed
remove ModifyQuantity
1 parent 27c227d commit 6bdad96

File tree

2 files changed

+14
-43
lines changed

2 files changed

+14
-43
lines changed

examples/matching-engine/src/contract.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use linera_sdk::{
1313
use matching_engine::{
1414
MatchingEngineAbi, Message, Operation, Order, OrderNature, Parameters, PendingOrderInfo, Price,
1515
};
16-
use state::{MatchingEngineState, ModifyQuantity, Transfer};
16+
use state::{MatchingEngineState, Transfer};
1717

1818
pub struct MatchingEngineContract {
1919
state: MatchingEngineState,
@@ -76,7 +76,7 @@ impl Contract for MatchingEngineContract {
7676
.await
7777
.expect("Failed to read existing order IDs");
7878
for order_id in order_ids {
79-
match self.state.modify_order(order_id, ModifyQuantity::All).await {
79+
match self.state.modify_order(order_id, Amount::ZERO).await {
8080
Some(transfer) => self.send_to(transfer),
8181
// Orders with amount zero may have been cleared in an earlier iteration.
8282
None => continue,
@@ -290,7 +290,7 @@ impl MatchingEngineContract {
290290

291291
let transfer = self
292292
.state
293-
.modify_order(order_id, ModifyQuantity::All)
293+
.modify_order(order_id, Amount::ZERO)
294294
.await
295295
.expect("Order is not present therefore cannot be cancelled");
296296
self.send_to(transfer);
@@ -329,27 +329,11 @@ impl MatchingEngineContract {
329329
.expect("Order should exist");
330330
let sender_chain_id = key_book.account.chain_id;
331331

332-
// Get the current quantity and calculate the reduction
333-
let current_quantity = self
334-
.state
335-
.get_order_quantity(&order_id)
336-
.await
337-
.expect("Order should have a current amount");
338-
339-
assert!(
340-
new_quantity <= current_quantity,
341-
"New quantity must be less than or equal to current quantity"
342-
);
343-
344-
let reduce_quantity = current_quantity
345-
.try_sub(new_quantity)
346-
.expect("Subtraction should not fail after validation");
347-
348332
let transfer = self
349333
.state
350-
.modify_order(order_id, ModifyQuantity::Partial(reduce_quantity))
334+
.modify_order(order_id, new_quantity)
351335
.await
352-
.expect("Order is not present therefore cannot be cancelled");
336+
.expect("Failed to modify order");
353337
self.send_to(transfer);
354338

355339
// Get the remaining quantity after modification

examples/matching-engine/src/state.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl MatchingEngineState {
167167
pub async fn modify_order(
168168
&mut self,
169169
order_id: OrderId,
170-
cancel_quantity: ModifyQuantity,
170+
new_quantity: Amount,
171171
) -> Option<Transfer> {
172172
let key_book = self
173173
.orders
@@ -178,7 +178,7 @@ impl MatchingEngineState {
178178
OrderNature::Bid => {
179179
let view = self.bid_level(&key_book.price.to_bid()).await;
180180
let (cancel_quantity, remove_order_id) =
181-
view.modify_order_level(order_id, cancel_quantity).await?;
181+
view.modify_order_level(order_id, new_quantity).await?;
182182
if remove_order_id {
183183
self.remove_order_id((key_book.account.owner, order_id))
184184
.await;
@@ -195,7 +195,7 @@ impl MatchingEngineState {
195195
OrderNature::Ask => {
196196
let view = self.ask_level(&key_book.price.to_ask()).await;
197197
let (cancel_quantity, remove_order_id) =
198-
view.modify_order_level(order_id, cancel_quantity).await?;
198+
view.modify_order_level(order_id, new_quantity).await?;
199199
if remove_order_id {
200200
self.remove_order_id((key_book.account.owner, order_id))
201201
.await;
@@ -426,15 +426,6 @@ pub struct Transfer {
426426
pub token_idx: u32,
427427
}
428428

429-
/// An order can be cancelled which removes it totally or
430-
/// modified which is a partial cancellation. The size of the
431-
/// order can never increase.
432-
#[derive(Clone, Debug)]
433-
pub enum ModifyQuantity {
434-
All,
435-
Partial(Amount),
436-
}
437-
438429
impl LevelView {
439430
fn parameters(&self) -> Parameters {
440431
*self.context().extra()
@@ -609,24 +600,20 @@ impl LevelView {
609600
pub async fn modify_order_level(
610601
&mut self,
611602
order_id: OrderId,
612-
reduce_quantity: ModifyQuantity,
603+
new_quantity: Amount,
613604
) -> Option<(Amount, bool)> {
614605
let mut iter = self
615606
.queue
616607
.iter_mut()
617608
.await
618609
.expect("Failed to load iterator over level queue");
619610
let state_order = iter.find(|order| order.order_id == order_id)?;
620-
let new_quantity = match reduce_quantity {
621-
ModifyQuantity::All => Amount::ZERO,
622-
ModifyQuantity::Partial(reduce_quantity) => state_order
623-
.quantity
624-
.try_sub(reduce_quantity)
625-
.expect("Attempt to cancel a larger quantity than available"),
626-
};
627-
let corr_reduce_quantity = state_order.quantity.try_sub(new_quantity).unwrap();
611+
let cancel_quantity = state_order
612+
.quantity
613+
.try_sub(new_quantity)
614+
.expect("Attempt to increase quantity");
628615
state_order.quantity = new_quantity;
629616
self.remove_zero_orders_from_level().await;
630-
Some((corr_reduce_quantity, new_quantity == Amount::ZERO))
617+
Some((cancel_quantity, new_quantity == Amount::ZERO))
631618
}
632619
}

0 commit comments

Comments
 (0)