Skip to content

Commit

Permalink
remove collateralization-check for unmatched orders
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanChou committed May 26, 2024
1 parent 5abaea9 commit dd125ee
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 148 deletions.
30 changes: 0 additions & 30 deletions protocol/mocks/MemClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions protocol/testutil/memclob/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,6 @@ func (f *FakeMemClobKeeper) GetStatefulOrdersTimeSlice(
return orderIds
}

func (f *FakeMemClobKeeper) AddOrderToOrderbookSubaccountUpdatesCheck(
ctx sdk.Context,
clobPairId types.ClobPairId,
subaccountOpenOrders map[satypes.SubaccountId][]types.PendingOpenOrder,
) (
success bool,
successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult,
) {
return f.collatCheckFn(subaccountOpenOrders)
}

func (f *FakeMemClobKeeper) addFakePositionSize(
ctx sdk.Context,
clobPairId types.ClobPairId,
Expand Down
87 changes: 0 additions & 87 deletions protocol/x/clob/memclob/memclob.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,40 +637,6 @@ func (m *MemClobPriceTimePriority) PlaceOrder(
return orderSizeOptimisticallyFilledFromMatchingQuantums, orderStatus, offchainUpdates, nil
}

// The taker order has unfilled size which will be added to the orderbook as a maker order.
// Verify the maker order can be added to the orderbook by performing the add-to-orderbook
// subaccount updates check.
addOrderOrderStatus := m.addOrderToOrderbookSubaccountUpdatesCheck(
ctx,
order,
)

// If the add order to orderbook subaccount updates check failed, we cannot add the order to the orderbook.
if !addOrderOrderStatus.IsSuccess() {
if m.generateOffchainUpdates {
// Send an off-chain update message indicating the order should be removed from the orderbook
// on the Indexer.
if message, success := off_chain_updates.CreateOrderRemoveMessage(
ctx,
order.OrderId,
addOrderOrderStatus,
nil,
ocutypes.OrderRemoveV1_ORDER_REMOVAL_STATUS_BEST_EFFORT_CANCELED,
); success {
offchainUpdates.AddRemoveMessage(order.OrderId, message)
}
}

// remove stateful orders which fail collateralization check while being added to orderbook
if order.IsStatefulOrder() && !m.operationsToPropose.IsOrderRemovalInOperationsQueue(order.OrderId) {
m.operationsToPropose.MustAddOrderRemovalToOperationsQueue(
order.OrderId,
types.OrderRemoval_REMOVAL_REASON_UNDERCOLLATERALIZED,
)
}
return orderSizeOptimisticallyFilledFromMatchingQuantums, addOrderOrderStatus, offchainUpdates, nil
}

// If this is a Short-Term order and it's not in the operations queue, add the TX bytes to the
// operations to propose.
if order.IsShortTermOrder() &&
Expand Down Expand Up @@ -1467,59 +1433,6 @@ func (m *MemClobPriceTimePriority) validateNewOrder(
return nil
}

// addOrderToOrderbookSubaccountUpdatesCheck will perform a check to verify that the subaccount updates
// if the new maker order were to be fully filled are valid.
// It returns the result of this subaccount updates check. If the check returns an error, it will return
// the error so that it can be surfaced to the client.
//
// This function will assume that all prior order validation has passed, including the pre-requisite validation of
// `validateNewOrder` and the actual validation performed within `validateNewOrder`.
// Note that this is a loose check, mainly for the purposes of spam mitigation. We perform an additional
// check on the subaccount updates for orders when we attempt to match them.
func (m *MemClobPriceTimePriority) addOrderToOrderbookSubaccountUpdatesCheck(
ctx sdk.Context,
order types.Order,
) types.OrderStatus {
defer telemetry.ModuleMeasureSince(
types.ModuleName,
time.Now(),
metrics.PlaceOrder,
metrics.Memclob,
metrics.AddToOrderbookCollateralizationCheck,
metrics.Latency,
)

orderId := order.OrderId
subaccountId := orderId.SubaccountId

// For the collateralization check, use the remaining amount of the order that is resting on the book.
remainingAmount, hasRemainingAmount := m.GetOrderRemainingAmount(ctx, order)
if !hasRemainingAmount {
panic(fmt.Sprintf("addOrderToOrderbookSubaccountUpdatesCheck: order has no remaining amount %v", order))
}

pendingOpenOrder := types.PendingOpenOrder{
RemainingQuantums: remainingAmount,
IsBuy: order.IsBuy(),
Subticks: order.GetOrderSubticks(),
ClobPairId: order.GetClobPairId(),
}

// Temporarily construct the subaccountOpenOrders with a single PendingOpenOrder.
subaccountOpenOrders := make(map[satypes.SubaccountId][]types.PendingOpenOrder)
subaccountOpenOrders[subaccountId] = []types.PendingOpenOrder{pendingOpenOrder}

// TODO(DEC-1896): AddOrderToOrderbookSubaccountUpdatesCheck should accept a single PendingOpenOrder as a
// parameter rather than the subaccountOpenOrders map.
_, successPerSubaccountUpdate := m.clobKeeper.AddOrderToOrderbookSubaccountUpdatesCheck(
ctx,
order.GetClobPairId(),
subaccountOpenOrders,
)

return updateResultToOrderStatus(successPerSubaccountUpdate[subaccountId])
}

// mustAddOrderToOrderbook will add the order to the resting orderbook.
// This function will assume that all order validation has already been done.
// If `forceToFrontOfLevel` is true, places the order at the head of the level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func TestGetOrderFilledAmount(t *testing.T) {
ClientId: 0,
}

memClobKeeper.On("AddOrderToOrderbookSubaccountUpdatesCheck", mock.Anything, mock.Anything).
Return(true, make(map[satypes.SubaccountId]satypes.UpdateResult))

memClobKeeper.On("GetStatePosition", mock.Anything, mock.Anything, mock.Anything).
Return(big.NewInt(0))

Expand Down
5 changes: 0 additions & 5 deletions protocol/x/clob/memclob/memclob_place_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2822,8 +2822,6 @@ func TestAddOrderToOrderbook_ErrorPlaceNewFullyFilledOrder(t *testing.T) {
memclob.SetClobKeeper(&memClobKeeper)
memclob.CreateOrderbook(ctx, constants.ClobPair_Btc)

memClobKeeper.On("AddOrderToOrderbookSubaccountUpdatesCheck", mock.Anything, mock.Anything, mock.Anything).
Return(true, make(map[satypes.SubaccountId]satypes.UpdateResult))
memClobKeeper.On("GetStatePosition", mock.Anything, mock.Anything, mock.Anything).
Return(big.NewInt(0))
memClobKeeper.On("ValidateSubaccountEquityTierLimitForNewOrder", mock.Anything, mock.Anything).
Expand Down Expand Up @@ -2856,9 +2854,6 @@ func TestAddOrderToOrderbook_PanicsIfFullyFilled(t *testing.T) {
orderId := order.OrderId
quantums := order.GetBaseQuantums()

memClobKeeper.On("AddOrderToOrderbookSubaccountUpdatesCheck", mock.Anything, mock.Anything, mock.Anything).
Return(true, make(map[satypes.SubaccountId]satypes.UpdateResult))

memClobKeeper.On("GetStatePosition", mock.Anything, mock.Anything, mock.Anything).
Return(big.NewInt(0))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ func TestPurgeInvalidMemclobState(t *testing.T) {
satypes.BaseQuantums(0),
uint32(0),
).Times(5)
mockMemClobKeeper.On("AddOrderToOrderbookSubaccountUpdatesCheck", mock.Anything, mock.Anything, mock.Anything).
Return(true, make(map[satypes.SubaccountId]satypes.UpdateResult)).Once()

// Mock out all remaining calls to GetOrderFillAmount, which is called in
// `memclob.PurgeInvalidMemclobState` and during test assertions.
Expand Down
2 changes: 0 additions & 2 deletions protocol/x/clob/memclob/memclob_remove_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ func TestRemoveOrderIfFilled(t *testing.T) {
memclob := NewMemClobPriceTimePriority(false)
memclob.SetClobKeeper(&memClobKeeper)

memClobKeeper.On("AddOrderToOrderbookSubaccountUpdatesCheck", mock.Anything, mock.Anything, mock.Anything).
Return(true, make(map[satypes.SubaccountId]satypes.UpdateResult))
memClobKeeper.On("ValidateSubaccountEquityTierLimitForNewOrder", mock.Anything, mock.Anything).Return(nil)
memClobKeeper.On("SendOrderbookUpdates", mock.Anything, mock.Anything, mock.Anything).Return().Maybe()

Expand Down
8 changes: 0 additions & 8 deletions protocol/x/clob/types/mem_clob_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ type MemClobKeeper interface {
offchainUpdates *OffchainUpdates,
err error,
)
AddOrderToOrderbookSubaccountUpdatesCheck(
ctx sdk.Context,
clobPairId ClobPairId,
subaccountOpenOrders map[satypes.SubaccountId][]PendingOpenOrder,
) (
success bool,
successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult,
)
CanDeleverageSubaccount(
ctx sdk.Context,
subaccountId satypes.SubaccountId,
Expand Down

0 comments on commit dd125ee

Please sign in to comment.