@unicitylabs/sphere-sdk 0.6.10 → 0.6.11

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.d.cts CHANGED
@@ -4341,7 +4341,7 @@ interface DiscoverAddressesOptions {
4341
4341
  gapLimit?: number;
4342
4342
  /** Batch size for transport queries (default: 20) */
4343
4343
  batchSize?: number;
4344
- /** Also run L1 balance scan (default: true) */
4344
+ /** Also run L1 balance scan (default: true when L1 is configured, false otherwise) */
4345
4345
  includeL1Scan?: boolean;
4346
4346
  /** Progress callback */
4347
4347
  onProgress?: (progress: DiscoverAddressProgress) => void;
package/dist/index.d.ts CHANGED
@@ -4341,7 +4341,7 @@ interface DiscoverAddressesOptions {
4341
4341
  gapLimit?: number;
4342
4342
  /** Batch size for transport queries (default: 20) */
4343
4343
  batchSize?: number;
4344
- /** Also run L1 balance scan (default: true) */
4344
+ /** Also run L1 balance scan (default: true when L1 is configured, false otherwise) */
4345
4345
  includeL1Scan?: boolean;
4346
4346
  /** Progress callback */
4347
4347
  onProgress?: (progress: DiscoverAddressProgress) => void;
package/dist/index.js CHANGED
@@ -577,6 +577,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
577
577
  return waitForConnection();
578
578
  }
579
579
  isConnecting = true;
580
+ if (ws) {
581
+ try {
582
+ ws.onopen = null;
583
+ ws.onclose = null;
584
+ ws.onerror = null;
585
+ ws.onmessage = null;
586
+ ws.close();
587
+ } catch {
588
+ }
589
+ ws = null;
590
+ }
591
+ const epoch = ++connectionEpoch;
580
592
  return new Promise((resolve, reject) => {
581
593
  let hasResolved = false;
582
594
  try {
@@ -588,6 +600,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
588
600
  return;
589
601
  }
590
602
  ws.onopen = () => {
603
+ if (epoch !== connectionEpoch) return;
591
604
  isConnected = true;
592
605
  isConnecting = false;
593
606
  reconnectAttempts = 0;
@@ -601,6 +614,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
601
614
  connectionCallbacks.length = 0;
602
615
  };
603
616
  ws.onclose = () => {
617
+ if (epoch !== connectionEpoch) return;
604
618
  isConnected = false;
605
619
  isBlockSubscribed = false;
606
620
  stopPingTimer();
@@ -609,16 +623,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
609
623
  req.reject(new Error("WebSocket connection closed"));
610
624
  });
611
625
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
612
- if (intentionalClose) {
613
- intentionalClose = false;
614
- isConnecting = false;
615
- reconnectAttempts = 0;
616
- if (!hasResolved) {
617
- hasResolved = true;
618
- reject(new Error("WebSocket connection closed intentionally"));
619
- }
620
- return;
621
- }
622
626
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
623
627
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
624
628
  isConnecting = false;
@@ -640,7 +644,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
640
644
  "L1",
641
645
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
642
646
  );
643
- setTimeout(() => {
647
+ reconnectTimer = setTimeout(() => {
648
+ reconnectTimer = null;
644
649
  connect(endpoint).then(() => {
645
650
  if (!hasResolved) {
646
651
  hasResolved = true;
@@ -787,8 +792,16 @@ async function getCurrentBlockHeight() {
787
792
  }
788
793
  function disconnect() {
789
794
  stopPingTimer();
795
+ if (reconnectTimer) {
796
+ clearTimeout(reconnectTimer);
797
+ reconnectTimer = null;
798
+ }
799
+ connectionEpoch++;
790
800
  if (ws) {
791
- intentionalClose = true;
801
+ ws.onopen = null;
802
+ ws.onclose = null;
803
+ ws.onerror = null;
804
+ ws.onmessage = null;
792
805
  ws.close();
793
806
  ws = null;
794
807
  }
@@ -796,18 +809,21 @@ function disconnect() {
796
809
  isConnecting = false;
797
810
  reconnectAttempts = 0;
798
811
  isBlockSubscribed = false;
812
+ const disconnectError = new Error("WebSocket disconnected");
799
813
  Object.values(pending).forEach((req) => {
800
814
  if (req.timeoutId) clearTimeout(req.timeoutId);
815
+ req.reject(disconnectError);
801
816
  });
802
817
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
803
818
  connectionCallbacks.forEach((cb) => {
804
819
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
820
+ cb.reject(disconnectError);
805
821
  });
806
822
  connectionCallbacks.length = 0;
807
823
  blockSubscribers.length = 0;
808
824
  lastBlockHeader = null;
809
825
  }
810
- var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, intentionalClose, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
826
+ var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, reconnectTimer, connectionEpoch, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
811
827
  var init_network = __esm({
812
828
  "l1/network.ts"() {
813
829
  "use strict";
@@ -819,11 +835,12 @@ var init_network = __esm({
819
835
  isConnected = false;
820
836
  isConnecting = false;
821
837
  requestId = 0;
822
- intentionalClose = false;
823
838
  reconnectAttempts = 0;
824
839
  isBlockSubscribed = false;
825
840
  lastBlockHeader = null;
826
841
  pingTimer = null;
842
+ reconnectTimer = null;
843
+ connectionEpoch = 0;
827
844
  pending = {};
828
845
  blockSubscribers = [];
829
846
  connectionCallbacks = [];
@@ -5340,9 +5357,7 @@ var L1PaymentsModule = class {
5340
5357
  }
5341
5358
  }
5342
5359
  destroy() {
5343
- if (isWebSocketConnected()) {
5344
- disconnect();
5345
- }
5360
+ disconnect();
5346
5361
  this._initialized = false;
5347
5362
  this._identity = void 0;
5348
5363
  this._addresses = [];
@@ -5353,9 +5368,7 @@ var L1PaymentsModule = class {
5353
5368
  */
5354
5369
  disable() {
5355
5370
  this._disabled = true;
5356
- if (isWebSocketConnected()) {
5357
- disconnect();
5358
- }
5371
+ disconnect();
5359
5372
  }
5360
5373
  /**
5361
5374
  * Re-enable this module. Connection will be established lazily on next operation.
@@ -15665,7 +15678,7 @@ var MarketModule = class {
15665
15678
  const MAX_ATTEMPTS = 10;
15666
15679
  let ws2 = null;
15667
15680
  let reconnectAttempts2 = 0;
15668
- let reconnectTimer = null;
15681
+ let reconnectTimer2 = null;
15669
15682
  let destroyed = false;
15670
15683
  function connect2() {
15671
15684
  if (destroyed) return;
@@ -15697,14 +15710,14 @@ var MarketModule = class {
15697
15710
  const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
15698
15711
  reconnectAttempts2++;
15699
15712
  logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
15700
- reconnectTimer = setTimeout(connect2, delay);
15713
+ reconnectTimer2 = setTimeout(connect2, delay);
15701
15714
  }
15702
15715
  connect2();
15703
15716
  return () => {
15704
15717
  destroyed = true;
15705
- if (reconnectTimer) {
15706
- clearTimeout(reconnectTimer);
15707
- reconnectTimer = null;
15718
+ if (reconnectTimer2) {
15719
+ clearTimeout(reconnectTimer2);
15720
+ reconnectTimer2 = null;
15708
15721
  }
15709
15722
  ws2?.close();
15710
15723
  ws2 = null;
@@ -18420,7 +18433,7 @@ var Sphere = class _Sphere {
18420
18433
  if (!this._transport.discoverAddresses) {
18421
18434
  throw new SphereError("Transport provider does not support address discovery", "INVALID_CONFIG");
18422
18435
  }
18423
- const includeL1Scan = options.includeL1Scan ?? true;
18436
+ const includeL1Scan = (options.includeL1Scan ?? true) && !!this._l1Config;
18424
18437
  const transportResult = await discoverAddressesImpl(
18425
18438
  (index) => {
18426
18439
  const addrInfo = this._deriveAddressInternal(index, false);