From 2bafa9a3c59fcd84c5eb2d08a31ac67244efa4b7 Mon Sep 17 00:00:00 2001 From: dydxwill <119354122+dydxwill@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:18:41 -0400 Subject: [PATCH] Code cleanup (#1679) --- .../stateful-order-placement-handler.test.ts | 30 ++------ indexer/services/ender/src/config.ts | 3 - .../stateful-order-placement-handler.ts | 58 +-------------- .../handlers/order-place-handler.test.ts | 71 ++++++++----------- .../src/handlers/order-place-handler.ts | 18 +---- 5 files changed, 40 insertions(+), 140 deletions(-) diff --git a/indexer/services/ender/__tests__/handlers/stateful-order/stateful-order-placement-handler.test.ts b/indexer/services/ender/__tests__/handlers/stateful-order/stateful-order-placement-handler.test.ts index 91bdd8de8d..b65e6fe6f6 100644 --- a/indexer/services/ender/__tests__/handlers/stateful-order/stateful-order-placement-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/stateful-order/stateful-order-placement-handler.test.ts @@ -13,10 +13,10 @@ import { TimeInForce, } from '@dydxprotocol-indexer/postgres'; import { + IndexerOrder, IndexerTendermintBlock, IndexerTendermintEvent, OffChainUpdateV1, - IndexerOrder, OrderPlaceV1_OrderPlacementStatus, StatefulOrderEventV1, } from '@dydxprotocol-indexer/v4-protos'; @@ -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'; @@ -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 () => { @@ -63,7 +61,6 @@ describe('statefulOrderPlacementHandler', () => { afterEach(async () => { await dbHelpers.clearData(); jest.clearAllMocks(); - config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS = false; }); afterAll(async () => { @@ -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, @@ -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([ diff --git a/indexer/services/ender/src/config.ts b/indexer/services/ender/src/config.ts index 6f79e75fb4..c22122b318 100644 --- a/indexer/services/ender/src/config.ts +++ b/indexer/services/ender/src/config.ts @@ -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); diff --git a/indexer/services/ender/src/handlers/stateful-order/stateful-order-placement-handler.ts b/indexer/services/ender/src/handlers/stateful-order/stateful-order-placement-handler.ts index 452982013f..8c4af7ff89 100644 --- a/indexer/services/ender/src/handlers/stateful-order/stateful-order-placement-handler.ts +++ b/indexer/services/ender/src/handlers/stateful-order/stateful-order-placement-handler.ts @@ -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'; @@ -40,24 +26,13 @@ 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 { + public async internalHandle(): Promise { let order: IndexerOrder; // TODO(IND-334): Remove after deprecating StatefulOrderPlacementEvent if (this.event.orderPlace !== undefined) { @@ -65,12 +40,11 @@ export class StatefulOrderPlacementHandler } 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[] = []; @@ -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; } } diff --git a/indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts b/indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts index 4fff013d98..8ff4be9f85 100644 --- a/indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts +++ b/indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts @@ -1,15 +1,12 @@ import { - logger, - stats, - STATS_FUNCTION_NAME, - wrapBackgroundTask, + logger, stats, STATS_FUNCTION_NAME, wrapBackgroundTask, } from '@dydxprotocol-indexer/base'; import { synchronizeWrapBackgroundTask } from '@dydxprotocol-indexer/dev'; import { createKafkaMessage, + getTriggerPrice, producer, SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION, - getTriggerPrice, } from '@dydxprotocol-indexer/kafka'; import { APIOrderStatus, @@ -31,37 +28,40 @@ import { } from '@dydxprotocol-indexer/postgres'; import * as redisPackage from '@dydxprotocol-indexer/redis'; import { - PriceLevel, - OrdersCache, + CanceledOrdersCache, + CanceledOrderStatus, OrderbookLevels, OrderbookLevelsCache, + OrdersCache, + PriceLevel, redis, redisTestConstants, + StatefulOrderUpdatesCache, SubaccountOrderIdsCache, - CanceledOrdersCache, updateOrder, - StatefulOrderUpdatesCache, - CanceledOrderStatus, } from '@dydxprotocol-indexer/redis'; import { - OffChainUpdateV1, IndexerOrder, + OffChainUpdateV1, OrderbookMessage, OrderPlaceV1_OrderPlacementStatus, + OrderUpdateV1, RedisOrder, SubaccountId, SubaccountMessage, - OrderUpdateV1, } from '@dydxprotocol-indexer/v4-protos'; import { KafkaMessage } from 'kafkajs'; import Long from 'long'; import { redisClient, redisClient as client } from '../../src/helpers/redis/redis-controller'; import { onMessage } from '../../src/lib/on-message'; import { expectCanceledOrderStatus, handleInitialOrderPlace } from '../helpers/helpers'; -import { expectOffchainUpdateMessage, expectWebsocketOrderbookMessage, expectWebsocketSubaccountMessage } from '../helpers/websocket-helpers'; -import { getOrderIdHash, isLongTermOrder, isStatefulOrder } from '@dydxprotocol-indexer/v4-proto-parser'; +import { + expectOffchainUpdateMessage, + expectWebsocketOrderbookMessage, + expectWebsocketSubaccountMessage, +} from '../helpers/websocket-helpers'; +import { getOrderIdHash, isStatefulOrder } from '@dydxprotocol-indexer/v4-proto-parser'; import { defaultKafkaHeaders } from '../helpers/constants'; -import config from '../../src/config'; jest.mock('@dydxprotocol-indexer/base', () => ({ ...jest.requireActual('@dydxprotocol-indexer/base'), @@ -84,10 +84,6 @@ describe('order-place-handler', () => { jest.useRealTimers(); }); - afterEach(() => { - config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS = true; - }); - describe('handle', () => { const replacementOrder: IndexerOrder = { ...redisTestConstants.defaultOrder, @@ -147,35 +143,35 @@ describe('order-place-handler', () => { orderPlace: { order: replacementOrder, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const replacementUpdateGoodTilBlockTime: OffChainUpdateV1 = { orderPlace: { order: replacementOrderGoodTilBlockTime, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const replacementUpdateConditional: OffChainUpdateV1 = { orderPlace: { order: replacementOrderConditional, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const replacementUpdateFok: OffChainUpdateV1 = { orderPlace: { order: replacementOrderFok, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const replacementUpdateIoc: OffChainUpdateV1 = { orderPlace: { order: replacementOrderIoc, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const replacementMessage: KafkaMessage = createKafkaMessage( @@ -305,7 +301,7 @@ describe('order-place-handler', () => { orderPlace: { order: orderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }); @@ -424,7 +420,7 @@ describe('order-place-handler', () => { orderPlace: { order: initialOrderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }); expectWebsocketMessagesSent( @@ -550,7 +546,7 @@ describe('order-place-handler', () => { orderPlace: { order: initialOrderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }); expectWebsocketMessagesSent( @@ -666,7 +662,7 @@ describe('order-place-handler', () => { orderPlace: { order: initialOrderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }); expectWebsocketMessagesSent( @@ -781,7 +777,7 @@ describe('order-place-handler', () => { orderPlace: { order: orderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_OPENED, }, }); @@ -813,38 +809,31 @@ describe('order-place-handler', () => { redisTestConstants.defaultOrderGoodTilBlockTime, redisTestConstants.defaultRedisOrderGoodTilBlockTime, dbOrderGoodTilBlockTime, - false, ], [ 'conditional', redisTestConstants.defaultConditionalOrder, redisTestConstants.defaultRedisOrderConditional, dbConditionalOrder, - false, ], [ 'good-til-block-time', redisTestConstants.defaultOrderGoodTilBlockTime, redisTestConstants.defaultRedisOrderGoodTilBlockTime, dbOrderGoodTilBlockTime, - true, ], [ 'conditional', redisTestConstants.defaultConditionalOrder, redisTestConstants.defaultRedisOrderConditional, dbConditionalOrder, - true, ], ])('handles order place with OPEN placement status, exists initially (with %s)', async ( _name: string, orderToPlace: IndexerOrder, expectedRedisOrder: RedisOrder, placedOrder: OrderFromDatabase, - sendSubaccountWebsocketMessage: boolean, ) => { - // eslint-disable-next-line max-len - config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS = sendSubaccountWebsocketMessage; synchronizeWrapBackgroundTask(wrapBackgroundTask); const producerSendSpy: jest.SpyInstance = jest.spyOn(producer, 'send').mockReturnThis(); // Handle the order place event for the initial order with BEST_EFFORT_OPENED @@ -853,7 +842,7 @@ describe('order-place-handler', () => { orderPlace: { order: orderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }); expectWebsocketMessagesSent( @@ -875,7 +864,7 @@ describe('order-place-handler', () => { orderPlace: { order: orderToPlace, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_OPENED, }, }); expectWebsocketMessagesSent( @@ -885,9 +874,7 @@ describe('order-place-handler', () => { testConstants.defaultPerpetualMarket, APIOrderStatusEnum.OPEN, // Subaccount messages should be sent for stateful order with OPEN status - !( - isLongTermOrder(expectedRedisOrder.order!.orderId!.orderFlags) && - !sendSubaccountWebsocketMessage), + true, ); expect(logger.error).not.toHaveBeenCalled(); @@ -1014,7 +1001,7 @@ describe('order-place-handler', () => { }, }, placementStatus: - OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, + OrderPlaceV1_OrderPlacementStatus.ORDER_PLACEMENT_STATUS_BEST_EFFORT_OPENED, }, }; const message: KafkaMessage = createKafkaMessage( diff --git a/indexer/services/vulcan/src/handlers/order-place-handler.ts b/indexer/services/vulcan/src/handlers/order-place-handler.ts index d32021c43b..1f1b343c39 100644 --- a/indexer/services/vulcan/src/handlers/order-place-handler.ts +++ b/indexer/services/vulcan/src/handlers/order-place-handler.ts @@ -9,17 +9,12 @@ import { } from '@dydxprotocol-indexer/postgres'; import { CanceledOrdersCache, + convertToRedisOrder, placeOrder, PlaceOrderResult, StatefulOrderUpdatesCache, - convertToRedisOrder, } from '@dydxprotocol-indexer/redis'; -import { - getOrderIdHash, - isLongTermOrder, - isStatefulOrder, - ORDER_FLAG_SHORT_TERM, -} from '@dydxprotocol-indexer/v4-proto-parser'; +import { getOrderIdHash, isStatefulOrder, ORDER_FLAG_SHORT_TERM } from '@dydxprotocol-indexer/v4-proto-parser'; import { IndexerOrder, OffChainUpdateV1, @@ -102,7 +97,6 @@ export class OrderPlaceHandler extends Handler { orderPlace, placeOrderResult, placementStatus, - redisOrder, )) { // TODO(IND-171): Determine whether we should always be sending a message, even when the cache // isn't updated. @@ -168,15 +162,7 @@ export class OrderPlaceHandler extends Handler { orderPlace: OrderPlaceV1, placeOrderResult: PlaceOrderResult, placementStatus: OrderPlaceV1_OrderPlacementStatus, - redisOrder: RedisOrder, ): boolean { - if ( - isLongTermOrder(redisOrder.order!.orderId!.orderFlags) && - !config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS - ) { - return false; - } - const orderFlags: number = orderPlace.order!.orderId!.orderFlags; const status: OrderPlaceV1_OrderPlacementStatus = orderPlace.placementStatus; // Best-effort-opened status should only be sent for short-term orders