Skip to content

Commit

Permalink
gate with flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed May 20, 2024
1 parent 25a7e84 commit b2a772c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
2 changes: 1 addition & 1 deletion indexer/packages/kafka/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const TO_ENDER_TOPIC: string = 'to-ender';

export const ORDERBOOKS_WEBSOCKET_MESSAGE_VERSION: string = '1.0.0';
export const SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION: string = '2.4.0';
export const SUBACCOUNTS_WEBSOCKET_MESSAGE_VERSION: string = '2.4.1';
export const TRADES_WEBSOCKET_MESSAGE_VERSION: string = '2.1.0';
export const MARKETS_WEBSOCKET_MESSAGE_VERSION: string = '1.0.0';
export const CANDLES_WEBSOCKET_MESSAGE_VERSION: string = '1.0.0';
24 changes: 12 additions & 12 deletions indexer/services/comlink/public/websocket-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,27 @@ export interface OrderSubaccountMessageContents {
id: string;
subaccountId: string;
clientId: string;
clobPairId: string;
side: OrderSide;
size: string;
ticker: string,
price: string;
type: OrderType;
timeInForce: APITimeInForce;
postOnly: boolean;
reduceOnly: boolean;
clobPairId?: string;
side?: OrderSide;
size?: string;
ticker?: string,
price?: string;
type?: OrderType;
timeInForce?: APITimeInForce;
postOnly?: boolean;
reduceOnly?: boolean;
status: APIOrderStatus;
orderFlags: string;
totalFilled?: string;
totalOptimisticFilled?: string;
goodTilBlock?: string;
goodTilBlockTime?: string;
removalReason?: string;
createdAtHeight?: string;
clientMetadata: string;
triggerPrice?: string;
updatedAt?: IsoString;
updatedAtHeight?: string;
removalReason?: string;
createdAtHeight?: string;
clientMetadata?: string;
}

export enum OrderSide {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
import { expectWebsocketOrderbookMessage, expectWebsocketSubaccountMessage } from '../helpers/websocket-helpers';
import { ORDER_FLAG_LONG_TERM } from '@dydxprotocol-indexer/v4-proto-parser';
import Long from 'long';
import config from '../../src/config';

jest.mock('@dydxprotocol-indexer/base', () => ({
...jest.requireActual('@dydxprotocol-indexer/base'),
Expand All @@ -99,6 +100,7 @@ describe('OrderRemoveHandler', () => {
await dbHelpers.clearData();
await redis.deleteAllAsync(redisClient);
jest.resetAllMocks();
config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_CANCELS_MISSING_ORDERS = false;
});

afterAll(async () => {
Expand Down Expand Up @@ -202,7 +204,26 @@ describe('OrderRemoveHandler', () => {
});

describe('Order Remove Message - not a Stateful Cancelation', () => {
it('successfully returns early if unable to find order in redis', async () => {
const offChainUpdate: OffChainUpdateV1 = orderRemoveToOffChainUpdate(defaultOrderRemove);

const orderRemoveHandler: OrderRemoveHandler = new OrderRemoveHandler();
await orderRemoveHandler.handleUpdate(
offChainUpdate,
defaultKafkaHeaders,
);

expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({
at: 'orderRemoveHandler#handleOrderRemoval',
message: 'Unable to find order',
orderId: defaultOrderRemove.removedOrderId,
}));
expect(logger.error).not.toHaveBeenCalled();
expectTimingStats();
});

it('successfully sends subaccount websocket message and returns if unable to find order in redis', async () => {
config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_CANCELS_MISSING_ORDERS = true;
const offChainUpdate: OffChainUpdateV1 = orderRemoveToOffChainUpdate(defaultOrderRemove);
const producerSendSpy: jest.SpyInstance = jest.spyOn(producer, 'send').mockReturnThis();

Expand Down Expand Up @@ -248,6 +269,7 @@ describe('OrderRemoveHandler', () => {

it('successfully sends subaccount websocket message with db order fields if unable to find order in redis',
async () => {
config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_CANCELS_MISSING_ORDERS = true;
await OrderTable.create(testConstants.defaultOrder);
const offChainUpdate: OffChainUpdateV1 = orderRemoveToOffChainUpdate(defaultOrderRemove);
const producerSendSpy: jest.SpyInstance = jest.spyOn(producer, 'send').mockReturnThis();
Expand Down
3 changes: 3 additions & 0 deletions indexer/services/vulcan/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const configSchema = {
SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_STATEFUL_ORDERS: parseBoolean({
default: true,
}),
SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_CANCELS_MISSING_ORDERS: parseBoolean({
default: false,
}),
};

export default parseSchema(configSchema);
38 changes: 20 additions & 18 deletions indexer/services/vulcan/src/handlers/order-remove-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,26 @@ export class OrderRemoveHandler extends Handler {
orderId: orderRemove.removedOrderId,
orderRemove,
});
const canceledOrder: OrderFromDatabase | undefined = await runFuncWithTimingStat(
OrderTable.findById(OrderTable.orderIdToUuid(orderRemove.removedOrderId!)),
this.generateTimingStatsOptions('find_order'),
);
const subaccountMessage: Message = {
value: this.createSubaccountWebsocketMessageFromOrderRemoveMessage(
canceledOrder,
orderRemove,
perpetualMarket.ticker,
),
headers,
};
const reason: OrderRemovalReason = orderRemove.reason;
if (!(
reason === OrderRemovalReason.ORDER_REMOVAL_REASON_INDEXER_EXPIRED ||
reason === OrderRemovalReason.ORDER_REMOVAL_REASON_FULLY_FILLED
)) {
sendMessageWrapper(subaccountMessage, KafkaTopics.TO_WEBSOCKETS_SUBACCOUNTS);
if (config.SEND_SUBACCOUNT_WEBSOCKET_MESSAGE_FOR_CANCELS_MISSING_ORDERS) {
const canceledOrder: OrderFromDatabase | undefined = await runFuncWithTimingStat(
OrderTable.findById(OrderTable.orderIdToUuid(orderRemove.removedOrderId!)),
this.generateTimingStatsOptions('find_order'),
);
const subaccountMessage: Message = {
value: this.createSubaccountWebsocketMessageFromOrderRemoveMessage(
canceledOrder,
orderRemove,
perpetualMarket.ticker,
),
headers,
};
const reason: OrderRemovalReason = orderRemove.reason;
if (!(
reason === OrderRemovalReason.ORDER_REMOVAL_REASON_INDEXER_EXPIRED ||
reason === OrderRemovalReason.ORDER_REMOVAL_REASON_FULLY_FILLED
)) {
sendMessageWrapper(subaccountMessage, KafkaTopics.TO_WEBSOCKETS_SUBACCOUNTS);
}
}
return;
}
Expand Down

0 comments on commit b2a772c

Please sign in to comment.