@satora/swap 1.7.2 → 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
@@ -720,13 +720,10 @@ async function fetchArkadeSpend(source, spentByTxid, paymentHash) {
720
720
 
721
721
  // src/contracts/arkade-manager.ts
722
722
  function isFunded(vtxo) {
723
- const { state } = vtxo.virtualStatus;
724
- return state === "preconfirmed" || state === "settled";
723
+ return !vtxo.isSpent && !vtxo.isSwept;
725
724
  }
726
725
  function spendTxid(vtxos) {
727
- const spent = vtxos.find(
728
- (v) => v.arkTxId || v.spentBy || v.isSpent || v.virtualStatus.state === "spent"
729
- );
726
+ const spent = vtxos.find((v) => v.arkTxId || v.spentBy || v.isSpent);
730
727
  return spent?.arkTxId ?? spent?.spentBy;
731
728
  }
732
729
  var ArkadeContractManager = class _ArkadeContractManager {
@@ -1772,6 +1769,17 @@ var SwapWorker = class {
1772
1769
  )
1773
1770
  );
1774
1771
  }
1772
+ /**
1773
+ * Put swaps on the hint feed as soon as they are tracked. {@link #onActions}
1774
+ * subscribes a swap when its first action is emitted, but a swap whose leg
1775
+ * cannot be classified yet (an unfunded lock on a chain without `eth_getLogs`)
1776
+ * emits nothing, and without this it would never receive the server hint
1777
+ * that its funding landed.
1778
+ */
1779
+ subscribe(swapIds) {
1780
+ if (swapIds.length === 0) return;
1781
+ this.#hintSource.subscribe(swapIds);
1782
+ }
1775
1783
  /** Stop reacting, cancel pending retries, and close the hint feed. */
1776
1784
  stop() {
1777
1785
  this.#started = false;
@@ -1823,7 +1831,7 @@ var SwapWorker = class {
1823
1831
  const actionId = actions.recommended;
1824
1832
  if (!actionId) return;
1825
1833
  this.#executing.add(swapId);
1826
- void execute(swapId, actionId).then(() => this.#cancelRetry(swapId)).catch((error) => {
1834
+ void execute(swapId, actionId, actions).then(() => this.#cancelRetry(swapId)).catch((error) => {
1827
1835
  if (needsWallet(error)) {
1828
1836
  this.#cancelRetry(swapId);
1829
1837
  this.#needsUser.add(swapId);
@@ -2236,7 +2244,7 @@ function swapToTracked(stored) {
2236
2244
  token: r.evm_htlc_kind === "native" ? import_lendaswap_sdk_pure2.NATIVE_TOKEN_ADDRESS : lockedTokenFallback(r.evm_chain_id, r.network),
2237
2245
  // The coordinator created the HTLC on the client's behalf, so it
2238
2246
  // is the lock's sender (refund address), as on evm_to_lightning.
2239
- sender: r.evm_coordinator_address,
2247
+ sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === import_lendaswap_sdk_pure2.NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
2240
2248
  timelockSec: r.evm_refund_locktime,
2241
2249
  createdAt: r.created_at
2242
2250
  }),
@@ -2286,7 +2294,7 @@ function swapToTracked(stored) {
2286
2294
  token: r.wbtc_address,
2287
2295
  // The coordinator created the HTLC on the client's behalf, so it
2288
2296
  // is the lock's sender (refund address).
2289
- sender: r.evm_coordinator_address,
2297
+ sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === import_lendaswap_sdk_pure2.NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
2290
2298
  timelockSec: r.evm_refund_locktime,
2291
2299
  createdAt: r.created_at
2292
2300
  }),
@@ -2381,7 +2389,7 @@ function swapToTracked(stored) {
2381
2389
  // the server claims the client's EVM HTLC
2382
2390
  expectedSats: r.evm_expected_sats,
2383
2391
  token: r.wbtc_address,
2384
- sender: r.evm_coordinator_address,
2392
+ sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === import_lendaswap_sdk_pure2.NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
2385
2393
  // the coordinator locks on the client's behalf
2386
2394
  timelockSec: r.evm_refund_locktime,
2387
2395
  createdAt: r.created_at
@@ -2447,6 +2455,13 @@ var CLAIM_SUBMITTED = /* @__PURE__ */ new Set([
2447
2455
  "clientredeemed",
2448
2456
  "serverredeemed"
2449
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];
2450
2465
  var DEFAULT_AT_RISK_RECONCILE_INTERVAL_MS = 6e4;
2451
2466
  var RETRY_UNREGISTERED_INTERVAL_MS = 6e4;
2452
2467
  var SwapTracker = class {
@@ -2469,6 +2484,8 @@ var SwapTracker = class {
2469
2484
  #lastRetryAt = 0;
2470
2485
  #eventUnsubs = [];
2471
2486
  #timer;
2487
+ /** Pending follow-up read per swap, see {@link HINT_REREAD_DELAYS_MS}. */
2488
+ #hintRereads = /* @__PURE__ */ new Map();
2472
2489
  constructor(managers, options) {
2473
2490
  this.#managers = managers;
2474
2491
  this.#refreshIntervalMs = options?.refreshIntervalMs ?? 0;
@@ -2659,7 +2676,9 @@ var SwapTracker = class {
2659
2676
  * status hint drives. Reconciles just that swap's legs (targeted, on the
2660
2677
  * managers), then recomputes its action. The hint is only a *trigger*: the
2661
2678
  * chain stays the source of truth, so a premature hint that the chain doesn't
2662
- * 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.
2663
2682
  */
2664
2683
  async applyHint(swapId, opts) {
2665
2684
  const swap = this.#swaps.get(swapId);
@@ -2677,6 +2696,38 @@ var SwapTracker = class {
2677
2696
  );
2678
2697
  });
2679
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);
2680
2731
  }
2681
2732
  /** Notify `cb` of the current action for each tracked swap, then on every change. */
2682
2733
  subscribeToActions(cb) {
@@ -2695,6 +2746,8 @@ var SwapTracker = class {
2695
2746
  stop() {
2696
2747
  if (this.#timer) clearInterval(this.#timer);
2697
2748
  this.#timer = void 0;
2749
+ for (const swapId of [...this.#hintRereads.keys()])
2750
+ this.#cancelHintReread(swapId);
2698
2751
  for (const unsub of this.#eventUnsubs) unsub();
2699
2752
  this.#eventUnsubs = [];
2700
2753
  for (const swap of this.#swaps.values())
@@ -2782,6 +2835,7 @@ var SwapTracker = class {
2782
2835
  if (actions.recommended === "none") this.#untrack(swap);
2783
2836
  }
2784
2837
  #untrack(swap) {
2838
+ this.#cancelHintReread(swap.swapId);
2785
2839
  this.#swaps.delete(swap.swapId);
2786
2840
  for (const leg of legsOf(swap)) void this.#managerFor(leg).unregister(leg);
2787
2841
  }
@@ -2955,10 +3009,6 @@ var Client = class {
2955
3009
  return this.#legacy.getQuote(...args);
2956
3010
  }
2957
3011
  /** Delegated to the legacy client (migration checkpoint). */
2958
- createLightningToEvmSwap(...args) {
2959
- return this.#legacy.createLightningToEvmSwap(...args);
2960
- }
2961
- /** Delegated to the legacy client (migration checkpoint). */
2962
3012
  getLightningSendQuote(...args) {
2963
3013
  return this.#legacy.getLightningSendQuote(...args);
2964
3014
  }
@@ -3003,6 +3053,9 @@ var Client = class {
3003
3053
  return this.#legacy.claim(...args);
3004
3054
  }
3005
3055
  /** Delegated to the legacy client (migration checkpoint). */
3056
+ canSponsorClaim(...args) {
3057
+ return this.#legacy.canSponsorClaim(...args);
3058
+ }
3006
3059
  claimEvmWithSigner(...args) {
3007
3060
  return this.#legacy.claimEvmWithSigner(...args);
3008
3061
  }
@@ -3031,6 +3084,26 @@ var Client = class {
3031
3084
  return this.#legacy.buildCollabRefundEvmTypedData(...args);
3032
3085
  }
3033
3086
  /** Delegated to the legacy client (migration checkpoint). */
3087
+ getCollabRefundEvmSignature(...args) {
3088
+ return this.#legacy.getCollabRefundEvmSignature(...args);
3089
+ }
3090
+ /** Delegated to the legacy client (migration checkpoint). */
3091
+ getRehashEvmParams(...args) {
3092
+ return this.#legacy.getRehashEvmParams(...args);
3093
+ }
3094
+ /** Delegated to the legacy client (migration checkpoint). */
3095
+ signRehashEvm(...args) {
3096
+ return this.#legacy.signRehashEvm(...args);
3097
+ }
3098
+ /** Delegated to the legacy client (migration checkpoint). */
3099
+ retrySwapWithInvoice(...args) {
3100
+ return this.#legacy.retrySwapWithInvoice(...args);
3101
+ }
3102
+ /** Delegated to the legacy client (migration checkpoint). */
3103
+ retrySwapWithInvoiceWithSigner(...args) {
3104
+ return this.#legacy.retrySwapWithInvoiceWithSigner(...args);
3105
+ }
3106
+ /** Delegated to the legacy client (migration checkpoint). */
3034
3107
  collabRefundEvmSwap(...args) {
3035
3108
  return this.#legacy.collabRefundEvmSwap(...args);
3036
3109
  }
@@ -3071,6 +3144,12 @@ var Client = class {
3071
3144
  );
3072
3145
  }
3073
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). */
3074
3153
  createLightningToBitcoinSwap(...args) {
3075
3154
  return this.#trackAfterCreate(
3076
3155
  this.#legacy.createLightningToBitcoinSwap(...args)
@@ -3106,9 +3185,14 @@ var Client = class {
3106
3185
  getPermit2FundingParamsUnsigned(...args) {
3107
3186
  return this.#legacy.getPermit2FundingParamsUnsigned(...args);
3108
3187
  }
3109
- /** Delegated to the legacy client (migration checkpoint). */
3110
- fundSwap(...args) {
3111
- return this.#legacy.fundSwap(...args);
3188
+ /**
3189
+ * Fund the swap, then re-verify it against the chain so the lock shows up
3190
+ * in the derived actions without waiting on a server hint.
3191
+ */
3192
+ async fundSwap(...args) {
3193
+ const result = await this.#legacy.fundSwap(...args);
3194
+ this.#reverifyAfterFunding(args[0]);
3195
+ return result;
3112
3196
  }
3113
3197
  /** Delegated to the legacy client (migration checkpoint). */
3114
3198
  fundSwapFromSolana(...args) {
@@ -3135,6 +3219,10 @@ var Client = class {
3135
3219
  return this.#legacy.fundSwapGasless(...args);
3136
3220
  }
3137
3221
  /** Delegated to the legacy client (migration checkpoint). */
3222
+ getEvmFundingExpectation(...args) {
3223
+ return this.#legacy.getEvmFundingExpectation(...args);
3224
+ }
3225
+ /** Delegated to the legacy client (migration checkpoint). */
3138
3226
  getSwapDepositorKey(...args) {
3139
3227
  return this.#legacy.getSwapDepositorKey(...args);
3140
3228
  }
@@ -3335,11 +3423,37 @@ var Client = class {
3335
3423
  for (const swap of swaps) {
3336
3424
  try {
3337
3425
  await tracker.track(swap);
3426
+ if (tracker.trackedSwapIds().includes(swap.swapId))
3427
+ this.#worker?.subscribe([swap.swapId]);
3338
3428
  } catch (error) {
3339
3429
  console.warn(`Client: tracking swap ${swap.swapId} failed:`, error);
3340
3430
  }
3341
3431
  }
3342
3432
  }
3433
+ /**
3434
+ * Re-verify a swap against the chain right after this client funded it.
3435
+ * The funding is mined when `fundSwap` resolves, but nothing else tells the
3436
+ * tracker so: a server hint arrives only after the server's own monitor
3437
+ * sees the lock (on Rootstock, a 30 s poll plus confirmations), and the
3438
+ * tracker's at-risk poll does not re-read an `absent` client leg. One
3439
+ * targeted read moves the swap to `clientfunded` at once. Fire-and-forget:
3440
+ * the funding succeeded whatever the re-verify does, and a swap that is not
3441
+ * tracked is a no-op.
3442
+ *
3443
+ * Chain-verified tracking only: a hints-only tracker has no chain to read,
3444
+ * its `applyHint` re-fetches the server's stored status, which lags the
3445
+ * lock by the same server-side delay this works around.
3446
+ */
3447
+ #reverifyAfterFunding(swapId) {
3448
+ if (typeof swapId !== "string" || !(this.#tracker instanceof SwapTracker))
3449
+ return;
3450
+ void this.#tracker.applyHint(swapId).catch((error) => {
3451
+ console.warn(
3452
+ `Client: re-verify after funding ${swapId} failed (the tracker's next read catches up):`,
3453
+ error
3454
+ );
3455
+ });
3456
+ }
3343
3457
  /**
3344
3458
  * Wire the server status WebSocket into a {@link SwapWorker} whenever a server
3345
3459
  * URL is configured: pushed status transitions trigger a targeted chain
@@ -3352,7 +3466,10 @@ var Client = class {
3352
3466
  const serverUrl = this.#tracking.serverUrl ?? legacyBaseUrl(this.#legacy);
3353
3467
  if (!serverUrl) return;
3354
3468
  const autoClaim = this.#tracking.autoClaim;
3355
- const hintSource = new WsStatusSource({ serverUrl });
3469
+ const hintSource = new WsStatusSource({
3470
+ serverUrl,
3471
+ webSocketFactory: this.#tracking.webSocketFactory
3472
+ });
3356
3473
  const worker = new SwapWorker({
3357
3474
  tracker,
3358
3475
  hintSource,