diff --git a/indexer/packages/v4-protos/src/codegen/dydxprotocol/indexer/protocol/v1/clob.ts b/indexer/packages/v4-protos/src/codegen/dydxprotocol/indexer/protocol/v1/clob.ts index 92995bc19d..09c933e0d9 100644 --- a/indexer/packages/v4-protos/src/codegen/dydxprotocol/indexer/protocol/v1/clob.ts +++ b/indexer/packages/v4-protos/src/codegen/dydxprotocol/indexer/protocol/v1/clob.ts @@ -311,8 +311,9 @@ export enum ClobPairStatus { /** * CLOB_PAIR_STATUS_FINAL_SETTLEMENT - CLOB_PAIR_STATUS_FINAL_SETTLEMENT represents a clob pair that has been * deactivated. Clob pairs in this state do not accept new orders and trading - * is blocked. All open positions are closed by the protocol when the clob - * pair gains this status. + * is blocked. All open positions are closed and open stateful orders canceled + * by the protocol when the clob pair transitions to this status. All + * short-term orders are left to expire. */ CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6, UNRECOGNIZED = -1, @@ -360,8 +361,9 @@ export enum ClobPairStatusSDKType { /** * CLOB_PAIR_STATUS_FINAL_SETTLEMENT - CLOB_PAIR_STATUS_FINAL_SETTLEMENT represents a clob pair that has been * deactivated. Clob pairs in this state do not accept new orders and trading - * is blocked. All open positions are closed by the protocol when the clob - * pair gains this status. + * is blocked. All open positions are closed and open stateful orders canceled + * by the protocol when the clob pair transitions to this status. All + * short-term orders are left to expire. */ CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6, UNRECOGNIZED = -1, diff --git a/proto/dydxprotocol/indexer/protocol/v1/clob.proto b/proto/dydxprotocol/indexer/protocol/v1/clob.proto index 60fd2b0367..b061dae39b 100644 --- a/proto/dydxprotocol/indexer/protocol/v1/clob.proto +++ b/proto/dydxprotocol/indexer/protocol/v1/clob.proto @@ -175,8 +175,8 @@ enum ClobPairStatus { CLOB_PAIR_STATUS_INITIALIZING = 5; // CLOB_PAIR_STATUS_FINAL_SETTLEMENT represents a clob pair that has been // deactivated. Clob pairs in this state do not accept new orders and trading - // is blocked. All open positions are closed by the protocol when the clob - // pair gains this status. All open stateful orders are closed and short-term - // orders are left to expire. + // is blocked. All open positions are closed and open stateful orders canceled + // by the protocol when the clob pair transitions to this status. All + // short-term orders are left to expire. CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6; } diff --git a/protocol/indexer/protocol/v1/clob.pb.go b/protocol/indexer/protocol/v1/clob.pb.go index a512c08e4c..1c4958743c 100644 --- a/protocol/indexer/protocol/v1/clob.pb.go +++ b/protocol/indexer/protocol/v1/clob.pb.go @@ -49,9 +49,9 @@ const ( ClobPairStatus_CLOB_PAIR_STATUS_INITIALIZING ClobPairStatus = 5 // CLOB_PAIR_STATUS_FINAL_SETTLEMENT represents a clob pair that has been // deactivated. Clob pairs in this state do not accept new orders and trading - // is blocked. All open positions are closed by the protocol when the clob - // pair gains this status. All open stateful orders are closed and short-term - // orders are left to expire. + // is blocked. All open positions are closed and open stateful orders canceled + // by the protocol when the clob pair transitions to this status. All + // short-term orders are left to expire. ClobPairStatus_CLOB_PAIR_STATUS_FINAL_SETTLEMENT ClobPairStatus = 6 ) diff --git a/protocol/x/clob/keeper/clob_pair.go b/protocol/x/clob/keeper/clob_pair.go index 2541a6d5f4..d9b1457f06 100644 --- a/protocol/x/clob/keeper/clob_pair.go +++ b/protocol/x/clob/keeper/clob_pair.go @@ -501,7 +501,7 @@ func (k Keeper) UpdateClobPair( // If newly transitioning to final settlement, enter final settlement. if newStatus == types.ClobPair_STATUS_FINAL_SETTLEMENT && oldStatus != newStatus { - k.mustEnterFinalSettlement(ctx, clobPair.GetClobPairId()) + k.mustTransitionToFinalSettlement(ctx, clobPair.GetClobPairId()) } return nil diff --git a/protocol/x/clob/keeper/final_settlement.go b/protocol/x/clob/keeper/final_settlement.go index 03f1145e8b..0ccf7b6b9e 100644 --- a/protocol/x/clob/keeper/final_settlement.go +++ b/protocol/x/clob/keeper/final_settlement.go @@ -8,13 +8,13 @@ import ( "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) -// MustEnterFinalSettlement holds logic executed when a market transitions to FINAL_SETTLEMENT status. +// mustTransitionToFinalSettlement holds logic executed when a market transitions to FINAL_SETTLEMENT status. // This function will forcefully cancel all stateful open orders for the clob pair. -func (k Keeper) mustEnterFinalSettlement(ctx sdk.Context, clobPairId types.ClobPairId) { - // Forcefully cancel all stateful orders from state for this clob pair +func (k Keeper) mustTransitionToFinalSettlement(ctx sdk.Context, clobPairId types.ClobPairId) { + // Forcefully cancel all stateful orders from state for this clob pair. k.mustCancelStatefulOrdersForFinalSettlement(ctx, clobPairId) - // Delete untriggered conditional orders for this clob pair from memory + // Delete untriggered conditional orders for this clob pair from memory. delete(k.UntriggeredConditionalOrders, clobPairId) } @@ -42,29 +42,31 @@ func (k Keeper) mustCancelStatefulOrdersForFinalSettlement(ctx sdk.Context, clob // TODO(CLOB-1053): Iterate over stateful orders for only specified clob pair for _, order := range statefulOrders { - if order.GetClobPairId() == clobPairId { - // Remove from state, recovering from panic if necessary - safelyRemoveStatefulOrder(ctx, order.OrderId) + if order.GetClobPairId() != clobPairId { + continue + } + + // Remove from state, recovering from panic if necessary + safelyRemoveStatefulOrder(ctx, order.OrderId) - // Append to RemovedStatefulOrderIds so this order gets removed - // from the memclob in PrepareCheckState during the PurgeInvalidMemclobState step - processProposerMatchesEvents.RemovedStatefulOrderIds = append( - processProposerMatchesEvents.RemovedStatefulOrderIds, - order.OrderId, - ) + // Append to RemovedStatefulOrderIds so this order gets removed + // from the memclob in PrepareCheckState during the PurgeInvalidMemclobState step + processProposerMatchesEvents.RemovedStatefulOrderIds = append( + processProposerMatchesEvents.RemovedStatefulOrderIds, + order.OrderId, + ) - k.GetIndexerEventManager().AddTxnEvent( - ctx, - indexerevents.SubtypeStatefulOrder, - indexerevents.StatefulOrderEventVersion, - indexer_manager.GetBytes( - indexerevents.NewStatefulOrderRemovalEvent( - order.OrderId, - indexershared.OrderRemovalReason_ORDER_REMOVAL_REASON_FINAL_SETTLEMENT, - ), + k.GetIndexerEventManager().AddTxnEvent( + ctx, + indexerevents.SubtypeStatefulOrder, + indexerevents.StatefulOrderEventVersion, + indexer_manager.GetBytes( + indexerevents.NewStatefulOrderRemovalEvent( + order.OrderId, + indexershared.OrderRemovalReason_ORDER_REMOVAL_REASON_FINAL_SETTLEMENT, ), - ) - } + ), + ) } k.MustSetProcessProposerMatchesEvents(ctx, processProposerMatchesEvents)