Skip to content

Commit

Permalink
Merge branch 'main' into jakob-dydx/oracle-price-deleveraging
Browse files Browse the repository at this point in the history
  • Loading branch information
jakob-dydx committed Dec 5, 2023
2 parents 64598c8 + 11fd8a0 commit 81bd4b7
Show file tree
Hide file tree
Showing 32 changed files with 805 additions and 419 deletions.
1 change: 1 addition & 0 deletions indexer/packages/postgres/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Record<SpecifiedClobPairStatus, PerpetualMarketStatus> = {
[ClobPairStatus.CLOB_PAIR_STATUS_PAUSED]: PerpetualMarketStatus.PAUSED,
[ClobPairStatus.CLOB_PAIR_STATUS_POST_ONLY]: PerpetualMarketStatus.POST_ONLY,
[ClobPairStatus.CLOB_PAIR_STATUS_INITIALIZING]: PerpetualMarketStatus.INITIALIZING,
[ClobPairStatus.CLOB_PAIR_STATUS_FINAL_SETTLEMENT]: PerpetualMarketStatus.FINAL_SETTLEMENT,
};

export const DEFAULT_POSTGRES_OPTIONS : Options = config.USE_READ_REPLICA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ export enum PerpetualMarketStatus {
CANCEL_ONLY = 'CANCEL_ONLY',
POST_ONLY = 'POST_ONLY',
INITIALIZING = 'INITIALIZING',
FINAL_SETTLEMENT = 'FINAL_SETTLEMENT',
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export enum ClobPair_Status {
* both short-term and post-only.
*/
STATUS_INITIALIZING = 5,

/**
* STATUS_FINAL_SETTLEMENT - STATUS_FINAL_SETTLEMENT represents a clob pair which is deactivated
* and trading has ceased. All open positions will be closed by the
* protocol. Open stateful orders will be cancelled. Open short-term
* orders will be left to expire.
*/
STATUS_FINAL_SETTLEMENT = 6,
UNRECOGNIZED = -1,
}
/** Status of the CLOB. */
Expand Down Expand Up @@ -68,6 +76,14 @@ export enum ClobPair_StatusSDKType {
* both short-term and post-only.
*/
STATUS_INITIALIZING = 5,

/**
* STATUS_FINAL_SETTLEMENT - STATUS_FINAL_SETTLEMENT represents a clob pair which is deactivated
* and trading has ceased. All open positions will be closed by the
* protocol. Open stateful orders will be cancelled. Open short-term
* orders will be left to expire.
*/
STATUS_FINAL_SETTLEMENT = 6,
UNRECOGNIZED = -1,
}
export function clobPair_StatusFromJSON(object: any): ClobPair_Status {
Expand Down Expand Up @@ -96,6 +112,10 @@ export function clobPair_StatusFromJSON(object: any): ClobPair_Status {
case "STATUS_INITIALIZING":
return ClobPair_Status.STATUS_INITIALIZING;

case 6:
case "STATUS_FINAL_SETTLEMENT":
return ClobPair_Status.STATUS_FINAL_SETTLEMENT;

case -1:
case "UNRECOGNIZED":
default:
Expand All @@ -122,6 +142,9 @@ export function clobPair_StatusToJSON(object: ClobPair_Status): string {
case ClobPair_Status.STATUS_INITIALIZING:
return "STATUS_INITIALIZING";

case ClobPair_Status.STATUS_FINAL_SETTLEMENT:
return "STATUS_FINAL_SETTLEMENT";

case ClobPair_Status.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export interface MatchPerpetualDeleveraging {
/** An ordered list of fills created by this liquidation. */

fills: MatchPerpetualDeleveraging_Fill[];
/**
* Flag denoting whether the deleveraging operation was for the purpose
* of final settlement. Final settlement matches are at the oracle price,
* whereas deleveraging happens at the bankruptcy price of the deleveraged
* subaccount.
*/

isFinalSettlement: boolean;
}
/**
* MatchPerpetualDeleveraging is an injected message used for deleveraging a
Expand All @@ -143,6 +151,14 @@ export interface MatchPerpetualDeleveragingSDKType {
/** An ordered list of fills created by this liquidation. */

fills: MatchPerpetualDeleveraging_FillSDKType[];
/**
* Flag denoting whether the deleveraging operation was for the purpose
* of final settlement. Final settlement matches are at the oracle price,
* whereas deleveraging happens at the bankruptcy price of the deleveraged
* subaccount.
*/

is_final_settlement: boolean;
}
/** Fill represents a fill between the liquidated and offsetting subaccount. */

Expand Down Expand Up @@ -451,7 +467,8 @@ function createBaseMatchPerpetualDeleveraging(): MatchPerpetualDeleveraging {
return {
liquidated: undefined,
perpetualId: 0,
fills: []
fills: [],
isFinalSettlement: false
};
}

Expand All @@ -469,6 +486,10 @@ export const MatchPerpetualDeleveraging = {
MatchPerpetualDeleveraging_Fill.encode(v!, writer.uint32(26).fork()).ldelim();
}

if (message.isFinalSettlement === true) {
writer.uint32(32).bool(message.isFinalSettlement);
}

return writer;
},

Expand All @@ -493,6 +514,10 @@ export const MatchPerpetualDeleveraging = {
message.fills.push(MatchPerpetualDeleveraging_Fill.decode(reader, reader.uint32()));
break;

case 4:
message.isFinalSettlement = reader.bool();
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -507,6 +532,7 @@ export const MatchPerpetualDeleveraging = {
message.liquidated = object.liquidated !== undefined && object.liquidated !== null ? SubaccountId.fromPartial(object.liquidated) : undefined;
message.perpetualId = object.perpetualId ?? 0;
message.fills = object.fills?.map(e => MatchPerpetualDeleveraging_Fill.fromPartial(e)) || [];
message.isFinalSettlement = object.isFinalSettlement ?? false;
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,20 @@ export interface DeleveragingEventV1 {
*/

fillAmount: Long;
/** Bankruptcy price of liquidated subaccount, in USDC quote quantums. */
/** Fill price of deleveraging event, in USDC quote quantums. */

price: Long;
/** `true` if liquidating a short position, `false` otherwise. */

isBuy: boolean;
/**
* `true` if the deleveraging event is for final settlement, indicating
* the match occurred at the oracle price rather than bankruptcy price.
* When this flag is `false`, the fill price is the bankruptcy price
* of the liquidated subaccount.
*/

isFinalSettlement: boolean;
}
/**
* DeleveragingEvent message contains all the information for a deleveraging
Expand All @@ -493,12 +501,20 @@ export interface DeleveragingEventV1SDKType {
*/

fill_amount: Long;
/** Bankruptcy price of liquidated subaccount, in USDC quote quantums. */
/** Fill price of deleveraging event, in USDC quote quantums. */

price: Long;
/** `true` if liquidating a short position, `false` otherwise. */

is_buy: boolean;
/**
* `true` if the deleveraging event is for final settlement, indicating
* the match occurred at the oracle price rather than bankruptcy price.
* When this flag is `false`, the fill price is the bankruptcy price
* of the liquidated subaccount.
*/

is_final_settlement: boolean;
}
/**
* LiquidationOrder represents the liquidation taker order to be included in a
Expand Down Expand Up @@ -1782,7 +1798,8 @@ function createBaseDeleveragingEventV1(): DeleveragingEventV1 {
perpetualId: 0,
fillAmount: Long.UZERO,
price: Long.UZERO,
isBuy: false
isBuy: false,
isFinalSettlement: false
};
}

Expand Down Expand Up @@ -1812,6 +1829,10 @@ export const DeleveragingEventV1 = {
writer.uint32(48).bool(message.isBuy);
}

if (message.isFinalSettlement === true) {
writer.uint32(56).bool(message.isFinalSettlement);
}

return writer;
},

Expand Down Expand Up @@ -1848,6 +1869,10 @@ export const DeleveragingEventV1 = {
message.isBuy = reader.bool();
break;

case 7:
message.isFinalSettlement = reader.bool();
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -1865,6 +1890,7 @@ export const DeleveragingEventV1 = {
message.fillAmount = object.fillAmount !== undefined && object.fillAmount !== null ? Long.fromValue(object.fillAmount) : Long.UZERO;
message.price = object.price !== undefined && object.price !== null ? Long.fromValue(object.price) : Long.UZERO;
message.isBuy = object.isBuy ?? false;
message.isFinalSettlement = object.isFinalSettlement ?? false;
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ export enum ClobPairStatus {
* both short-term and post-only.
*/
CLOB_PAIR_STATUS_INITIALIZING = 5,

/**
* 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.
*/
CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6,
UNRECOGNIZED = -1,
}
/**
Expand Down Expand Up @@ -348,6 +356,14 @@ export enum ClobPairStatusSDKType {
* both short-term and post-only.
*/
CLOB_PAIR_STATUS_INITIALIZING = 5,

/**
* 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.
*/
CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6,
UNRECOGNIZED = -1,
}
export function clobPairStatusFromJSON(object: any): ClobPairStatus {
Expand Down Expand Up @@ -376,6 +392,10 @@ export function clobPairStatusFromJSON(object: any): ClobPairStatus {
case "CLOB_PAIR_STATUS_INITIALIZING":
return ClobPairStatus.CLOB_PAIR_STATUS_INITIALIZING;

case 6:
case "CLOB_PAIR_STATUS_FINAL_SETTLEMENT":
return ClobPairStatus.CLOB_PAIR_STATUS_FINAL_SETTLEMENT;

case -1:
case "UNRECOGNIZED":
default:
Expand All @@ -402,6 +422,9 @@ export function clobPairStatusToJSON(object: ClobPairStatus): string {
case ClobPairStatus.CLOB_PAIR_STATUS_INITIALIZING:
return "CLOB_PAIR_STATUS_INITIALIZING";

case ClobPairStatus.CLOB_PAIR_STATUS_FINAL_SETTLEMENT:
return "CLOB_PAIR_STATUS_FINAL_SETTLEMENT";

case ClobPairStatus.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export enum OrderRemovalReason {
* equity tier requirements.
*/
ORDER_REMOVAL_REASON_EQUITY_TIER = 13,

/** ORDER_REMOVAL_REASON_FINAL_SETTLEMENT - The order has been removed since its ClobPair has entered final settlement. */
ORDER_REMOVAL_REASON_FINAL_SETTLEMENT = 14,
UNRECOGNIZED = -1,
}
/** OrderRemovalReason is an enum of all the reasons an order was removed. */
Expand Down Expand Up @@ -155,6 +158,9 @@ export enum OrderRemovalReasonSDKType {
* equity tier requirements.
*/
ORDER_REMOVAL_REASON_EQUITY_TIER = 13,

/** ORDER_REMOVAL_REASON_FINAL_SETTLEMENT - The order has been removed since its ClobPair has entered final settlement. */
ORDER_REMOVAL_REASON_FINAL_SETTLEMENT = 14,
UNRECOGNIZED = -1,
}
export function orderRemovalReasonFromJSON(object: any): OrderRemovalReason {
Expand Down Expand Up @@ -215,6 +221,10 @@ export function orderRemovalReasonFromJSON(object: any): OrderRemovalReason {
case "ORDER_REMOVAL_REASON_EQUITY_TIER":
return OrderRemovalReason.ORDER_REMOVAL_REASON_EQUITY_TIER;

case 14:
case "ORDER_REMOVAL_REASON_FINAL_SETTLEMENT":
return OrderRemovalReason.ORDER_REMOVAL_REASON_FINAL_SETTLEMENT;

case -1:
case "UNRECOGNIZED":
default:
Expand Down Expand Up @@ -265,6 +275,9 @@ export function orderRemovalReasonToJSON(object: OrderRemovalReason): string {
case OrderRemovalReason.ORDER_REMOVAL_REASON_EQUITY_TIER:
return "ORDER_REMOVAL_REASON_EQUITY_TIER";

case OrderRemovalReason.ORDER_REMOVAL_REASON_FINAL_SETTLEMENT:
return "ORDER_REMOVAL_REASON_FINAL_SETTLEMENT";

case OrderRemovalReason.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
Expand Down
1 change: 1 addition & 0 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,7 @@ or
|*anonymous*|CANCEL_ONLY|
|*anonymous*|POST_ONLY|
|*anonymous*|INITIALIZING|
|*anonymous*|FINAL_SETTLEMENT|

## PerpetualMarketResponseObject

Expand Down
3 changes: 2 additions & 1 deletion indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@
"PAUSED",
"CANCEL_ONLY",
"POST_ONLY",
"INITIALIZING"
"INITIALIZING",
"FINAL_SETTLEMENT"
],
"type": "string"
},
Expand Down
1 change: 1 addition & 0 deletions indexer/services/ender/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export const defaultDeleveragingEvent: DeleveragingEventV1 = {
fillAmount: Long.fromValue(10_000, true),
price: Long.fromValue(1_000_000_000, true),
isBuy: true,
isFinalSettlement: false,
};
export const defaultDepositEvent: TransferEventV1 = {
assetId: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ BEGIN
WHEN '3'::jsonb THEN RETURN 'CANCEL_ONLY'; /** CLOB_PAIR_STATUS_CANCEL_ONLY */
WHEN '4'::jsonb THEN RETURN 'POST_ONLY'; /** CLOB_PAIR_STATUS_POST_ONLY */
WHEN '5'::jsonb THEN RETURN 'INITIALIZING'; /** CLOB_PAIR_STATUS_INITIALIZING */
WHEN '6'::jsonb THEN RETURN 'FINAL_SETTLEMENT'; /** CLOB_PAIR_STATUS_FINAL_SETTLEMENT */
ELSE RAISE EXCEPTION 'Invalid clob pair status: %', status;
END CASE;
END;
Expand Down
5 changes: 5 additions & 0 deletions proto/dydxprotocol/clob/clob_pair.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ message ClobPair {
// Clob pairs in this state only accept orders which are
// both short-term and post-only.
STATUS_INITIALIZING = 5;
// STATUS_FINAL_SETTLEMENT represents a clob pair which is deactivated
// and trading has ceased. All open positions will be closed by the
// protocol. Open stateful orders will be cancelled. Open short-term
// orders will be left to expire.
STATUS_FINAL_SETTLEMENT = 6;
}

Status status = 7;
Expand Down
6 changes: 6 additions & 0 deletions proto/dydxprotocol/clob/matches.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ message MatchPerpetualDeleveraging {
}
// An ordered list of fills created by this liquidation.
repeated Fill fills = 3 [ (gogoproto.nullable) = false ];

// Flag denoting whether the deleveraging operation was for the purpose
// of final settlement. Final settlement matches are at the oracle price,
// whereas deleveraging happens at the bankruptcy price of the deleveraged
// subaccount.
bool is_final_settlement = 4;
}
7 changes: 6 additions & 1 deletion proto/dydxprotocol/indexer/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,15 @@ message DeleveragingEventV1 {
// The amount filled between the liquidated and offsetting position, in
// base quantums.
uint64 fill_amount = 4;
// Bankruptcy price of liquidated subaccount, in USDC quote quantums.
// Fill price of deleveraging event, in USDC quote quantums.
uint64 price = 5;
// `true` if liquidating a short position, `false` otherwise.
bool is_buy = 6;
// `true` if the deleveraging event is for final settlement, indicating
// the match occurred at the oracle price rather than bankruptcy price.
// When this flag is `false`, the fill price is the bankruptcy price
// of the liquidated subaccount.
bool is_final_settlement = 7;
}

// LiquidationOrder represents the liquidation taker order to be included in a
Expand Down
5 changes: 5 additions & 0 deletions proto/dydxprotocol/indexer/protocol/v1/clob.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ enum ClobPairStatus {
// Clob pairs in this state only accept orders which are
// both short-term and post-only.
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.
CLOB_PAIR_STATUS_FINAL_SETTLEMENT = 6;
}
Loading

0 comments on commit 81bd4b7

Please sign in to comment.