From b2aa6fca7ec4b29796c694a2a9fa3f1eadce7cfc Mon Sep 17 00:00:00 2001 From: Jakob Herlitz Date: Fri, 22 Dec 2023 10:38:18 -0800 Subject: [PATCH] pr nits --- protocol/indexer/events/deleveraging.go | 4 ++++ protocol/x/clob/keeper/clob_pair.go | 2 +- protocol/x/clob/keeper/deleveraging.go | 13 +++++++------ protocol/x/clob/keeper/final_settlement.go | 4 ++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/protocol/indexer/events/deleveraging.go b/protocol/indexer/events/deleveraging.go index a7a9b4feed..b846e42001 100644 --- a/protocol/indexer/events/deleveraging.go +++ b/protocol/indexer/events/deleveraging.go @@ -7,6 +7,10 @@ import ( // NewDeleveragingEvent creates a DeleveragingEvent representing a deleveraging // where a liquidated subaccount's position is offset by another subaccount. +// Due to the support of final settlement deleveraging matches, sometimes the +// liquidatedSubaccountId is not actually an account that is liquidatable. More +// specifically, it may be a well-collateralized subaccount with an open position +// in a market with the final settlement status. func NewDeleveragingEvent( liquidatedSubaccountId satypes.SubaccountId, offsettingSubaccountId satypes.SubaccountId, diff --git a/protocol/x/clob/keeper/clob_pair.go b/protocol/x/clob/keeper/clob_pair.go index ff9fc7b936..4af9d4a436 100644 --- a/protocol/x/clob/keeper/clob_pair.go +++ b/protocol/x/clob/keeper/clob_pair.go @@ -639,7 +639,7 @@ func (k Keeper) validateInternalOperationAgainstClobPairStatus( ) case types.ClobPair_STATUS_FINAL_SETTLEMENT: // Only allow deleveraging events. This allows the protocol to close out open - // positions in the market. All trading is blocked. + // positions in the market. All other operations are not allowed. if match := internalOperation.GetMatch(); match != nil && match.GetMatchPerpetualDeleveraging() != nil { return nil } diff --git a/protocol/x/clob/keeper/deleveraging.go b/protocol/x/clob/keeper/deleveraging.go index 8d402a61a5..f1b0e4b1c1 100644 --- a/protocol/x/clob/keeper/deleveraging.go +++ b/protocol/x/clob/keeper/deleveraging.go @@ -42,7 +42,8 @@ func (k Keeper) MaybeDeleverageSubaccount( return new(big.Int), err } - // Early return to skip deleveraging if the subaccount can't be deleveraged. + // Early return to skip deleveraging if the subaccount doesn't have negative equity or a position in a final + // settlement market. if !shouldDeleverageAtBankruptcyPrice && !shouldDeleverageAtOraclePrice { metrics.IncrCounter( metrics.ClobPrepareCheckStateCannotDeleverageSubaccount, @@ -168,17 +169,17 @@ func (k Keeper) CanDeleverageSubaccount( return false, false, err } + // Negative TNC, deleverage at bankruptcy price. + if bigNetCollateral.Sign() == -1 { + return true, false, nil + } + clobPairId, err := k.GetClobPairIdForPerpetual(ctx, perpetualId) if err != nil { return false, false, err } clobPair := k.mustGetClobPair(ctx, clobPairId) - // Negative TNC, deleverage at bankruptcy price. - if bigNetCollateral.Sign() == -1 { - return true, false, nil - } - // Non-negative TNC, deleverage at oracle price if market is in final settlement. Deleveraging at oracle price // is always a valid state transition when TNC is non-negative. This is because the TNC/TMMR ratio is improving; // TNC is staying constant while TMMR is decreasing. diff --git a/protocol/x/clob/keeper/final_settlement.go b/protocol/x/clob/keeper/final_settlement.go index 0ccf7b6b9e..4e1468ff73 100644 --- a/protocol/x/clob/keeper/final_settlement.go +++ b/protocol/x/clob/keeper/final_settlement.go @@ -27,7 +27,7 @@ func (k Keeper) mustCancelStatefulOrdersForFinalSettlement(ctx sdk.Context, clob // This logic is executed in EndBlocker and should not panic. This would be unexpected, // but if it happens we would rather recover and continue if an order fails to be removed from state // rather than halt the chain. - safelyRemoveStatefulOrder := func(ctx sdk.Context, orderId types.OrderId) { + removeStatefulOrderWithoutPanicing := func(ctx sdk.Context, orderId types.OrderId) { defer func() { if r := recover(); r != nil { k.Logger(ctx).Error( @@ -47,7 +47,7 @@ func (k Keeper) mustCancelStatefulOrdersForFinalSettlement(ctx sdk.Context, clob } // Remove from state, recovering from panic if necessary - safelyRemoveStatefulOrder(ctx, order.OrderId) + removeStatefulOrderWithoutPanicing(ctx, order.OrderId) // Append to RemovedStatefulOrderIds so this order gets removed // from the memclob in PrepareCheckState during the PurgeInvalidMemclobState step