Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLOB-1017] final settlement deleveraging step in PrepareCheckState (#…
Browse files Browse the repository at this point in the history
…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
jakob-dydx authored Dec 20, 2023
1 parent c70b5e9 commit 5dd3bae
Showing 25 changed files with 1,164 additions and 285 deletions.
14 changes: 8 additions & 6 deletions protocol/indexer/events/deleveraging.go
Original file line number Diff line number Diff line change
@@ -14,15 +14,17 @@ func NewDeleveragingEvent(
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"
@@ -26,6 +27,7 @@ func TestNewDeleveragingEvent_Success(t *testing.T) {
fillAmount,
price,
isBuy,
false,
)
indexerLiquidatedSubaccountId := v1.SubaccountIdToIndexerSubaccountId(liquidatedSubaccountId)
indexerOffsettingSubaccountId := v1.SubaccountIdToIndexerSubaccountId(offsettingSubaccountId)
213 changes: 107 additions & 106 deletions protocol/lib/metrics/constants.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions protocol/lib/metrics/metric_keys.go
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ const (
ClobDeleveragingNonOverlappingBankrupcyPricesCount = "clob_deleveraging_non_overlapping_bankruptcy_prices_count"
ClobDeleveragingNoOpenPositionOnOppositeSideCount = "clob_deleveraging_no_open_position_on_opposite_side_count"
ClobDeleverageSubaccountFilledQuoteQuantums = "clob_deleverage_subaccount_filled_quote_quantums"
ClobSubaccountsWithFinalSettlementPositionsCount = "clob_subaccounts_with_final_settlement_positions_count"
LiquidationsLiquidatableSubaccountIdsCount = "liquidations_liquidatable_subaccount_ids_count"
LiquidationsPercentFilledDistribution = "liquidations_percent_filled_distribution"
LiquidationsPlacePerpetualLiquidationQuoteQuantumsDistribution = "liquidations_place_perpetual_liquidation_quote_quantums_distribution"
83 changes: 58 additions & 25 deletions protocol/mocks/MemClob.go

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

Loading

0 comments on commit 5dd3bae

Please sign in to comment.