@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.
@@ -3658,7 +3658,7 @@ interface DiscoverAddressesOptions {
3658
3658
  gapLimit?: number;
3659
3659
  /** Batch size for transport queries (default: 20) */
3660
3660
  batchSize?: number;
3661
- /** Also run L1 balance scan (default: true) */
3661
+ /** Also run L1 balance scan (default: true when L1 is configured, false otherwise) */
3662
3662
  includeL1Scan?: boolean;
3663
3663
  /** Progress callback */
3664
3664
  onProgress?: (progress: DiscoverAddressProgress) => void;
@@ -3658,7 +3658,7 @@ interface DiscoverAddressesOptions {
3658
3658
  gapLimit?: number;
3659
3659
  /** Batch size for transport queries (default: 20) */
3660
3660
  batchSize?: number;
3661
- /** Also run L1 balance scan (default: true) */
3661
+ /** Also run L1 balance scan (default: true when L1 is configured, false otherwise) */
3662
3662
  includeL1Scan?: boolean;
3663
3663
  /** Progress callback */
3664
3664
  onProgress?: (progress: DiscoverAddressProgress) => void;
@@ -550,6 +550,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
550
550
  return waitForConnection();
551
551
  }
552
552
  isConnecting = true;
553
+ if (ws) {
554
+ try {
555
+ ws.onopen = null;
556
+ ws.onclose = null;
557
+ ws.onerror = null;
558
+ ws.onmessage = null;
559
+ ws.close();
560
+ } catch {
561
+ }
562
+ ws = null;
563
+ }
564
+ const epoch = ++connectionEpoch;
553
565
  return new Promise((resolve, reject) => {
554
566
  let hasResolved = false;
555
567
  try {
@@ -561,6 +573,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
561
573
  return;
562
574
  }
563
575
  ws.onopen = () => {
576
+ if (epoch !== connectionEpoch) return;
564
577
  isConnected = true;
565
578
  isConnecting = false;
566
579
  reconnectAttempts = 0;
@@ -574,6 +587,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
574
587
  connectionCallbacks.length = 0;
575
588
  };
576
589
  ws.onclose = () => {
590
+ if (epoch !== connectionEpoch) return;
577
591
  isConnected = false;
578
592
  isBlockSubscribed = false;
579
593
  stopPingTimer();
@@ -582,16 +596,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
582
596
  req.reject(new Error("WebSocket connection closed"));
583
597
  });
584
598
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
585
- if (intentionalClose) {
586
- intentionalClose = false;
587
- isConnecting = false;
588
- reconnectAttempts = 0;
589
- if (!hasResolved) {
590
- hasResolved = true;
591
- reject(new Error("WebSocket connection closed intentionally"));
592
- }
593
- return;
594
- }
595
599
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
596
600
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
597
601
  isConnecting = false;
@@ -613,7 +617,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
613
617
  "L1",
614
618
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
615
619
  );
616
- setTimeout(() => {
620
+ reconnectTimer = setTimeout(() => {
621
+ reconnectTimer = null;
617
622
  connect(endpoint).then(() => {
618
623
  if (!hasResolved) {
619
624
  hasResolved = true;
@@ -760,8 +765,16 @@ async function getCurrentBlockHeight() {
760
765
  }
761
766
  function disconnect() {
762
767
  stopPingTimer();
768
+ if (reconnectTimer) {
769
+ clearTimeout(reconnectTimer);
770
+ reconnectTimer = null;
771
+ }
772
+ connectionEpoch++;
763
773
  if (ws) {
764
- intentionalClose = true;
774
+ ws.onopen = null;
775
+ ws.onclose = null;
776
+ ws.onerror = null;
777
+ ws.onmessage = null;
765
778
  ws.close();
766
779
  ws = null;
767
780
  }
@@ -769,18 +782,21 @@ function disconnect() {
769
782
  isConnecting = false;
770
783
  reconnectAttempts = 0;
771
784
  isBlockSubscribed = false;
785
+ const disconnectError = new Error("WebSocket disconnected");
772
786
  Object.values(pending).forEach((req) => {
773
787
  if (req.timeoutId) clearTimeout(req.timeoutId);
788
+ req.reject(disconnectError);
774
789
  });
775
790
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
776
791
  connectionCallbacks.forEach((cb) => {
777
792
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
793
+ cb.reject(disconnectError);
778
794
  });
779
795
  connectionCallbacks.length = 0;
780
796
  blockSubscribers.length = 0;
781
797
  lastBlockHeader = null;
782
798
  }
783
- 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;
799
+ 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;
784
800
  var init_network = __esm({
785
801
  "l1/network.ts"() {
786
802
  "use strict";
@@ -792,11 +808,12 @@ var init_network = __esm({
792
808
  isConnected = false;
793
809
  isConnecting = false;
794
810
  requestId = 0;
795
- intentionalClose = false;
796
811
  reconnectAttempts = 0;
797
812
  isBlockSubscribed = false;
798
813
  lastBlockHeader = null;
799
814
  pingTimer = null;
815
+ reconnectTimer = null;
816
+ connectionEpoch = 0;
800
817
  pending = {};
801
818
  blockSubscribers = [];
802
819
  connectionCallbacks = [];
@@ -5162,9 +5179,7 @@ var L1PaymentsModule = class {
5162
5179
  }
5163
5180
  }
5164
5181
  destroy() {
5165
- if (isWebSocketConnected()) {
5166
- disconnect();
5167
- }
5182
+ disconnect();
5168
5183
  this._initialized = false;
5169
5184
  this._identity = void 0;
5170
5185
  this._addresses = [];
@@ -5175,9 +5190,7 @@ var L1PaymentsModule = class {
5175
5190
  */
5176
5191
  disable() {
5177
5192
  this._disabled = true;
5178
- if (isWebSocketConnected()) {
5179
- disconnect();
5180
- }
5193
+ disconnect();
5181
5194
  }
5182
5195
  /**
5183
5196
  * Re-enable this module. Connection will be established lazily on next operation.
@@ -15396,7 +15409,7 @@ var MarketModule = class {
15396
15409
  const MAX_ATTEMPTS = 10;
15397
15410
  let ws2 = null;
15398
15411
  let reconnectAttempts2 = 0;
15399
- let reconnectTimer = null;
15412
+ let reconnectTimer2 = null;
15400
15413
  let destroyed = false;
15401
15414
  function connect2() {
15402
15415
  if (destroyed) return;
@@ -15428,14 +15441,14 @@ var MarketModule = class {
15428
15441
  const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
15429
15442
  reconnectAttempts2++;
15430
15443
  logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
15431
- reconnectTimer = setTimeout(connect2, delay);
15444
+ reconnectTimer2 = setTimeout(connect2, delay);
15432
15445
  }
15433
15446
  connect2();
15434
15447
  return () => {
15435
15448
  destroyed = true;
15436
- if (reconnectTimer) {
15437
- clearTimeout(reconnectTimer);
15438
- reconnectTimer = null;
15449
+ if (reconnectTimer2) {
15450
+ clearTimeout(reconnectTimer2);
15451
+ reconnectTimer2 = null;
15439
15452
  }
15440
15453
  ws2?.close();
15441
15454
  ws2 = null;
@@ -18236,7 +18249,7 @@ var Sphere = class _Sphere {
18236
18249
  if (!this._transport.discoverAddresses) {
18237
18250
  throw new SphereError("Transport provider does not support address discovery", "INVALID_CONFIG");
18238
18251
  }
18239
- const includeL1Scan = options.includeL1Scan ?? true;
18252
+ const includeL1Scan = (options.includeL1Scan ?? true) && !!this._l1Config;
18240
18253
  const transportResult = await discoverAddressesImpl(
18241
18254
  (index) => {
18242
18255
  const addrInfo = this._deriveAddressInternal(index, false);