@binance/alpha 2.0.0 → 4.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
@@ -12,6 +12,7 @@
12
12
  This is a client library for the Binance Alpha API, enabling developers to interact programmatically with Binance Alpha. The library provides tools to access curated early-stage token data, track Alpha project metrics and integrate discovery-focused market information into applications through the REST API:
13
13
 
14
14
  - [REST API](./src/rest-api/rest-api.ts)
15
+ - [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
15
16
 
16
17
  ## Table of Contents
17
18
 
@@ -19,6 +20,7 @@ This is a client library for the Binance Alpha API, enabling developers to inter
19
20
  - [Installation](#installation)
20
21
  - [Documentation](#documentation)
21
22
  - [REST APIs](#rest-apis)
23
+ - [Websocket Streams](#websocket-streams)
22
24
  - [Testing](#testing)
23
25
  - [Contributing](#contributing)
24
26
  - [Licence](#licence)
@@ -27,6 +29,7 @@ This is a client library for the Binance Alpha API, enabling developers to inter
27
29
 
28
30
  - REST API Endpoints:
29
31
  - `/bapi/defi/v1/*`
32
+ - WebSocket Endpoints: Real-time data streaming.
30
33
  - Inclusion of test cases and examples for quick onboarding.
31
34
 
32
35
  ## Installation
@@ -141,6 +144,93 @@ See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed
141
144
 
142
145
  If `basePath` is not provided, it defaults to `https://www.binance.com`.
143
146
 
147
+ ### Websocket Streams
148
+
149
+ 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.
150
+
151
+ ```typescript
152
+ import { Alpha, ALPHA_WS_STREAMS_PROD_URL } from '@binance/alpha';
153
+
154
+ const configurationWebsocketStreams = {
155
+ wsURL: ALPHA_WS_STREAMS_PROD_URL,
156
+ };
157
+ const client = new Alpha({ configurationWebsocketStreams });
158
+
159
+ client.websocketStreams
160
+ .connect()
161
+ .then((connection) => {
162
+ const stream = connection.allBookTickerStream();
163
+ stream.on('message', (data) => console.info(data));
164
+ })
165
+ .catch((err) => console.error(err));
166
+ ```
167
+
168
+ More examples are available in the [`examples/websocket-streams`](./examples/websocket-streams/) folder.
169
+
170
+ #### Configuration Options
171
+
172
+ The WebSocket Streams API supports the following advanced configuration options:
173
+
174
+ - `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
175
+ - `compression`: Enable or disable compression for WebSocket messages (default: true).
176
+ - `agent`: Customize the WebSocket agent for advanced configurations.
177
+ - `mode`: Choose between `single` and `pool` connection modes.
178
+ - `single`: A single WebSocket connection.
179
+ - `pool`: A pool of WebSocket connections.
180
+ - `poolSize`: Define the number of WebSocket connections in pool mode.
181
+
182
+ ##### Reconnect Delay
183
+
184
+ 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.
185
+
186
+ ##### Compression
187
+
188
+ Enable or disable compression for WebSocket Streams messages. See the [Compression example](./docs/websocket-streams/compression.md) for detailed usage.
189
+
190
+ ##### WebSocket Agent
191
+
192
+ Customize the agent for advanced configurations. See the [WebSocket Agent example](./docs/websocket-streams/agent.md) for detailed usage.
193
+
194
+ ##### Connection Mode
195
+
196
+ 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.
197
+
198
+ ##### Certificate Pinning
199
+
200
+ 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.
201
+
202
+ #### Unsubscribing from Streams
203
+
204
+ You can unsubscribe from specific WebSocket streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
205
+
206
+ ```typescript
207
+ import { Alpha, ALPHA_WS_STREAMS_PROD_URL } from '@binance/alpha';
208
+
209
+ const configurationWebsocketStreams = {
210
+ wsURL: ALPHA_WS_STREAMS_PROD_URL,
211
+ };
212
+ const client = new Alpha({ configurationWebsocketStreams });
213
+
214
+ client.websocketStreams
215
+ .connect()
216
+ .then((connection) => {
217
+ const stream = connection.allBookTickerStream();
218
+ stream.on('message', (data) => console.info(data));
219
+
220
+ setTimeout(() => {
221
+ stream.unsubscribe();
222
+ console.log('Unsubscribed from allBookTickerStream stream');
223
+ }, 10000);
224
+ })
225
+ .catch((err) => console.error(err));
226
+ ```
227
+
228
+ If `wsURL` is not provided, it defaults to `wss://nbstream.binance.com/w3w/wsa`.
229
+
230
+ ### Automatic Connection Renewal
231
+
232
+ The WebSocket connection is automatically renewed for the WebSocket Streams connections, before the 24 hours expiration of the API key. This ensures continuous connectivity.
233
+
144
234
  ## Testing
145
235
 
146
236
  To run the tests:
@@ -171,6 +261,19 @@ Please ensure that all tests pass if you're making a direct contribution. Submit
171
261
 
172
262
  Thank you for your contributions!
173
263
 
264
+ ## Disclaimer
265
+
266
+ This SDK is provided by Binance on an "as is" and "as available" basis for use at your own risk. Binance makes no representations or warranties of any kind, whether express or implied, as to the operation of the SDK, its accuracy, reliability, completeness, or fitness for any particular purpose.
267
+
268
+ To the fullest extent permitted by law, Binance shall not be liable for any losses, damages, or expenses of any kind arising from or in connection with your use of, or inability to use, this SDK, including but not limited to any financial losses resulting from errors, bugs, interruptions, or inaccuracies in the SDK.
269
+
270
+ Your use of this SDK to access the Binance Platform is subject to the Binance API Key Terms and the Binance Terms of Use, which shall prevail in the event of any conflict with this disclaimer. You are solely responsible for any orders or transactions executed through the Binance Platform using this SDK.
271
+
272
+ This SDK is not intended to constitute investment advice or a recommendation to buy, sell, or hold any digital asset. You should independently evaluate and verify all information before acting.
273
+
274
+ - [Binance Terms of Use](https://www.binance.com/en/terms)
275
+ - [Binance API Key Terms](https://www.binance.com/en/about-legal/terms-binance-api)
276
+
174
277
  ## Licence
175
278
 
176
279
  This project is licensed under the MIT License. See the [LICENCE](./LICENCE) file for details.
package/dist/index.d.mts CHANGED
@@ -2724,10 +2724,10 @@ interface AggregateTradeStreamRequest {
2724
2724
  readonly symbol: string;
2725
2725
  /**
2726
2726
  * Unique WebSocket request ID.
2727
- * @type {string}
2727
+ * @type {number}
2728
2728
  * @memberof ApiAggregateTradeStream
2729
2729
  */
2730
- readonly id?: string;
2730
+ readonly id?: number;
2731
2731
  }
2732
2732
  /**
2733
2733
  * Request parameters for allBookTickerStream operation in Api.
@@ -2736,10 +2736,10 @@ interface AggregateTradeStreamRequest {
2736
2736
  interface AllBookTickerStreamRequest {
2737
2737
  /**
2738
2738
  * Unique WebSocket request ID.
2739
- * @type {string}
2739
+ * @type {number}
2740
2740
  * @memberof ApiAllBookTickerStream
2741
2741
  */
2742
- readonly id?: string;
2742
+ readonly id?: number;
2743
2743
  }
2744
2744
  /**
2745
2745
  * Request parameters for allMiniTickerStream operation in Api.
@@ -2748,10 +2748,10 @@ interface AllBookTickerStreamRequest {
2748
2748
  interface AllMiniTickerStreamRequest {
2749
2749
  /**
2750
2750
  * Unique WebSocket request ID.
2751
- * @type {string}
2751
+ * @type {number}
2752
2752
  * @memberof ApiAllMiniTickerStream
2753
2753
  */
2754
- readonly id?: string;
2754
+ readonly id?: number;
2755
2755
  }
2756
2756
  /**
2757
2757
  * Request parameters for allTickerStream operation in Api.
@@ -2760,10 +2760,10 @@ interface AllMiniTickerStreamRequest {
2760
2760
  interface AllTickerStreamRequest {
2761
2761
  /**
2762
2762
  * Unique WebSocket request ID.
2763
- * @type {string}
2763
+ * @type {number}
2764
2764
  * @memberof ApiAllTickerStream
2765
2765
  */
2766
- readonly id?: string;
2766
+ readonly id?: number;
2767
2767
  }
2768
2768
  /**
2769
2769
  * Request parameters for allTokens24hTickerStream operation in Api.
@@ -2772,10 +2772,10 @@ interface AllTickerStreamRequest {
2772
2772
  interface AllTokens24hTickerStreamRequest {
2773
2773
  /**
2774
2774
  * Unique WebSocket request ID.
2775
- * @type {string}
2775
+ * @type {number}
2776
2776
  * @memberof ApiAllTokens24hTickerStream
2777
2777
  */
2778
- readonly id?: string;
2778
+ readonly id?: number;
2779
2779
  }
2780
2780
  /**
2781
2781
  * Request parameters for bookTickerStream operation in Api.
@@ -2790,10 +2790,10 @@ interface BookTickerStreamRequest {
2790
2790
  readonly symbol: string;
2791
2791
  /**
2792
2792
  * Unique WebSocket request ID.
2793
- * @type {string}
2793
+ * @type {number}
2794
2794
  * @memberof ApiBookTickerStream
2795
2795
  */
2796
- readonly id?: string;
2796
+ readonly id?: number;
2797
2797
  }
2798
2798
  /**
2799
2799
  * Request parameters for contractKlineStream operation in Api.
@@ -2820,10 +2820,10 @@ interface ContractKlineStreamRequest {
2820
2820
  readonly interval: ContractKlineStreamIntervalEnum;
2821
2821
  /**
2822
2822
  * Unique WebSocket request ID.
2823
- * @type {string}
2823
+ * @type {number}
2824
2824
  * @memberof ApiContractKlineStream
2825
2825
  */
2826
- readonly id?: string;
2826
+ readonly id?: number;
2827
2827
  }
2828
2828
  /**
2829
2829
  * Request parameters for fullDepthStream operation in Api.
@@ -2844,10 +2844,10 @@ interface FullDepthStreamRequest {
2844
2844
  readonly interval: FullDepthStreamIntervalEnum;
2845
2845
  /**
2846
2846
  * Unique WebSocket request ID.
2847
- * @type {string}
2847
+ * @type {number}
2848
2848
  * @memberof ApiFullDepthStream
2849
2849
  */
2850
- readonly id?: string;
2850
+ readonly id?: number;
2851
2851
  }
2852
2852
  /**
2853
2853
  * Request parameters for klineStream operation in Api.
@@ -2868,10 +2868,10 @@ interface KlineStreamRequest {
2868
2868
  readonly interval: KlineStreamIntervalEnum;
2869
2869
  /**
2870
2870
  * Unique WebSocket request ID.
2871
- * @type {string}
2871
+ * @type {number}
2872
2872
  * @memberof ApiKlineStream
2873
2873
  */
2874
- readonly id?: string;
2874
+ readonly id?: number;
2875
2875
  }
2876
2876
  /**
2877
2877
  * Request parameters for miniTickerStream operation in Api.
@@ -2886,10 +2886,10 @@ interface MiniTickerStreamRequest {
2886
2886
  readonly symbol: string;
2887
2887
  /**
2888
2888
  * Unique WebSocket request ID.
2889
- * @type {string}
2889
+ * @type {number}
2890
2890
  * @memberof ApiMiniTickerStream
2891
2891
  */
2892
- readonly id?: string;
2892
+ readonly id?: number;
2893
2893
  }
2894
2894
  /**
2895
2895
  * Request parameters for partialDepthStream operation in Api.
@@ -2916,10 +2916,10 @@ interface PartialDepthStreamRequest {
2916
2916
  readonly interval: PartialDepthStreamIntervalEnum;
2917
2917
  /**
2918
2918
  * Unique WebSocket request ID.
2919
- * @type {string}
2919
+ * @type {number}
2920
2920
  * @memberof ApiPartialDepthStream
2921
2921
  */
2922
- readonly id?: string;
2922
+ readonly id?: number;
2923
2923
  }
2924
2924
  /**
2925
2925
  * Request parameters for tickerStream operation in Api.
@@ -2934,10 +2934,10 @@ interface TickerStreamRequest {
2934
2934
  readonly symbol: string;
2935
2935
  /**
2936
2936
  * Unique WebSocket request ID.
2937
- * @type {string}
2937
+ * @type {number}
2938
2938
  * @memberof ApiTickerStream
2939
2939
  */
2940
- readonly id?: string;
2940
+ readonly id?: number;
2941
2941
  }
2942
2942
  /**
2943
2943
  * Request parameters for tradeStream operation in Api.
@@ -2952,10 +2952,10 @@ interface TradeStreamRequest {
2952
2952
  readonly symbol: string;
2953
2953
  /**
2954
2954
  * Unique WebSocket request ID.
2955
- * @type {string}
2955
+ * @type {number}
2956
2956
  * @memberof ApiTradeStream
2957
2957
  */
2958
- readonly id?: string;
2958
+ readonly id?: number;
2959
2959
  }
2960
2960
  /**
2961
2961
  * Api - interface
@@ -3197,7 +3197,7 @@ declare class WebsocketStreamsConnection {
3197
3197
  * @param id Optional subscription ID
3198
3198
  * @returns void
3199
3199
  */
3200
- subscribe(stream: string | string[], id?: string): void;
3200
+ subscribe(stream: string | string[], id?: number): void;
3201
3201
  /**
3202
3202
  * Unsubscribes from one or multiple WebSocket streams
3203
3203
  * Handles both single and pool modes
@@ -3205,7 +3205,7 @@ declare class WebsocketStreamsConnection {
3205
3205
  * @param id Optional unsubscription ID
3206
3206
  * @returns void
3207
3207
  */
3208
- unsubscribe(stream: string | string[], id?: string): void;
3208
+ unsubscribe(stream: string | string[], id?: number): void;
3209
3209
  /**
3210
3210
  * Checks if the WebSocket connection is subscribed to the specified stream.
3211
3211
  * @param stream The name of the WebSocket stream to check.
package/dist/index.d.ts CHANGED
@@ -2725,10 +2725,10 @@ interface AggregateTradeStreamRequest {
2725
2725
  readonly symbol: string;
2726
2726
  /**
2727
2727
  * Unique WebSocket request ID.
2728
- * @type {string}
2728
+ * @type {number}
2729
2729
  * @memberof ApiAggregateTradeStream
2730
2730
  */
2731
- readonly id?: string;
2731
+ readonly id?: number;
2732
2732
  }
2733
2733
  /**
2734
2734
  * Request parameters for allBookTickerStream operation in Api.
@@ -2737,10 +2737,10 @@ interface AggregateTradeStreamRequest {
2737
2737
  interface AllBookTickerStreamRequest {
2738
2738
  /**
2739
2739
  * Unique WebSocket request ID.
2740
- * @type {string}
2740
+ * @type {number}
2741
2741
  * @memberof ApiAllBookTickerStream
2742
2742
  */
2743
- readonly id?: string;
2743
+ readonly id?: number;
2744
2744
  }
2745
2745
  /**
2746
2746
  * Request parameters for allMiniTickerStream operation in Api.
@@ -2749,10 +2749,10 @@ interface AllBookTickerStreamRequest {
2749
2749
  interface AllMiniTickerStreamRequest {
2750
2750
  /**
2751
2751
  * Unique WebSocket request ID.
2752
- * @type {string}
2752
+ * @type {number}
2753
2753
  * @memberof ApiAllMiniTickerStream
2754
2754
  */
2755
- readonly id?: string;
2755
+ readonly id?: number;
2756
2756
  }
2757
2757
  /**
2758
2758
  * Request parameters for allTickerStream operation in Api.
@@ -2761,10 +2761,10 @@ interface AllMiniTickerStreamRequest {
2761
2761
  interface AllTickerStreamRequest {
2762
2762
  /**
2763
2763
  * Unique WebSocket request ID.
2764
- * @type {string}
2764
+ * @type {number}
2765
2765
  * @memberof ApiAllTickerStream
2766
2766
  */
2767
- readonly id?: string;
2767
+ readonly id?: number;
2768
2768
  }
2769
2769
  /**
2770
2770
  * Request parameters for allTokens24hTickerStream operation in Api.
@@ -2773,10 +2773,10 @@ interface AllTickerStreamRequest {
2773
2773
  interface AllTokens24hTickerStreamRequest {
2774
2774
  /**
2775
2775
  * Unique WebSocket request ID.
2776
- * @type {string}
2776
+ * @type {number}
2777
2777
  * @memberof ApiAllTokens24hTickerStream
2778
2778
  */
2779
- readonly id?: string;
2779
+ readonly id?: number;
2780
2780
  }
2781
2781
  /**
2782
2782
  * Request parameters for bookTickerStream operation in Api.
@@ -2791,10 +2791,10 @@ interface BookTickerStreamRequest {
2791
2791
  readonly symbol: string;
2792
2792
  /**
2793
2793
  * Unique WebSocket request ID.
2794
- * @type {string}
2794
+ * @type {number}
2795
2795
  * @memberof ApiBookTickerStream
2796
2796
  */
2797
- readonly id?: string;
2797
+ readonly id?: number;
2798
2798
  }
2799
2799
  /**
2800
2800
  * Request parameters for contractKlineStream operation in Api.
@@ -2821,10 +2821,10 @@ interface ContractKlineStreamRequest {
2821
2821
  readonly interval: ContractKlineStreamIntervalEnum;
2822
2822
  /**
2823
2823
  * Unique WebSocket request ID.
2824
- * @type {string}
2824
+ * @type {number}
2825
2825
  * @memberof ApiContractKlineStream
2826
2826
  */
2827
- readonly id?: string;
2827
+ readonly id?: number;
2828
2828
  }
2829
2829
  /**
2830
2830
  * Request parameters for fullDepthStream operation in Api.
@@ -2845,10 +2845,10 @@ interface FullDepthStreamRequest {
2845
2845
  readonly interval: FullDepthStreamIntervalEnum;
2846
2846
  /**
2847
2847
  * Unique WebSocket request ID.
2848
- * @type {string}
2848
+ * @type {number}
2849
2849
  * @memberof ApiFullDepthStream
2850
2850
  */
2851
- readonly id?: string;
2851
+ readonly id?: number;
2852
2852
  }
2853
2853
  /**
2854
2854
  * Request parameters for klineStream operation in Api.
@@ -2869,10 +2869,10 @@ interface KlineStreamRequest {
2869
2869
  readonly interval: KlineStreamIntervalEnum;
2870
2870
  /**
2871
2871
  * Unique WebSocket request ID.
2872
- * @type {string}
2872
+ * @type {number}
2873
2873
  * @memberof ApiKlineStream
2874
2874
  */
2875
- readonly id?: string;
2875
+ readonly id?: number;
2876
2876
  }
2877
2877
  /**
2878
2878
  * Request parameters for miniTickerStream operation in Api.
@@ -2887,10 +2887,10 @@ interface MiniTickerStreamRequest {
2887
2887
  readonly symbol: string;
2888
2888
  /**
2889
2889
  * Unique WebSocket request ID.
2890
- * @type {string}
2890
+ * @type {number}
2891
2891
  * @memberof ApiMiniTickerStream
2892
2892
  */
2893
- readonly id?: string;
2893
+ readonly id?: number;
2894
2894
  }
2895
2895
  /**
2896
2896
  * Request parameters for partialDepthStream operation in Api.
@@ -2917,10 +2917,10 @@ interface PartialDepthStreamRequest {
2917
2917
  readonly interval: PartialDepthStreamIntervalEnum;
2918
2918
  /**
2919
2919
  * Unique WebSocket request ID.
2920
- * @type {string}
2920
+ * @type {number}
2921
2921
  * @memberof ApiPartialDepthStream
2922
2922
  */
2923
- readonly id?: string;
2923
+ readonly id?: number;
2924
2924
  }
2925
2925
  /**
2926
2926
  * Request parameters for tickerStream operation in Api.
@@ -2935,10 +2935,10 @@ interface TickerStreamRequest {
2935
2935
  readonly symbol: string;
2936
2936
  /**
2937
2937
  * Unique WebSocket request ID.
2938
- * @type {string}
2938
+ * @type {number}
2939
2939
  * @memberof ApiTickerStream
2940
2940
  */
2941
- readonly id?: string;
2941
+ readonly id?: number;
2942
2942
  }
2943
2943
  /**
2944
2944
  * Request parameters for tradeStream operation in Api.
@@ -2953,10 +2953,10 @@ interface TradeStreamRequest {
2953
2953
  readonly symbol: string;
2954
2954
  /**
2955
2955
  * Unique WebSocket request ID.
2956
- * @type {string}
2956
+ * @type {number}
2957
2957
  * @memberof ApiTradeStream
2958
2958
  */
2959
- readonly id?: string;
2959
+ readonly id?: number;
2960
2960
  }
2961
2961
  /**
2962
2962
  * Api - interface
@@ -3198,7 +3198,7 @@ declare class WebsocketStreamsConnection {
3198
3198
  * @param id Optional subscription ID
3199
3199
  * @returns void
3200
3200
  */
3201
- subscribe(stream: string | string[], id?: string): void;
3201
+ subscribe(stream: string | string[], id?: number): void;
3202
3202
  /**
3203
3203
  * Unsubscribes from one or multiple WebSocket streams
3204
3204
  * Handles both single and pool modes
@@ -3206,7 +3206,7 @@ declare class WebsocketStreamsConnection {
3206
3206
  * @param id Optional unsubscription ID
3207
3207
  * @returns void
3208
3208
  */
3209
- unsubscribe(stream: string | string[], id?: string): void;
3209
+ unsubscribe(stream: string | string[], id?: number): void;
3210
3210
  /**
3211
3211
  * Checks if the WebSocket connection is subscribed to the specified stream.
3212
3212
  * @param stream The name of the WebSocket stream to check.
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ let __binance_common = require("@binance/common");
14
14
 
15
15
  //#region package.json
16
16
  var name = "@binance/alpha";
17
- var version = "2.0.0";
17
+ var version = "4.0.0";
18
18
 
19
19
  //#endregion
20
20
  //#region src/rest-api/modules/market-data-api.ts
@@ -1025,6 +1025,7 @@ var WebsocketStreams = class {
1025
1025
  ...mode && { mode },
1026
1026
  ...poolSize && { poolSize }
1027
1027
  });
1028
+ websocketBase.streamIdIsStrictlyNumber = true;
1028
1029
  const websocketStreamsConnection = new WebsocketStreamsConnection(websocketBase);
1029
1030
  await websocketBase.connect(stream);
1030
1031
  return websocketStreamsConnection;