@sodax/dapp-kit 2.3.0-rc.4 → 2.4.0-rc.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.
@@ -1,4 +1,11 @@
1
- import { ATOMIC_BATCH_UNCONFIRMED, ChainKeys, SodaxError, type CreateIntentParams } from '@sodax/sdk';
1
+ import {
2
+ ATOMIC_BATCH_UNCONFIRMED,
3
+ ChainKeys,
4
+ IndeterminateOutcomeError,
5
+ intentCreationFailed,
6
+ SodaxError,
7
+ type CreateIntentParams,
8
+ } from '@sodax/sdk';
2
9
  import { beforeEach, describe, expect, it, vi } from 'vitest';
3
10
 
4
11
  const strategyHook = vi.fn();
@@ -306,6 +313,25 @@ describe('useSwapLifecycle', () => {
306
313
  expect(lifecycle.error).toBe(error);
307
314
  });
308
315
 
316
+ it('keeps a recovery-only attempt pending across an input edit without sending again', async () => {
317
+ const recovery = { validUntil: 42 };
318
+ const failure = intentCreationFailed(
319
+ 'swap',
320
+ new IndeterminateOutcomeError('submit reply lost', {
321
+ cause: new Error('timeout'),
322
+ recovery,
323
+ }),
324
+ );
325
+ stub({ swap: { status: 'error', error: failure, variables: { params: PARAMS } } });
326
+ const lifecycle = run({ intentParams: { ...PARAMS, minOutputAmount: 899_000n } });
327
+
328
+ expect(lifecycle.state).toEqual({ kind: 'pending', srcChainKey: PARAMS.srcChainKey, recovery, error: failure });
329
+ expect(statusHook).toHaveBeenCalledWith({ params: { srcChainKey: undefined, srcTxHash: undefined } });
330
+ await expect(lifecycle.next()).resolves.toBeUndefined();
331
+ expect(mutateAsyncSafe).not.toHaveBeenCalled();
332
+ expect(resetSwap).not.toHaveBeenCalled();
333
+ });
334
+
309
335
  it('ignores reset() while the swap call is in flight', () => {
310
336
  stub({ swap: { status: 'pending', isPending: true, variables: { params: PARAMS } } });
311
337
  const lifecycle = run();
@@ -1,4 +1,4 @@
1
- import { ChainKeys } from '@sodax/sdk';
1
+ import { ATOMIC_BATCH_UNCONFIRMED, ChainKeys, SodaxError } from '@sodax/sdk';
2
2
  import { beforeEach, describe, expect, it, vi } from 'vitest';
3
3
 
4
4
  const VARS = {
@@ -8,6 +8,7 @@ const VARS = {
8
8
 
9
9
  type CapturedMutation = {
10
10
  mutationKey: unknown;
11
+ retry: (failureCount: number, error: Error) => boolean;
11
12
  mutationFn: (vars: unknown) => Promise<unknown>;
12
13
  onSuccess: (data: unknown, vars: typeof VARS, ctx: unknown) => Promise<void>;
13
14
  };
@@ -33,6 +34,28 @@ beforeEach(() => {
33
34
  });
34
35
 
35
36
  describe('useSwapWithApproval', () => {
37
+ it.each([
38
+ { batchId: 'batch-1' },
39
+ { reason: ATOMIC_BATCH_UNCONFIRMED },
40
+ ])('suppresses configured retries after an unconfirmed batch ($context)', context => {
41
+ useSwapWithApproval({ mutationOptions: { retry: 3 } });
42
+ const error = new SodaxError('TX_VERIFICATION_FAILED', 'Batch was sent but not confirmed', {
43
+ feature: 'swap',
44
+ context,
45
+ });
46
+ expect(captured.retry(0, error)).toBe(false);
47
+ });
48
+
49
+ it('suppresses configured retries when the original source hash is known', () => {
50
+ useSwapWithApproval({ mutationOptions: { retry: 3 } });
51
+ const error = new SodaxError('RELAY_TIMEOUT', 'Settlement pending', {
52
+ feature: 'swap',
53
+ context: { srcTxHash: '0x1234' },
54
+ });
55
+ expect(captured.retry(0, error)).toBe(false);
56
+ expect(captured.retry(0, new Error('Before submission'))).toBe(true);
57
+ });
58
+
36
59
  it('keys the mutation under swap and calls swapWithApproval in signed mode', async () => {
37
60
  swapWithApproval.mockResolvedValueOnce({ ok: true, value: { approvalStrategy: 'atomic-batch' } });
38
61
  useSwapWithApproval();
@@ -4,6 +4,7 @@ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
4
  import { invalidateBalances } from '../shared/invalidateBalances.js';
5
5
  import type { MutationHookParams } from '../shared/types.js';
6
6
  import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
7
+ import { retryBeforeSubmission } from '../shared/retryBeforeSubmission.js';
7
8
  import { unwrapResult } from '../shared/unwrapResult.js';
8
9
 
9
10
  /**
@@ -34,6 +35,11 @@ export function useSwapWithApproval<K extends SpokeChainKey = SpokeChainKey>({
34
35
  return useSafeMutation<SwapWithApprovalResponse, Error, UseSwapWithApprovalVars<K>>({
35
36
  mutationKey: ['swap', 'swapWithApproval'],
36
37
  ...mutationOptions,
38
+ retry: retryBeforeSubmission(
39
+ mutationOptions?.retry ??
40
+ queryClient.getMutationDefaults?.(mutationOptions?.mutationKey ?? ['swap', 'swapWithApproval'])?.retry ??
41
+ queryClient.getDefaultOptions?.().mutations?.retry,
42
+ ),
37
43
  mutationFn: async vars => unwrapResult(await sodax.swaps.swapWithApproval({ ...vars, raw: false })),
38
44
  onSuccess: async (data, vars, ctx) => {
39
45
  invalidateBalances(queryClient, vars.params.srcChainKey, vars.params.dstChainKey);
@@ -27,7 +27,8 @@ export type SwapSetupReason =
27
27
  * - `checking` — waiting before anything can be done: a gate or the approval strategy is still reading, or a
28
28
  * setup tx (activation, trustline, NEAR storage) is in flight or its check is refreshing.
29
29
  * - `submitting` — the swap call is in flight: signing, and on the default backend path settlement too.
30
- * - `pending` — the source tx is on-chain and its status is being read. `error` is set when the swap call
30
+ * - `pending` — the source tx status is being read, or a recovery handle is waiting for identification.
31
+ * `error` is set when the swap call
31
32
  * failed after broadcast, or when the status read cannot recover (a rejected API key); the swap may
32
33
  * still complete either way. The read can also stop polling without an error (after its budget of
33
34
  * ambiguous reads), so keep `reset()` reachable here.
@@ -43,6 +44,7 @@ export type SwapLifecycleState =
43
44
  | { kind: 'ready'; approvalStrategy: SwapApprovalStrategy }
44
45
  | { kind: 'submitting' }
45
46
  | { kind: 'pending'; srcChainKey: SpokeChainKey; srcTxHash: string; error?: Error }
47
+ | { kind: 'pending'; srcChainKey: SpokeChainKey; srcTxHash?: never; recovery: unknown; error: Error }
46
48
  | { kind: 'unconfirmed'; batchId: string; callCount?: number; error: Error }
47
49
  | { kind: 'settled'; srcChainKey: SpokeChainKey; srcTxHash: string; fillTxHash?: Hex }
48
50
  | { kind: 'failed'; error: Error; srcChainKey?: SpokeChainKey; srcTxHash?: string };
@@ -60,6 +62,7 @@ export type SwapAttempt =
60
62
  | { phase: 'none' }
61
63
  | { phase: 'submitting' }
62
64
  | { phase: 'broadcast'; srcChainKey: SpokeChainKey; srcTxHash: string; error?: Error }
65
+ | { phase: 'recovering'; srcChainKey: SpokeChainKey; recovery: unknown; error: Error }
63
66
  | { phase: 'unconfirmed'; batchId: string; callCount?: number; error: Error }
64
67
  | { phase: 'failed'; error: Error };
65
68
 
@@ -112,7 +115,9 @@ export function toSwapAttempt(mutation: SwapAttemptSource, intentParams: CreateI
112
115
  if (mutation.status === 'idle' || !attempted) return { phase: 'none' };
113
116
  const attempt = toFinishedAttempt(mutation, attempted);
114
117
  const mayStillLand =
115
- attempt.phase === 'unconfirmed' || (attempt.phase === 'broadcast' && attempt.error !== undefined);
118
+ attempt.phase === 'unconfirmed' ||
119
+ attempt.phase === 'recovering' ||
120
+ (attempt.phase === 'broadcast' && attempt.error !== undefined);
116
121
  if (mayStillLand || (intentParams && isSameIntent(attempted, intentParams))) return attempt;
117
122
  return { phase: 'none' };
118
123
  }
@@ -127,6 +132,9 @@ function toFinishedAttempt(mutation: SwapAttemptSource, attempted: CreateIntentP
127
132
  if (typeof context?.srcTxHash === 'string') {
128
133
  return { phase: 'broadcast', srcChainKey: attempted.srcChainKey, srcTxHash: context.srcTxHash, error };
129
134
  }
135
+ if (context?.recovery !== undefined) {
136
+ return { phase: 'recovering', srcChainKey: attempted.srcChainKey, recovery: context.recovery, error };
137
+ }
130
138
  if (context?.reason === ATOMIC_BATCH_UNCONFIRMED && typeof context.batchId === 'string') {
131
139
  return typeof context.callCount === 'number'
132
140
  ? { phase: 'unconfirmed', batchId: context.batchId, callCount: context.callCount, error }
@@ -152,6 +160,10 @@ export function resolveSwapLifecycle(inputs: SwapLifecycleInputs): SwapLifecycle
152
160
  const error = attempt.error ?? inputs.statusError;
153
161
  return error ? { kind: 'pending', srcChainKey, srcTxHash, error } : { kind: 'pending', srcChainKey, srcTxHash };
154
162
  }
163
+ if (attempt.phase === 'recovering') {
164
+ const { srcChainKey, recovery, error } = attempt;
165
+ return { kind: 'pending', srcChainKey, recovery, error };
166
+ }
155
167
  if (attempt.phase === 'unconfirmed') {
156
168
  const { batchId, callCount, error } = attempt;
157
169
  return callCount === undefined