Skip to content

Commit

Permalink
Increase separation of orderbooks in the memclob (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
roy-dydx authored May 28, 2024
1 parent bd91a73 commit bf80d2e
Show file tree
Hide file tree
Showing 33 changed files with 530 additions and 754 deletions.
90 changes: 36 additions & 54 deletions protocol/mocks/MemClob.go

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

18 changes: 0 additions & 18 deletions protocol/mocks/MemClobKeeper.go

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

4 changes: 2 additions & 2 deletions protocol/x/clob/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func TestEndBlocker_Success(t *testing.T) {
err := keepertest.CreateUsdcAsset(ctx, ks.AssetsKeeper)
require.NoError(t, err)

memClob.On("CreateOrderbook", ctx, constants.ClobPair_Btc).Return()
memClob.On("CreateOrderbook", constants.ClobPair_Btc).Return()

// PerpetualMarketCreateEvents are emitted when initializing the genesis state, so we need to mock
// the indexer event manager to expect these events.
Expand Down Expand Up @@ -700,7 +700,7 @@ func TestEndBlocker_Success(t *testing.T) {
constants.ClobPair_Btc.Status,
)
require.NoError(t, err)
memClob.On("CreateOrderbook", ctx, constants.ClobPair_Eth).Return()
memClob.On("CreateOrderbook", constants.ClobPair_Eth).Return()
// PerpetualMarketCreateEvents are emitted when initializing the genesis state, so we need to mock
// the indexer event manager to expect these events.
mockIndexerEventManager.On("AddTxnEvent",
Expand Down
3 changes: 0 additions & 3 deletions protocol/x/clob/e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ func TestHydrationInPreBlocker(t *testing.T) {

// Order should be on the orderbook
_, found = tApp.App.ClobKeeper.MemClob.GetOrder(
ctx,
constants.LongTermOrder_Carl_Num0_Id0_Clob0_Buy1BTC_Price50000_GTBT10.OrderId,
)
require.True(t, found)
Expand Down Expand Up @@ -492,13 +491,11 @@ func TestHydrationWithMatchPreBlocker(t *testing.T) {

// Make sure orders are not on the orderbook.
_, found = tApp.App.ClobKeeper.MemClob.GetOrder(
ctx,
constants.LongTermOrder_Carl_Num0_Id0_Clob0_Buy1BTC_Price50000_GTBT10.OrderId,
)
require.False(t, found)

_, found = tApp.App.ClobKeeper.MemClob.GetOrder(
ctx,
constants.LongTermOrder_Dave_Num0_Id0_Clob0_Sell1BTC_Price50000_GTBT10.OrderId,
)
require.False(t, found)
Expand Down
8 changes: 4 additions & 4 deletions protocol/x/clob/e2e/batch_cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ func TestBatchCancelSingleCancelFunctionality(t *testing.T) {

// Verify expectations
for orderId, shouldHaveOrder := range tc.expectedOrderIdsInMemclob {
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, shouldHaveOrder, exists)
}
for orderId, expectedCancelExpirationBlock := range tc.expectedCancelExpirationsInMemclob {
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(ctx, orderId)
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(orderId)
require.True(t, exists)
require.Equal(t, expectedCancelExpirationBlock, cancelExpirationBlock)
}
Expand Down Expand Up @@ -650,11 +650,11 @@ func TestBatchCancelBatchFunctionality(t *testing.T) {

// Verify expectations
for orderId, shouldHaveOrder := range tc.expectedOrderIdsInMemclob {
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, shouldHaveOrder, exists)
}
for orderId, expectedCancelExpirationBlock := range tc.expectedCancelExpirationsInMemclob {
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(ctx, orderId)
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(orderId)
if expectedCancelExpirationBlock > 0 {
require.True(t, exists)
require.Equal(t, expectedCancelExpirationBlock, cancelExpirationBlock)
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/e2e/long_term_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ func TestPlaceLongTermOrder(t *testing.T) {
}

// Verify orderbook
_, found := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, tc.order.OrderId)
_, found := tApp.App.ClobKeeper.MemClob.GetOrder(tc.order.OrderId)
require.Equal(t, tc.orderShouldRestOnOrderbook, found)

// Verify fill amount
Expand Down Expand Up @@ -1843,7 +1843,7 @@ func TestRegression_InvalidTimeInForce(t *testing.T) {
}

// Verify orderbook
_, found := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, tc.order.OrderId)
_, found := tApp.App.ClobKeeper.MemClob.GetOrder(tc.order.OrderId)
require.Equal(t, tc.orderShouldRestOnOrderbook, found)

// Verify fill amount
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/e2e/reduce_only_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func TestReduceOnlyOrders(t *testing.T) {

// Verify expectations.
for orderId, exists := range tc.expectedOrderOnMemClob {
_, existsOnMemclob := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
_, existsOnMemclob := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, exists, existsOnMemclob)
}

Expand Down
8 changes: 4 additions & 4 deletions protocol/x/clob/e2e/short_term_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ func TestShortTermOrderReplacements(t *testing.T) {
}

for orderId, expectations := range block.orderIdsExpectations {
order, exists := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
order, exists := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, expectations.shouldExistOnMemclob, exists)
if expectations.shouldExistOnMemclob {
require.Equal(t, expectations.expectedOrder, order)
Expand Down Expand Up @@ -1267,11 +1267,11 @@ func TestCancelShortTermOrder(t *testing.T) {

// Verify expectations
for orderId, shouldHaveOrder := range tc.expectedOrderIdsInMemclob {
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, shouldHaveOrder, exists)
}
for orderId, expectedCancelExpirationBlock := range tc.expectedCancelExpirationsInMemclob {
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(ctx, orderId)
cancelExpirationBlock, exists := tApp.App.ClobKeeper.MemClob.GetCancelOrder(orderId)
require.True(t, exists)
require.Equal(t, expectedCancelExpirationBlock, cancelExpirationBlock)
}
Expand Down Expand Up @@ -1842,7 +1842,7 @@ func TestShortTermAdvancedOrders(t *testing.T) {
}

for orderId, shouldHaveOrder := range tc.expectedOrderIdsInMemclob {
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(ctx, orderId)
_, exists := tApp.App.ClobKeeper.MemClob.GetOrder(orderId)
require.Equal(t, shouldHaveOrder, exists)
}

Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func (k Keeper) validateClobPair(ctx sdk.Context, clobPair *types.ClobPair) erro
// maybeCreateOrderbook creates a new orderbook in the memclob.
func (k Keeper) maybeCreateOrderbook(ctx sdk.Context, clobPair types.ClobPair) {
// Create the corresponding orderbook in the memclob.
k.MemClob.MaybeCreateOrderbook(ctx, clobPair)
k.MemClob.MaybeCreateOrderbook(clobPair)
}

// createOrderbook creates a new orderbook in the memclob.
func (k Keeper) createOrderbook(ctx sdk.Context, clobPair types.ClobPair) {
// Create the corresponding orderbook in the memclob.
k.MemClob.CreateOrderbook(ctx, clobPair)
k.MemClob.CreateOrderbook(clobPair)
}

// createClobPair creates a new `ClobPair` in the store and creates the corresponding orderbook in the memclob.
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (k Keeper) GateWithdrawalsIfNegativeTncSubaccountSeen(
)
}
perpetualId := subaccount.PerpetualPositions[0].PerpetualId
k.MemClob.InsertZeroFillDeleveragingIntoOperationsQueue(ctx, negativeTncSubaccountId, perpetualId)
k.MemClob.InsertZeroFillDeleveragingIntoOperationsQueue(negativeTncSubaccountId, perpetualId)
metrics.IncrCountMetricWithLabels(
types.ModuleName,
metrics.SubaccountsNegativeTncSubaccountSeen,
Expand Down
43 changes: 0 additions & 43 deletions protocol/x/clob/keeper/equity_tier_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,6 @@ func (k Keeper) getEquityTierLimitForSubaccount(
return equityTierLimit, nil, nil
}

// Deprecated: Equity tier limits were removed for short term orders.
// See https://github.com/dydxprotocol/v4-chain/pull/1318.
//
// ValidateSubaccountEquityTierLimitForShortTermOrder returns an error if adding the order would exceed the equity
// tier limit on how many short term open orders a subaccount can have. Short-term fill-or-kill and immediate-or-cancel
// orders never rest on the book and will always be allowed as they do not apply to the number of open orders that
// equity tier limits enforce.
func (k Keeper) ValidateSubaccountEquityTierLimitForShortTermOrder(ctx sdk.Context, order types.Order) error {
if order.RequiresImmediateExecution() {
return nil
}

equityTierLimits := k.GetEquityTierLimitConfiguration(ctx).ShortTermOrderEquityTiers
if len(equityTierLimits) == 0 {
return nil
}

equityTierLimit, netCollateral, err := k.getEquityTierLimitForSubaccount(
ctx,
order.GetSubaccountId(),
equityTierLimits,
)
if err != nil {
return err
}

// For short term orders we just count how many orders exist on the memclob.
equityTierCount := k.MemClob.CountSubaccountShortTermOrders(ctx, order.GetSubaccountId())

// Verify that opening this order would not exceed the maximum amount of orders for the equity tier.
if lib.MustConvertIntegerToUint32(equityTierCount) >= equityTierLimit.Limit {
return errorsmod.Wrapf(
types.ErrOrderWouldExceedMaxOpenOrdersEquityTierLimit,
"Opening order would exceed equity tier limit of %d. Order count: %d, total net collateral: %+v, order id: %+v",
equityTierLimit.Limit,
equityTierCount,
netCollateral,
order.GetOrderId(),
)
}
return nil
}

// ValidateSubaccountEquityTierLimitForStatefulOrder returns an error if adding the order would exceed the equity
// tier limit on how many open orders a subaccount can have.
//
Expand Down
Loading

0 comments on commit bf80d2e

Please sign in to comment.