@sodax/dapp-kit 2.2.0-rc.6 → 2.2.0-rc.7
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/README.md +20 -3
- package/dist/index.d.ts +107 -13
- package/dist/index.mjs +118 -38
- package/package.json +4 -4
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.test.ts +144 -0
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.ts +52 -0
- package/src/hooks/bridge/index.ts +2 -0
- package/src/hooks/bridge/useBridgeDetailedStatus.test.ts +93 -0
- package/src/hooks/bridge/useBridgeDetailedStatus.ts +79 -0
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.test.ts +81 -0
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.ts +25 -0
- package/src/hooks/bridgeApi/useBridgeApiAllowance.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiApprove.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiBridgeableAmount.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiFee.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiIsBridgeable.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiSubmitTx.ts +5 -3
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.test.ts +63 -0
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.ts +7 -10
- package/src/hooks/bridgeApi/useBridgeApiTokens.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiTokensByChain.ts +2 -1
- package/src/hooks/leverageYield/index.ts +1 -0
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.test.ts +131 -0
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.ts +83 -0
- package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +13 -3
- package/src/hooks/shared/notFoundStreak.ts +59 -0
- package/src/hooks/shared/retryUnlessAuthFailure.ts +1 -1
- package/src/hooks/{swap/getSwapStatusRefetchInterval.test.ts → shared/solverStatusPolicy.test.ts} +25 -25
- package/src/hooks/shared/solverStatusPolicy.ts +117 -0
- package/src/hooks/swap/useDetailedStatus.test.ts +65 -21
- package/src/hooks/swap/useDetailedStatus.ts +9 -9
- package/src/hooks/swap/useStatus.ts +4 -7
- package/src/hooks/swap/getSwapStatusRefetchInterval.ts +0 -126
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DetailedLeverageYieldStatus,
|
|
3
|
+
LeverageYieldDetailedStatusError,
|
|
4
|
+
RequestOverrideConfig,
|
|
5
|
+
Result,
|
|
6
|
+
SpokeChainKey,
|
|
7
|
+
} from '@sodax/sdk';
|
|
8
|
+
import { useRef } from 'react';
|
|
9
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
10
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
11
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
12
|
+
import { advanceNotFoundStreak, INITIAL_NOT_FOUND_STREAK } from '../shared/notFoundStreak.js';
|
|
13
|
+
import {
|
|
14
|
+
getSolverDetailedStatusRefetchInterval,
|
|
15
|
+
isSolverNotFound,
|
|
16
|
+
toNotFoundBudgetRead,
|
|
17
|
+
} from '../shared/solverStatusPolicy.js';
|
|
18
|
+
|
|
19
|
+
export type UseLeverageYieldDetailedStatusResult =
|
|
20
|
+
| Result<DetailedLeverageYieldStatus, LeverageYieldDetailedStatusError>
|
|
21
|
+
| undefined;
|
|
22
|
+
|
|
23
|
+
export type UseLeverageYieldDetailedStatusParams = ReadHookParams<
|
|
24
|
+
UseLeverageYieldDetailedStatusResult,
|
|
25
|
+
{
|
|
26
|
+
srcChainKey: SpokeChainKey | undefined;
|
|
27
|
+
srcTxHash: string | undefined;
|
|
28
|
+
apiConfig?: RequestOverrideConfig;
|
|
29
|
+
}
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Hook for monitoring a leverage-yield vault swap from its source-chain transaction.
|
|
34
|
+
*
|
|
35
|
+
* `useLeverageYieldApiSubmitTxStatus` needs the backend to still hold a usable record; this takes
|
|
36
|
+
* `(srcChainKey, srcTxHash)` — the pair you hold after `vaultSwap()` — and routes to whichever
|
|
37
|
+
* source can answer, so it also covers a vault swap the client-side fallback completed. `data.value`
|
|
38
|
+
* is discriminated on `source`, and each variant carries that source's payload unmodified.
|
|
39
|
+
*
|
|
40
|
+
* Polls every 3s. Stops once the answering source is terminal, once the backend rejects the API
|
|
41
|
+
* key, and — like `useDetailedStatus` — after 40 consecutive *ambiguous* reads: a solver
|
|
42
|
+
* `NOT_FOUND`, or a `LOOKUP_FAILED` whose relay has no packet for the tx. Neither can distinguish "still in flight" from "never will be", so the
|
|
43
|
+
* budget is what stops a vault swap nothing can resolve. A dependency outage — relay unreachable,
|
|
44
|
+
* solver down — keeps polling instead, so the read recovers by itself; any real status resets the
|
|
45
|
+
* budget. Override with `queryOptions.refetchInterval`.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const { data } = useLeverageYieldDetailedStatus({ params: { srcChainKey, srcTxHash } });
|
|
50
|
+
* if (data?.ok && data.value.source === 'backend') console.log(data.value.data.status);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export const useLeverageYieldDetailedStatus = ({
|
|
54
|
+
params,
|
|
55
|
+
queryOptions,
|
|
56
|
+
}: UseLeverageYieldDetailedStatusParams = {}): UseQueryResult<UseLeverageYieldDetailedStatusResult> => {
|
|
57
|
+
const { sodax } = useSodaxContext();
|
|
58
|
+
const srcChainKey = params?.srcChainKey;
|
|
59
|
+
const srcTxHash = params?.srcTxHash;
|
|
60
|
+
const apiConfig = params?.apiConfig;
|
|
61
|
+
// Composite identity: a new vault swap must start its own NOT_FOUND budget.
|
|
62
|
+
const pollKey = srcChainKey && srcTxHash ? `${srcChainKey}:${srcTxHash}` : undefined;
|
|
63
|
+
const notFoundStreakRef = useRef({ ...INITIAL_NOT_FOUND_STREAK });
|
|
64
|
+
|
|
65
|
+
return useQuery({
|
|
66
|
+
queryKey: ['leverageYield', 'detailedStatus', srcChainKey, srcTxHash],
|
|
67
|
+
queryFn: async () => {
|
|
68
|
+
if (!srcChainKey || !srcTxHash) return undefined;
|
|
69
|
+
return sodax.leverageYield.getDetailedStatus({ srcChainKey, srcTxHash }, apiConfig);
|
|
70
|
+
},
|
|
71
|
+
enabled: !!srcChainKey && !!srcTxHash,
|
|
72
|
+
refetchInterval: query => {
|
|
73
|
+
notFoundStreakRef.current = advanceNotFoundStreak(
|
|
74
|
+
notFoundStreakRef.current,
|
|
75
|
+
pollKey,
|
|
76
|
+
isSolverNotFound(toNotFoundBudgetRead(query.state.data)),
|
|
77
|
+
query.state.dataUpdateCount,
|
|
78
|
+
);
|
|
79
|
+
return getSolverDetailedStatusRefetchInterval(query.state.data, notFoundStreakRef.current.consecutiveNotFound);
|
|
80
|
+
},
|
|
81
|
+
...queryOptions,
|
|
82
|
+
});
|
|
83
|
+
};
|
|
@@ -21,9 +21,19 @@ export type UseLeverageYieldVaultSwapVars<K extends SpokeChainKey = SpokeChainKe
|
|
|
21
21
|
*
|
|
22
22
|
* Spread a `LeverageYieldSwapPayload` built by {@link useLeverageYieldDeposit} /
|
|
23
23
|
* {@link useLeverageYieldWithdraw} into `mutate` alongside the wallet provider:
|
|
24
|
-
* `vaultSwap({ ...payload, walletProvider })`. Runs `sodax.leverageYield.vaultSwap
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* `vaultSwap({ ...payload, walletProvider })`. Runs `sodax.leverageYield.vaultSwap`, so vault flows
|
|
25
|
+
* never touch the generic swap surface.
|
|
26
|
+
*
|
|
27
|
+
* Which transport it takes is config, not an argument. With `leverageYield.useBackendSubmitTx` on,
|
|
28
|
+
* it creates the intent and hands the broadcast tx to the leverage-yield API, which relays and
|
|
29
|
+
* post-executes server-side, falling back to the client-side path (verify → relay → notify solver)
|
|
30
|
+
* on any non-success. With it off, that client-side path runs outright. Read the effective value on
|
|
31
|
+
* `sodax.config.leverageYieldUseBackendSubmitTx` rather than assuming which way it points.
|
|
32
|
+
* Either way the resolved value is the same `VaultSwapResponse`.
|
|
33
|
+
*
|
|
34
|
+
* Pass `extras.apiKey` to key the backend leg per action, overriding the instance key. Poll the
|
|
35
|
+
* result with {@link useLeverageYieldDetailedStatus}, which reads a vault swap's status from the
|
|
36
|
+
* source-chain tx hash whichever transport completed it.
|
|
27
37
|
*
|
|
28
38
|
* Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
|
|
29
39
|
* `onError`, `retry`). Returns the unwrapped `VaultSwapResponse` on success.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The consecutive-ambiguous-read budget shared by the status polls.
|
|
3
|
+
*
|
|
4
|
+
* Some reads cannot distinguish "still in flight" from "will never resolve" — a solver that has not
|
|
5
|
+
* seen an intent, a relay with no packet for a source tx. Polling those forever is the failure mode
|
|
6
|
+
* a budget exists to stop, but only for that shape: a dependency failing *right now* must keep
|
|
7
|
+
* polling, or a two-minute outage would permanently stick a transfer that is fine.
|
|
8
|
+
*
|
|
9
|
+
* Each feature decides what counts as an ambiguous miss and passes a boolean; the counting,
|
|
10
|
+
* identity and de-duplication are the same everywhere and live here.
|
|
11
|
+
*
|
|
12
|
+
* Kept free of React and context imports so it is unit-testable in dapp-kit's `node` test
|
|
13
|
+
* environment — importing a hook pulls in `useSodaxContext`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const STATUS_POLL_MS = 3000;
|
|
17
|
+
/** Cap consecutive ambiguous polls (~2 min at 3s). The first one is a race, not a stop. */
|
|
18
|
+
export const MAX_NOT_FOUND_POLLS = 40;
|
|
19
|
+
|
|
20
|
+
export type NotFoundStreakState = {
|
|
21
|
+
/** Identity of what is being polled — an intent tx hash, or a composite source-chain/tx key. */
|
|
22
|
+
pollKey: string | undefined;
|
|
23
|
+
seenUpdates: number;
|
|
24
|
+
consecutiveNotFound: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const INITIAL_NOT_FOUND_STREAK: NotFoundStreakState = {
|
|
28
|
+
pollKey: undefined,
|
|
29
|
+
seenUpdates: 0,
|
|
30
|
+
consecutiveNotFound: 0,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function nextNotFoundStreak(isAmbiguousMiss: boolean, previousStreak: number): number {
|
|
34
|
+
return isAmbiguousMiss ? previousStreak + 1 : 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Advances the counter once per successful query update. A `pollKey` change starts a new streak so
|
|
39
|
+
* a prior transfer's count cannot stop the next one. React Query may call `refetchInterval` more
|
|
40
|
+
* than once per fetch — same `dataUpdateCount` is a no-op.
|
|
41
|
+
*/
|
|
42
|
+
export function advanceNotFoundStreak(
|
|
43
|
+
state: NotFoundStreakState,
|
|
44
|
+
pollKey: string | undefined,
|
|
45
|
+
isAmbiguousMiss: boolean,
|
|
46
|
+
dataUpdateCount: number,
|
|
47
|
+
): NotFoundStreakState {
|
|
48
|
+
if (state.pollKey !== pollKey) {
|
|
49
|
+
state = { pollKey, seenUpdates: 0, consecutiveNotFound: 0 };
|
|
50
|
+
}
|
|
51
|
+
if (state.seenUpdates === dataUpdateCount) {
|
|
52
|
+
return state;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
pollKey,
|
|
56
|
+
seenUpdates: dataUpdateCount,
|
|
57
|
+
consecutiveNotFound: nextNotFoundStreak(isAmbiguousMiss, state.consecutiveNotFound),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -4,7 +4,7 @@ import { isAuthFailure } from '@sodax/sdk';
|
|
|
4
4
|
const MAX_RETRIES = 3;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Default React Query `retry` policy for the backend swaps-
|
|
7
|
+
* Default React Query `retry` policy for the backend API hooks (swaps, leverage-yield, bridge): replay a transport blip, never a
|
|
8
8
|
* terminal API-key rejection (see `isAuthFailure`). Retrying a rejected key only multiplies doomed
|
|
9
9
|
* requests before the consumer sees the error.
|
|
10
10
|
*
|
package/src/hooks/{swap/getSwapStatusRefetchInterval.test.ts → shared/solverStatusPolicy.test.ts}
RENAMED
|
@@ -2,12 +2,12 @@ import { SolverIntentErrorCode, SolverIntentStatusCode } from '@sodax/sdk';
|
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
3
|
import {
|
|
4
4
|
advanceNotFoundStreak,
|
|
5
|
-
getSwapStatusRefetchInterval,
|
|
6
5
|
INITIAL_NOT_FOUND_STREAK,
|
|
7
6
|
MAX_NOT_FOUND_POLLS,
|
|
8
7
|
nextNotFoundStreak,
|
|
9
8
|
STATUS_POLL_MS,
|
|
10
|
-
} from './
|
|
9
|
+
} from './notFoundStreak.js';
|
|
10
|
+
import { getSolverStatusRefetchInterval, isSolverNotFound } from './solverStatusPolicy.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Guards the polling-stop invariant for `useStatus.refetchInterval`: keep polling until SOLVED/
|
|
@@ -23,52 +23,52 @@ const solverError = {
|
|
|
23
23
|
error: { detail: { code: SolverIntentErrorCode.INTENT_NOT_FOUND, message: 'missing' } },
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
describe('
|
|
26
|
+
describe('getSolverStatusRefetchInterval', () => {
|
|
27
27
|
it('stops immediately on SOLVED (3) and FAILED (4), even at consecutive count 1', () => {
|
|
28
|
-
expect(
|
|
29
|
-
expect(
|
|
28
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.SOLVED), 1)).toBe(false);
|
|
29
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.FAILED), 1)).toBe(false);
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
it('keeps polling NOT_FOUND until MAX_NOT_FOUND_POLLS consecutive, then stops', () => {
|
|
33
|
-
expect(
|
|
33
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.NOT_FOUND), MAX_NOT_FOUND_POLLS - 1)).toBe(
|
|
34
34
|
STATUS_POLL_MS,
|
|
35
35
|
);
|
|
36
|
-
expect(
|
|
36
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.NOT_FOUND), MAX_NOT_FOUND_POLLS)).toBe(false);
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
it('never stops in-flight NOT_STARTED_YET (1) or STARTED_NOT_FINISHED (2), even at a high count', () => {
|
|
40
|
-
expect(
|
|
41
|
-
expect(
|
|
40
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.NOT_STARTED_YET), 100)).toBe(STATUS_POLL_MS);
|
|
41
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.STARTED_NOT_FINISHED), 100)).toBe(STATUS_POLL_MS);
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
it('keeps polling the first NOT_FOUND after a long in-flight streak', () => {
|
|
45
|
-
expect(
|
|
45
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.NOT_FOUND), 1)).toBe(STATUS_POLL_MS);
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('keeps polling when no status has arrived yet (undefined) or the Result is ok: false', () => {
|
|
49
|
-
expect(
|
|
50
|
-
expect(
|
|
49
|
+
expect(getSolverStatusRefetchInterval(undefined, 1)).toBe(STATUS_POLL_MS);
|
|
50
|
+
expect(getSolverStatusRefetchInterval(solverError, MAX_NOT_FOUND_POLLS)).toBe(STATUS_POLL_MS);
|
|
51
51
|
});
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
describe('nextNotFoundStreak', () => {
|
|
55
55
|
it('increments only while status is NOT_FOUND and resets otherwise', () => {
|
|
56
|
-
expect(nextNotFoundStreak(ok(SolverIntentStatusCode.NOT_FOUND), 0)).toBe(1);
|
|
57
|
-
expect(nextNotFoundStreak(ok(SolverIntentStatusCode.NOT_FOUND), 39)).toBe(40);
|
|
58
|
-
expect(nextNotFoundStreak(ok(SolverIntentStatusCode.STARTED_NOT_FINISHED), 39)).toBe(0);
|
|
59
|
-
expect(nextNotFoundStreak(undefined, 5)).toBe(0);
|
|
60
|
-
expect(nextNotFoundStreak(solverError, 5)).toBe(0);
|
|
56
|
+
expect(nextNotFoundStreak(isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 0)).toBe(1);
|
|
57
|
+
expect(nextNotFoundStreak(isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 39)).toBe(40);
|
|
58
|
+
expect(nextNotFoundStreak(isSolverNotFound(ok(SolverIntentStatusCode.STARTED_NOT_FINISHED)), 39)).toBe(0);
|
|
59
|
+
expect(nextNotFoundStreak(isSolverNotFound(undefined), 5)).toBe(0);
|
|
60
|
+
expect(nextNotFoundStreak(isSolverNotFound(solverError), 5)).toBe(0);
|
|
61
61
|
});
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
describe('advanceNotFoundStreak', () => {
|
|
65
65
|
it('increments consecutive NOT_FOUND once per dataUpdateCount, not per refetchInterval call', () => {
|
|
66
66
|
let state = INITIAL_NOT_FOUND_STREAK;
|
|
67
|
-
state = advanceNotFoundStreak(state, HASH_A, ok(SolverIntentStatusCode.NOT_FOUND), 1);
|
|
67
|
+
state = advanceNotFoundStreak(state, HASH_A, isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 1);
|
|
68
68
|
expect(state.consecutiveNotFound).toBe(1);
|
|
69
|
-
state = advanceNotFoundStreak(state, HASH_A, ok(SolverIntentStatusCode.NOT_FOUND), 1);
|
|
69
|
+
state = advanceNotFoundStreak(state, HASH_A, isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 1);
|
|
70
70
|
expect(state.consecutiveNotFound).toBe(1);
|
|
71
|
-
state = advanceNotFoundStreak(state, HASH_A, ok(SolverIntentStatusCode.NOT_FOUND), 2);
|
|
71
|
+
state = advanceNotFoundStreak(state, HASH_A, isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 2);
|
|
72
72
|
expect(state.consecutiveNotFound).toBe(2);
|
|
73
73
|
});
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ describe('advanceNotFoundStreak', () => {
|
|
|
76
76
|
const started = advanceNotFoundStreak(
|
|
77
77
|
{ pollKey: HASH_A, seenUpdates: 39, consecutiveNotFound: 39 },
|
|
78
78
|
HASH_A,
|
|
79
|
-
ok(SolverIntentStatusCode.STARTED_NOT_FINISHED),
|
|
79
|
+
isSolverNotFound(ok(SolverIntentStatusCode.STARTED_NOT_FINISHED)),
|
|
80
80
|
40,
|
|
81
81
|
);
|
|
82
82
|
expect(started.consecutiveNotFound).toBe(0);
|
|
@@ -84,10 +84,10 @@ describe('advanceNotFoundStreak', () => {
|
|
|
84
84
|
|
|
85
85
|
it('starts a new streak at 1 after in-flight polls, so a solver restart cannot stop the query', () => {
|
|
86
86
|
let state = INITIAL_NOT_FOUND_STREAK;
|
|
87
|
-
state = advanceNotFoundStreak(state, HASH_A, ok(SolverIntentStatusCode.STARTED_NOT_FINISHED), 40);
|
|
88
|
-
state = advanceNotFoundStreak(state, HASH_A, ok(SolverIntentStatusCode.NOT_FOUND), 41);
|
|
87
|
+
state = advanceNotFoundStreak(state, HASH_A, isSolverNotFound(ok(SolverIntentStatusCode.STARTED_NOT_FINISHED)), 40);
|
|
88
|
+
state = advanceNotFoundStreak(state, HASH_A, isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)), 41);
|
|
89
89
|
expect(state.consecutiveNotFound).toBe(1);
|
|
90
|
-
expect(
|
|
90
|
+
expect(getSolverStatusRefetchInterval(ok(SolverIntentStatusCode.NOT_FOUND), state.consecutiveNotFound)).toBe(
|
|
91
91
|
STATUS_POLL_MS,
|
|
92
92
|
);
|
|
93
93
|
});
|
|
@@ -96,7 +96,7 @@ describe('advanceNotFoundStreak', () => {
|
|
|
96
96
|
const state = advanceNotFoundStreak(
|
|
97
97
|
{ pollKey: HASH_A, seenUpdates: 39, consecutiveNotFound: 39 },
|
|
98
98
|
HASH_B,
|
|
99
|
-
ok(SolverIntentStatusCode.NOT_FOUND),
|
|
99
|
+
isSolverNotFound(ok(SolverIntentStatusCode.NOT_FOUND)),
|
|
100
100
|
1,
|
|
101
101
|
);
|
|
102
102
|
expect(state.consecutiveNotFound).toBe(1);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The polling policy shared by every solver-backed status read.
|
|
3
|
+
*
|
|
4
|
+
* Two features now resolve a source tx to a solver answer — swap and leverage yield — and their
|
|
5
|
+
* status unions are structurally the same, so the terminality rules and the ambiguous-miss mapping
|
|
6
|
+
* are identical. They live here rather than in one feature's folder: `hooks/` has no cross-feature
|
|
7
|
+
* imports anywhere else, and a second verbatim copy is what this extraction exists to prevent.
|
|
8
|
+
* Bridge keeps its own policy beside its hook, because its second arm is a relay packet that is
|
|
9
|
+
* terminal on arrival and has no solver vocabulary at all.
|
|
10
|
+
*
|
|
11
|
+
* The read types are declared structurally rather than imported from either feature: this module
|
|
12
|
+
* must not own another hook's public contract, and both `DetailedSwapStatus` and
|
|
13
|
+
* `DetailedLeverageYieldStatus` satisfy the shape below.
|
|
14
|
+
*
|
|
15
|
+
* Kept free of React and context imports so it is unit-testable in dapp-kit's `node` test
|
|
16
|
+
* environment — importing a hook pulls in `useSodaxContext`.
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
DETAILED_STATUS_NOT_DELIVERED,
|
|
20
|
+
isAuthFailure,
|
|
21
|
+
SolverIntentStatusCode,
|
|
22
|
+
type Result,
|
|
23
|
+
type SodaxError,
|
|
24
|
+
type SolverErrorResponse,
|
|
25
|
+
type SolverIntentStatusResponse,
|
|
26
|
+
} from '@sodax/sdk';
|
|
27
|
+
|
|
28
|
+
import { MAX_NOT_FOUND_POLLS, STATUS_POLL_MS } from './notFoundStreak.js';
|
|
29
|
+
|
|
30
|
+
/** A solver status read, as `useStatus` holds it. */
|
|
31
|
+
export type SolverStatusRead = Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The structural minimum a detailed-status read must satisfy to use this policy: a backend arm
|
|
35
|
+
* reporting terminality in the submit-tx vocabulary, and a solver arm carrying the solver's own
|
|
36
|
+
* response. A feature's arm may carry more (leverage yield and swap both hoist `dstTxHash`); only
|
|
37
|
+
* what is read here is required.
|
|
38
|
+
*/
|
|
39
|
+
export type SolverDetailedStatusRead =
|
|
40
|
+
| Result<
|
|
41
|
+
{ source: 'backend'; data: { status: string } } | { source: 'solver'; data: SolverIntentStatusResponse },
|
|
42
|
+
SodaxError<'LOOKUP_FAILED'>
|
|
43
|
+
>
|
|
44
|
+
| undefined;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Polling interval for a plain solver status read (`useStatus`). Stops on SOLVED/FAILED immediately;
|
|
48
|
+
* stops on NOT_FOUND only after `MAX_NOT_FOUND_POLLS` consecutive successful fetches (solver forgot /
|
|
49
|
+
* never saw the intent). In-flight statuses keep polling with no global cap.
|
|
50
|
+
*/
|
|
51
|
+
export function getSolverStatusRefetchInterval(data: SolverStatusRead, consecutiveNotFound: number): number | false {
|
|
52
|
+
const status = data?.ok ? data.value.status : undefined;
|
|
53
|
+
if (status === SolverIntentStatusCode.SOLVED || status === SolverIntentStatusCode.FAILED) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (status === SolverIntentStatusCode.NOT_FOUND && consecutiveNotFound >= MAX_NOT_FOUND_POLLS) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return STATUS_POLL_MS;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The read shape the budget counts: a solver that has not seen this intent. */
|
|
63
|
+
export function isSolverNotFound(data: SolverStatusRead): boolean {
|
|
64
|
+
return (data?.ok ? data.value.status : undefined) === SolverIntentStatusCode.NOT_FOUND;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Maps a detailed-status read onto the shape the shared NOT_FOUND policy consumes, three ways:
|
|
69
|
+
*
|
|
70
|
+
* - solver arm → passes through, so its status drives the budget as it does for `useStatus`;
|
|
71
|
+
* - backend arm → `undefined`, which resets the budget; the solver is not the one being asked;
|
|
72
|
+
* - failed read → counts as `NOT_FOUND` **only** when the relay has no packet for this source tx
|
|
73
|
+
* (`DETAILED_STATUS_NOT_DELIVERED`). That miss is ambiguous — a transfer still in flight and one
|
|
74
|
+
* whose tx never relayed look the same — so a budget is the only way to stop the second case.
|
|
75
|
+
*
|
|
76
|
+
* Every other `LOOKUP_FAILED` is a dependency failing right now: the relay unreachable, a malformed
|
|
77
|
+
* response, the solver down. Those reset the budget and keep polling, because that is how the read
|
|
78
|
+
* recovers when the outage ends. Counting them would stop polling a healthy transfer after ~2 min of
|
|
79
|
+
* downtime and never resume.
|
|
80
|
+
*/
|
|
81
|
+
export function toNotFoundBudgetRead(data: SolverDetailedStatusRead): SolverStatusRead {
|
|
82
|
+
if (data === undefined) return undefined;
|
|
83
|
+
if (data.ok) {
|
|
84
|
+
return data.value.source === 'solver' ? { ok: true, value: data.value.data } : undefined;
|
|
85
|
+
}
|
|
86
|
+
return data.error?.context?.reason === DETAILED_STATUS_NOT_DELIVERED
|
|
87
|
+
? { ok: true, value: { status: SolverIntentStatusCode.NOT_FOUND } }
|
|
88
|
+
: undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Polling interval for a detailed-status read. Backend records report terminality in their own
|
|
93
|
+
* vocabulary; everything else reuses the solver policy verbatim, so `MAX_NOT_FOUND_POLLS` stays the
|
|
94
|
+
* single cutoff — for a forgotten intent and for a transfer no source can resolve.
|
|
95
|
+
*
|
|
96
|
+
* A rejected API key is the one stop that sits outside both: `retryUnlessAuthFailure` cannot catch
|
|
97
|
+
* it, because the SDK returns the 401/403 as a `Result` rather than throwing, so React Query never
|
|
98
|
+
* sees an error to withhold a retry from. Without this branch a bad key polls forever whenever the
|
|
99
|
+
* relay has also not delivered — the miss is unprovable behind an unanswered backend, so it never
|
|
100
|
+
* consumes the not-delivered budget either.
|
|
101
|
+
*/
|
|
102
|
+
export function getSolverDetailedStatusRefetchInterval(
|
|
103
|
+
data: SolverDetailedStatusRead,
|
|
104
|
+
consecutiveNotFound: number,
|
|
105
|
+
): number | false {
|
|
106
|
+
// A rejected key is terminal — only a corrected key changes the answer, so stop asking.
|
|
107
|
+
if (data && !data.ok && isAuthFailure(data.error)) return false;
|
|
108
|
+
if (data?.ok && data.value.source === 'backend') {
|
|
109
|
+
// Both terminal states of the submit-tx status vocabulary. Today the SDK routes `'failed'`
|
|
110
|
+
// records to the solver so only `'solved'` reaches us, but that is its routing rule, not this
|
|
111
|
+
// hook's contract — encoding the wire contract keeps a terminal record from being polled forever
|
|
112
|
+
// if it changes.
|
|
113
|
+
const { status } = data.value.data;
|
|
114
|
+
return status === 'solved' || status === 'failed' ? false : STATUS_POLL_MS;
|
|
115
|
+
}
|
|
116
|
+
return getSolverStatusRefetchInterval(toNotFoundBudgetRead(data), consecutiveNotFound);
|
|
117
|
+
}
|
|
@@ -2,12 +2,15 @@ import { DETAILED_STATUS_NOT_DELIVERED, SodaxError, SolverIntentStatusCode, type
|
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
3
|
import {
|
|
4
4
|
advanceNotFoundStreak,
|
|
5
|
-
getDetailedStatusRefetchInterval,
|
|
6
5
|
INITIAL_NOT_FOUND_STREAK,
|
|
7
6
|
MAX_NOT_FOUND_POLLS,
|
|
8
7
|
STATUS_POLL_MS,
|
|
8
|
+
} from '../shared/notFoundStreak.js';
|
|
9
|
+
import {
|
|
10
|
+
getSolverDetailedStatusRefetchInterval,
|
|
11
|
+
isSolverNotFound,
|
|
9
12
|
toNotFoundBudgetRead,
|
|
10
|
-
} from '
|
|
13
|
+
} from '../shared/solverStatusPolicy.js';
|
|
11
14
|
|
|
12
15
|
/**
|
|
13
16
|
* Guards the polling-stop invariant for `useDetailedStatus`. The two variants report terminality in
|
|
@@ -48,23 +51,33 @@ const outage = {
|
|
|
48
51
|
error: new SodaxError('LOOKUP_FAILED', 'relay down', { feature: 'swap' }),
|
|
49
52
|
};
|
|
50
53
|
|
|
54
|
+
/** A key the backend rejected. Terminal, and reached before any budget — see the stop below. */
|
|
55
|
+
const rejectedKey = {
|
|
56
|
+
ok: false as const,
|
|
57
|
+
error: new SodaxError('LOOKUP_FAILED', 'rejected', { feature: 'swap', context: { status: 401 } }),
|
|
58
|
+
};
|
|
59
|
+
|
|
51
60
|
const KEY_A = 'arb:0xaaa';
|
|
52
61
|
const KEY_B = 'arb:0xbbb';
|
|
53
62
|
|
|
54
63
|
/** Drives the streak the way the hook does, one query update per call. */
|
|
55
64
|
const advance = (
|
|
56
65
|
state: typeof INITIAL_NOT_FOUND_STREAK,
|
|
57
|
-
reads: (ReturnType<typeof ok> | typeof notDelivered | typeof outage)[],
|
|
66
|
+
reads: (ReturnType<typeof ok> | typeof notDelivered | typeof outage | typeof rejectedKey)[],
|
|
58
67
|
key = KEY_A,
|
|
59
68
|
from = 1,
|
|
60
|
-
) =>
|
|
69
|
+
) =>
|
|
70
|
+
reads.reduce(
|
|
71
|
+
(acc, read, i) => advanceNotFoundStreak(acc, key, isSolverNotFound(toNotFoundBudgetRead(read)), from + i),
|
|
72
|
+
state,
|
|
73
|
+
);
|
|
61
74
|
|
|
62
|
-
describe('
|
|
75
|
+
describe('getSolverDetailedStatusRefetchInterval', () => {
|
|
63
76
|
// Both terminal states of the `SubmitSwapTxStatusV2` wire contract. `'failed'` is unreachable via
|
|
64
77
|
// `getDetailedStatus` today (the SDK routes abandoned records to the solver) — asserted here as
|
|
65
78
|
// the hook's own contract, so a terminal record is never polled forever if that routing changes.
|
|
66
79
|
it.each(['solved', 'failed'] as const)('stops on a backend record at the terminal status %s', status => {
|
|
67
|
-
expect(
|
|
80
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(backend(status)), 0)).toBe(false);
|
|
68
81
|
});
|
|
69
82
|
|
|
70
83
|
it.each([
|
|
@@ -74,44 +87,73 @@ describe('getDetailedStatusRefetchInterval', () => {
|
|
|
74
87
|
'posting_execution',
|
|
75
88
|
'posted_execution',
|
|
76
89
|
] as const)('keeps polling a backend record at %s', status => {
|
|
77
|
-
expect(
|
|
90
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(backend(status)), 0)).toBe(STATUS_POLL_MS);
|
|
78
91
|
});
|
|
79
92
|
|
|
80
93
|
// A backend record is never stopped by the solver's budget — different vocabulary.
|
|
81
94
|
it('keeps polling an in-flight backend record even at the cutoff', () => {
|
|
82
|
-
expect(
|
|
95
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(backend('relaying')), MAX_NOT_FOUND_POLLS)).toBe(STATUS_POLL_MS);
|
|
83
96
|
});
|
|
84
97
|
|
|
85
98
|
it.each([SolverIntentStatusCode.SOLVED, SolverIntentStatusCode.FAILED])('stops on solver code %s', status => {
|
|
86
|
-
expect(
|
|
99
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(solver(status)), 0)).toBe(false);
|
|
87
100
|
});
|
|
88
101
|
|
|
89
102
|
it.each([
|
|
90
103
|
SolverIntentStatusCode.NOT_STARTED_YET,
|
|
91
104
|
SolverIntentStatusCode.STARTED_NOT_FINISHED,
|
|
92
105
|
])('never stops in-flight solver code %s, even at a high count', status => {
|
|
93
|
-
expect(
|
|
106
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(solver(status)), 100)).toBe(STATUS_POLL_MS);
|
|
94
107
|
});
|
|
95
108
|
|
|
96
109
|
it('inherits the useStatus NOT_FOUND cutoff: polls below it, stops at it', () => {
|
|
97
110
|
const notFound = ok(solver(SolverIntentStatusCode.NOT_FOUND));
|
|
98
|
-
expect(
|
|
99
|
-
expect(
|
|
111
|
+
expect(getSolverDetailedStatusRefetchInterval(notFound, MAX_NOT_FOUND_POLLS - 1)).toBe(STATUS_POLL_MS);
|
|
112
|
+
expect(getSolverDetailedStatusRefetchInterval(notFound, MAX_NOT_FOUND_POLLS)).toBe(false);
|
|
100
113
|
});
|
|
101
114
|
|
|
102
115
|
it('spends the budget on an undelivered relay packet, rather than polling it forever', () => {
|
|
103
|
-
expect(
|
|
104
|
-
expect(
|
|
116
|
+
expect(getSolverDetailedStatusRefetchInterval(notDelivered, MAX_NOT_FOUND_POLLS - 1)).toBe(STATUS_POLL_MS);
|
|
117
|
+
expect(getSolverDetailedStatusRefetchInterval(notDelivered, MAX_NOT_FOUND_POLLS)).toBe(false);
|
|
105
118
|
});
|
|
106
119
|
|
|
107
120
|
// An outage is recoverable and polling is how it recovers — budgeting it would strand the read.
|
|
108
121
|
it('never stops on a dependency outage, however long it lasts', () => {
|
|
109
|
-
expect(
|
|
110
|
-
expect(
|
|
122
|
+
expect(getSolverDetailedStatusRefetchInterval(outage, MAX_NOT_FOUND_POLLS)).toBe(STATUS_POLL_MS);
|
|
123
|
+
expect(getSolverDetailedStatusRefetchInterval(outage, 10_000)).toBe(STATUS_POLL_MS);
|
|
111
124
|
});
|
|
112
125
|
|
|
113
126
|
it('keeps polling before the first read lands', () => {
|
|
114
|
-
expect(
|
|
127
|
+
expect(getSolverDetailedStatusRefetchInterval(undefined, 0)).toBe(STATUS_POLL_MS);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Only a corrected key changes the answer, so there is nothing to wait for and no budget to spend.
|
|
131
|
+
it.each([401, 403])('stops on a rejected API key at the first read (%i)', status => {
|
|
132
|
+
const rejected = {
|
|
133
|
+
ok: false as const,
|
|
134
|
+
error: new SodaxError('LOOKUP_FAILED', 'rejected', { feature: 'swap', context: { status } }),
|
|
135
|
+
};
|
|
136
|
+
expect(getSolverDetailedStatusRefetchInterval(rejected, 0)).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// The regression this guards: a rejected key leaves the backend unanswered, so a relay miss behind
|
|
140
|
+
// it is unprovable and never tagged `DETAILED_STATUS_NOT_DELIVERED`. Without its own stop the read
|
|
141
|
+
// would poll both endpoints forever, because the budget it would need to exhaust never advances.
|
|
142
|
+
it('stops a rejected key even though the not-delivered budget never advances', () => {
|
|
143
|
+
expect(advance(INITIAL_NOT_FOUND_STREAK, Array(MAX_NOT_FOUND_POLLS).fill(rejectedKey)).consecutiveNotFound).toBe(0);
|
|
144
|
+
expect(getSolverDetailedStatusRefetchInterval(rejectedKey, 0)).toBe(false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// 503 is the transient key-verification failure, which is retried rather than surfaced.
|
|
148
|
+
it('keeps polling a 503 from the key-verification path', () => {
|
|
149
|
+
const unavailable = {
|
|
150
|
+
ok: false as const,
|
|
151
|
+
error: new SodaxError('LOOKUP_FAILED', 'verification unavailable', {
|
|
152
|
+
feature: 'swap',
|
|
153
|
+
context: { status: 503 },
|
|
154
|
+
}),
|
|
155
|
+
};
|
|
156
|
+
expect(getSolverDetailedStatusRefetchInterval(unavailable, MAX_NOT_FOUND_POLLS)).toBe(STATUS_POLL_MS);
|
|
115
157
|
});
|
|
116
158
|
});
|
|
117
159
|
|
|
@@ -138,7 +180,7 @@ describe('detailed-status budget', () => {
|
|
|
138
180
|
const state = advance({ ...INITIAL_NOT_FOUND_STREAK }, Array(MAX_NOT_FOUND_POLLS).fill(notFound));
|
|
139
181
|
|
|
140
182
|
expect(state.consecutiveNotFound).toBe(MAX_NOT_FOUND_POLLS);
|
|
141
|
-
expect(
|
|
183
|
+
expect(getSolverDetailedStatusRefetchInterval(notFound, state.consecutiveNotFound)).toBe(false);
|
|
142
184
|
});
|
|
143
185
|
|
|
144
186
|
// The regression this guards: a swap whose relay packet never lands used to poll forever, because
|
|
@@ -147,7 +189,7 @@ describe('detailed-status budget', () => {
|
|
|
147
189
|
const state = advance({ ...INITIAL_NOT_FOUND_STREAK }, Array(MAX_NOT_FOUND_POLLS).fill(notDelivered));
|
|
148
190
|
|
|
149
191
|
expect(state.consecutiveNotFound).toBe(MAX_NOT_FOUND_POLLS);
|
|
150
|
-
expect(
|
|
192
|
+
expect(getSolverDetailedStatusRefetchInterval(notDelivered, state.consecutiveNotFound)).toBe(false);
|
|
151
193
|
});
|
|
152
194
|
|
|
153
195
|
// The regression this guards: a 2-minute solver or relay outage used to exhaust the budget and
|
|
@@ -156,7 +198,7 @@ describe('detailed-status budget', () => {
|
|
|
156
198
|
const state = advance({ ...INITIAL_NOT_FOUND_STREAK }, Array(MAX_NOT_FOUND_POLLS * 2).fill(outage));
|
|
157
199
|
|
|
158
200
|
expect(state.consecutiveNotFound).toBe(0);
|
|
159
|
-
expect(
|
|
201
|
+
expect(getSolverDetailedStatusRefetchInterval(outage, state.consecutiveNotFound)).toBe(STATUS_POLL_MS);
|
|
160
202
|
});
|
|
161
203
|
|
|
162
204
|
it('lets a transient failure be forgiven once a real status arrives', () => {
|
|
@@ -179,7 +221,9 @@ describe('detailed-status budget', () => {
|
|
|
179
221
|
);
|
|
180
222
|
|
|
181
223
|
expect(state.consecutiveNotFound).toBe(0);
|
|
182
|
-
expect(
|
|
224
|
+
expect(getSolverDetailedStatusRefetchInterval(ok(backend('relaying')), state.consecutiveNotFound)).toBe(
|
|
225
|
+
STATUS_POLL_MS,
|
|
226
|
+
);
|
|
183
227
|
});
|
|
184
228
|
|
|
185
229
|
it('starts a fresh budget when the composite pollKey changes', () => {
|
|
@@ -3,12 +3,12 @@ import { useRef } from 'react';
|
|
|
3
3
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
4
4
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
5
5
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
|
+
import { advanceNotFoundStreak, INITIAL_NOT_FOUND_STREAK } from '../shared/notFoundStreak.js';
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
INITIAL_NOT_FOUND_STREAK,
|
|
8
|
+
getSolverDetailedStatusRefetchInterval,
|
|
9
|
+
isSolverNotFound,
|
|
10
10
|
toNotFoundBudgetRead,
|
|
11
|
-
} from '
|
|
11
|
+
} from '../shared/solverStatusPolicy.js';
|
|
12
12
|
|
|
13
13
|
export type UseDetailedStatusResult = Result<DetailedSwapStatus, DetailedStatusError> | undefined;
|
|
14
14
|
|
|
@@ -25,9 +25,9 @@ export type UseDetailedStatusParams = ReadHookParams<
|
|
|
25
25
|
* `data.value` is discriminated on `source`, and each variant carries that source's payload
|
|
26
26
|
* unmodified.
|
|
27
27
|
*
|
|
28
|
-
* Polls every 3s. Stops once the answering source is terminal,
|
|
29
|
-
* consecutive *ambiguous* reads: a solver `NOT_FOUND`, or a
|
|
30
|
-
* packet for the tx. Neither can distinguish "still in flight" from "never will be", so the budget
|
|
28
|
+
* Polls every 3s. Stops once the answering source is terminal, once the backend rejects the API
|
|
29
|
+
* key, and — like `useStatus` — after 40 consecutive *ambiguous* reads: a solver `NOT_FOUND`, or a
|
|
30
|
+
* `LOOKUP_FAILED` whose relay has no packet for the tx. Neither can distinguish "still in flight" from "never will be", so the budget
|
|
31
31
|
* is what stops a swap nothing can resolve. A dependency outage — relay unreachable, solver down —
|
|
32
32
|
* keeps polling instead, so the read recovers by itself; any real status resets the budget.
|
|
33
33
|
* Override with `queryOptions.refetchInterval`.
|
|
@@ -60,10 +60,10 @@ export const useDetailedStatus = ({
|
|
|
60
60
|
notFoundStreakRef.current = advanceNotFoundStreak(
|
|
61
61
|
notFoundStreakRef.current,
|
|
62
62
|
pollKey,
|
|
63
|
-
toNotFoundBudgetRead(query.state.data),
|
|
63
|
+
isSolverNotFound(toNotFoundBudgetRead(query.state.data)),
|
|
64
64
|
query.state.dataUpdateCount,
|
|
65
65
|
);
|
|
66
|
-
return
|
|
66
|
+
return getSolverDetailedStatusRefetchInterval(query.state.data, notFoundStreakRef.current.consecutiveNotFound);
|
|
67
67
|
},
|
|
68
68
|
...queryOptions,
|
|
69
69
|
});
|