@binance/derivatives-trading-coin-futures 1.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/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Binance
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,383 @@
1
+ # Binance JavaScript Derivatives Trading (COIN-M Futures) Connector
2
+
3
+ [![Open Issues](https://img.shields.io/github/issues/binance/binance-connector-js)](https://github.com/binance/binance-connector-js/issues)
4
+ [![Code Style: Prettier](https://img.shields.io/badge/code%20style-prettier-ff69b4)](https://prettier.io/)
5
+ [![npm version](https://badge.fury.io/js/@binance%2Fderivatives-trading-coin-futures.svg)](https://badge.fury.io/js/@binance%2Fderivatives-trading-coin-futures)
6
+ [![npm Downloads](https://img.shields.io/npm/dm/@binance/derivatives-trading-coin-futures.svg)](https://www.npmjs.com/package/@binance/derivatives-trading-coin-futures)
7
+ ![Node.js Version](https://img.shields.io/badge/Node.js-%3E=22.12.0-brightgreen)
8
+ [![Known Vulnerabilities](https://snyk.io/test/github/binance/binance-connector-js/badge.svg)](https://snyk.io/test/github/binance/binance-connector-js)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
+
11
+ This is a client library for the Binance Derivatives Trading COIN-M Futures API, enabling developers to interact programmatically with Binance's API to suit their derivative trading needs, through three distinct endpoints:
12
+
13
+ - [REST API](./src/rest-api/rest-api.ts)
14
+ - [Websocket API](./src/websocket-api/websocket-api-connection.ts)
15
+ - [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
16
+
17
+ ## Table of Contents
18
+
19
+ - [Supported Features](#supported-features)
20
+ - [Installation](#installation)
21
+ - [Documentation](#documentation)
22
+ - [REST APIs](#rest-apis)
23
+ - [Websocket APIs](#websocket-apis)
24
+ - [Websocket Streams](#websocket-streams)
25
+ - [Testing](#testing)
26
+ - [Migration Guide](#migration-guide)
27
+ - [Contributing](#contributing)
28
+ - [Licence](#licence)
29
+
30
+ ## Supported Features
31
+
32
+ - REST API Endpoints:
33
+ - `/dapi/*`
34
+ - WebSocket Endpoints: Real-time data streaming and request-response communication.
35
+ - Inclusion of test cases and examples for quick onboarding.
36
+
37
+ ## Installation
38
+
39
+ To use this library, ensure your environment is running Node.js version **22.12.0** or later. If you're using `nvm` (Node Version Manager), you can set the correct version as follows:
40
+
41
+ ```bash
42
+ nvm install 22.12.0
43
+ nvm use 22.12.0
44
+ ```
45
+
46
+ Then install the library using `npm`:
47
+
48
+ ```bash
49
+ npm install @binance/derivatives-trading-coin-futures
50
+ ```
51
+
52
+ ## Documentation
53
+
54
+ For detailed information, refer to the [Binance API Documentation](https://developers.binance.com/docs/derivatives/coin-margined-futures/general-info).
55
+
56
+ ### REST APIs
57
+
58
+ All REST API endpoints are available through the [`rest-api`](./src/rest-api/rest-api.ts) module. Note that some endpoints require authentication using your Binance API credentials.
59
+
60
+ ```typescript
61
+ import { DerivativesTradingCoinFutures } from '@binance/derivatives-trading-coin-futures';
62
+
63
+ const configurationRestAPI = {
64
+ apiKey: 'your-api-key',
65
+ apiSecret: 'your-api-secret',
66
+ };
67
+ const client = new DerivativesTradingCoinFutures({ configurationRestAPI });
68
+
69
+ client.restAPI
70
+ .exchangeInformation()
71
+ .then((res) => res.data())
72
+ .then((data) => console.log(data))
73
+ .catch((err) => console.error(err));
74
+ ```
75
+
76
+ More examples can be found in the [`examples/rest-api`](./examples/rest-api/) folder.
77
+
78
+ #### Configuration Options
79
+
80
+ The REST API supports the following advanced configuration options:
81
+
82
+ - `timeout`: Timeout for requests in milliseconds (default: 1000 ms).
83
+ - `proxy`: Proxy configuration:
84
+ - `host`: Proxy server hostname.
85
+ - `port`: Proxy server port.
86
+ - `protocol`: Proxy protocol (http or https).
87
+ - `auth`: Proxy authentication credentials:
88
+ - `username`: Proxy username.
89
+ - `password`: Proxy password.
90
+ - `keepAlive`: Enable HTTP keep-alive (default: true).
91
+ - `compression`: Enable response compression (default: true).
92
+ - `retries`: Number of retry attempts for failed requests (default: 3).
93
+ - `backoff`: Delay in milliseconds between retries (default: 1000 ms).
94
+ - `httpsAgent`: Custom HTTPS agent for advanced TLS configuration.
95
+ - `privateKey`: RSA or ED25519 private key for authentication.
96
+ - `privateKeyPassphrase`: Passphrase for the private key, if encrypted.
97
+
98
+ ##### Timeout
99
+
100
+ You can configure a timeout for requests in milliseconds. If the request exceeds the specified timeout, it will be aborted. See the [Timeout example](./docs/rest-api/timeout.md) for detailed usage.
101
+
102
+ ##### Proxy
103
+
104
+ The REST API supports HTTP/HTTPS proxy configurations. See the [Proxy example](./docs/rest-api/proxy.md) for detailed usage.
105
+
106
+ ##### Keep-Alive
107
+
108
+ Enable HTTP keep-alive for persistent connections. See the [Keep-Alive example](./docs/rest-api/keepAlive.md) for detailed usage.
109
+
110
+ ##### Compression
111
+
112
+ Enable or disable response compression. See the [Compression example](./docs/rest-api/compression.md) for detailed usage.
113
+
114
+ ##### Retries
115
+
116
+ Configure the number of retry attempts and delay in milliseconds between retries for failed requests. See the [Retries example](./docs/rest-api/retries.md) for detailed usage.
117
+
118
+ ##### HTTPS Agent
119
+
120
+ Customize the HTTPS agent for advanced TLS configurations. See the [HTTPS Agent example](./docs/rest-api/httpsAgent.md) for detailed usage.
121
+
122
+ ##### Key Pair Based Authentication
123
+
124
+ The REST API supports key pair-based authentication for secure communication. You can use `RSA` or `ED25519` keys for signing requests. See the [Key Pair Based Authentication example](./docs/rest-api/key-pair-authentication.md) for detailed usage.
125
+
126
+ ##### Certificate Pinning
127
+
128
+ To enhance security, you can use certificate pinning with the `httpsAgent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/rest-api/certificate-pinning.md) for detailed usage.
129
+
130
+ #### Error Handling
131
+
132
+ The REST API provides detailed error types to help you handle issues effectively:
133
+
134
+ - `ConnectorClientError`: General client error.
135
+ - `RequiredError`: Thrown when a required parameter is missing.
136
+ - `UnauthorizedError`: Indicates missing or invalid authentication credentials.
137
+ - `ForbiddenError`: Access to the requested resource is forbidden.
138
+ - `TooManyRequestsError`: Rate limit exceeded.
139
+ - `RateLimitBanError`: IP address banned for exceeding rate limits.
140
+ - `ServerError`: Internal server error.
141
+ - `NetworkError`: Issues with network connectivity.
142
+ - `NotFoundError`: Resource not found.
143
+ - `BadRequestError`: Invalid request.
144
+
145
+ See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed usage.
146
+
147
+ #### Testnet
148
+
149
+ For testing purposes, `/dapi/*` endpoints can be used in the [Futures Testnet](https://testnet.binancefuture.com/). Update the `basePath` in your configuration:
150
+
151
+ ```typescript
152
+ import { DerivativesTradingCoinFutures, DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL } from '@binance/derivatives-trading-coin-futures';
153
+
154
+ const configurationRestAPI = {
155
+ apiKey: 'your-api-key',
156
+ apiSecret: 'your-api-secret',
157
+ basePath: DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL,
158
+ };
159
+ const client = new DerivativesTradingCoinFutures({ configurationRestAPI });
160
+ ```
161
+
162
+ If `basePath` is not provided, it defaults to `https://dapi.binance.com`.
163
+
164
+ ### Websocket APIs
165
+
166
+ The WebSocket API provides request-response communication for market data and trading actions. Use the [`websocket-api`](./src/websocket-api/websocket-api.ts) module to interact with these endpoints.
167
+
168
+ ```typescript
169
+ import { DerivativesTradingCoinFutures } from '@binance/derivatives-trading-coin-futures';
170
+
171
+ const configurationWebsocketAPI = {
172
+ apiKey: 'your-api-key',
173
+ apiSecret: 'your-api-secret',
174
+ };
175
+ const client = new DerivativesTradingCoinFutures({ configurationWebsocketAPI });
176
+
177
+ client.websocketAPI
178
+ .connect()
179
+ .then((connection) => connection.positionInformation())
180
+ .then((res) => console.log(res.data))
181
+ .catch((err) => console.error(err));
182
+ ```
183
+
184
+ More examples are available in the [`examples/websocket-api`](./examples/websocket-api/) folder.
185
+
186
+ #### Configuration Options
187
+
188
+ The WebSocket API supports the following advanced configuration options:
189
+
190
+ - `timeout`: Set the timeout for WebSocket requests (default: 5000 ms).
191
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
192
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
193
+ - `agent`: Customize the WebSocket agent for advanced configurations.
194
+ - `mode`: Choose between `single` and `pool` connection modes.
195
+ - `single`: A single WebSocket connection.
196
+ - `pool`: A pool of WebSocket connections.
197
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
198
+ - `privateKey`: RSA or ED25519 private key for authentication.
199
+ - `privateKeyPassphrase`: Passphrase for the private key, if encrypted.
200
+
201
+ ##### Timeout
202
+
203
+ Set the timeout for WebSocket API requests in milliseconds. See the [Timeout example](./docs/websocket-api/timeout.md) for detailed usage.
204
+
205
+ ##### Reconnect Delay
206
+
207
+ Specify the delay in milliseconds between WebSocket reconnection attempts. See the [Reconnect Delay example](./docs/websocket-api/reconnect-delay.md) for detailed usage.
208
+
209
+ ##### Compression
210
+
211
+ Enable or disable compression for WebSocket messages. See the [Compression example](./docs/websocket-api/compression.md) for detailed usage.
212
+
213
+ ##### WebSocket Agent
214
+
215
+ Customize the agent for advanced configurations. See the [WebSocket Agent example](./docs/websocket-api/agent.md) for detailed usage.
216
+
217
+ ##### Connection Mode
218
+
219
+ Choose between `single` and `pool` connection modes for WebSocket connections. The `single` mode uses a single WebSocket connection, while the `pool` mode uses a pool of WebSocket connections. See the [Connection Mode example](./docs/websocket-api/connection-mode.md) for detailed usage.
220
+
221
+ ##### Key Pair Based Authentication
222
+
223
+ Use RSA or ED25519 private keys for WebSocket API authentication. See the [Key Pair Authentication example](./docs/websocket-api/key-pair-authentication.md) for detailed usage.
224
+
225
+ ##### Certificate Pinning
226
+
227
+ To enhance security, you can use certificate pinning with the `agent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/websocket-api/certificate-pinning.md) for detailed usage.
228
+
229
+ #### Testnet
230
+
231
+ For testing purposes, the Websocket API also supports a testnet environment. Update the `wsURL` in your configuration:
232
+
233
+ ```typescript
234
+ import { DerivativesTradingCoinFutures, DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL } from '@binance/derivatives-trading-coin-futures';
235
+
236
+ const configurationWebsocketAPI = {
237
+ apiKey: 'your-api-key',
238
+ apiSecret: 'your-api-secret',
239
+ wsURL: DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL,
240
+ };
241
+ const client = new DerivativesTradingCoinFutures({ configurationWebsocketAPI });
242
+ ```
243
+
244
+ If `wsURL` is not provided, it defaults to `wss://ws-dapi.binance.com/ws-dapi/v1`.
245
+
246
+ ### Websocket Streams
247
+
248
+ WebSocket Streams provide real-time data feeds for market trades, candlesticks, and more. Use the [websocket-streams](./src/websocket-streams/websocket-streams.ts) module to subscribe to these streams.
249
+
250
+ ```typescript
251
+ import { DerivativesTradingCoinFutures, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-coin-futures';
252
+
253
+ const configurationWebsocketStreams = {
254
+ wsURL: DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL,
255
+ };
256
+ const client = new DerivativesTradingCoinFutures({ configurationWebsocketStreams });
257
+
258
+ client.websocketStreams
259
+ .connect()
260
+ .then((connection) => {
261
+ const stream = connection.allBookTickersStream();
262
+ stream.on('message', (data) => console.info(data));
263
+ })
264
+ .catch((err) => console.error(err));
265
+ ```
266
+
267
+ More examples are available in the [`examples/websocket-streams`](./examples/websocket-streams/) folder.
268
+
269
+ #### Configuration Options
270
+
271
+ The WebSocket Streams API supports the following advanced configuration options:
272
+
273
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
274
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
275
+ - `agent`: Customize the WebSocket agent for advanced configurations.
276
+ - `mode`: Choose between `single` and `pool` connection modes.
277
+ - `single`: A single WebSocket connection.
278
+ - `pool`: A pool of WebSocket connections.
279
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
280
+
281
+ ##### Reconnect Delay
282
+
283
+ Specify the delay in milliseconds between WebSocket reconnection attempts for streams. See the [Reconnect Delay example](./docs/websocket-streams/reconnect-delay.md) for detailed usage.
284
+
285
+ ##### Compression
286
+
287
+ Enable or disable compression for WebSocket Streams messages. See the [Compression example](./docs/websocket-streams/compression.md) for detailed usage.
288
+
289
+ ##### WebSocket Agent
290
+
291
+ Customize the agent for advanced configurations. See the [WebSocket Agent example](./docs/websocket-streams/agent.md) for detailed usage.
292
+
293
+ ##### Connection Mode
294
+
295
+ Choose between `single` and `pool` connection modes for WebSocket Streams. The `single` mode uses a single WebSocket connection, while the `pool` mode uses a pool of WebSocket connections. See the [Connection Mode example](./docs/websocket-streams/connection-mode.md) for detailed usage.
296
+
297
+ ##### Certificate Pinning
298
+
299
+ To enhance security, you can use certificate pinning with the `agent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/websocket-streams/certificate-pinning.md) for detailed usage.
300
+
301
+ #### Unsubscribing from Streams
302
+
303
+ You can unsubscribe from specific WebSocket streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
304
+
305
+ ```typescript
306
+ import { DerivativesTradingCoinFutures, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-coin-futures';
307
+
308
+ const configurationWebsocketStreams = {
309
+ wsURL: DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL,
310
+ };
311
+ const client = new DerivativesTradingCoinFutures({ configurationWebsocketStreams });
312
+
313
+ client.websocketStreams
314
+ .connect()
315
+ .then((connection) => {
316
+ const stream = connection.allBookTickersStream();
317
+ stream.on('message', (data) => console.info(data));
318
+
319
+ setTimeout(() => {
320
+ stream.unsubscribe();
321
+ console.log('Unsubscribed from allBookTickersStream stream');
322
+ }, 10000);
323
+ })
324
+ .catch((err) => console.error(err));
325
+ ```
326
+
327
+ #### Testnet
328
+
329
+ Websocket Streams also support a testnet environment for development and testing. Update the `wsURL` in your configuration:
330
+
331
+ ```typescript
332
+ import { DerivativesTradingCoinFutures, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL } from '@binance/derivatives-trading-coin-futures';
333
+
334
+ const configurationWebsocketStreams = {
335
+ wsURL: DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL,
336
+ };
337
+ const client = new DerivativesTradingCoinFutures({ configurationWebsocketStreams });
338
+ ```
339
+
340
+ If `wsURL` is not provided, it defaults to `wss://dstream.binance.com`.
341
+
342
+ ### Automatic Connection Renewal
343
+
344
+ 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.
345
+
346
+ ## Testing
347
+
348
+ To run the tests:
349
+
350
+ ```bash
351
+ npm install
352
+
353
+ npm run test
354
+ ```
355
+
356
+ The tests cover:
357
+
358
+ - REST API endpoints
359
+ - WebSocket API and Streams
360
+ - Error handling and edge cases
361
+
362
+ ## Migration Guide
363
+
364
+ If you are upgrading to the new modularized structure, refer to the [Migration Guide](../../MIGRATION.md) for detailed steps.
365
+
366
+ ## Contributing
367
+
368
+ Contributions are welcome!
369
+
370
+ Since this repository contains auto-generated code, we encourage you to start by opening a GitHub issue to discuss your ideas or suggest improvements. This helps ensure that changes align with the project's goals and auto-generation processes.
371
+
372
+ To contribute:
373
+
374
+ 1. Open a GitHub issue describing your suggestion or the bug you've identified.
375
+ 2. If it's determined that changes are necessary, the maintainers will merge the changes into the main branch.
376
+
377
+ Please ensure that all tests pass if you're making a direct contribution. Submit a pull request only after discussing and confirming the change.
378
+
379
+ Thank you for your contributions!
380
+
381
+ ## Licence
382
+
383
+ This project is licensed under the MIT License. See the [LICENCE](./LICENCE) file for details.