Skip to content

Commit 6b0479c

Browse files
committed
chore(): more refactoring around base ws architecture
1 parent af538ec commit 6b0479c

File tree

7 files changed

+607
-621
lines changed

7 files changed

+607
-621
lines changed

src/types/websockets/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './request';
2+
export * from './ws-api';
23
export * from './ws-events';
34
export * from './ws-general';

src/types/websockets/request.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export type WsTradeOp =
1313
| 'amend-order'
1414
| 'batch-amend-order';
1515

16-
export type WsRequestOp = 'login' | 'subscribe' | 'unsubscribe';
17-
1816
export type WsPrivateChannel =
1917
| 'account'
2018
| 'positions'
@@ -171,11 +169,6 @@ export type WsBusinessChannel =
171169

172170
export type WsChannel = WsPublicChannel | WsPrivateChannel | WsBusinessChannel;
173171

174-
export interface WsBaseRequest {
175-
op: WsRequestOp;
176-
args: any[];
177-
}
178-
179172
/** Used to trigger order actions over websockets (e.g. placing & cancelling orders) */
180173
export interface WsTradeBaseRequest {
181174
op: WsTradeOp;
@@ -303,24 +296,3 @@ export type WsChannelSubUnSubRequestArg =
303296
| WsPublicChannelArgOptionSummary
304297
| WsPublicChannelArgStatus
305298
| WsPublicChannelArgLiquidationOrders;
306-
307-
/**
308-
*
309-
* Top level requests with args
310-
*
311-
*/
312-
313-
export interface WsSubRequest extends WsBaseRequest {
314-
op: 'subscribe';
315-
args: WsChannelSubUnSubRequestArg[];
316-
}
317-
318-
export interface WsUnsubRequest extends WsBaseRequest {
319-
op: 'unsubscribe';
320-
args: WsChannelSubUnSubRequestArg[];
321-
}
322-
323-
export interface WsAuthRequest extends WsBaseRequest {
324-
op: 'login';
325-
args: WsAuthRequestArg[];
326-
}

src/types/websockets/ws-api.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { WS_KEY_MAP, WsKey } from '../../util';
2+
import { WsAuthRequestArg, WsChannelSubUnSubRequestArg } from './request';
23

34
export interface WSAPIRequestFlags {
45
/** If true, will skip auth requirement for WS API connection */
@@ -7,6 +8,47 @@ export interface WSAPIRequestFlags {
78

89
export type WSOperation = 'subscribe' | 'unsubscribe' | 'login';
910

11+
/**
12+
*
13+
* Top level requests with args
14+
*
15+
*/
16+
17+
/**
18+
19+
/**
20+
* request looks like this:
21+
{
22+
"id": "1512",
23+
"op": "subscribe",
24+
"args": [
25+
{
26+
"channel": "tickers",
27+
"instId": "BTC-USDT"
28+
}
29+
]
30+
}
31+
*/
32+
export interface WsRequestOperationOKX<TWSRequestArg> {
33+
id: string;
34+
op: WSOperation;
35+
args?: TWSRequestArg[];
36+
}
37+
38+
export interface WsSubRequest
39+
extends WsRequestOperationOKX<WsChannelSubUnSubRequestArg> {
40+
op: 'subscribe';
41+
}
42+
43+
export interface WsUnsubRequest
44+
extends WsRequestOperationOKX<WsChannelSubUnSubRequestArg> {
45+
op: 'unsubscribe';
46+
}
47+
48+
export interface WsAuthRequest extends WsRequestOperationOKX<WsAuthRequestArg> {
49+
op: 'login';
50+
}
51+
1052
// When new WS API operations are added, make sure to also update WS_API_Operations[] below
1153
export type WSAPIOperation =
1254
| 'place-order'
@@ -21,6 +63,7 @@ export const WS_API_Operations: WSAPIOperation[] = [
2163
'batch-cancel',
2264
];
2365

66+
// TODO:
2467
export interface WSAPIRequestOKX<TSomething> {
2568
id: 1;
2669
empty: TSomething;

src/util/BaseWSClient.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { WSConnectedResult, WsConnectionStateEnum } from './WsStore.types';
2424

2525
export interface WSClientEventMap<
2626
WsKey extends string,
27-
TUpdateEventData = any,
27+
TWSUpdateEventData = any,
2828
> {
2929
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
3030
open: (evt: {
@@ -55,7 +55,7 @@ export interface WSClientEventMap<
5555
update: (response: any & { wsKey: WsKey }) => void;
5656

5757
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
58-
exception: (response: TUpdateEventData & { wsKey: WsKey }) => void;
58+
exception: (response: TWSUpdateEventData & { wsKey: WsKey }) => void;
5959

6060
/** Confirmation that a connection successfully authenticated */
6161
authenticated: (event: { wsKey: WsKey; event: any }) => void;
@@ -66,15 +66,16 @@ export interface BaseWebsocketClient<
6666
TWSKey extends string,
6767
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6868
TWSRequestEvent extends object,
69+
TWSUpdateEventData = any,
6970
> {
70-
on<U extends keyof WSClientEventMap<TWSKey>>(
71+
on<U extends keyof WSClientEventMap<TWSKey, TWSUpdateEventData>>(
7172
event: U,
72-
listener: WSClientEventMap<TWSKey>[U],
73+
listener: WSClientEventMap<TWSKey, TWSUpdateEventData>[U],
7374
): this;
7475

75-
emit<U extends keyof WSClientEventMap<TWSKey>>(
76+
emit<U extends keyof WSClientEventMap<TWSKey, TWSUpdateEventData>>(
7677
event: U,
77-
...args: Parameters<WSClientEventMap<TWSKey>[U]>
78+
...args: Parameters<WSClientEventMap<TWSKey, TWSUpdateEventData>[U]>
7879
): boolean;
7980
}
8081

@@ -177,7 +178,8 @@ export abstract class BaseWebsocketClient<
177178
market: 'GLOBAL',
178179

179180
demoTrading: false,
180-
pongTimeout: 1000,
181+
182+
pongTimeout: 2000,
181183
pingInterval: 10000,
182184
reconnectTimeout: 500,
183185

src/util/websocket-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export function getWsUrlForWsKey(
344344
}
345345
}
346346

347-
export function getMaxTopicsPerSubscribeEvent(
347+
export function getMaxTopicsPerSubscribeEventForMarket(
348348
market: APIMarket,
349349
): number | null {
350350
switch (market) {

src/websocket-client-legacy.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './types';
1717
import {
1818
DefaultLogger,
19-
getMaxTopicsPerSubscribeEvent,
19+
getMaxTopicsPerSubscribeEventForMarket,
2020
getWsKeyForTopicChannel,
2121
isConnCountEvent,
2222
isWsDataEvent,
@@ -43,7 +43,7 @@ import WsStore from './util/WsStore';
4343
import { WsConnectionStateEnum } from './util/WsStore.types';
4444

4545
// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
46-
export declare interface WebsocketClient {
46+
export declare interface WebsocketClientLegacy {
4747
on<U extends keyof WSClientEventMap<WsKey, WsDataEvent>>(
4848
event: U,
4949
listener: WSClientEventMap<WsKey>[U],
@@ -55,17 +55,14 @@ export declare interface WebsocketClient {
5555
): boolean;
5656
}
5757

58-
export class WebsocketClient extends EventEmitter {
59-
private logger: typeof DefaultLogger;
58+
export class WebsocketClientLegacy extends EventEmitter {
59+
private logger: DefaultLogger;
6060

6161
private options: WebsocketClientOptions;
6262

6363
private wsStore: WsStore<WsKey, WsChannelSubUnSubRequestArg>;
6464

65-
constructor(
66-
options: WSClientConfigurableOptions,
67-
logger?: typeof DefaultLogger,
68-
) {
65+
constructor(options: WSClientConfigurableOptions, logger?: DefaultLogger) {
6966
super();
7067

7168
this.logger = logger || DefaultLogger;
@@ -87,7 +84,7 @@ export class WebsocketClient extends EventEmitter {
8784

8885
if ((this.options.market as any) === 'demo') {
8986
throw new Error(
90-
'ERROR: to use demo trading, set the "demoTrading: true" flag in the constructor',
87+
'ERROR: to use demo trading, set the "demoTrading: true" flag in the constructor. The "demo" market is not valid any more.',
9188
);
9289
}
9390
}
@@ -296,7 +293,7 @@ export class WebsocketClient extends EventEmitter {
296293
*/
297294
private async getWsAuthRequest(
298295
wsKey: WsKey,
299-
): Promise<WsAuthRequest | undefined> {
296+
): Promise<Omit<WsAuthRequest, 'id'> | undefined> {
300297
const isPublicWsKey = PUBLIC_WS_KEYS.includes(wsKey);
301298
const accounts = this.options.accounts;
302299
const hasAccountsToAuth = !!accounts?.length;
@@ -339,7 +336,7 @@ export class WebsocketClient extends EventEmitter {
339336
(request): request is WsAuthRequestArg => !!request,
340337
);
341338

342-
const authParams: WsAuthRequest = {
339+
const authParams: Omit<WsAuthRequest, 'id'> = {
343340
op: 'login',
344341
args: authRequests,
345342
};
@@ -539,7 +536,7 @@ export class WebsocketClient extends EventEmitter {
539536
return;
540537
}
541538

542-
const maxTopicsPerEvent = getMaxTopicsPerSubscribeEvent(
539+
const maxTopicsPerEvent = getMaxTopicsPerSubscribeEventForMarket(
543540
this.options.market,
544541
);
545542
if (maxTopicsPerEvent && topics.length > maxTopicsPerEvent) {
@@ -557,7 +554,7 @@ export class WebsocketClient extends EventEmitter {
557554
return;
558555
}
559556

560-
const request: WsSubRequest = {
557+
const request: Omit<WsSubRequest, 'id'> = {
561558
op: 'subscribe',
562559
args: topics,
563560
};
@@ -578,7 +575,7 @@ export class WebsocketClient extends EventEmitter {
578575
return;
579576
}
580577

581-
const maxTopicsPerEvent = getMaxTopicsPerSubscribeEvent(
578+
const maxTopicsPerEvent = getMaxTopicsPerSubscribeEventForMarket(
582579
this.options.market,
583580
);
584581
if (maxTopicsPerEvent && topics.length > maxTopicsPerEvent) {
@@ -596,7 +593,7 @@ export class WebsocketClient extends EventEmitter {
596593
return;
597594
}
598595

599-
const request: WsUnsubRequest = {
596+
const request: Omit<WsUnsubRequest, 'id'> = {
600597
op: 'unsubscribe',
601598
args: topics,
602599
};

0 commit comments

Comments
 (0)