Skip to content

Commit 8d5207e

Browse files
committed
remove Order::Cancel and Message::OrderRemoved
1 parent 6bdad96 commit 8d5207e

File tree

4 files changed

+44
-114
lines changed

4 files changed

+44
-114
lines changed

examples/matching-engine/src/contract.rs

Lines changed: 28 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,16 @@ impl Contract for MatchingEngineContract {
137137
.expect("Failed to load pending orders")
138138
.expect("Account should have pending orders");
139139

140-
// Update the pending order
141-
if let Some(order) = pending_orders.get_mut(&order_id) {
142-
order.quantity = new_quantity;
140+
if new_quantity == Amount::ZERO {
141+
// Remove the pending order
142+
pending_orders.remove(&order_id);
143+
} else {
144+
// Update the pending order
145+
if let Some(order) = pending_orders.get_mut(&order_id) {
146+
order.quantity = new_quantity;
147+
}
143148
}
144149
}
145-
Message::OrderRemoved { owner, order_id } => {
146-
// This message is received on the sender chain from the matching engine
147-
let pending_orders = self
148-
.state
149-
.pending_orders
150-
.get_mut(&owner)
151-
.await
152-
.expect("Failed to load pending orders")
153-
.expect("Account should have pending orders");
154-
155-
// Remove the pending order
156-
pending_orders.remove(&order_id);
157-
}
158150
}
159151
}
160152

@@ -238,9 +230,10 @@ impl MatchingEngineContract {
238230
let matching_engine_chain = self.runtime.chain_id();
239231
for (filled_chain_id, filled_owner, filled_order_id) in filled_orders {
240232
if filled_chain_id != matching_engine_chain {
241-
let removal_message = Message::OrderRemoved {
233+
let removal_message = Message::OrderUpdated {
242234
owner: filled_owner,
243235
order_id: filled_order_id,
236+
new_quantity: Amount::ZERO,
244237
};
245238
self.runtime
246239
.prepare_message(removal_message)
@@ -277,7 +270,11 @@ impl MatchingEngineContract {
277270
}
278271
}
279272
}
280-
Order::Cancel { owner, order_id } => {
273+
Order::Modify {
274+
owner,
275+
order_id,
276+
new_quantity,
277+
} => {
281278
self.state.check_order_id(&order_id, &owner).await;
282279
let key_book = self
283280
.state
@@ -290,16 +287,21 @@ impl MatchingEngineContract {
290287

291288
let transfer = self
292289
.state
293-
.modify_order(order_id, Amount::ZERO)
290+
.modify_order(order_id, new_quantity)
294291
.await
295-
.expect("Order is not present therefore cannot be cancelled");
292+
.expect("Failed to modify order");
296293
self.send_to(transfer);
297294

298-
// Send removal notification to the sender chain (if different from matching engine chain)
295+
// Order still exists with reduced amount
299296
if sender_chain_id != self.runtime.chain_id() {
300-
let removal_message = Message::OrderRemoved { owner, order_id };
297+
// Send update notification to the sender chain (if different from matching engine chain)
298+
let update_message = Message::OrderUpdated {
299+
owner,
300+
order_id,
301+
new_quantity,
302+
};
301303
self.runtime
302-
.prepare_message(removal_message)
304+
.prepare_message(update_message)
303305
.with_authentication()
304306
.send_to(sender_chain_id);
305307
} else {
@@ -310,77 +312,9 @@ impl MatchingEngineContract {
310312
.await
311313
.expect("Failed to load pending orders")
312314
.expect("Account should have pending orders");
313-
// Remove the pending order
314-
pending_orders.remove(&order_id);
315-
}
316-
}
317-
Order::Modify {
318-
owner,
319-
order_id,
320-
new_quantity,
321-
} => {
322-
self.state.check_order_id(&order_id, &owner).await;
323-
let key_book = self
324-
.state
325-
.orders
326-
.get(&order_id)
327-
.await
328-
.expect("Failed to load order")
329-
.expect("Order should exist");
330-
let sender_chain_id = key_book.account.chain_id;
331-
332-
let transfer = self
333-
.state
334-
.modify_order(order_id, new_quantity)
335-
.await
336-
.expect("Failed to modify order");
337-
self.send_to(transfer);
338-
339-
// Get the remaining quantity after modification
340-
if let Some(new_quantity) = self.state.get_order_quantity(&order_id).await {
341-
// Order still exists with reduced amount
342-
if sender_chain_id != self.runtime.chain_id() {
343-
// Send update notification to the sender chain (if different from matching engine chain)
344-
let update_message = Message::OrderUpdated {
345-
owner,
346-
order_id,
347-
new_quantity,
348-
};
349-
self.runtime
350-
.prepare_message(update_message)
351-
.with_authentication()
352-
.send_to(sender_chain_id);
353-
} else {
354-
let pending_orders = self
355-
.state
356-
.pending_orders
357-
.get_mut(&owner)
358-
.await
359-
.expect("Failed to load pending orders")
360-
.expect("Account should have pending orders");
361-
// Update the pending order
362-
if let Some(order) = pending_orders.get_mut(&order_id) {
363-
order.quantity = new_quantity;
364-
}
365-
}
366-
} else {
367-
// Order was fully cancelled
368-
if sender_chain_id != self.runtime.chain_id() {
369-
let removal_message = Message::OrderRemoved { owner, order_id };
370-
self.runtime
371-
.prepare_message(removal_message)
372-
.with_authentication()
373-
.send_to(sender_chain_id);
374-
} else {
375-
let pending_orders = self
376-
.state
377-
.pending_orders
378-
.get_mut(&owner)
379-
.await
380-
.expect("Failed to load pending orders")
381-
.expect("Account should have pending orders");
382-
// Remove the pending order
383-
pending_orders.remove(&order_id);
315+
// Update the pending order
316+
if let Some(order) = pending_orders.get_mut(&order_id) {
317+
order.quantity = new_quantity;
384318
}
385319
}
386320
}

examples/matching-engine/src/lib.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,15 @@ scalar!(OrderNature);
136136

137137
#[derive(Clone, Debug, Serialize, Deserialize)]
138138
pub enum Order {
139-
/// Insertion of an order
139+
/// Insert an order
140140
Insert {
141141
owner: AccountOwner,
142142
quantity: Amount,
143143
nature: OrderNature,
144144
price: Price,
145145
},
146-
/// Cancelling of an order
147-
Cancel {
148-
owner: AccountOwner,
149-
order_id: OrderId,
150-
},
151-
/// Modifying order (only decreasing is allowed)
146+
/// Modify an order. The quantity can only be decreased. If nul, the order is
147+
/// canceled.
152148
Modify {
153149
owner: AccountOwner,
154150
order_id: OrderId,
@@ -163,7 +159,6 @@ impl Order {
163159
pub fn owner(&self) -> AccountOwner {
164160
match self {
165161
Order::Insert { owner, .. } => *owner,
166-
Order::Cancel { owner, .. } => *owner,
167162
Order::Modify { owner, .. } => *owner,
168163
}
169164
}
@@ -178,7 +173,6 @@ impl Order {
178173
let quantity = match self {
179174
Order::Insert { quantity, .. } => *quantity,
180175
Order::Modify { new_quantity, .. } => *new_quantity,
181-
Order::Cancel { .. } => return Ok(()), // No quantity to check
182176
};
183177

184178
// Calculate the minimum precision unit allowed
@@ -282,17 +276,12 @@ pub enum Message {
282276
order_id: OrderId,
283277
order_info: PendingOrderInfo,
284278
},
285-
/// Notification sent from the matching engine when an order is modified or cancelled.
279+
/// Notification sent from the matching engine when an order is modified, filled, or cancelled.
286280
OrderUpdated {
287281
owner: AccountOwner,
288282
order_id: OrderId,
289283
new_quantity: Amount,
290284
},
291-
/// Notification sent from the matching engine when an order is fully cancelled or filled.
292-
OrderRemoved {
293-
owner: AccountOwner,
294-
order_id: OrderId,
295-
},
296285
}
297286

298287
#[cfg(test)]
@@ -342,8 +331,12 @@ mod tests {
342331
};
343332
assert!(modify_invalid.check_precision(price_decimals).is_err());
344333

345-
// Test Cancel order (should always succeed)
346-
let cancel_order = Order::Cancel { owner, order_id: 1 };
334+
// Test order cancellation.
335+
let cancel_order = Order::Modify {
336+
owner,
337+
order_id: 1,
338+
new_quantity: Amount::ZERO,
339+
};
347340
assert!(cancel_order.check_precision(price_decimals).is_ok());
348341

349342
// Test with price_decimals = 0 (any quantity should be valid)

examples/matching-engine/tests/transaction.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ async fn single_transaction() {
246246
}
247247

248248
// Cancel A's order.
249-
let order = Order::Cancel {
249+
let order = Order::Modify {
250250
owner: owner_a,
251251
order_id: order_ids_a[0],
252+
new_quantity: Amount::ZERO,
252253
};
253254
let operation = Operation::ExecuteOrder { order };
254255
let order_certificate = user_chain_a

linera-service/tests/linera_net_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,17 +3262,19 @@ async fn test_wasm_end_to_end_matching_engine(config: impl LineraNetConfig) -> R
32623262
// Now cancelling all the orders
32633263
for order_id in order_ids_a {
32643264
app_matching_a
3265-
.order(matching_engine::Order::Cancel {
3265+
.order(matching_engine::Order::Modify {
32663266
owner: owner_a,
32673267
order_id,
3268+
new_quantity: Amount::ZERO,
32683269
})
32693270
.await;
32703271
}
32713272
for order_id in order_ids_b {
32723273
app_matching_b
3273-
.order(matching_engine::Order::Cancel {
3274+
.order(matching_engine::Order::Modify {
32743275
owner: owner_b,
32753276
order_id,
3277+
new_quantity: Amount::ZERO,
32763278
})
32773279
.await;
32783280
}

0 commit comments

Comments
 (0)