@@ -508,10 +508,23 @@ export class DLOB {
508508 new BN ( slot )
509509 ) ;
510510
511+ const stepSize = isVariant ( marketType , 'perp' )
512+ ? ( marketAccount as PerpMarketAccount ) . amm . orderStepSize
513+ : ( marketAccount as SpotMarketAccount ) . orderStepSize ;
514+
515+ const cancelReduceOnlyNodesToFill =
516+ this . findUnfillableReduceOnlyOrdersToCancel (
517+ marketIndex ,
518+ marketType ,
519+ stepSize
520+ ) ;
521+
511522 return this . mergeNodesToFill (
512523 restingLimitOrderNodesToFill ,
513524 takingOrderNodesToFill
514- ) . concat ( expiredNodesToFill ) ;
525+ )
526+ . concat ( expiredNodesToFill )
527+ . concat ( cancelReduceOnlyNodesToFill ) ;
515528 }
516529
517530 getMakerRebate (
@@ -1011,6 +1024,52 @@ export class DLOB {
10111024 return nodesToFill ;
10121025 }
10131026
1027+ public findUnfillableReduceOnlyOrdersToCancel (
1028+ marketIndex : number ,
1029+ marketType : MarketType ,
1030+ stepSize : BN
1031+ ) : NodeToFill [ ] {
1032+ const nodesToFill = new Array < NodeToFill > ( ) ;
1033+
1034+ const marketTypeStr = getVariant ( marketType ) as MarketTypeStr ;
1035+ const nodeLists = this . orderLists . get ( marketTypeStr ) . get ( marketIndex ) ;
1036+
1037+ if ( ! nodeLists ) {
1038+ return nodesToFill ;
1039+ }
1040+
1041+ const generators = [
1042+ nodeLists . takingLimit . bid . getGenerator ( ) ,
1043+ nodeLists . restingLimit . bid . getGenerator ( ) ,
1044+ nodeLists . floatingLimit . bid . getGenerator ( ) ,
1045+ nodeLists . market . bid . getGenerator ( ) ,
1046+ nodeLists . signedMsg . bid . getGenerator ( ) ,
1047+ nodeLists . takingLimit . ask . getGenerator ( ) ,
1048+ nodeLists . restingLimit . ask . getGenerator ( ) ,
1049+ nodeLists . floatingLimit . ask . getGenerator ( ) ,
1050+ nodeLists . market . ask . getGenerator ( ) ,
1051+ nodeLists . signedMsg . ask . getGenerator ( ) ,
1052+ nodeLists . trigger . above . getGenerator ( ) ,
1053+ nodeLists . trigger . below . getGenerator ( ) ,
1054+ ] ;
1055+
1056+ for ( const generator of generators ) {
1057+ for ( const node of generator ) {
1058+ if ( ! node . order . reduceOnly ) {
1059+ continue ;
1060+ }
1061+
1062+ if ( node . baseAssetAmount . lt ( stepSize ) ) {
1063+ nodesToFill . push ( {
1064+ node,
1065+ makerNodes : [ ] ,
1066+ } ) ;
1067+ }
1068+ }
1069+ }
1070+
1071+ return nodesToFill ;
1072+ }
10141073 * getTakingBids < T extends MarketType > (
10151074 marketIndex : number ,
10161075 marketType : T ,
0 commit comments