@@ -288,25 +288,37 @@ class OrderBook extends EventEmitter {
288
288
// perform match. maker orders will be removed from the repository
289
289
const tp = this . getTradingPair ( order . pairId ) ;
290
290
const matchingResult = tp . match ( order ) ;
291
-
292
- // instantiate the final response object
293
- const result : PlaceOrderResult = {
294
- internalMatches : [ ] ,
295
- swapResults : [ ] ,
296
- remainingOrder : matchingResult . remainingOrder ,
291
+ /** Any portion of the placed order that could not be swapped or matched internally. */
292
+ let { remainingOrder } = matchingResult ;
293
+ /** Local orders that matched with the placed order. */
294
+ const internalMatches : OwnOrder [ ] = [ ] ;
295
+ /** Successful swaps performed for the placed order. */
296
+ const swapResults : SwapResult [ ] = [ ] ;
297
+
298
+ const retryFailedSwap = async ( failedSwapQuantity : number ) => {
299
+ this . logger . debug ( `repeating matching routine for ${ order . id } for failed quantity of ${ failedSwapQuantity } ` ) ;
300
+ const orderToRetry : OwnOrder = { ...order , quantity : failedSwapQuantity } ;
301
+
302
+ // invoke placeOrder recursively, append matches/swaps and any remaining order
303
+ const retryResult = await this . placeOrder ( orderToRetry , true , onUpdate , maxTime ) ;
304
+ internalMatches . push ( ...retryResult . internalMatches ) ;
305
+ swapResults . push ( ...retryResult . swapResults ) ;
306
+ if ( retryResult . remainingOrder ) {
307
+ if ( remainingOrder ) {
308
+ remainingOrder . quantity += retryResult . remainingOrder . quantity ;
309
+ } else {
310
+ remainingOrder = retryResult . remainingOrder ;
311
+ }
312
+ }
297
313
} ;
298
314
299
- /** The quantity that was attempted to be swapped but failed. */
300
- let failedSwapQuantity = 0 ;
301
-
302
- // iterate over the matches
303
- for ( const { maker, taker } of matchingResult . matches ) {
315
+ const handleMatch = async ( maker : Order , taker : OwnOrder ) => {
304
316
const portion : OrderPortion = { id : maker . id , pairId : maker . pairId , quantity : maker . quantity } ;
305
317
if ( isOwnOrder ( maker ) ) {
306
318
// internal match
307
319
this . logger . info ( `internal match executed on taker ${ taker . id } and maker ${ maker . id } for ${ maker . quantity } ` ) ;
308
320
portion . localId = maker . localId ;
309
- result . internalMatches . push ( maker ) ;
321
+ internalMatches . push ( maker ) ;
310
322
this . emit ( 'ownOrder.filled' , portion ) ;
311
323
await this . persistTrade ( portion . quantity , maker , taker ) ;
312
324
onUpdate && onUpdate ( { type : PlaceOrderEventType . InternalMatch , payload : maker } ) ;
@@ -315,8 +327,7 @@ class OrderBook extends EventEmitter {
315
327
// swaps should only be undefined during integration testing of the order book
316
328
// for now we treat this case like a swap failure
317
329
this . emit ( 'peerOrder.invalidation' , portion ) ;
318
- failedSwapQuantity += portion . quantity ;
319
- continue ;
330
+ return ;
320
331
}
321
332
322
333
try {
@@ -327,43 +338,39 @@ class OrderBook extends EventEmitter {
327
338
// swap was only partially completed
328
339
portion . quantity = swapResult . quantity ;
329
340
const rejectedQuantity = maker . quantity - swapResult . quantity ;
330
- failedSwapQuantity += rejectedQuantity ; // add unswapped quantity to failed quantity
331
341
this . logger . info ( `match partially executed on taker ${ taker . id } and maker ${ maker . id } for ${ swapResult . quantity } ` +
332
342
`with peer ${ maker . peerPubKey } , ${ rejectedQuantity } quantity not accepted and will repeat matching routine` ) ;
343
+ await retryFailedSwap ( rejectedQuantity ) ;
333
344
} else {
334
345
this . logger . info ( `match executed on taker ${ taker . id } and maker ${ maker . id } for ${ maker . quantity } with peer ${ maker . peerPubKey } ` ) ;
335
346
}
336
- result . swapResults . push ( swapResult ) ;
347
+ swapResults . push ( swapResult ) ;
337
348
onUpdate && onUpdate ( { type : PlaceOrderEventType . SwapSuccess , payload : swapResult } ) ;
338
349
} catch ( err ) {
339
- failedSwapQuantity += portion . quantity ;
340
- onUpdate && onUpdate ( { type : PlaceOrderEventType . SwapFailure , payload : maker } ) ;
341
350
this . logger . warn ( `swap for ${ portion . quantity } failed during order matching, will repeat matching routine for failed swap quantity` ) ;
351
+ onUpdate && onUpdate ( { type : PlaceOrderEventType . SwapFailure , payload : maker } ) ;
352
+ await retryFailedSwap ( portion . quantity ) ;
342
353
}
343
354
}
344
- }
345
-
346
- // if we have swap failures, attempt one retry for all available quantity. don't re-add the maker orders
347
- if ( failedSwapQuantity > 0 ) {
348
- this . logger . debug ( `${ failedSwapQuantity } quantity of swaps failed for order ${ order . id } , repeating matching routine for failed quantity` ) ;
349
- // aggregate failures quantities with the remaining order
350
- const remainingOrder : OwnOrder = result . remainingOrder || { ...order , quantity : 0 } ;
351
- remainingOrder . quantity += failedSwapQuantity ;
355
+ } ;
352
356
353
- // invoke placeOrder recursively, append matches/swaps and set the consecutive remaining order
354
- const remainingOrderResult = await this . placeOrder ( remainingOrder , true , onUpdate , maxTime ) ;
355
- result . internalMatches . push ( ...remainingOrderResult . internalMatches ) ;
356
- result . swapResults . push ( ...remainingOrderResult . swapResults ) ;
357
- result . remainingOrder = remainingOrderResult . remainingOrder ;
357
+ // iterate over the matches
358
+ const matchPromises : Promise < void > [ ] = [ ] ;
359
+ for ( const { maker, taker } of matchingResult . matches ) {
360
+ matchPromises . push ( handleMatch ( maker , taker ) ) ;
358
361
}
362
+ await Promise . all ( matchPromises ) ;
359
363
360
- const { remainingOrder } = result ;
361
364
if ( remainingOrder && ! discardRemaining ) {
362
365
this . addOwnOrder ( remainingOrder ) ;
363
366
onUpdate && onUpdate ( { type : PlaceOrderEventType . RemainingOrder , payload : remainingOrder } ) ;
364
367
}
365
368
366
- return result ;
369
+ return {
370
+ internalMatches,
371
+ swapResults,
372
+ remainingOrder,
373
+ } ;
367
374
}
368
375
369
376
/**
0 commit comments