@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/dist/index.mjs CHANGED
@@ -8,12 +8,14 @@ var __export = (target, all) => {
8
8
  import {
9
9
  buildUserAgent,
10
10
  ConfigurationRestAPI as ConfigurationRestAPI9,
11
- MARGIN_TRADING_REST_API_PROD_URL
11
+ ConfigurationWebsocketStreams as ConfigurationWebsocketStreams2,
12
+ MARGIN_TRADING_REST_API_PROD_URL,
13
+ MARGIN_TRADING_WS_STREAMS_PROD_URL
12
14
  } from "@binance/common";
13
15
 
14
16
  // package.json
15
17
  var name = "@binance/margin-trading";
16
- var version = "5.0.1";
18
+ var version = "6.0.0";
17
19
 
18
20
  // src/rest-api/index.ts
19
21
  var rest_api_exports = {};
@@ -5563,6 +5565,147 @@ var RestAPI = class {
5563
5565
  }
5564
5566
  };
5565
5567
 
5568
+ // src/websocket-streams/index.ts
5569
+ var websocket_streams_exports = {};
5570
+ __export(websocket_streams_exports, {
5571
+ WebsocketStreams: () => WebsocketStreams,
5572
+ WebsocketStreamsConnection: () => WebsocketStreamsConnection
5573
+ });
5574
+
5575
+ // src/websocket-streams/websocket-streams.ts
5576
+ import { WebsocketStreamsBase as WebsocketStreamsBase2 } from "@binance/common";
5577
+
5578
+ // src/websocket-streams/websocket-streams-connection.ts
5579
+ import { createStreamHandler } from "@binance/common";
5580
+ var WebsocketStreamsConnection = class {
5581
+ constructor(websocketBase) {
5582
+ this.websocketBase = websocketBase;
5583
+ }
5584
+ /**
5585
+ * Adds an event listener for the specified WebSocket event.
5586
+ * @param event - The WebSocket event to listen for, such as 'open', 'message', 'error', 'close', 'ping', or 'pong'.
5587
+ * @param listener - The callback function to be executed when the event is triggered. The function can accept any number of arguments.
5588
+ */
5589
+ on(event, listener) {
5590
+ this.websocketBase.on(event, listener);
5591
+ }
5592
+ /**
5593
+ * Removes an event listener for the specified WebSocket event.
5594
+ * @param event - The WebSocket event to stop listening for, such as 'open', 'message', 'error', 'close', 'ping', or 'pong'.
5595
+ * @param listener - The callback function that was previously added as the event listener.
5596
+ */
5597
+ off(event, listener) {
5598
+ this.websocketBase.off(event, listener);
5599
+ }
5600
+ /**
5601
+ * Disconnects from the WebSocket server.
5602
+ * If there is no active connection, a warning is logged.
5603
+ * Otherwise, all connections in the connection pool are closed gracefully,
5604
+ * and a message is logged indicating that the connection has been disconnected.
5605
+ * @returns A Promise that resolves when all connections have been closed.
5606
+ * @throws Error if the WebSocket client is not set.
5607
+ */
5608
+ disconnect() {
5609
+ return this.websocketBase.disconnect();
5610
+ }
5611
+ /**
5612
+ * Checks if the WebSocket connection is currently open.
5613
+ * @returns `true` if the connection is open, `false` otherwise.
5614
+ */
5615
+ isConnected() {
5616
+ return this.websocketBase.isConnected();
5617
+ }
5618
+ /**
5619
+ * Sends a ping message to all connected Websocket servers in the pool.
5620
+ * If no connections are ready, a warning is logged.
5621
+ * For each active connection, the ping message is sent, and debug logs provide details.
5622
+ * @throws Error if a Websocket client is not set for a connection.
5623
+ */
5624
+ pingServer() {
5625
+ this.websocketBase.pingServer();
5626
+ }
5627
+ /**
5628
+ * Subscribes to one or multiple WebSocket streams
5629
+ * Handles both single and pool modes
5630
+ * @param stream Single stream name or array of stream names to subscribe to
5631
+ * @param id Optional subscription ID
5632
+ * @returns void
5633
+ */
5634
+ subscribe(stream, id) {
5635
+ this.websocketBase.subscribe(stream, id);
5636
+ }
5637
+ /**
5638
+ * Unsubscribes from one or multiple WebSocket streams
5639
+ * Handles both single and pool modes
5640
+ * @param stream Single stream name or array of stream names to unsubscribe from
5641
+ * @param id Optional unsubscription ID
5642
+ * @returns void
5643
+ */
5644
+ unsubscribe(stream, id) {
5645
+ this.websocketBase.unsubscribe(stream, id);
5646
+ }
5647
+ /**
5648
+ * Checks if the WebSocket connection is subscribed to the specified stream.
5649
+ * @param stream The name of the WebSocket stream to check.
5650
+ * @returns `true` if the connection is subscribed to the stream, `false` otherwise.
5651
+ */
5652
+ isSubscribed(stream) {
5653
+ return this.websocketBase.isSubscribed(stream);
5654
+ }
5655
+ /**
5656
+ * Subscribes to the risk data WebSocket stream using the provided listen key.
5657
+ * @param listenKey - The listen key for the risk data WebSocket stream.
5658
+ * @param id - Optional risk data stream ID
5659
+ * @returns A WebSocket stream handler for the risk data stream.
5660
+ */
5661
+ riskData(listenKey, id) {
5662
+ return createStreamHandler(this.websocketBase, listenKey, id);
5663
+ }
5664
+ /**
5665
+ * Subscribes to the trade data WebSocket stream using the provided listen key.
5666
+ * @param listenKey - The listen key for the trade data WebSocket stream.
5667
+ * @param id - Optional trade data stream ID
5668
+ * @returns A WebSocket stream handler for the trade data stream.
5669
+ */
5670
+ tradeData(listenKey, id) {
5671
+ return createStreamHandler(
5672
+ this.websocketBase,
5673
+ listenKey,
5674
+ id
5675
+ );
5676
+ }
5677
+ };
5678
+
5679
+ // src/websocket-streams/websocket-streams.ts
5680
+ var WebsocketStreams = class {
5681
+ constructor(configuration) {
5682
+ this.configuration = configuration;
5683
+ }
5684
+ /**
5685
+ * Connects to the Binance WebSocket streams and returns a `WebsocketStreamsConnection` instance.
5686
+ *
5687
+ * @param {object} [options] - Optional connection options.
5688
+ * @param {string|string[]} [options.stream] - The stream(s) to connect to.
5689
+ * @param {'single'|'pool'} [options.mode] - The connection mode, either 'single' or 'pool'. Overwrite the `mode` option in the configuration.
5690
+ * @param {number} [options.poolSize] - The number of connections to use in pool mode. Overwrite the `poolSize` option in the configuration.
5691
+ * @returns {Promise<WebsocketStreamsConnection>} - A promise that resolves to a `WebsocketStreamsConnection` instance.
5692
+ */
5693
+ async connect({
5694
+ stream,
5695
+ mode,
5696
+ poolSize
5697
+ } = {}) {
5698
+ const websocketBase = new WebsocketStreamsBase2({
5699
+ ...this.configuration,
5700
+ ...mode && { mode },
5701
+ ...poolSize && { poolSize }
5702
+ });
5703
+ const websocketStreamsConnection = new WebsocketStreamsConnection(websocketBase);
5704
+ await websocketBase.connect(stream);
5705
+ return websocketStreamsConnection;
5706
+ }
5707
+ };
5708
+
5566
5709
  // src/margin-trading.ts
5567
5710
  var MarginTrading = class {
5568
5711
  constructor(config) {
@@ -5579,12 +5722,22 @@ var MarginTrading = class {
5579
5722
  };
5580
5723
  this.restAPI = new RestAPI(configRestAPI);
5581
5724
  }
5725
+ if (config?.configurationWebsocketStreams) {
5726
+ const configWebsocketStreams = new ConfigurationWebsocketStreams2(
5727
+ config.configurationWebsocketStreams
5728
+ );
5729
+ configWebsocketStreams.wsURL = configWebsocketStreams.wsURL || MARGIN_TRADING_WS_STREAMS_PROD_URL;
5730
+ configWebsocketStreams.userAgent = userAgent;
5731
+ this.websocketStreams = new WebsocketStreams(configWebsocketStreams);
5732
+ }
5582
5733
  }
5583
5734
  };
5584
5735
 
5585
5736
  // src/index.ts
5586
5737
  import {
5587
5738
  MARGIN_TRADING_REST_API_PROD_URL as MARGIN_TRADING_REST_API_PROD_URL2,
5739
+ MARGIN_TRADING_WS_STREAMS_PROD_URL as MARGIN_TRADING_WS_STREAMS_PROD_URL2,
5740
+ MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL,
5588
5741
  ConnectorClientError,
5589
5742
  RequiredError,
5590
5743
  UnauthorizedError,
@@ -5601,8 +5754,11 @@ export {
5601
5754
  ConnectorClientError,
5602
5755
  ForbiddenError,
5603
5756
  MARGIN_TRADING_REST_API_PROD_URL2 as MARGIN_TRADING_REST_API_PROD_URL,
5757
+ MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL,
5758
+ MARGIN_TRADING_WS_STREAMS_PROD_URL2 as MARGIN_TRADING_WS_STREAMS_PROD_URL,
5604
5759
  MarginTrading,
5605
5760
  rest_api_exports as MarginTradingRestAPI,
5761
+ websocket_streams_exports as MarginTradingWebsocketStreams,
5606
5762
  NetworkError,
5607
5763
  NotFoundError,
5608
5764
  RateLimitBanError,