Skip to content

Commit

Permalink
Code cleanup (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Jun 12, 2024
1 parent b431e04 commit 2bafa9a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
TimeInForce,
} from '@dydxprotocol-indexer/postgres';
import {
IndexerOrder,
IndexerTendermintBlock,
IndexerTendermintEvent,
OffChainUpdateV1,
IndexerOrder,
OrderPlaceV1_OrderPlacementStatus,
StatefulOrderEventV1,
} from '@dydxprotocol-indexer/v4-protos';
Expand All @@ -36,7 +36,6 @@ import { updateBlockCache } from '../../../src/caches/block-cache';
import {
createIndexerTendermintBlock,
createIndexerTendermintEvent,
expectOrderSubaccountKafkaMessage,
expectVulcanKafkaMessage,
} from '../../helpers/indexer-proto-helpers';
import { StatefulOrderPlacementHandler } from '../../../src/handlers/stateful-order/stateful-order-placement-handler';
Expand All @@ -45,7 +44,6 @@ import { STATEFUL_ORDER_ORDER_FILL_EVENT_TYPE } from '../../../src/constants';
import { producer } from '@dydxprotocol-indexer/kafka';
import { ORDER_FLAG_LONG_TERM } from '@dydxprotocol-indexer/v4-proto-parser';
import { createPostgresFunctions } from '../../../src/helpers/postgres/postgres-functions';
import config from '../../../src/config';

describe('statefulOrderPlacementHandler', () => {
beforeAll(async () => {
Expand All @@ -63,7 +61,6 @@ describe('statefulOrderPlacementHandler', () => {
afterEach(async () => {
await dbHelpers.clearData();
jest.clearAllMocks();
config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS = false;
});

afterAll(async () => {
Expand Down Expand Up @@ -138,21 +135,15 @@ describe('statefulOrderPlacementHandler', () => {

it.each([
// TODO(IND-334): Remove after deprecating StatefulOrderPlacementEvent
['stateful order placement as txn event', defaultStatefulOrderEvent, false, 0],
['stateful long term order placement as txn event', defaultStatefulOrderLongTermEvent, false, 0],
['stateful order placement as txn event', defaultStatefulOrderEvent, true, 0],
['stateful long term order placement as txn event', defaultStatefulOrderLongTermEvent, true, 0],
['stateful order placement as block event', defaultStatefulOrderEvent, false, -1],
['stateful long term order placement as block event', defaultStatefulOrderLongTermEvent, false, -1],
['stateful order placement as block event', defaultStatefulOrderEvent, true, -1],
['stateful long term order placement as block event', defaultStatefulOrderLongTermEvent, true, -1],
])('successfully places order with %s (emit subaccount websocket msg: %s)', async (
['stateful order placement as txn event', defaultStatefulOrderEvent, 0],
['stateful long term order placement as txn event', defaultStatefulOrderLongTermEvent, 0],
['stateful order placement as block event', defaultStatefulOrderEvent, -1],
['stateful long term order placement as block event', defaultStatefulOrderLongTermEvent, -1],
])('successfully places order with %s', async (
_name: string,
statefulOrderEvent: StatefulOrderEventV1,
emitSubaccountMessage: boolean,
transactionIndex: number,
) => {
config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS = emitSubaccountMessage;
const kafkaMessage: KafkaMessage = createKafkaMessageFromStatefulOrderEvent(
statefulOrderEvent,
transactionIndex,
Expand Down Expand Up @@ -195,15 +186,6 @@ describe('statefulOrderPlacementHandler', () => {
offchainUpdate: expectedOffchainUpdate,
headers: { message_received_timestamp: kafkaMessage.timestamp, event_type: 'StatefulOrderPlacement' },
});
if (emitSubaccountMessage) {
expectOrderSubaccountKafkaMessage(
producerSendMock,
defaultOrder.orderId!.subaccountId!,
order!,
defaultHeight.toString(),
transactionIndex,
);
}
});

it.each([
Expand Down
3 changes: 0 additions & 3 deletions indexer/services/ender/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export const configSchema = {
SEND_WEBSOCKET_MESSAGES: parseBoolean({
default: true,
}),
SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS: parseBoolean({
default: false,
}),
};

export default parseSchema(configSchema);
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { generateSubaccountMessageContents } from '@dydxprotocol-indexer/kafka';
import {
OrderFromDatabase,
OrderModel,
OrderTable,
PerpetualMarketFromDatabase,
perpetualMarketRefresher,
SubaccountMessageContents,
} from '@dydxprotocol-indexer/postgres';
import { convertToRedisOrder } from '@dydxprotocol-indexer/redis';
import { OrderTable } from '@dydxprotocol-indexer/postgres';
import { getOrderIdHash } from '@dydxprotocol-indexer/v4-proto-parser';
import {
IndexerOrder,
IndexerSubaccountId,
OffChainUpdateV1,
OrderPlaceV1_OrderPlacementStatus,
RedisOrder,
StatefulOrderEventV1,
SubaccountId,
} from '@dydxprotocol-indexer/v4-protos';
import * as pg from 'pg';

import config from '../../config';
import { ConsolidatedKafkaEvent } from '../../lib/types';
import { AbstractStatefulOrderHandler } from '../abstract-stateful-order-handler';

Expand All @@ -40,37 +26,25 @@ export class StatefulOrderPlacementHandler
return orderId;
}

public getSubaccountId(): IndexerSubaccountId {
let subaccountId: IndexerSubaccountId;
// TODO(IND-334): Remove after deprecating StatefulOrderPlacementEvent
if (this.event.orderPlace !== undefined) {
subaccountId = this.event.orderPlace!.order!.orderId!.subaccountId!;
} else {
subaccountId = this.event.longTermOrderPlacement!.order!.orderId!.subaccountId!;
}
return subaccountId;
}

public getParallelizationIds(): string[] {
// Stateful Order Events with the same orderId
return this.getParallelizationIdsFromOrderId(this.getOrderId());
}

// eslint-disable-next-line @typescript-eslint/require-await
public async internalHandle(resultRow: pg.QueryResultRow): Promise<ConsolidatedKafkaEvent[]> {
public async internalHandle(): Promise<ConsolidatedKafkaEvent[]> {
let order: IndexerOrder;
// TODO(IND-334): Remove after deprecating StatefulOrderPlacementEvent
if (this.event.orderPlace !== undefined) {
order = this.event.orderPlace!.order!;
} else {
order = this.event.longTermOrderPlacement!.order!;
}
return this.createKafkaEvents(order, resultRow);
return this.createKafkaEvents(order);
}

private createKafkaEvents(
order: IndexerOrder,
resultRow: pg.QueryResultRow,
): ConsolidatedKafkaEvent[] {
const kafkaEvents: ConsolidatedKafkaEvent[] = [];

Expand All @@ -88,32 +62,6 @@ export class StatefulOrderPlacementHandler
event_type: 'StatefulOrderPlacement',
},
));

if (config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS) {
const perpetualMarket: PerpetualMarketFromDatabase = perpetualMarketRefresher
.getPerpetualMarketFromClobPairId(order.orderId!.clobPairId.toString())!;
const dbOrder: OrderFromDatabase = OrderModel.fromJson(resultRow.order) as OrderFromDatabase;
const redisOrder: RedisOrder = convertToRedisOrder(order, perpetualMarket);
const subaccountContent: SubaccountMessageContents = generateSubaccountMessageContents(
redisOrder,
dbOrder,
perpetualMarket,
OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_OPENED,
this.block.height.toString(),
);

const subaccountIdProto: SubaccountId = {
owner: this.getSubaccountId().owner,
number: this.getSubaccountId().number,
};
kafkaEvents.push(this.generateConsolidatedSubaccountKafkaEvent(
JSON.stringify(subaccountContent),
subaccountIdProto,
this.getOrderId(),
false,
subaccountContent,
));
}
return kafkaEvents;
}
}
Loading

0 comments on commit 2bafa9a

Please sign in to comment.