Skip to content

Commit

Permalink
pr nits
Browse files Browse the repository at this point in the history
  • Loading branch information
jakob-dydx committed Dec 22, 2023
1 parent dc58646 commit b2aa6fc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions protocol/indexer/events/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 7 additions & 6 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/final_settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit b2aa6fc

Please sign in to comment.