Skip to content

Commit

Permalink
[IND-486] add new trade type (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Nov 10, 2023
1 parent ad87ef1 commit dd16df6
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 15 deletions.
1 change: 1 addition & 0 deletions indexer/packages/postgres/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './utility-types';
export * from './asset-types';
export * from './asset-position-types';
export * from './transfer-types';
export * from './trade-types';
export * from './market-types';
export * from './oracle-price-types';
export * from './websocket-message-types';
Expand Down
23 changes: 23 additions & 0 deletions indexer/packages/postgres/src/types/trade-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FillType } from './fill-types';

export enum TradeType {
// LIMIT is the trade type for a fill with a limit taker order.
LIMIT = 'LIMIT',
// LIQUIDATED is the trade type for a fill with a liquidated taker order.
LIQUIDATED = 'LIQUIDATED',
// DELEVERAGED is the trade type for a fill with a deleveraged taker order.
DELEVERAGED = 'DELEVERAGED',
}

export function fillTypeToTradeType(fillType: FillType): TradeType {
switch (fillType) {
case FillType.LIMIT:
return TradeType.LIMIT;
case FillType.LIQUIDATED:
return TradeType.LIQUIDATED;
case FillType.DELEVERAGED:
return TradeType.DELEVERAGED;
default:
throw new Error(`Unknown fill type: ${fillType}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { PerpetualMarketStatus } from './perpetual-market-types';
import { PerpetualPositionStatus } from './perpetual-position-types';
import { PositionSide } from './position-types';
import { TradeType } from './trade-types';
import { TransferType } from './transfer-types';
import { IsoString } from './utility-types';

Expand Down Expand Up @@ -175,7 +176,7 @@ export interface TradeContent {
price: string,
side: string,
createdAt: IsoString,
type: FillType,
type: TradeType,
}

/* ------- MarketMessageContents ------- */
Expand Down
28 changes: 27 additions & 1 deletion indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,32 @@ or
|iso|[IsoString](#schemaisostring)|true|none|none|
|epoch|number(double)|true|none|none|

## TradeType

<a id="schematradetype"></a>
<a id="schema_TradeType"></a>
<a id="tocStradetype"></a>
<a id="tocstradetype"></a>

```json
"LIMIT"

```

### Properties

|Name|Type|Required|Restrictions|Description|
|---|---|---|---|---|
|*anonymous*|string|false|none|none|

#### Enumerated Values

|Property|Value|
|---|---|
|*anonymous*|LIMIT|
|*anonymous*|LIQUIDATED|
|*anonymous*|DELEVERAGED|

## TradeResponseObject

<a id="schematraderesponseobject"></a>
Expand Down Expand Up @@ -3077,7 +3103,7 @@ or
|side|[OrderSide](#schemaorderside)|true|none|none|
|size|string|true|none|none|
|price|string|true|none|none|
|type|[FillType](#schemafilltype)|true|none|none|
|type|[TradeType](#schematradetype)|true|none|none|
|createdAt|[IsoString](#schemaisostring)|true|none|none|
|createdAtHeight|string|true|none|none|

Expand Down
10 changes: 9 additions & 1 deletion indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,14 @@
"type": "object",
"additionalProperties": false
},
"TradeType": {
"enum": [
"LIMIT",
"LIQUIDATED",
"DELEVERAGED"
],
"type": "string"
},
"TradeResponseObject": {
"properties": {
"id": {
Expand All @@ -859,7 +867,7 @@
"type": "string"
},
"type": {
"$ref": "#/components/schemas/FillType"
"$ref": "#/components/schemas/TradeType"
},
"createdAt": {
"$ref": "#/components/schemas/IsoString"
Expand Down
15 changes: 12 additions & 3 deletions indexer/services/comlink/public/websocket-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ export enum FillType {
OFFSETTING = 'OFFSETTING',
}

export enum TradeType {
// LIMIT is the trade type for a fill with a limit taker order.
LIMIT = 'LIMIT',
// LIQUIDATED is the trade type for a fill with a liquidated taker order.
LIQUIDATED = 'LIQUIDATED',
// DELEVERAGED is the trade type for a fill with a deleveraged taker order.
DELEVERAGED = 'DELEVERAGED',
}

export interface TransferSubaccountMessageContents {
sender: {
address: string,
Expand Down Expand Up @@ -712,7 +721,7 @@ interface TradeContent {
price: string,
side: string,
createdAt: IsoString,
type: FillType,
type: TradeType,
}
```

Expand All @@ -734,15 +743,15 @@ interface TradeContent {
"price": "27839",
"side": "BUY",
"createdAt": "2023-04-04T00:29:19.353Z",
"type": "LIQUIDATION"
"type": "LIQUIDATED"
},
{
"id": "38e64479-af09-5417-a795-195f83879156",
"size": "0.000000004",
"price": "27839",
"side": "BUY",
"createdAt": "2023-04-04T00:29:19.353Z",
"type": "LIQUIDATION"
"type": "LIQUIDATED"
},
{
"id": "d310c32c-f066-5ba8-a97d-10a29d9a6c84",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CandleColumns,
CandleFromDatabase,
FillFromDatabase,
fillTypeToTradeType,
FundingIndexUpdatesFromDatabase,
helpers,
LiquidityTiersFromDatabase,
Expand Down Expand Up @@ -177,7 +178,7 @@ export function fillToTradeResponseObject(
side: fill.side,
size: fill.size,
price: fill.price,
type: fill.type,
type: fillTypeToTradeType(fill.type),
createdAt: fill.createdAt,
createdAtHeight: fill.createdAtHeight,
};
Expand Down
3 changes: 2 additions & 1 deletion indexer/services/comlink/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
PerpetualPositionStatus,
PositionSide,
SubaccountFromDatabase,
TradeType,
TransferType,
} from '@dydxprotocol-indexer/postgres';
import { RedisOrder } from '@dydxprotocol-indexer/v4-protos';
Expand Down Expand Up @@ -174,7 +175,7 @@ export interface TradeResponseObject {
side: OrderSide,
size: string,
price: string,
type: FillType,
type: TradeType,
createdAt: IsoString,
createdAtHeight: string,
}
Expand Down
4 changes: 2 additions & 2 deletions indexer/services/ender/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION } from '@dydxprotocol-indexer/kafka';
import { FillType, testConstants, TradeContent } from '@dydxprotocol-indexer/postgres';
import { testConstants, TradeContent, TradeType } from '@dydxprotocol-indexer/postgres';
import {
bigIntToBytes,
ORDER_FLAG_CONDITIONAL,
Expand Down Expand Up @@ -325,7 +325,7 @@ export const defaultTradeContent: TradeContent = {
price: '10000',
side: 'BUY',
createdAt: 'createdAt',
type: FillType.LIMIT,
type: TradeType.LIMIT,
};
export const defaultTradeMessage: SingleTradeMessage = contentToSingleTradeMessage(
defaultTradeContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
PerpetualMarketFromDatabase,
PerpetualMarketTable,
IsoString,
fillTypeToTradeType,
} from '@dydxprotocol-indexer/postgres';
import { getOrderIdHash } from '@dydxprotocol-indexer/v4-proto-parser';
import {
Expand Down Expand Up @@ -805,7 +806,7 @@ export async function expectDefaultTradeKafkaMessageFromTakerFillId(
price: takerFill!.price,
side: takerFill!.side.toString(),
createdAt: takerFill!.createdAt,
type: takerFill!.type,
type: fillTypeToTradeType(takerFill!.type),
},
],
};
Expand Down
7 changes: 4 additions & 3 deletions indexer/services/ender/__tests__/lib/kafka-publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
testConstants,
TradeContent,
TradeMessageContents,
TradeType,
TransferFromDatabase,
} from '@dydxprotocol-indexer/postgres';
import { IndexerSubaccountId, SubaccountMessage, TradeMessage } from '@dydxprotocol-indexer/v4-protos';
Expand Down Expand Up @@ -409,7 +410,7 @@ describe('kafka-publisher', () => {
price: '10000',
side: 'side',
createdAt: 'today',
type: FillType.LIMIT,
type: TradeType.LIMIT,
};
const singleTrade1: SingleTradeMessage = contentToSingleTradeMessage(
tradeContent1,
Expand All @@ -422,7 +423,7 @@ describe('kafka-publisher', () => {
price: '12000',
side: 'side',
createdAt: 'today',
type: FillType.LIMIT,
type: TradeType.LIMIT,
};
const singleTrade2: SingleTradeMessage = contentToSingleTradeMessage(
tradeContent2,
Expand All @@ -436,7 +437,7 @@ describe('kafka-publisher', () => {
price: '1000',
side: 'side',
createdAt: 'today',
type: FillType.LIMIT,
type: TradeType.LIMIT,
};
const singleTrade3: SingleTradeMessage = contentToSingleTradeMessage(
tradeContent3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FillFromDatabase,
FillTable,
FillType,
fillTypeToTradeType,
Liquidity,
OrderCreateObject,
OrderFromDatabase,
Expand Down Expand Up @@ -426,7 +427,7 @@ export abstract class AbstractOrderFillHandler<T> extends Handler<T> {
price: fill.price,
side: fill.side.toString(),
createdAt: fill.createdAt,
type: fill.type,
type: fillTypeToTradeType(fill.type),
},
],
};
Expand Down

0 comments on commit dd16df6

Please sign in to comment.