Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uncommitted stateful orders from equity tier check #1569

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to make sure - we still have the onchain equity tier limit check in deliver tx, right?

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
Loading