@sodax/dapp-kit 2.2.0-rc.6 → 2.2.0-rc.8

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.
Files changed (34) hide show
  1. package/README.md +24 -3
  2. package/dist/index.d.ts +107 -13
  3. package/dist/index.mjs +118 -38
  4. package/package.json +4 -4
  5. package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.test.ts +144 -0
  6. package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.ts +52 -0
  7. package/src/hooks/bridge/index.ts +2 -0
  8. package/src/hooks/bridge/useBridgeDetailedStatus.test.ts +93 -0
  9. package/src/hooks/bridge/useBridgeDetailedStatus.ts +79 -0
  10. package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.test.ts +81 -0
  11. package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.ts +25 -0
  12. package/src/hooks/bridgeApi/useBridgeApiAllowance.ts +2 -1
  13. package/src/hooks/bridgeApi/useBridgeApiApprove.ts +2 -1
  14. package/src/hooks/bridgeApi/useBridgeApiBridgeableAmount.ts +2 -1
  15. package/src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts +2 -1
  16. package/src/hooks/bridgeApi/useBridgeApiFee.ts +2 -1
  17. package/src/hooks/bridgeApi/useBridgeApiIsBridgeable.ts +2 -1
  18. package/src/hooks/bridgeApi/useBridgeApiSubmitTx.ts +5 -3
  19. package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.test.ts +63 -0
  20. package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.ts +7 -10
  21. package/src/hooks/bridgeApi/useBridgeApiTokens.ts +2 -1
  22. package/src/hooks/bridgeApi/useBridgeApiTokensByChain.ts +2 -1
  23. package/src/hooks/leverageYield/index.ts +1 -0
  24. package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.test.ts +131 -0
  25. package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.ts +83 -0
  26. package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +13 -3
  27. package/src/hooks/shared/notFoundStreak.ts +59 -0
  28. package/src/hooks/shared/retryUnlessAuthFailure.ts +1 -1
  29. package/src/hooks/{swap/getSwapStatusRefetchInterval.test.ts → shared/solverStatusPolicy.test.ts} +25 -25
  30. package/src/hooks/shared/solverStatusPolicy.ts +117 -0
  31. package/src/hooks/swap/useDetailedStatus.test.ts +65 -21
  32. package/src/hooks/swap/useDetailedStatus.ts +9 -9
  33. package/src/hooks/swap/useStatus.ts +4 -7
  34. package/src/hooks/swap/getSwapStatusRefetchInterval.ts +0 -126
@@ -4,11 +4,8 @@ import { useRef } from 'react';
4
4
  import { useQuery, type UseQueryResult } from '@tanstack/react-query';
5
5
  import { useSodaxContext } from '../shared/useSodaxContext.js';
6
6
  import type { ReadHookParams } from '../shared/types.js';
7
- import {
8
- advanceNotFoundStreak,
9
- getSwapStatusRefetchInterval,
10
- INITIAL_NOT_FOUND_STREAK,
11
- } from './getSwapStatusRefetchInterval.js';
7
+ import { advanceNotFoundStreak, INITIAL_NOT_FOUND_STREAK } from '../shared/notFoundStreak.js';
8
+ import { getSolverStatusRefetchInterval, isSolverNotFound } from '../shared/solverStatusPolicy.js';
12
9
 
13
10
  export type UseStatusParams = ReadHookParams<
14
11
  Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined,
@@ -46,10 +43,10 @@ export const useStatus = ({
46
43
  notFoundStreakRef.current = advanceNotFoundStreak(
47
44
  notFoundStreakRef.current,
48
45
  intentTxHash,
49
- query.state.data,
46
+ isSolverNotFound(query.state.data),
50
47
  query.state.dataUpdateCount,
51
48
  );
52
- return getSwapStatusRefetchInterval(query.state.data, notFoundStreakRef.current.consecutiveNotFound);
49
+ return getSolverStatusRefetchInterval(query.state.data, notFoundStreakRef.current.consecutiveNotFound);
53
50
  },
54
51
  ...queryOptions,
55
52
  });
@@ -1,126 +0,0 @@
1
- import {
2
- DETAILED_STATUS_NOT_DELIVERED,
3
- SolverIntentStatusCode,
4
- type DetailedStatusError,
5
- type DetailedSwapStatus,
6
- type Result,
7
- type SolverErrorResponse,
8
- type SolverIntentStatusResponse,
9
- } from '@sodax/sdk';
10
-
11
- export const STATUS_POLL_MS = 3000;
12
- /** Cap consecutive NOT_FOUND polls (~2 min at 3s). First NOT_FOUND is a race, not a stop. */
13
- export const MAX_NOT_FOUND_POLLS = 40;
14
-
15
- export type SwapStatusResult = Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined;
16
-
17
- export type NotFoundStreakState = {
18
- /** Identity of what is being polled — an intent tx hash, or a composite source-chain/tx key. */
19
- pollKey: string | undefined;
20
- seenUpdates: number;
21
- consecutiveNotFound: number;
22
- };
23
-
24
- export const INITIAL_NOT_FOUND_STREAK: NotFoundStreakState = {
25
- pollKey: undefined,
26
- seenUpdates: 0,
27
- consecutiveNotFound: 0,
28
- };
29
-
30
- /**
31
- * Polling interval for `useStatus`. Stops on SOLVED/FAILED immediately; stops on NOT_FOUND only
32
- * after `MAX_NOT_FOUND_POLLS` consecutive successful fetches (solver forgot / never saw the intent).
33
- * In-flight statuses keep polling with no global cap.
34
- *
35
- * Kept in its own pure module (no React/context imports) so it is unit-testable in dapp-kit's
36
- * `node` test environment — importing the hook itself pulls in `useSodaxContext`.
37
- */
38
- export function getSwapStatusRefetchInterval(data: SwapStatusResult, consecutiveNotFound: number): number | false {
39
- const status = data?.ok ? data.value.status : undefined;
40
- if (status === SolverIntentStatusCode.SOLVED || status === SolverIntentStatusCode.FAILED) {
41
- return false;
42
- }
43
- if (status === SolverIntentStatusCode.NOT_FOUND && consecutiveNotFound >= MAX_NOT_FOUND_POLLS) {
44
- return false;
45
- }
46
- return STATUS_POLL_MS;
47
- }
48
-
49
- export function nextNotFoundStreak(data: SwapStatusResult, previousStreak: number): number {
50
- const status = data?.ok ? data.value.status : undefined;
51
- return status === SolverIntentStatusCode.NOT_FOUND ? previousStreak + 1 : 0;
52
- }
53
-
54
- /**
55
- * Advances the consecutive-NOT_FOUND counter once per successful query update. A `pollKey` change
56
- * starts a new streak so a prior intent's count cannot stop the next one. React Query may call
57
- * `refetchInterval` more than once per fetch — same `dataUpdateCount` is a no-op.
58
- */
59
- export function advanceNotFoundStreak(
60
- state: NotFoundStreakState,
61
- pollKey: string | undefined,
62
- data: SwapStatusResult,
63
- dataUpdateCount: number,
64
- ): NotFoundStreakState {
65
- if (state.pollKey !== pollKey) {
66
- state = { pollKey, seenUpdates: 0, consecutiveNotFound: 0 };
67
- }
68
- if (state.seenUpdates === dataUpdateCount) {
69
- return state;
70
- }
71
- return {
72
- pollKey,
73
- seenUpdates: dataUpdateCount,
74
- consecutiveNotFound: nextNotFoundStreak(data, state.consecutiveNotFound),
75
- };
76
- }
77
-
78
- /**
79
- * Structural mirror of `useDetailedStatus`'s data type. Declared locally rather than imported so
80
- * this module does not own another hook's public contract — `UseDetailedStatusResult` lives with
81
- * the hook.
82
- */
83
- type DetailedStatusRead = Result<DetailedSwapStatus, DetailedStatusError> | undefined;
84
-
85
- /**
86
- * Maps a detailed-status read onto the shape the shared NOT_FOUND policy consumes, three ways:
87
- *
88
- * - solver arm → passes through, so its status drives the budget as it does for `useStatus`;
89
- * - backend arm → `undefined`, which resets the budget; the solver is not the one being asked;
90
- * - failed read → counts as `NOT_FOUND` **only** when the relay has no packet for this source tx
91
- * (`DETAILED_STATUS_NOT_DELIVERED`). That miss is ambiguous — a swap still in flight and one whose
92
- * tx never relayed look the same — so a budget is the only way to stop the second case.
93
- *
94
- * Every other `LOOKUP_FAILED` is a dependency failing right now: the relay unreachable, a malformed
95
- * response, the solver down. Those reset the budget and keep polling, because that is how the read
96
- * recovers when the outage ends. Counting them would stop polling a healthy swap after ~2 min of
97
- * downtime and never resume.
98
- */
99
- export function toNotFoundBudgetRead(data: DetailedStatusRead): SwapStatusResult {
100
- if (data === undefined) return undefined;
101
- if (data.ok) {
102
- return data.value.source === 'solver' ? { ok: true, value: data.value.data } : undefined;
103
- }
104
- return data.error?.context?.reason === DETAILED_STATUS_NOT_DELIVERED
105
- ? { ok: true, value: { status: SolverIntentStatusCode.NOT_FOUND } }
106
- : undefined;
107
- }
108
-
109
- /**
110
- * Polling interval for `useDetailedStatus`. Backend records report terminality in their own
111
- * vocabulary; everything else reuses `useStatus`'s policy verbatim, so `MAX_NOT_FOUND_POLLS` stays
112
- * the single cutoff — for a forgotten intent and for a swap no source can resolve.
113
- */
114
- export function getDetailedStatusRefetchInterval(
115
- data: DetailedStatusRead,
116
- consecutiveNotFound: number,
117
- ): number | false {
118
- if (data?.ok && data.value.source === 'backend') {
119
- // Both terminal states of `SubmitSwapTxStatusV2`. Today the SDK routes `'failed'` records to the
120
- // solver so only `'solved'` reaches us, but that is its routing rule, not this hook's contract —
121
- // encoding the wire contract keeps a terminal record from being polled forever if it changes.
122
- const { status } = data.value.data;
123
- return status === 'solved' || status === 'failed' ? false : STATUS_POLL_MS;
124
- }
125
- return getSwapStatusRefetchInterval(toNotFoundBudgetRead(data), consecutiveNotFound);
126
- }