Skip to content

Commit dcc299b

Browse files
committed
feat(): upgrade WsStore to latest generation. Refactor DefaultLogger. Begin preparing base WS client/
1 parent 632bfed commit dcc299b

File tree

15 files changed

+2509
-221
lines changed

15 files changed

+2509
-221
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
export * from './';
21
export * from './coinm-client';
3-
export * from './logger';
42
export * from './main-client';
53
export * from './portfolio-client';
64
export * from './types/coin';
75
export * from './types/futures';
6+
export * from './types/portfolio-margin';
87
export * from './types/shared';
98
export * from './types/spot';
109
export * from './types/websockets';
1110
export * from './usdm-client';
11+
export * from './util/logger';
1212
export * from './util/requestUtils';
1313
export * from './util/typeGuards';
1414
export * from './util/usdm';
15-
export * from './util/WsStore';
15+
export * from './util/websockets/WsStore';
1616
export * from './websocket-client';

src/logger.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/types/websockets/ws-api.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { WS_KEY_MAP } from '../../util/websockets/websocket-util';
2+
import { WsKey } from './ws-general';
3+
4+
export type WSAPIOperation = 'order.create' | 'order.amend' | 'order.cancel';
5+
6+
export type WsOperation =
7+
| 'subscribe'
8+
| 'unsubscribe'
9+
| 'auth'
10+
| 'ping'
11+
| 'pong';
12+
13+
export const WS_API_Operations: WSAPIOperation[] = [
14+
'order.create',
15+
'order.amend',
16+
'order.cancel',
17+
];
18+
19+
export interface WsRequestOperationBybit<TWSTopic extends string> {
20+
req_id: string;
21+
op: WsOperation;
22+
args?: (TWSTopic | string | number)[];
23+
}
24+
25+
export interface WSAPIRequest<
26+
TRequestParams = undefined,
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
TWSOperation extends WSAPIOperation = any,
29+
> {
30+
reqId: string;
31+
op: TWSOperation;
32+
header: {
33+
'X-BAPI-TIMESTAMP': string;
34+
'X-BAPI-RECV-WINDOW': string;
35+
// Referer: typeof APIID;
36+
};
37+
args: [TRequestParams];
38+
}
39+
40+
export interface WSAPIResponse<
41+
TResponseData extends object = object,
42+
TOperation extends WSAPIOperation = WSAPIOperation,
43+
> {
44+
wsKey: WsKey;
45+
/** Auto-generated */
46+
reqId: string;
47+
retCode: 0 | number;
48+
retMsg: 'OK' | string;
49+
op: TOperation;
50+
data: TResponseData;
51+
header?: {
52+
'X-Bapi-Limit': string;
53+
'X-Bapi-Limit-Status': string;
54+
'X-Bapi-Limit-Reset-Timestamp': string;
55+
Traceid: string;
56+
Timenow: string;
57+
};
58+
connId: string;
59+
}
60+
61+
export type Exact<T> = {
62+
// This part says: if there's any key that's not in T, it's an error
63+
[K: string]: never;
64+
} & {
65+
[K in keyof T]: T[K];
66+
};
67+
68+
/**
69+
* List of operations supported for this WsKey (connection)
70+
*/
71+
export interface WsAPIWsKeyTopicMap {
72+
[WS_KEY_MAP.v5PrivateTrade]: WSAPIOperation;
73+
}
74+
75+
/**
76+
* Request parameters expected per operation
77+
*/
78+
export interface WsAPITopicRequestParamMap {
79+
'order.create': never;
80+
}
81+
82+
/**
83+
* Response structure expected for each operation
84+
*/
85+
export interface WsAPIOperationResponseMap {
86+
'order.create': WSAPIResponse<never, 'order.create'>;
87+
ping: {
88+
retCode: 0 | number;
89+
retMsg: 'OK' | string;
90+
op: 'pong';
91+
data: [string];
92+
connId: string;
93+
};
94+
}

src/types/websockets/ws-general.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { RestClientOptions } from '../../util/requestUtils';
2+
import { WS_KEY_MAP } from '../../util/websockets/websocket-util';
3+
4+
/** For spot markets, spotV3 is recommended */
5+
export type APIMarket = 'v5';
6+
7+
// Same as inverse futures
8+
export type WsPublicInverseTopic =
9+
| 'orderBookL2_25'
10+
| 'orderBookL2_200'
11+
| 'trade'
12+
| 'insurance'
13+
| 'instrument_info'
14+
| 'klineV2';
15+
16+
export type WsPublicUSDTPerpTopic =
17+
| 'orderBookL2_25'
18+
| 'orderBookL2_200'
19+
| 'trade'
20+
| 'insurance'
21+
| 'instrument_info'
22+
| 'kline';
23+
24+
export type WsPublicSpotV1Topic =
25+
| 'trade'
26+
| 'realtimes'
27+
| 'kline'
28+
| 'depth'
29+
| 'mergedDepth'
30+
| 'diffDepth';
31+
32+
export type WsPublicSpotV2Topic =
33+
| 'depth'
34+
| 'kline'
35+
| 'trade'
36+
| 'bookTicker'
37+
| 'realtimes';
38+
39+
export type WsPublicTopics =
40+
| WsPublicInverseTopic
41+
| WsPublicUSDTPerpTopic
42+
| WsPublicSpotV1Topic
43+
| WsPublicSpotV2Topic
44+
| string;
45+
46+
// Same as inverse futures
47+
export type WsPrivateInverseTopic =
48+
| 'position'
49+
| 'execution'
50+
| 'order'
51+
| 'stop_order';
52+
53+
export type WsPrivateUSDTPerpTopic =
54+
| 'position'
55+
| 'execution'
56+
| 'order'
57+
| 'stop_order'
58+
| 'wallet';
59+
60+
export type WsPrivateSpotTopic =
61+
| 'outboundAccountInfo'
62+
| 'executionReport'
63+
| 'ticketInfo';
64+
65+
export type WsPrivateTopic =
66+
| WsPrivateInverseTopic
67+
| WsPrivateUSDTPerpTopic
68+
| WsPrivateSpotTopic
69+
| string;
70+
71+
export type WsTopic = WsPublicTopics | WsPrivateTopic;
72+
73+
/** This is used to differentiate between each of the available websocket streams (as bybit has multiple websockets) */
74+
export type WsKey = (typeof WS_KEY_MAP)[keyof typeof WS_KEY_MAP];
75+
export type WsMarket = 'all';
76+
77+
export interface WSClientConfigurableOptions {
78+
/** Your API key */
79+
key?: string;
80+
81+
/** Your API secret */
82+
secret?: string;
83+
84+
/**
85+
* Set to `true` to connect to Bybit's testnet environment.
86+
*
87+
* Notes:
88+
*
89+
* - If demo trading, `testnet` should be set to false!
90+
* - If testing a strategy, use demo trading instead. Testnet market data is very different from real market conditions.
91+
*/
92+
testnet?: boolean;
93+
94+
/**
95+
* Set to `true` to connect to Bybit's V5 demo trading: https://bybit-exchange.github.io/docs/v5/demo
96+
*
97+
* Only the "V5" "market" is supported here.
98+
*/
99+
demoTrading?: boolean;
100+
101+
/**
102+
* The API group this client should connect to. The V5 market is currently used by default.
103+
*
104+
* Only the "V5" "market" is supported here.
105+
*/
106+
market?: APIMarket;
107+
108+
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
109+
recvWindow?: number;
110+
111+
/** How often to check if the connection is alive */
112+
pingInterval?: number;
113+
114+
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
115+
pongTimeout?: number;
116+
117+
/** Delay in milliseconds before respawning the connection */
118+
reconnectTimeout?: number;
119+
120+
restOptions?: RestClientOptions;
121+
122+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
123+
requestOptions?: any;
124+
125+
wsUrl?: string;
126+
127+
/**
128+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
129+
*
130+
* Look in the examples folder for a demonstration on using node's createHmac instead.
131+
*/
132+
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
133+
}
134+
135+
/**
136+
* WS configuration that's always defined, regardless of user configuration
137+
* (usually comes from defaults if there's no user-provided values)
138+
*/
139+
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
140+
market: APIMarket;
141+
pongTimeout: number;
142+
pingInterval: number;
143+
reconnectTimeout: number;
144+
recvWindow: number;
145+
authPrivateConnectionsOnConnect: boolean;
146+
authPrivateRequests: boolean;
147+
}

0 commit comments

Comments
 (0)