@binance/margin-trading 5.0.1 → 6.0.1

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
@@ -6,11 +6,13 @@
6
6
  [![npm Downloads](https://img.shields.io/npm/dm/@binance/margin-trading.svg)](https://www.npmjs.com/package/@binance/margin-trading)
7
7
  ![Node.js Version](https://img.shields.io/badge/Node.js-%3E=22.12.0-brightgreen)
8
8
  [![Known Vulnerabilities](https://snyk.io/test/github/binance/binance-connector-js/badge.svg)](https://snyk.io/test/github/binance/binance-connector-js)
9
+ [![Docs](https://img.shields.io/badge/docs-online-blue?style=flat-square)](https://binance.github.io/binance-connector-js/modules/_binance_margin-trading.html)
9
10
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
11
 
11
12
  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
13
 
13
14
  - [REST API](./src/rest-api/rest-api.ts)
15
+ - [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
14
16
 
15
17
  ## Table of Contents
16
18
 
@@ -18,6 +20,7 @@ This is a client library for the Binance Margin Trading API, enabling developers
18
20
  - [Installation](#installation)
19
21
  - [Documentation](#documentation)
20
22
  - [REST APIs](#rest-apis)
23
+ - [Websocket Streams](#websocket-streams)
21
24
  - [Testing](#testing)
22
25
  - [Migration Guide](#migration-guide)
23
26
  - [Contributing](#contributing)
@@ -143,6 +146,129 @@ See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed
143
146
 
144
147
  If `basePath` is not provided, it defaults to `https://api.binance.com`.
145
148
 
149
+ ### Websocket Streams
150
+
151
+ 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.
152
+
153
+ #### Configuration Options
154
+
155
+ The WebSocket Streams API supports the following advanced configuration options:
156
+
157
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
158
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
159
+ - `agent`: Customize the WebSocket agent for advanced configurations.
160
+ - `mode`: Choose between `single` and `pool` connection modes.
161
+ - `single`: A single WebSocket connection.
162
+ - `pool`: A pool of WebSocket connections.
163
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
164
+
165
+ #### Subscribe to Risk and Trade Data Streams
166
+
167
+ 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:
168
+
169
+ ```typescript
170
+ import { MarginTrading, MARGIN_TRADING_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
171
+
172
+ const configurationWebsocketStreams = {
173
+ wsURL: MARGIN_TRADING_WS_STREAMS_PROD_URL,
174
+ };
175
+ const client = new MarginTrading({ configurationWebsocketStreams });
176
+
177
+ client.websocketStreams
178
+ .connect()
179
+ .then((connection) => {
180
+ const tradeStream = connection.tradeData('listenKey');
181
+ tradeStream.on('message', (data) => {
182
+ switch (data.e) {
183
+ case 'balanceUpdate':
184
+ console.log('balance update stream', data);
185
+ break;
186
+ case 'outboundAccountPosition':
187
+ console.log('outbound account position stream', data);
188
+ break;
189
+ // …handle other variants…
190
+ default:
191
+ console.log('unknown stream', data);
192
+ break;
193
+ }
194
+ });
195
+ })
196
+ .catch((err) => console.error(err));
197
+ ```
198
+
199
+ ```typescript
200
+ import { MarginTrading, MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
201
+
202
+ const configurationWebsocketStreams = {
203
+ wsURL: MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL,
204
+ };
205
+ const client = new MarginTrading({ configurationWebsocketStreams });
206
+
207
+ client.websocketStreams
208
+ .connect()
209
+ .then((connection) => {
210
+ const riskStream = connection.riskData('listenKey');
211
+ riskStream.on('message', (data) => {
212
+ switch (data.e) {
213
+ case 'MARGIN_LEVEL_STATUS_CHANGE':
214
+ console.log('risk level change stream', data);
215
+ break;
216
+ case 'USER_LIABILITY_CHANGE':
217
+ console.log('risk level change stream', data);
218
+ break;
219
+ default:
220
+ console.log('unknown stream', data);
221
+ break;
222
+ }
223
+ });
224
+ })
225
+ .catch((err) => console.error(err));
226
+ ```
227
+
228
+ #### Unsubscribing from Streams
229
+
230
+ 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.
231
+
232
+ ```typescript
233
+ import { MarginTrading, MARGIN_TRADING_WS_STREAMS_PROD_URL } from '@binance/margin-trading';
234
+
235
+ const configurationWebsocketStreams = {
236
+ wsURL: MARGIN_TRADING_WS_STREAMS_PROD_URL,
237
+ };
238
+ const client = new MarginTrading({ configurationWebsocketStreams });
239
+
240
+ client.websocketStreams
241
+ .connect()
242
+ .then((connection) => {
243
+ const tradeStream = connection.tradeData('listenKey');
244
+ tradeStream.on('message', (data) => {
245
+ switch (data.e) {
246
+ case 'balanceUpdate':
247
+ console.log('balance update stream', data);
248
+ break;
249
+ case 'outboundAccountPosition':
250
+ console.log('outbound account position stream', data);
251
+ break;
252
+ default:
253
+ console.log('unknown stream', data);
254
+ break;
255
+ }
256
+ });
257
+
258
+ setTimeout(() => {
259
+ stream.unsubscribe();
260
+ console.log('Unsubscribed from trade data streams');
261
+ }, 10000);
262
+ })
263
+ .catch((err) => console.error(err));
264
+ ```
265
+
266
+ If `wsURL` is not provided, it defaults to `wss://stream.binance.com:9443`.
267
+
268
+ ### Automatic Connection Renewal
269
+
270
+ 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.
271
+
146
272
  ## Testing
147
273
 
148
274
  To run the tests: