@satora/swap 1.8.0 → 1.8.1

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.cjs CHANGED
@@ -2455,6 +2455,13 @@ var CLAIM_SUBMITTED = /* @__PURE__ */ new Set([
2455
2455
  "clientredeemed",
2456
2456
  "serverredeemed"
2457
2457
  ]);
2458
+ var SERVER_LEG_FUNDED = /* @__PURE__ */ new Set([
2459
+ "serverfunded",
2460
+ "clientredeeming",
2461
+ "clientredeemed",
2462
+ "serverredeemed"
2463
+ ]);
2464
+ var HINT_REREAD_DELAYS_MS = [1e3, 2e3, 4e3, 8e3];
2458
2465
  var DEFAULT_AT_RISK_RECONCILE_INTERVAL_MS = 6e4;
2459
2466
  var RETRY_UNREGISTERED_INTERVAL_MS = 6e4;
2460
2467
  var SwapTracker = class {
@@ -2477,6 +2484,8 @@ var SwapTracker = class {
2477
2484
  #lastRetryAt = 0;
2478
2485
  #eventUnsubs = [];
2479
2486
  #timer;
2487
+ /** Pending follow-up read per swap, see {@link HINT_REREAD_DELAYS_MS}. */
2488
+ #hintRereads = /* @__PURE__ */ new Map();
2480
2489
  constructor(managers, options) {
2481
2490
  this.#managers = managers;
2482
2491
  this.#refreshIntervalMs = options?.refreshIntervalMs ?? 0;
@@ -2667,7 +2676,9 @@ var SwapTracker = class {
2667
2676
  * status hint drives. Reconciles just that swap's legs (targeted, on the
2668
2677
  * managers), then recomputes its action. The hint is only a *trigger*: the
2669
2678
  * chain stays the source of truth, so a premature hint that the chain doesn't
2670
- * yet reflect changes nothing. A no-op for an untracked swap.
2679
+ * yet reflect changes nothing, except that a hint saying the server's HTLC is
2680
+ * funded is followed by a few more reads of that leg until the chain shows it.
2681
+ * A no-op for an untracked swap.
2671
2682
  */
2672
2683
  async applyHint(swapId, opts) {
2673
2684
  const swap = this.#swaps.get(swapId);
@@ -2685,6 +2696,38 @@ var SwapTracker = class {
2685
2696
  );
2686
2697
  });
2687
2698
  this.#recompute(swap, opts?.force ?? false);
2699
+ this.#cancelHintReread(swapId);
2700
+ this.#rereadUntilServerLegSeen(swap, 0);
2701
+ }
2702
+ #rereadUntilServerLegSeen(swap, attempt) {
2703
+ const leg = swap.serverHtlc;
2704
+ const hint = this.#hintStatuses.get(swap.swapId);
2705
+ if (!leg || hint === void 0 || !SERVER_LEG_FUNDED.has(hint)) return;
2706
+ if (this.#swaps.get(swap.swapId) !== swap) return;
2707
+ const seen = this.#managerFor(leg).getState(leg);
2708
+ if (seen !== void 0 && seen !== "absent") return;
2709
+ const delay = HINT_REREAD_DELAYS_MS[attempt];
2710
+ if (delay === void 0) return;
2711
+ const timer = setTimeout(() => {
2712
+ this.#hintRereads.delete(swap.swapId);
2713
+ if (this.#swaps.get(swap.swapId) !== swap) return;
2714
+ void this.#managerFor(leg).reconcile(leg).catch((error) => {
2715
+ console.warn(
2716
+ `SwapTracker: re-read after hint failed for ${swap.swapId}:`,
2717
+ error
2718
+ );
2719
+ }).then(() => {
2720
+ this.#recompute(swap);
2721
+ if (!this.#hintRereads.has(swap.swapId))
2722
+ this.#rereadUntilServerLegSeen(swap, attempt + 1);
2723
+ });
2724
+ }, delay);
2725
+ this.#hintRereads.set(swap.swapId, timer);
2726
+ }
2727
+ #cancelHintReread(swapId) {
2728
+ const timer = this.#hintRereads.get(swapId);
2729
+ if (timer !== void 0) clearTimeout(timer);
2730
+ this.#hintRereads.delete(swapId);
2688
2731
  }
2689
2732
  /** Notify `cb` of the current action for each tracked swap, then on every change. */
2690
2733
  subscribeToActions(cb) {
@@ -2703,6 +2746,8 @@ var SwapTracker = class {
2703
2746
  stop() {
2704
2747
  if (this.#timer) clearInterval(this.#timer);
2705
2748
  this.#timer = void 0;
2749
+ for (const swapId of [...this.#hintRereads.keys()])
2750
+ this.#cancelHintReread(swapId);
2706
2751
  for (const unsub of this.#eventUnsubs) unsub();
2707
2752
  this.#eventUnsubs = [];
2708
2753
  for (const swap of this.#swaps.values())
@@ -2790,6 +2835,7 @@ var SwapTracker = class {
2790
2835
  if (actions.recommended === "none") this.#untrack(swap);
2791
2836
  }
2792
2837
  #untrack(swap) {
2838
+ this.#cancelHintReread(swap.swapId);
2793
2839
  this.#swaps.delete(swap.swapId);
2794
2840
  for (const leg of legsOf(swap)) void this.#managerFor(leg).unregister(leg);
2795
2841
  }
@@ -2963,10 +3009,6 @@ var Client = class {
2963
3009
  return this.#legacy.getQuote(...args);
2964
3010
  }
2965
3011
  /** Delegated to the legacy client (migration checkpoint). */
2966
- createLightningToEvmSwap(...args) {
2967
- return this.#legacy.createLightningToEvmSwap(...args);
2968
- }
2969
- /** Delegated to the legacy client (migration checkpoint). */
2970
3012
  getLightningSendQuote(...args) {
2971
3013
  return this.#legacy.getLightningSendQuote(...args);
2972
3014
  }
@@ -3102,6 +3144,12 @@ var Client = class {
3102
3144
  );
3103
3145
  }
3104
3146
  /** Delegated to the legacy client (migration checkpoint). */
3147
+ createLightningToEvmSwap(...args) {
3148
+ return this.#trackAfterCreate(
3149
+ this.#legacy.createLightningToEvmSwap(...args)
3150
+ );
3151
+ }
3152
+ /** Delegated to the legacy client (migration checkpoint). */
3105
3153
  createLightningToBitcoinSwap(...args) {
3106
3154
  return this.#trackAfterCreate(
3107
3155
  this.#legacy.createLightningToBitcoinSwap(...args)