@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 +135 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +107 -79
- package/dist/index.d.ts +107 -79
- package/dist/index.js +135 -18
- package/dist/index.js.map +1 -1
- package/package.json +10 -4
package/dist/index.js
CHANGED
|
@@ -419,13 +419,10 @@ async function fetchArkadeSpend(source, spentByTxid, paymentHash) {
|
|
|
419
419
|
|
|
420
420
|
// src/contracts/arkade-manager.ts
|
|
421
421
|
function isFunded(vtxo) {
|
|
422
|
-
|
|
423
|
-
return state === "preconfirmed" || state === "settled";
|
|
422
|
+
return !vtxo.isSpent && !vtxo.isSwept;
|
|
424
423
|
}
|
|
425
424
|
function spendTxid(vtxos) {
|
|
426
|
-
const spent = vtxos.find(
|
|
427
|
-
(v) => v.arkTxId || v.spentBy || v.isSpent || v.virtualStatus.state === "spent"
|
|
428
|
-
);
|
|
425
|
+
const spent = vtxos.find((v) => v.arkTxId || v.spentBy || v.isSpent);
|
|
429
426
|
return spent?.arkTxId ?? spent?.spentBy;
|
|
430
427
|
}
|
|
431
428
|
var ArkadeContractManager = class _ArkadeContractManager {
|
|
@@ -1478,6 +1475,17 @@ var SwapWorker = class {
|
|
|
1478
1475
|
)
|
|
1479
1476
|
);
|
|
1480
1477
|
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Put swaps on the hint feed as soon as they are tracked. {@link #onActions}
|
|
1480
|
+
* subscribes a swap when its first action is emitted, but a swap whose leg
|
|
1481
|
+
* cannot be classified yet (an unfunded lock on a chain without `eth_getLogs`)
|
|
1482
|
+
* emits nothing, and without this it would never receive the server hint
|
|
1483
|
+
* that its funding landed.
|
|
1484
|
+
*/
|
|
1485
|
+
subscribe(swapIds) {
|
|
1486
|
+
if (swapIds.length === 0) return;
|
|
1487
|
+
this.#hintSource.subscribe(swapIds);
|
|
1488
|
+
}
|
|
1481
1489
|
/** Stop reacting, cancel pending retries, and close the hint feed. */
|
|
1482
1490
|
stop() {
|
|
1483
1491
|
this.#started = false;
|
|
@@ -1529,7 +1537,7 @@ var SwapWorker = class {
|
|
|
1529
1537
|
const actionId = actions.recommended;
|
|
1530
1538
|
if (!actionId) return;
|
|
1531
1539
|
this.#executing.add(swapId);
|
|
1532
|
-
void execute(swapId, actionId).then(() => this.#cancelRetry(swapId)).catch((error) => {
|
|
1540
|
+
void execute(swapId, actionId, actions).then(() => this.#cancelRetry(swapId)).catch((error) => {
|
|
1533
1541
|
if (needsWallet(error)) {
|
|
1534
1542
|
this.#cancelRetry(swapId);
|
|
1535
1543
|
this.#needsUser.add(swapId);
|
|
@@ -1947,7 +1955,7 @@ function swapToTracked(stored) {
|
|
|
1947
1955
|
token: r.evm_htlc_kind === "native" ? NATIVE_TOKEN_ADDRESS : lockedTokenFallback(r.evm_chain_id, r.network),
|
|
1948
1956
|
// The coordinator created the HTLC on the client's behalf, so it
|
|
1949
1957
|
// is the lock's sender (refund address), as on evm_to_lightning.
|
|
1950
|
-
sender: r.evm_coordinator_address,
|
|
1958
|
+
sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
|
|
1951
1959
|
timelockSec: r.evm_refund_locktime,
|
|
1952
1960
|
createdAt: r.created_at
|
|
1953
1961
|
}),
|
|
@@ -1997,7 +2005,7 @@ function swapToTracked(stored) {
|
|
|
1997
2005
|
token: r.wbtc_address,
|
|
1998
2006
|
// The coordinator created the HTLC on the client's behalf, so it
|
|
1999
2007
|
// is the lock's sender (refund address).
|
|
2000
|
-
sender: r.evm_coordinator_address,
|
|
2008
|
+
sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
|
|
2001
2009
|
timelockSec: r.evm_refund_locktime,
|
|
2002
2010
|
createdAt: r.created_at
|
|
2003
2011
|
}),
|
|
@@ -2092,7 +2100,7 @@ function swapToTracked(stored) {
|
|
|
2092
2100
|
// the server claims the client's EVM HTLC
|
|
2093
2101
|
expectedSats: r.evm_expected_sats,
|
|
2094
2102
|
token: r.wbtc_address,
|
|
2095
|
-
sender: r.evm_coordinator_address,
|
|
2103
|
+
sender: r.evm_htlc_kind === "native" && r.evm_coordinator_address.toLowerCase() === NATIVE_TOKEN_ADDRESS ? r.client_evm_address ?? void 0 : r.evm_coordinator_address,
|
|
2096
2104
|
// the coordinator locks on the client's behalf
|
|
2097
2105
|
timelockSec: r.evm_refund_locktime,
|
|
2098
2106
|
createdAt: r.created_at
|
|
@@ -2158,6 +2166,13 @@ var CLAIM_SUBMITTED = /* @__PURE__ */ new Set([
|
|
|
2158
2166
|
"clientredeemed",
|
|
2159
2167
|
"serverredeemed"
|
|
2160
2168
|
]);
|
|
2169
|
+
var SERVER_LEG_FUNDED = /* @__PURE__ */ new Set([
|
|
2170
|
+
"serverfunded",
|
|
2171
|
+
"clientredeeming",
|
|
2172
|
+
"clientredeemed",
|
|
2173
|
+
"serverredeemed"
|
|
2174
|
+
]);
|
|
2175
|
+
var HINT_REREAD_DELAYS_MS = [1e3, 2e3, 4e3, 8e3];
|
|
2161
2176
|
var DEFAULT_AT_RISK_RECONCILE_INTERVAL_MS = 6e4;
|
|
2162
2177
|
var RETRY_UNREGISTERED_INTERVAL_MS = 6e4;
|
|
2163
2178
|
var SwapTracker = class {
|
|
@@ -2180,6 +2195,8 @@ var SwapTracker = class {
|
|
|
2180
2195
|
#lastRetryAt = 0;
|
|
2181
2196
|
#eventUnsubs = [];
|
|
2182
2197
|
#timer;
|
|
2198
|
+
/** Pending follow-up read per swap, see {@link HINT_REREAD_DELAYS_MS}. */
|
|
2199
|
+
#hintRereads = /* @__PURE__ */ new Map();
|
|
2183
2200
|
constructor(managers, options) {
|
|
2184
2201
|
this.#managers = managers;
|
|
2185
2202
|
this.#refreshIntervalMs = options?.refreshIntervalMs ?? 0;
|
|
@@ -2370,7 +2387,9 @@ var SwapTracker = class {
|
|
|
2370
2387
|
* status hint drives. Reconciles just that swap's legs (targeted, on the
|
|
2371
2388
|
* managers), then recomputes its action. The hint is only a *trigger*: the
|
|
2372
2389
|
* chain stays the source of truth, so a premature hint that the chain doesn't
|
|
2373
|
-
* yet reflect changes nothing
|
|
2390
|
+
* yet reflect changes nothing, except that a hint saying the server's HTLC is
|
|
2391
|
+
* funded is followed by a few more reads of that leg until the chain shows it.
|
|
2392
|
+
* A no-op for an untracked swap.
|
|
2374
2393
|
*/
|
|
2375
2394
|
async applyHint(swapId, opts) {
|
|
2376
2395
|
const swap = this.#swaps.get(swapId);
|
|
@@ -2388,6 +2407,38 @@ var SwapTracker = class {
|
|
|
2388
2407
|
);
|
|
2389
2408
|
});
|
|
2390
2409
|
this.#recompute(swap, opts?.force ?? false);
|
|
2410
|
+
this.#cancelHintReread(swapId);
|
|
2411
|
+
this.#rereadUntilServerLegSeen(swap, 0);
|
|
2412
|
+
}
|
|
2413
|
+
#rereadUntilServerLegSeen(swap, attempt) {
|
|
2414
|
+
const leg = swap.serverHtlc;
|
|
2415
|
+
const hint = this.#hintStatuses.get(swap.swapId);
|
|
2416
|
+
if (!leg || hint === void 0 || !SERVER_LEG_FUNDED.has(hint)) return;
|
|
2417
|
+
if (this.#swaps.get(swap.swapId) !== swap) return;
|
|
2418
|
+
const seen = this.#managerFor(leg).getState(leg);
|
|
2419
|
+
if (seen !== void 0 && seen !== "absent") return;
|
|
2420
|
+
const delay = HINT_REREAD_DELAYS_MS[attempt];
|
|
2421
|
+
if (delay === void 0) return;
|
|
2422
|
+
const timer = setTimeout(() => {
|
|
2423
|
+
this.#hintRereads.delete(swap.swapId);
|
|
2424
|
+
if (this.#swaps.get(swap.swapId) !== swap) return;
|
|
2425
|
+
void this.#managerFor(leg).reconcile(leg).catch((error) => {
|
|
2426
|
+
console.warn(
|
|
2427
|
+
`SwapTracker: re-read after hint failed for ${swap.swapId}:`,
|
|
2428
|
+
error
|
|
2429
|
+
);
|
|
2430
|
+
}).then(() => {
|
|
2431
|
+
this.#recompute(swap);
|
|
2432
|
+
if (!this.#hintRereads.has(swap.swapId))
|
|
2433
|
+
this.#rereadUntilServerLegSeen(swap, attempt + 1);
|
|
2434
|
+
});
|
|
2435
|
+
}, delay);
|
|
2436
|
+
this.#hintRereads.set(swap.swapId, timer);
|
|
2437
|
+
}
|
|
2438
|
+
#cancelHintReread(swapId) {
|
|
2439
|
+
const timer = this.#hintRereads.get(swapId);
|
|
2440
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2441
|
+
this.#hintRereads.delete(swapId);
|
|
2391
2442
|
}
|
|
2392
2443
|
/** Notify `cb` of the current action for each tracked swap, then on every change. */
|
|
2393
2444
|
subscribeToActions(cb) {
|
|
@@ -2406,6 +2457,8 @@ var SwapTracker = class {
|
|
|
2406
2457
|
stop() {
|
|
2407
2458
|
if (this.#timer) clearInterval(this.#timer);
|
|
2408
2459
|
this.#timer = void 0;
|
|
2460
|
+
for (const swapId of [...this.#hintRereads.keys()])
|
|
2461
|
+
this.#cancelHintReread(swapId);
|
|
2409
2462
|
for (const unsub of this.#eventUnsubs) unsub();
|
|
2410
2463
|
this.#eventUnsubs = [];
|
|
2411
2464
|
for (const swap of this.#swaps.values())
|
|
@@ -2493,6 +2546,7 @@ var SwapTracker = class {
|
|
|
2493
2546
|
if (actions.recommended === "none") this.#untrack(swap);
|
|
2494
2547
|
}
|
|
2495
2548
|
#untrack(swap) {
|
|
2549
|
+
this.#cancelHintReread(swap.swapId);
|
|
2496
2550
|
this.#swaps.delete(swap.swapId);
|
|
2497
2551
|
for (const leg of legsOf(swap)) void this.#managerFor(leg).unregister(leg);
|
|
2498
2552
|
}
|
|
@@ -2666,10 +2720,6 @@ var Client = class {
|
|
|
2666
2720
|
return this.#legacy.getQuote(...args);
|
|
2667
2721
|
}
|
|
2668
2722
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2669
|
-
createLightningToEvmSwap(...args) {
|
|
2670
|
-
return this.#legacy.createLightningToEvmSwap(...args);
|
|
2671
|
-
}
|
|
2672
|
-
/** Delegated to the legacy client (migration checkpoint). */
|
|
2673
2723
|
getLightningSendQuote(...args) {
|
|
2674
2724
|
return this.#legacy.getLightningSendQuote(...args);
|
|
2675
2725
|
}
|
|
@@ -2714,6 +2764,9 @@ var Client = class {
|
|
|
2714
2764
|
return this.#legacy.claim(...args);
|
|
2715
2765
|
}
|
|
2716
2766
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2767
|
+
canSponsorClaim(...args) {
|
|
2768
|
+
return this.#legacy.canSponsorClaim(...args);
|
|
2769
|
+
}
|
|
2717
2770
|
claimEvmWithSigner(...args) {
|
|
2718
2771
|
return this.#legacy.claimEvmWithSigner(...args);
|
|
2719
2772
|
}
|
|
@@ -2742,6 +2795,26 @@ var Client = class {
|
|
|
2742
2795
|
return this.#legacy.buildCollabRefundEvmTypedData(...args);
|
|
2743
2796
|
}
|
|
2744
2797
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2798
|
+
getCollabRefundEvmSignature(...args) {
|
|
2799
|
+
return this.#legacy.getCollabRefundEvmSignature(...args);
|
|
2800
|
+
}
|
|
2801
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2802
|
+
getRehashEvmParams(...args) {
|
|
2803
|
+
return this.#legacy.getRehashEvmParams(...args);
|
|
2804
|
+
}
|
|
2805
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2806
|
+
signRehashEvm(...args) {
|
|
2807
|
+
return this.#legacy.signRehashEvm(...args);
|
|
2808
|
+
}
|
|
2809
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2810
|
+
retrySwapWithInvoice(...args) {
|
|
2811
|
+
return this.#legacy.retrySwapWithInvoice(...args);
|
|
2812
|
+
}
|
|
2813
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2814
|
+
retrySwapWithInvoiceWithSigner(...args) {
|
|
2815
|
+
return this.#legacy.retrySwapWithInvoiceWithSigner(...args);
|
|
2816
|
+
}
|
|
2817
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2745
2818
|
collabRefundEvmSwap(...args) {
|
|
2746
2819
|
return this.#legacy.collabRefundEvmSwap(...args);
|
|
2747
2820
|
}
|
|
@@ -2782,6 +2855,12 @@ var Client = class {
|
|
|
2782
2855
|
);
|
|
2783
2856
|
}
|
|
2784
2857
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2858
|
+
createLightningToEvmSwap(...args) {
|
|
2859
|
+
return this.#trackAfterCreate(
|
|
2860
|
+
this.#legacy.createLightningToEvmSwap(...args)
|
|
2861
|
+
);
|
|
2862
|
+
}
|
|
2863
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2785
2864
|
createLightningToBitcoinSwap(...args) {
|
|
2786
2865
|
return this.#trackAfterCreate(
|
|
2787
2866
|
this.#legacy.createLightningToBitcoinSwap(...args)
|
|
@@ -2817,9 +2896,14 @@ var Client = class {
|
|
|
2817
2896
|
getPermit2FundingParamsUnsigned(...args) {
|
|
2818
2897
|
return this.#legacy.getPermit2FundingParamsUnsigned(...args);
|
|
2819
2898
|
}
|
|
2820
|
-
/**
|
|
2821
|
-
|
|
2822
|
-
|
|
2899
|
+
/**
|
|
2900
|
+
* Fund the swap, then re-verify it against the chain so the lock shows up
|
|
2901
|
+
* in the derived actions without waiting on a server hint.
|
|
2902
|
+
*/
|
|
2903
|
+
async fundSwap(...args) {
|
|
2904
|
+
const result = await this.#legacy.fundSwap(...args);
|
|
2905
|
+
this.#reverifyAfterFunding(args[0]);
|
|
2906
|
+
return result;
|
|
2823
2907
|
}
|
|
2824
2908
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2825
2909
|
fundSwapFromSolana(...args) {
|
|
@@ -2846,6 +2930,10 @@ var Client = class {
|
|
|
2846
2930
|
return this.#legacy.fundSwapGasless(...args);
|
|
2847
2931
|
}
|
|
2848
2932
|
/** Delegated to the legacy client (migration checkpoint). */
|
|
2933
|
+
getEvmFundingExpectation(...args) {
|
|
2934
|
+
return this.#legacy.getEvmFundingExpectation(...args);
|
|
2935
|
+
}
|
|
2936
|
+
/** Delegated to the legacy client (migration checkpoint). */
|
|
2849
2937
|
getSwapDepositorKey(...args) {
|
|
2850
2938
|
return this.#legacy.getSwapDepositorKey(...args);
|
|
2851
2939
|
}
|
|
@@ -3046,11 +3134,37 @@ var Client = class {
|
|
|
3046
3134
|
for (const swap of swaps) {
|
|
3047
3135
|
try {
|
|
3048
3136
|
await tracker.track(swap);
|
|
3137
|
+
if (tracker.trackedSwapIds().includes(swap.swapId))
|
|
3138
|
+
this.#worker?.subscribe([swap.swapId]);
|
|
3049
3139
|
} catch (error) {
|
|
3050
3140
|
console.warn(`Client: tracking swap ${swap.swapId} failed:`, error);
|
|
3051
3141
|
}
|
|
3052
3142
|
}
|
|
3053
3143
|
}
|
|
3144
|
+
/**
|
|
3145
|
+
* Re-verify a swap against the chain right after this client funded it.
|
|
3146
|
+
* The funding is mined when `fundSwap` resolves, but nothing else tells the
|
|
3147
|
+
* tracker so: a server hint arrives only after the server's own monitor
|
|
3148
|
+
* sees the lock (on Rootstock, a 30 s poll plus confirmations), and the
|
|
3149
|
+
* tracker's at-risk poll does not re-read an `absent` client leg. One
|
|
3150
|
+
* targeted read moves the swap to `clientfunded` at once. Fire-and-forget:
|
|
3151
|
+
* the funding succeeded whatever the re-verify does, and a swap that is not
|
|
3152
|
+
* tracked is a no-op.
|
|
3153
|
+
*
|
|
3154
|
+
* Chain-verified tracking only: a hints-only tracker has no chain to read,
|
|
3155
|
+
* its `applyHint` re-fetches the server's stored status, which lags the
|
|
3156
|
+
* lock by the same server-side delay this works around.
|
|
3157
|
+
*/
|
|
3158
|
+
#reverifyAfterFunding(swapId) {
|
|
3159
|
+
if (typeof swapId !== "string" || !(this.#tracker instanceof SwapTracker))
|
|
3160
|
+
return;
|
|
3161
|
+
void this.#tracker.applyHint(swapId).catch((error) => {
|
|
3162
|
+
console.warn(
|
|
3163
|
+
`Client: re-verify after funding ${swapId} failed (the tracker's next read catches up):`,
|
|
3164
|
+
error
|
|
3165
|
+
);
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3054
3168
|
/**
|
|
3055
3169
|
* Wire the server status WebSocket into a {@link SwapWorker} whenever a server
|
|
3056
3170
|
* URL is configured: pushed status transitions trigger a targeted chain
|
|
@@ -3063,7 +3177,10 @@ var Client = class {
|
|
|
3063
3177
|
const serverUrl = this.#tracking.serverUrl ?? legacyBaseUrl(this.#legacy);
|
|
3064
3178
|
if (!serverUrl) return;
|
|
3065
3179
|
const autoClaim = this.#tracking.autoClaim;
|
|
3066
|
-
const hintSource = new WsStatusSource({
|
|
3180
|
+
const hintSource = new WsStatusSource({
|
|
3181
|
+
serverUrl,
|
|
3182
|
+
webSocketFactory: this.#tracking.webSocketFactory
|
|
3183
|
+
});
|
|
3067
3184
|
const worker = new SwapWorker({
|
|
3068
3185
|
tracker,
|
|
3069
3186
|
hintSource,
|