Skip to content

Commit

Permalink
[CLOB-1009] - Allow FINAL_SETTLEMENT, and cancel open stateful orders (
Browse files Browse the repository at this point in the history
…#829)

* add protos for final settlement clob pair status and final settlement removal reason

* implement cancellation of open stateful orders for final settlement clob pairs upon acceptance of gov proposal

* format

* pr nits

* pr nits

* [CLOB-1010] final settlement checktx validation (#842)

* checktx logic to reject order placements for final settlement markets

* fix test with incorrect order

* [CLOB-1017]  final settlement deleveraging step in PrepareCheckState (#848)

* add functionality in ProcessDeleveraging to allow settlement at oracle price instead of bankruptcy price

* pull deltaQuoteQuantums out of ProcessDeleveraging

* fix lint

* fix issue with test

* pr nits

* update getDeleveragingQuoteQuantumsDelta helper

* initial implementation of final settlement deleveraging

* add delivertx logic for deleveraging match

* update to have DeliverTx calculate price based off of IsFinalSettlement flag instead of calculating implicitly based off of clob pair status, and update indexer deleveraging event so we can send isFinalSettlement flag

* update delivertx validation

* allow final settlement subaccounts to be deleveraged by regular deleveraging flow

* remove superfluous comment

* update err name

* fix import formatting

* lint

* begin adding tests

* update logic so that isFinalSettlement flag on operation is used

* re-use helper function

* pr nits, redefine CanDeleverageSubaccount

* update tests for CanDeleverageSubaccount

* split up PCS steps 6 and 7 to be for liquidations and then for deleveraging

* update to use new subaccountOpenPositionInfo type

* update tests

* pr nits

* lint

* formatting

* nits and tests

* more nits and comment updates

* [CLOB-1021] final settlement DeliverTx validation to block trading (#834)

* add missing indexer constants

* update proto formatting

* fix indexer test

* set up ground work for allowing only deleveraging events for final settlement markets

* test trading is blocked in process proposer operations for final settlement

* update comments

* add DeliverTx tests for process operations final settlement deleveraging operations

* pr nits

* nit

* merge from upstream

* format

* update test to verify transitions from a status to itself is accepted

* fix defer statement with latency metric

* pr nits
  • Loading branch information
jakob-dydx authored Dec 22, 2023
1 parent b1ac285 commit 781153c
Show file tree
Hide file tree
Showing 32 changed files with 1,488 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions proto/dydxprotocol/indexer/protocol/v1/clob.proto
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +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.
// 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;
}
18 changes: 12 additions & 6 deletions protocol/indexer/events/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ 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,
perpetualId uint32,
fillAmount satypes.BaseQuantums,
price satypes.BaseQuantums,
isBuy bool,
isFinalSettlement bool,
) *DeleveragingEventV1 {
indexerLiquidatedSubaccountId := v1.SubaccountIdToIndexerSubaccountId(liquidatedSubaccountId)
indexerOffsettingSubaccountId := v1.SubaccountIdToIndexerSubaccountId(offsettingSubaccountId)
return &DeleveragingEventV1{
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
Price: price.ToUint64(),
IsBuy: isBuy,
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
Price: price.ToUint64(),
IsBuy: isBuy,
IsFinalSettlement: isFinalSettlement,
}
}
4 changes: 3 additions & 1 deletion protocol/indexer/events/deleveraging_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package events_test

import (
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
"testing"

satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"

"github.com/dydxprotocol/v4-chain/protocol/indexer/events"
v1 "github.com/dydxprotocol/v4-chain/protocol/indexer/protocol/v1"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
Expand All @@ -26,6 +27,7 @@ func TestNewDeleveragingEvent_Success(t *testing.T) {
fillAmount,
price,
isBuy,
false,
)
indexerLiquidatedSubaccountId := v1.SubaccountIdToIndexerSubaccountId(liquidatedSubaccountId)
indexerOffsettingSubaccountId := v1.SubaccountIdToIndexerSubaccountId(offsettingSubaccountId)
Expand Down
5 changes: 3 additions & 2 deletions protocol/indexer/protocol/v1/clob.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 781153c

Please sign in to comment.