-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathwebsocket-api-client.ts
More file actions
204 lines (188 loc) · 5.37 KB
/
websocket-api-client.ts
File metadata and controls
204 lines (188 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { CancelOrderRequestV3 } from './types/request/v3/trade.js';
import { CancelOrderResponseV3 } from './types/response/v3/trade.js';
import { WSAPIResponse } from './types/websockets/ws-api.js';
import { WSAPIPlaceOrderRequestV3 } from './types/websockets/ws-api-request.js';
import { WSAPIPlaceOrderResponseV3 } from './types/websockets/ws-api-response.js';
import {
BitgetInstTypeV3,
WSClientConfigurableOptions,
} from './types/websockets/ws-general.js';
import { DefaultLogger } from './util/logger.js';
import { WS_KEY_MAP } from './util/websocket-util.js';
import { WebsocketClientV3 } from './websocket-client-v3.js';
/**
* Configurable options specific to only the REST-like WebsocketAPIClient
*/
export interface WSAPIClientConfigurableOptions {
/**
* Default: true
*
* Attach default event listeners, which will console log any high level
* events (opened/reconnecting/reconnected/etc).
*
* If you disable this, you should set your own event listeners
* on the embedded WS Client `wsApiClient.getWSClient().on(....)`.
*/
attachEventListeners: boolean;
}
/**
* This is a minimal Websocket API wrapper around the WebsocketClient.
*
* Note: You can also directly use the sendWSAPIRequest() method to make WS API calls, but some
* may find the below methods slightly more intuitive.
*
* Refer to the WS API promises example for a more detailed example on using sendWSAPIRequest() directly:
* https://github.com/tiagosiebler/bitget-api/blob/master/examples/V3/ws-api-trade-raw.ts
*/
export class WebsocketAPIClient {
private wsClient: WebsocketClientV3;
private options: WSClientConfigurableOptions & WSAPIClientConfigurableOptions;
constructor(
options?: WSClientConfigurableOptions &
Partial<WSAPIClientConfigurableOptions>,
logger?: DefaultLogger,
) {
this.wsClient = new WebsocketClientV3(options, logger);
this.options = {
attachEventListeners: true,
...options,
};
this.setupDefaultEventListeners();
}
public getWSClient(): WebsocketClientV3 {
return this.wsClient;
}
public setTimeOffsetMs(newOffset: number): void {
return this.getWSClient().setTimeOffsetMs(newOffset);
}
/*
* Bitget WebSocket API Methods
* https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel
*/
/**
* Submit a new order
*
* https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel
*
* @returns
*/
submitNewOrder(
category: BitgetInstTypeV3,
params: WSAPIPlaceOrderRequestV3,
): Promise<WSAPIResponse<[WSAPIPlaceOrderResponseV3], 'place-order'>> {
return this.wsClient.sendWSAPIRequest(
WS_KEY_MAP.v3Private,
'place-order',
category,
params,
);
}
/**
* Submit a new order
*
* https://www.bitget.com/api-doc/uta/websocket/private/Batch-Place-Order-Channel
*
* @returns
*/
placeBatchOrders(
category: BitgetInstTypeV3,
params: WSAPIPlaceOrderRequestV3[],
): Promise<WSAPIResponse<WSAPIPlaceOrderResponseV3[], 'batch-place'>> {
return this.wsClient.sendWSAPIRequest(
WS_KEY_MAP.v3Private,
'batch-place',
category,
params,
);
}
/*
* Bitget WebSocket API Methods
* https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel
*/
/**
* Cancel Order
*
* https://www.bitget.com/api-doc/uta/websocket/private/Cancel-Order-Channel
*
* @returns
*/
cancelOrder(
category: BitgetInstTypeV3,
params: CancelOrderRequestV3,
): Promise<WSAPIResponse<[CancelOrderResponseV3], 'cancel-order'>> {
return this.wsClient.sendWSAPIRequest(
WS_KEY_MAP.v3Private,
'cancel-order',
category,
params,
);
}
/**
* Batch Cancel Order
*
* https://www.bitget.com/api-doc/uta/websocket/private/Batch-Cancel-Order-Channel
*
* @returns
*/
cancelBatchOrders(
category: BitgetInstTypeV3,
params: CancelOrderRequestV3[],
): Promise<WSAPIResponse<CancelOrderResponseV3[], 'batch-cancel'>> {
return this.wsClient.sendWSAPIRequest(
WS_KEY_MAP.v3Private,
'batch-cancel',
category,
params,
);
}
/**
*
*
*
*
*
*
*
* Private methods for handling some of the convenience/automation provided by the WS API Client
*
*
*
*
*
*
*
*/
private setupDefaultEventListeners() {
if (this.options.attachEventListeners) {
/**
* General event handlers for monitoring the WebsocketClient
*/
this.wsClient
.on('open', (data) => {
console.log(new Date(), 'ws connected', data.wsKey);
})
.on('reconnect', ({ wsKey }) => {
console.log(new Date(), 'ws automatically reconnecting.... ', wsKey);
})
.on('reconnected', (data) => {
console.log(new Date(), 'ws has reconnected ', data?.wsKey);
})
.on('authenticated', (data) => {
console.info(new Date(), 'ws has authenticated ', data?.wsKey);
})
.on('exception', (data) => {
try {
// Blind JSON.stringify can fail on circular references
console.error(
new Date(),
'ws exception: ',
JSON.stringify(data),
// JSON.stringify({ ...data, target: 'WebSocket' }),
);
} catch {
console.error(new Date(), 'ws exception: ', data);
}
});
}
}
}