Skip to content

Commit

Permalink
Remove uncommitted stateful orders from equity tier check (#1569)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx authored May 24, 2024
1 parent 76c548c commit 5662a8b
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 144 deletions.
124 changes: 0 additions & 124 deletions protocol/x/clob/e2e/equity_tier_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,130 +26,6 @@ func TestPlaceOrder_EquityTierLimit(t *testing.T) {
expectError bool
crashingAppCheckTxNonDeterminsmChecksDisabled bool
}{
"Long-term order would exceed max open stateful orders in same block": {
allowedOrders: []clobtypes.Order{
testapp.MustScaleOrder(
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_StopLoss20,
testapp.DefaultGenesis(),
),
},
limitedOrder: testapp.MustScaleOrder(
constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15,
testapp.DefaultGenesis(),
),
equityTierLimitConfiguration: clobtypes.EquityTierLimitConfiguration{
StatefulOrderEquityTiers: []clobtypes.EquityTierLimit{
{
UsdTncRequired: dtypes.NewInt(0),
Limit: 0,
},
{
UsdTncRequired: dtypes.NewInt(5_000_000_000), // $5,000
Limit: 1,
},
{
UsdTncRequired: dtypes.NewInt(70_000_000_000), // $70,000
Limit: 100,
},
},
},
expectError: true,
},
"Long-term order would exceed max open stateful orders in same block with multiple orders": {
allowedOrders: []clobtypes.Order{
testapp.MustScaleOrder(
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_StopLoss20,
testapp.DefaultGenesis(),
),
testapp.MustScaleOrder(
constants.ConditionalOrder_Alice_Num0_Id0_Clob1_Buy5_Price10_GTBT15_StopLoss20,
testapp.DefaultGenesis(),
),
},
limitedOrder: testapp.MustScaleOrder(
constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15,
testapp.DefaultGenesis(),
),
equityTierLimitConfiguration: clobtypes.EquityTierLimitConfiguration{
StatefulOrderEquityTiers: []clobtypes.EquityTierLimit{
{
UsdTncRequired: dtypes.NewInt(0),
Limit: 0,
},
{
UsdTncRequired: dtypes.NewInt(5_000_000_000), // $5,000
Limit: 2,
},
{
UsdTncRequired: dtypes.NewInt(70_000_000_000), // $70,000
Limit: 100,
},
},
},
expectError: true,
},
"Conditional order would exceed max open stateful orders in same block": {
allowedOrders: []clobtypes.Order{
testapp.MustScaleOrder(
constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15,
testapp.DefaultGenesis(),
),
},
limitedOrder: testapp.MustScaleOrder(
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_StopLoss20,
testapp.DefaultGenesis(),
),
equityTierLimitConfiguration: clobtypes.EquityTierLimitConfiguration{
StatefulOrderEquityTiers: []clobtypes.EquityTierLimit{
{
UsdTncRequired: dtypes.NewInt(0),
Limit: 0,
},
{
UsdTncRequired: dtypes.NewInt(5_000_000_000), // $5,000
Limit: 1,
},
{
UsdTncRequired: dtypes.NewInt(70_000_000_000), // $70,000
Limit: 100,
},
},
},
expectError: true,
},
"Conditional order would exceed max open stateful orders in same block with multiple orders": {
allowedOrders: []clobtypes.Order{
testapp.MustScaleOrder(
constants.LongTermOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15,
testapp.DefaultGenesis(),
),
testapp.MustScaleOrder(
constants.LongTermOrder_Alice_Num0_Id1_Clob1_Sell65_Price15_GTBT25,
testapp.DefaultGenesis(),
),
},
limitedOrder: testapp.MustScaleOrder(
constants.ConditionalOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTBT15_StopLoss20,
testapp.DefaultGenesis(),
),
equityTierLimitConfiguration: clobtypes.EquityTierLimitConfiguration{
StatefulOrderEquityTiers: []clobtypes.EquityTierLimit{
{
UsdTncRequired: dtypes.NewInt(0),
Limit: 0,
},
{
UsdTncRequired: dtypes.NewInt(5_000_000_000), // $5,000
Limit: 2,
},
{
UsdTncRequired: dtypes.NewInt(70_000_000_000), // $70,000
Limit: 100,
},
},
},
expectError: true,
},
"Long-term order would exceed max open stateful orders across blocks": {
allowedOrders: []clobtypes.Order{
testapp.MustScaleOrder(
Expand Down
20 changes: 0 additions & 20 deletions protocol/x/clob/keeper/equity_tier_limit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -145,7 +144,6 @@ func (k Keeper) ValidateSubaccountEquityTierLimitForShortTermOrder(ctx sdk.Conte
// stateful orders. For `deliverState`, we sum:
// - the number of long term orders.
// - the number of conditional orders.
// - the number of uncommitted stateful orders in the mempool (checkState only)
func (k Keeper) ValidateSubaccountEquityTierLimitForStatefulOrder(ctx sdk.Context, order types.Order) error {
equityTierLimits := k.GetEquityTierLimitConfiguration(ctx).StatefulOrderEquityTiers
if len(equityTierLimits) == 0 {
Expand All @@ -162,24 +160,6 @@ func (k Keeper) ValidateSubaccountEquityTierLimitForStatefulOrder(ctx sdk.Contex
}

equityTierCount := k.GetStatefulOrderCount(ctx, order.OrderId.SubaccountId)
// If this is `CheckTx` then we must also add the number of uncommitted stateful orders that this validator
// is aware of (orders that are part of the mempool but have yet to proposed in a block).
if !lib.IsDeliverTxMode(ctx) {
equityTierCountMaybeNegative := k.GetUncommittedStatefulOrderCount(ctx, order.OrderId) + int32(equityTierCount)
if equityTierCountMaybeNegative < 0 {
panic(
fmt.Errorf(
"Expected ValidateSubaccountEquityTierLimitForStatefulOrder for new order %+v to be >= 0. "+
"equityTierCount %d, statefulOrderCount %d, uncommittedStatefulOrderCount %d.",
order,
equityTierCountMaybeNegative,
k.GetStatefulOrderCount(ctx, order.OrderId.SubaccountId),
k.GetUncommittedStatefulOrderCount(ctx, order.OrderId),
),
)
}
equityTierCount = uint32(equityTierCountMaybeNegative)
}

// Verify that opening this order would not exceed the maximum amount of orders for the equity tier.
if lib.MustConvertIntegerToUint32(equityTierCount) >= equityTierLimit.Limit {
Expand Down

0 comments on commit 5662a8b

Please sign in to comment.