Skip to content

Commit

Permalink
v3.8.1: feat() add type guard and type for ws orderbook events
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Jan 17, 2024
1 parent 1ac1aa5 commit 9cb22d1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bybit-api",
"version": "3.8.0",
"version": "3.8.1",
"description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
29 changes: 29 additions & 0 deletions src/types/websocket.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ import {
} from './v5-shared';
import { WsKey } from './websockets';

export interface WSOrderbookEventV5 {
topic: string;
/** Event timestamp */
ts: number;
type: 'delta' | 'snapshot';
data: {
/** Symbol */
s: string;
/** [price, qty][] */
b: [string, string][];
/** [price, qty][] */
a: [string, string][];
/** Update ID */
u: number;
/**
* Cross sequence
*/
seq: number;
};
/**
* matching engine timestamp (correlated with T from public trade channel)
*/
cts: number;
/**
* Internal reference, can be used to determine if this is spot/linear/inverse/etc
*/
wsKey: WsKey;
}

export interface WSAccountOrderV5 {
qty: string;
price: string;
Expand Down
5 changes: 3 additions & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './BaseRestClient';
export * from './requestUtils';
export * from './WsStore';
export * from './logger';
export * from './requestUtils';
export * from './typeGuards';
export * from './websocket-util';
export * from './WsStore';
29 changes: 29 additions & 0 deletions src/util/typeGuards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Use type guards to narrow down types with minimal efforts.
*/

import { WSOrderbookEventV5 } from '../types/websocket.events';

/**
* Type guard to detect a V5 orderbook event (delta & snapshots)
*
* @param event
* @returns
*/
export function isWsOrderbookEventV5(
event: unknown,
): event is WSOrderbookEventV5 {
if (
typeof event !== 'object' ||
!event ||
typeof event['topic'] !== 'string' ||
typeof event['type'] !== 'string'
) {
return false;
}

return (
['delta', 'snapshot'].includes(event['type']) &&
event['topic'].startsWith('orderbook')
);
}

0 comments on commit 9cb22d1

Please sign in to comment.