From a2f40b6b6ec2806b51393ea2386ed3a096bf2d36 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:04:03 +0200 Subject: [PATCH] Add type guards for ws order events and ws execution events --- src/util/typeGuards.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/util/typeGuards.ts b/src/util/typeGuards.ts index e6de059..c8b42a0 100644 --- a/src/util/typeGuards.ts +++ b/src/util/typeGuards.ts @@ -2,7 +2,7 @@ * Use type guards to narrow down types with minimal efforts. */ -import { WSOrderbookEventV5 } from '../types/websocket.events'; +import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5 } from '../types/websocket.events'; /** * Type guard to detect a V5 orderbook event (delta & snapshots) @@ -27,3 +27,43 @@ export function isWsOrderbookEventV5( event['topic'].startsWith('orderbook') ); } + +/** + * Type guard to detect a V5 order event. + * + * @param event + * @returns + */ +export function isWsAccountOrderEventV5( + event: unknown, +): event is WSAccountOrderEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'order'; +} + +/** + * Type guard to detect a V5 execution event. + * + * @param event + * @returns + */ +export function isWsExecutionEventV5( + event: unknown, +): event is WSExecutionEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'execution'; +} \ No newline at end of file