@@ -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 }
0 commit comments