Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IND-461] handle deleveraging events #730

Merged
merged 31 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export async function up(knex: Knex): Promise<void> {
'LIMIT',
'LIQUIDATED',
'LIQUIDATION',
'DELEVERAGED',
'OFFSETTING',
dydxwill marked this conversation as resolved.
Show resolved Hide resolved
]).notNullable();
table.bigInteger('clobPairId').notNullable();
table.uuid('orderId').nullable();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex.raw(`
ALTER TABLE ONLY fills
DROP CONSTRAINT IF EXISTS fills_type_check;

ALTER TABLE ONLY fills
ADD CONSTRAINT fills_type_check
CHECK (type = ANY (ARRAY['MARKET'::text, 'LIMIT'::text, 'LIQUIDATED'::text, 'LIQUIDATION'::text, 'DELEVERAGED'::text, 'OFFSETTING'::text]));
dydxwill marked this conversation as resolved.
Show resolved Hide resolved
`);
}

export async function down(knex: Knex): Promise<void> {
return knex.raw(`
ALTER TABLE ONLY fills
DROP CONSTRAINT IF EXISTS fills_type_check;

ALTER TABLE ONLY fills
ADD CONSTRAINT fills_type_check
CHECK (type = ANY (ARRAY['MARKET'::text, 'LIMIT'::text, 'LIQUIDATED'::text, 'LIQUIDATION'::text]));
`);
}
6 changes: 6 additions & 0 deletions indexer/packages/postgres/src/types/fill-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export enum FillType {
LIQUIDATED = 'LIQUIDATED',
// LIQUIDATION is for the maker side of the fill, never used for orders
LIQUIDATION = 'LIQUIDATION',
// DELEVERAGED is for the subaccount that was deleveraged in a deleveraging event.
// The fill type will be set to taker.
DELEVERAGED = 'DELEVERAGED',
// OFFSETTING is for the offsetting subaccount in a deleveraging event.
// The fill type will be set to maker.
OFFSETTING = 'OFFSETTING',
}

export interface FillCreateObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ export interface TradeContent {
price: string,
side: string,
createdAt: IsoString,
liquidation: boolean,
liquidation?: boolean,
deleveraging?: boolean,
dydxwill marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made them required

}

/* ------- MarketMessageContents ------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,68 @@ export interface OrderFillEventV1SDKType {

total_filled_taker: Long;
}
/**
* DeleveragingEvent message contains all the information for a deleveraging
* on the dYdX chain. This includes the liquidated/offsetting subaccounts and
* the amount filled.
*/

export interface DeleveragingEventV1 {
/** ID of the subaccount that was liquidated. */
liquidated?: IndexerSubaccountId;
/** ID of the subaccount that was used to offset the position. */

offsetting?: IndexerSubaccountId;
/** The ID of the clob pair that was liquidated. */

clobPairId: number;
/**
* The amount filled between the liquidated and offsetting position, in
* base quantums.
*/

fillAmount: Long;
/**
* The closing price in subticks. Bankruptcy price of liquidated subaccount
* is not guaranteed to be a multiple of subticks_per_tick.
*/

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

isBuy: boolean;
}
/**
* DeleveragingEvent message contains all the information for a deleveraging
* on the dYdX chain. This includes the liquidated/offsetting subaccounts and
* the amount filled.
*/

export interface DeleveragingEventV1SDKType {
/** ID of the subaccount that was liquidated. */
liquidated?: IndexerSubaccountIdSDKType;
/** ID of the subaccount that was used to offset the position. */

offsetting?: IndexerSubaccountIdSDKType;
/** The ID of the clob pair that was liquidated. */

clob_pair_id: number;
/**
* The amount filled between the liquidated and offsetting position, in
* base quantums.
*/

fill_amount: Long;
/**
* The closing price in subticks. Bankruptcy price of liquidated subaccount
* is not guaranteed to be a multiple of subticks_per_tick.
*/

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

is_buy: boolean;
}
/**
* LiquidationOrder represents the liquidation taker order to be included in a
* liquidation order fill event.
Expand Down Expand Up @@ -1719,6 +1781,101 @@ export const OrderFillEventV1 = {

};

function createBaseDeleveragingEventV1(): DeleveragingEventV1 {
return {
liquidated: undefined,
offsetting: undefined,
clobPairId: 0,
fillAmount: Long.UZERO,
subticks: Long.UZERO,
isBuy: false
};
}

export const DeleveragingEventV1 = {
encode(message: DeleveragingEventV1, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.liquidated !== undefined) {
IndexerSubaccountId.encode(message.liquidated, writer.uint32(10).fork()).ldelim();
}

if (message.offsetting !== undefined) {
IndexerSubaccountId.encode(message.offsetting, writer.uint32(18).fork()).ldelim();
}

if (message.clobPairId !== 0) {
writer.uint32(24).uint32(message.clobPairId);
}

if (!message.fillAmount.isZero()) {
writer.uint32(32).uint64(message.fillAmount);
}

if (!message.subticks.isZero()) {
writer.uint32(40).uint64(message.subticks);
}

if (message.isBuy === true) {
writer.uint32(48).bool(message.isBuy);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): DeleveragingEventV1 {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseDeleveragingEventV1();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.liquidated = IndexerSubaccountId.decode(reader, reader.uint32());
break;

case 2:
message.offsetting = IndexerSubaccountId.decode(reader, reader.uint32());
break;

case 3:
message.clobPairId = reader.uint32();
break;

case 4:
message.fillAmount = (reader.uint64() as Long);
break;

case 5:
message.subticks = (reader.uint64() as Long);
break;

case 6:
message.isBuy = reader.bool();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<DeleveragingEventV1>): DeleveragingEventV1 {
const message = createBaseDeleveragingEventV1();
message.liquidated = object.liquidated !== undefined && object.liquidated !== null ? IndexerSubaccountId.fromPartial(object.liquidated) : undefined;
message.offsetting = object.offsetting !== undefined && object.offsetting !== null ? IndexerSubaccountId.fromPartial(object.offsetting) : undefined;
message.clobPairId = object.clobPairId ?? 0;
message.fillAmount = object.fillAmount !== undefined && object.fillAmount !== null ? Long.fromValue(object.fillAmount) : Long.UZERO;
message.subticks = object.subticks !== undefined && object.subticks !== null ? Long.fromValue(object.subticks) : Long.UZERO;
message.isBuy = object.isBuy ?? false;
return message;
}

};

function createBaseLiquidationOrderV1(): LiquidationOrderV1 {
return {
liquidated: undefined,
Expand Down
2 changes: 2 additions & 0 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,8 @@ This operation does not require authentication
|*anonymous*|LIMIT|
|*anonymous*|LIQUIDATED|
|*anonymous*|LIQUIDATION|
|*anonymous*|DELEVERAGED|
|*anonymous*|OFFSETTING|

## MarketType

Expand Down
4 changes: 3 additions & 1 deletion indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@
"MARKET",
"LIMIT",
"LIQUIDATED",
"LIQUIDATION"
"LIQUIDATION",
"DELEVERAGED",
"OFFSETTING"
],
"type": "string"
},
Expand Down
Loading
Loading