Skip to content

Commit

Permalink
Disallow order removal reason fully filled #776
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfung-dydx authored Nov 20, 2023
1 parent 8487af8 commit 2ebe335
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion protocol/lib/metrics/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const (
Status = "status"
SubaccountPendingMatches = "subaccount_pending_matches"
TimeInForce = "time_in_force"
TotalOrdersClobPair = "total_orders_in_clob"
TotalOrdersInClob = "total_orders_in_clob"
TotalQuoteQuantums = "total_quote_quantums"
Unfilled = "unfilled"
UnfilledLiquidationOrders = "unfilled_liquidation_orders"
Expand Down
2 changes: 1 addition & 1 deletion protocol/testutil/constants/clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var (
QuantumConversionExponent: -8,
Status: clobtypes.ClobPair_STATUS_ACTIVE,
}
ClobPair_Btc_Init = clobtypes.ClobPair{
ClobPair_Btc_Initializing = clobtypes.ClobPair{
Id: 0,
Metadata: &clobtypes.ClobPair_PerpetualClobMetadata{
PerpetualClobMetadata: &clobtypes.PerpetualClobMetadata{
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/clob_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ func TestIsPerpetualClobPairActive(t *testing.T) {
perpetualIdToClobPairId: map[uint32][]types.ClobPairId{
0: {types.ClobPairId(0)},
},
clobPair: &constants.ClobPair_Btc_Init,
clobPair: &constants.ClobPair_Btc_Initializing,
resp: false,
},
"Succeeds when clob pair is active": {
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/get_price_premium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestGetPricePremiumForPerpetual(t *testing.T) {
"Success, premium is zeroed for initializing clob pair": {
perpetualId: 0,
args: testMemClobMethodArgs{
clobPair: constants.ClobPair_Btc_Init,
clobPair: constants.ClobPair_Btc_Initializing,
},
setUpMockMemClob: func(mck *mocks.MemClob, args testMemClobMethodArgs) {},
},
Expand Down
18 changes: 8 additions & 10 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func (k Keeper) PersistOrderRemovalToState(
ctx sdk.Context,
orderRemoval types.OrderRemoval,
) error {
lib.AssertDeliverTxMode(ctx)
orderIdToRemove := orderRemoval.GetOrderId()
orderIdToRemove.MustBeStatefulOrder()

Expand Down Expand Up @@ -356,16 +357,13 @@ func (k Keeper) PersistOrderRemovalToState(
)
}
case types.OrderRemoval_REMOVAL_REASON_FULLY_FILLED:
// The order should be fully filled.
remainingAmount, hasRemainingAmount := k.MemClob.GetOrderRemainingAmount(ctx, orderToRemove)
if hasRemainingAmount {
return errorsmod.Wrapf(
types.ErrOrderHasRemainingSize,
"Fill amount (%+v) and total size (%+v).",
remainingAmount,
orderToRemove.GetBaseQuantums(),
)
}
// Order removal reason fully filled is only used within indexer services.
// Fully filled orders are removed from the protocol after persisting the operations queue
// to state, instead of through the operations queue.
return errorsmod.Wrapf(
types.ErrInvalidOrderRemovalReason,
"Order removal reason fully filled should not be part of the operations queue.",
)
// TODO - uncomment when reduce only orders are enabled. Order Removals of this type will fail ValidateBasic.
// case types.OrderRemoval_REMOVAL_REASON_INVALID_REDUCE_ONLY:
// if !orderToRemove.IsReduceOnly() {
Expand Down
25 changes: 22 additions & 3 deletions protocol/x/clob/keeper/process_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ func TestProcessProposerOperations(t *testing.T) {
},
perpetualFeeParams: &constants.PerpetualFeeParams,
clobPairs: []types.ClobPair{
constants.ClobPair_Btc_Init,
constants.ClobPair_Btc_Initializing,
},
rawOperations: []types.OperationRaw{
clobtest.NewMatchOperationRaw(
Expand All @@ -1379,7 +1379,7 @@ func TestProcessProposerOperations(t *testing.T) {
},
perpetualFeeParams: &constants.PerpetualFeeParams,
clobPairs: []types.ClobPair{
constants.ClobPair_Btc_Init,
constants.ClobPair_Btc_Initializing,
},
rawOperations: []types.OperationRaw{
clobtest.NewShortTermOrderPlacementOperationRaw(
Expand All @@ -1394,7 +1394,7 @@ func TestProcessProposerOperations(t *testing.T) {
},
perpetualFeeParams: &constants.PerpetualFeeParams,
clobPairs: []types.ClobPair{
constants.ClobPair_Btc_Init,
constants.ClobPair_Btc_Initializing,
},
preExistingStatefulOrders: []types.Order{
constants.LongTermOrder_Bob_Num0_Id0_Clob0_Buy25_Price30_GTBT10,
Expand All @@ -1407,6 +1407,25 @@ func TestProcessProposerOperations(t *testing.T) {
},
expectedError: types.ErrOperationConflictsWithClobPairStatus,
},
"Fails with order removal reason fully filled": {
perpetuals: []*perptypes.Perpetual{
&constants.BtcUsd_100PercentMarginRequirement,
},
perpetualFeeParams: &constants.PerpetualFeeParams,
clobPairs: []types.ClobPair{
constants.ClobPair_Btc,
},
preExistingStatefulOrders: []types.Order{
constants.LongTermOrder_Bob_Num0_Id0_Clob0_Buy25_Price30_GTBT10,
},
rawOperations: []types.OperationRaw{
clobtest.NewOrderRemovalOperationRaw(
constants.LongTermOrder_Bob_Num0_Id0_Clob0_Buy25_Price30_GTBT10.OrderId,
types.OrderRemoval_REMOVAL_REASON_FULLY_FILLED,
),
},
expectedError: types.ErrInvalidOrderRemoval,
},
}

for name, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/memclob/memclob.go
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ func (m *MemClobPriceTimePriority) SetMemclobGauges(
telemetry.SetGaugeWithLabels(
[]string{
types.ModuleName,
metrics.TotalOrdersClobPair,
metrics.TotalOrdersInClob,
},
float32(orderbook.TotalOpenOrders),
[]gometrics.Label{
Expand Down
5 changes: 4 additions & 1 deletion protocol/x/clob/types/message_proposed_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func ValidateAndTransformRawOperations(
if err := orderRemoval.OrderId.Validate(); err != nil {
return nil, err
}
if orderRemoval.RemovalReason == OrderRemoval_REMOVAL_REASON_UNSPECIFIED {
// Order removal reason fully filled is only used by indexer and should not be
// placed in the operations queue.
if orderRemoval.RemovalReason == OrderRemoval_REMOVAL_REASON_UNSPECIFIED ||
orderRemoval.RemovalReason == OrderRemoval_REMOVAL_REASON_FULLY_FILLED {
return nil, errorsmod.Wrapf(
ErrInvalidOrderRemoval,
"Invalid order removal reason: %+v",
Expand Down

0 comments on commit 2ebe335

Please sign in to comment.