Add type guards for ws order events and ws execution events
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
* Use type guards to narrow down types with minimal efforts.
|
* 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)
|
* Type guard to detect a V5 orderbook event (delta & snapshots)
|
||||||
@@ -27,3 +27,43 @@ export function isWsOrderbookEventV5(
|
|||||||
event['topic'].startsWith('orderbook')
|
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';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user