Skip to content

Commit

Permalink
Post only order breaks out of matching loop + Add post only crosses m…
Browse files Browse the repository at this point in the history
…aker order as first class order status field. (dydxprotocol#1996)
  • Loading branch information
jonfung-dydx committed Aug 1, 2024
1 parent a7f676a commit 1c7f0a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 12 additions & 5 deletions protocol/x/clob/memclob/memclob.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,9 @@ func (m *MemClobPriceTimePriority) matchOrder(

var matchingErr error

// If the order is post only and it's not the rewind step, then it cannot be filled.
// If the order is post only and crosses the book,
// Set the matching error so that the order is canceled.
// TODO(DEC-998): Determine if allowing post-only orders to match in rewind step is valid.
if len(newMakerFills) > 0 &&
!order.IsLiquidation() &&
order.MustGetOrder().TimeInForce == types.Order_TIME_IN_FORCE_POST_ONLY {
if !order.IsLiquidation() && takerOrderStatus.OrderStatus == types.PostOnlyWouldCrossMakerOrder {
matchingErr = types.ErrPostOnlyWouldCrossMakerOrder
}

Expand Down Expand Up @@ -1757,6 +1754,16 @@ func (m *MemClobPriceTimePriority) mustPerformTakerOrderMatching(
continue
}

// If a valid match has been generated but the taker order is a post only order,
// end the matching loop. Because of this, post-only orders can cause
// undercollateralized maker orders to be removed from the book up to the first valid match.
if takerOrderCrossesMakerOrder &&
!newTakerOrder.IsLiquidation() &&
newTakerOrder.MustGetOrder().TimeInForce == types.Order_TIME_IN_FORCE_POST_ONLY {
takerOrderStatus.OrderStatus = types.PostOnlyWouldCrossMakerOrder
break
}

// The orders have matched successfully, and the state has been updated.
// To mark the orders as matched, perform the following actions:
// 1. Deduct `matchedAmount` from the taker order's remaining quantums, and add the matched
Expand Down
7 changes: 2 additions & 5 deletions protocol/x/clob/memclob/memclob_place_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2834,17 +2834,14 @@ func TestPlaceOrder_PostOnly(t *testing.T) {
},
},
expectedRemainingAsks: []OrderWithRemainingSize{},
// Second order is not collat check'd since the first order generates a valid
// match, so the matching loop ends.
expectedCollatCheck: []expectedMatch{
{
makerOrder: &constants.Order_Bob_Num0_Id11_Clob1_Buy5_Price40_GTB32,
takerOrder: &constants.Order_Alice_Num1_Id1_Clob1_Sell10_Price15_GTB20_PO,
matchedQuantums: 5,
},
{
makerOrder: &constants.Order_Bob_Num0_Id4_Clob1_Buy20_Price35_GTB22,
takerOrder: &constants.Order_Alice_Num1_Id1_Clob1_Sell10_Price15_GTB20_PO,
matchedQuantums: 5,
},
},
expectedExistingMatches: []expectedMatch{},
expectedOperations: []types.Operation{},
Expand Down
3 changes: 3 additions & 0 deletions protocol/x/clob/types/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ const (
// with either multiple positions in isolated perpetuals or both an isolated and a cross perpetual
// position.
ViolatesIsolatedSubaccountConstraints
// PostOnlyWouldCrossMakerOrder indicates that matching the post only taker order would cross the
// orderbook, and was therefore canceled.
PostOnlyWouldCrossMakerOrder
)

// String returns a string representation of this `OrderStatus` enum.
Expand Down

0 comments on commit 1c7f0a2

Please sign in to comment.