@binance/margin-trading 5.0.1 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  This is a client library for the Binance Margin Trading API, enabling developers to interact programmatically with Binance's Margin Trading trading platform. The library provides tools to use funds provided by a third party to conduct asset transactions through the REST API:
12
12
 
13
13
  - [REST API](./src/rest-api/rest-api.ts)
14
+ - [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
14
15
 
15
16
  ## Table of Contents
16
17
 
@@ -18,6 +19,7 @@ This is a client library for the Binance Margin Trading API, enabling developers
18
19
  - [Installation](#installation)
19
20
  - [Documentation](#documentation)
20
21
  - [REST APIs](#rest-apis)
22
+ - [Websocket Streams](#websocket-streams)
21
23
  - [Testing](#testing)
22
24
  - [Migration Guide](#migration-guide)
23
25
  - [Contributing](#contributing)
@@ -143,6 +145,129 @@ See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed
143
145
 
144
146
  If `basePath` is not provided, it defaults to `https://api.binance.com`.
145
147
 
148
+ ### Websocket Streams
149
+
150
+ WebSocket Streams in `margin-trading` is used for subscribing to risk and trade data streams. Use the [websocket-streams](./src/websocket-streams/websocket-streams.ts) module to interact with it.
151
+
152
+ #### Configuration Options
153
+
154
+ The WebSocket Streams API supports the following advanced configuration options:
155
+
156
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
157
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
158
+ - `agent`: Customize the WebSocket agent for advanced configurations.
159
+ - `mode`: Choose between `single` and `pool` connection modes.
160
+ - `single`: A single WebSocket connection.
161
+ - `pool`: A pool of WebSocket connections.
162
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
163
+
164
+ #### Subscribe to Risk and Trade Data Streams
165
+
166
+ You can consume the risk and trade data stream, which sends account-level events such as account and order updates. First create a listen-key via REST API; then:
167
+
168
+ ```typescript
169
+ import { MarginTrading, MARGIN_TRADING_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
170
+
171
+ const configurationWebsocketStreams = {
172
+ wsURL: MARGIN_TRADING_WS_STREAMS_PROD_URL,
173
+ };
174
+ const client = new MarginTrading({ configurationWebsocketStreams });
175
+
176
+ client.websocketStreams
177
+ .connect()
178
+ .then((connection) => {
179
+ const tradeStream = connection.tradeData('listenKey');
180
+ tradeStream.on('message', (data) => {
181
+ switch (data.e) {
182
+ case 'balanceUpdate':
183
+ console.log('balance update stream', data);
184
+ break;
185
+ case 'outboundAccountPosition':
186
+ console.log('outbound account position stream', data);
187
+ break;
188
+ // …handle other variants…
189
+ default:
190
+ console.log('unknown stream', data);
191
+ break;
192
+ }
193
+ });
194
+ })
195
+ .catch((err) => console.error(err));
196
+ ```
197
+
198
+ ```typescript
199
+ import { MarginTrading, MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
200
+
201
+ const configurationWebsocketStreams = {
202
+ wsURL: MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL,
203
+ };
204
+ const client = new MarginTrading({ configurationWebsocketStreams });
205
+
206
+ client.websocketStreams
207
+ .connect()
208
+ .then((connection) => {
209
+ const riskStream = connection.riskData('listenKey');
210
+ riskStream.on('message', (data) => {
211
+ switch (data.e) {
212
+ case 'MARGIN_LEVEL_STATUS_CHANGE':
213
+ console.log('risk level change stream', data);
214
+ break;
215
+ case 'USER_LIABILITY_CHANGE':
216
+ console.log('risk level change stream', data);
217
+ break;
218
+ default:
219
+ console.log('unknown stream', data);
220
+ break;
221
+ }
222
+ });
223
+ })
224
+ .catch((err) => console.error(err));
225
+ ```
226
+
227
+ #### Unsubscribing from Streams
228
+
229
+ You can unsubscribe from the risk and trade data streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
230
+
231
+ ```typescript
232
+ import { MarginTrading, MARGIN_TRADING_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
233
+
234
+ const configurationWebsocketStreams = {
235
+ wsURL: MARGIN_TRADING_WS_STREAMS_PROD_URL,
236
+ };
237
+ const client = new MarginTrading({ configurationWebsocketStreams });
238
+
239
+ client.websocketStreams
240
+ .connect()
241
+ .then((connection) => {
242
+ const tradeStream = connection.tradeData('listenKey');
243
+ tradeStream.on('message', (data) => {
244
+ switch (data.e) {
245
+ case 'balanceUpdate':
246
+ console.log('balance update stream', data);
247
+ break;
248
+ case 'outboundAccountPosition':
249
+ console.log('outbound account position stream', data);
250
+ break;
251
+ default:
252
+ console.log('unknown stream', data);
253
+ break;
254
+ }
255
+ });
256
+
257
+ setTimeout(() => {
258
+ stream.unsubscribe();
259
+ console.log('Unsubscribed from trade data streams');
260
+ }, 10000);
261
+ })
262
+ .catch((err) => console.error(err));
263
+ ```
264
+
265
+ If `wsURL` is not provided, it defaults to `wss://stream.binance.com:9443`.
266
+
267
+ ### Automatic Connection Renewal
268
+
269
+ The WebSocket connection is automatically renewed for both WebSocket API and WebSocket Streams connections, before the 24 hours expiration of the API key. This ensures continuous connectivity.
270
+
146
271
  ## Testing
147
272
 
148
273
  To run the tests: