@binance/derivatives-trading-portfolio-margin 4.0.2 → 5.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 +110 -0
- package/dist/index.d.mts +1890 -365
- package/dist/index.d.ts +1890 -365
- package/dist/index.js +172 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +157 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
This is a client library for the Binance Derivatives Trading Portfolio Margin API, enabling developers to interact programmatically with Binance's API to suit their derivative trading needs, through three distinct endpoints:
|
|
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 Derivatives Trading Portfolio Margin AP
|
|
|
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)
|
|
@@ -156,6 +158,114 @@ const client = new DerivativesTradingPortfolioMargin({ configurationRestAPI });
|
|
|
156
158
|
|
|
157
159
|
If `basePath` is not provided, it defaults to `https://papi.binance.com`.
|
|
158
160
|
|
|
161
|
+
### Websocket Streams
|
|
162
|
+
|
|
163
|
+
WebSocket Streams in `derivatives-trading-portfolio-margin` is used for subscribing to user data streams. Use the [websocket-streams](./src/websocket-streams/websocket-streams.ts) module to interact with it.
|
|
164
|
+
|
|
165
|
+
#### Configuration Options
|
|
166
|
+
|
|
167
|
+
The WebSocket Streams API supports the following advanced configuration options:
|
|
168
|
+
|
|
169
|
+
- `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
|
|
170
|
+
- `compression`: Enable or disable compression for WebSocket messages (default: true).
|
|
171
|
+
- `agent`: Customize the WebSocket agent for advanced configurations.
|
|
172
|
+
- `mode`: Choose between `single` and `pool` connection modes.
|
|
173
|
+
- `single`: A single WebSocket connection.
|
|
174
|
+
- `pool`: A pool of WebSocket connections.
|
|
175
|
+
- `poolSize`: Define the number of WebSocket connections in pool mode.
|
|
176
|
+
|
|
177
|
+
#### Subscribe to User Data Streams
|
|
178
|
+
|
|
179
|
+
You can consume the user data stream, which sends account-level events such as account and order updates. First create a listen-key via REST API; then:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-portfolio-margin';
|
|
183
|
+
|
|
184
|
+
const configurationWebsocketStreams = {
|
|
185
|
+
wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL,
|
|
186
|
+
};
|
|
187
|
+
const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
|
|
188
|
+
|
|
189
|
+
client.websocketStreams
|
|
190
|
+
.connect()
|
|
191
|
+
.then((connection) => {
|
|
192
|
+
const stream = connection.userData('listenKey');
|
|
193
|
+
stream.on('message', (data) => {
|
|
194
|
+
switch (data.e) {
|
|
195
|
+
case 'ACCOUNT_UPDATE':
|
|
196
|
+
console.log('account update stream', data);
|
|
197
|
+
break;
|
|
198
|
+
case 'ORDER_TRADE_UPDATE':
|
|
199
|
+
console.log('order trade update stream', data);
|
|
200
|
+
break;
|
|
201
|
+
// …handle other variants…
|
|
202
|
+
default:
|
|
203
|
+
console.log('unknown stream', data);
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
})
|
|
208
|
+
.catch((err) => console.error(err));
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Unsubscribing from Streams
|
|
212
|
+
|
|
213
|
+
You can unsubscribe from the user data streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-portfolio-margin';
|
|
217
|
+
|
|
218
|
+
const configurationWebsocketStreams = {
|
|
219
|
+
wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL,
|
|
220
|
+
};
|
|
221
|
+
const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
|
|
222
|
+
|
|
223
|
+
client.websocketStreams
|
|
224
|
+
.connect()
|
|
225
|
+
.then((connection) => {
|
|
226
|
+
const stream = connection.userData('listenKey');
|
|
227
|
+
stream.on('message', (data) => {
|
|
228
|
+
switch (data.e) {
|
|
229
|
+
case 'ACCOUNT_UPDATE':
|
|
230
|
+
console.log('account update stream', data);
|
|
231
|
+
break;
|
|
232
|
+
case 'ORDER_TRADE_UPDATE':
|
|
233
|
+
console.log('order trade update stream', data);
|
|
234
|
+
break;
|
|
235
|
+
// …handle other variants…
|
|
236
|
+
default:
|
|
237
|
+
console.log('unknown stream', data);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
setTimeout(() => {
|
|
243
|
+
stream.unsubscribe();
|
|
244
|
+
console.log('Unsubscribed from user data streams');
|
|
245
|
+
}, 10000);
|
|
246
|
+
})
|
|
247
|
+
.catch((err) => console.error(err));
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### Testnet
|
|
251
|
+
|
|
252
|
+
Websocket Streams also support a testnet environment for development and testing. Update the `wsURL` in your configuration:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import { DerivativesTradingPortfolioMargin, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL } from '@binance/derivatives-trading-portfolio-margin';
|
|
256
|
+
|
|
257
|
+
const configurationWebsocketStreams = {
|
|
258
|
+
wsURL: DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL,
|
|
259
|
+
};
|
|
260
|
+
const client = new DerivativesTradingPortfolioMargin({ configurationWebsocketStreams });
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
If `wsURL` is not provided, it defaults to `wss://fstream.binance.com/pm`.
|
|
264
|
+
|
|
265
|
+
### Automatic Connection Renewal
|
|
266
|
+
|
|
267
|
+
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.
|
|
268
|
+
|
|
159
269
|
## Testing
|
|
160
270
|
|
|
161
271
|
To run the tests:
|