@sodax/dapp-kit 2.2.0-rc.5 → 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 +77 -7
- package/dist/index.d.ts +382 -14
- package/dist/index.mjs +336 -38
- package/package.json +4 -4
- package/src/hooks/_mutationContract.test.ts +4 -0
- 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 +13 -0
- package/src/hooks/leverageYield/useApproveLeveragePositionFunding.ts +48 -0
- package/src/hooks/leverageYield/useLeveragePositionAccount.ts +45 -0
- package/src/hooks/leverageYield/useLeveragePositionCollateral.ts +47 -0
- package/src/hooks/leverageYield/useLeveragePositionFundingAllowance.ts +51 -0
- package/src/hooks/leverageYield/useLeveragePositionInfo.ts +39 -0
- package/src/hooks/leverageYield/useLeveragePositionPayoutAddress.ts +36 -0
- package/src/hooks/leverageYield/useLeveragePositionPending.ts +48 -0
- package/src/hooks/leverageYield/useLeveragePositions.ts +41 -0
- package/src/hooks/leverageYield/useLeveragePositionsForUser.ts +45 -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/leverageYield/useOpenLeveragePosition.ts +59 -0
- package/src/hooks/leverageYield/useRunLeveragePositionOperation.ts +63 -0
- package/src/hooks/leverageYield/useSubmitLeveragePositionIntent.ts +55 -0
- package/src/hooks/leverageYieldApi/index.ts +2 -2
- package/src/hooks/mm/index.ts +1 -0
- package/src/hooks/mm/useEModes.ts +31 -0
- 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/swapsApi/index.ts +2 -2
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +2 -2
- package/src/hooks/swap/getSwapStatusRefetchInterval.ts +0 -126
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DETAILED_STATUS_NOT_DELIVERED,
|
|
3
|
+
SodaxError,
|
|
4
|
+
type BridgeDetailedStatusError,
|
|
5
|
+
type DetailedBridgeStatus,
|
|
6
|
+
type PacketData,
|
|
7
|
+
type Result,
|
|
8
|
+
} from '@sodax/sdk';
|
|
9
|
+
import { describe, expect, it } from 'vitest';
|
|
10
|
+
import {
|
|
11
|
+
advanceNotFoundStreak,
|
|
12
|
+
INITIAL_NOT_FOUND_STREAK,
|
|
13
|
+
MAX_NOT_FOUND_POLLS,
|
|
14
|
+
STATUS_POLL_MS,
|
|
15
|
+
} from '../shared/notFoundStreak.js';
|
|
16
|
+
import {
|
|
17
|
+
getBridgeDetailedStatusRefetchInterval,
|
|
18
|
+
isBridgeNotDelivered,
|
|
19
|
+
} from './getBridgeDetailedStatusRefetchInterval.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Guards the polling-stop invariant for `useBridgeDetailedStatus.refetchInterval`: stop on a
|
|
23
|
+
* terminal or abandoned backend record, on any relay packet (always terminal), on a rejected API
|
|
24
|
+
* key, and on an exhausted not-delivered budget — but keep polling through an outage. Tested as a
|
|
25
|
+
* pure function because dapp-kit's vitest runs in the `node` environment.
|
|
26
|
+
*/
|
|
27
|
+
const KEY_A = 'arb:0xaaa';
|
|
28
|
+
const KEY_B = 'arb:0xbbb';
|
|
29
|
+
|
|
30
|
+
type Read = Result<DetailedBridgeStatus, BridgeDetailedStatusError> | undefined;
|
|
31
|
+
|
|
32
|
+
const backend = (status: string, abandonedAt?: string): Read => ({
|
|
33
|
+
ok: true,
|
|
34
|
+
value: {
|
|
35
|
+
source: 'backend',
|
|
36
|
+
data: { txHash: '0xaaa', srcChainKey: '0xa4b1.arbitrum', status, processingAttempts: 1, abandonedAt },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const packet: PacketData = {
|
|
41
|
+
src_chain_id: 23,
|
|
42
|
+
src_tx_hash: '0xaaa',
|
|
43
|
+
src_address: '0xsrc',
|
|
44
|
+
status: 'executed',
|
|
45
|
+
dst_chain_id: 146,
|
|
46
|
+
conn_sn: 1,
|
|
47
|
+
dst_address: '0xdst',
|
|
48
|
+
dst_tx_hash: '0xdst',
|
|
49
|
+
signatures: [],
|
|
50
|
+
payload: '0x',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const relayed: Read = { ok: true, value: { source: 'relay', data: packet } };
|
|
54
|
+
|
|
55
|
+
const lookupFailed = (context: Record<string, unknown>): Read => ({
|
|
56
|
+
ok: false,
|
|
57
|
+
error: new SodaxError('LOOKUP_FAILED', 'no source could answer', {
|
|
58
|
+
feature: 'bridge',
|
|
59
|
+
context: { phase: 'lookup', method: 'getDetailedStatus', ...context },
|
|
60
|
+
}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const notDelivered = lookupFailed({ reason: DETAILED_STATUS_NOT_DELIVERED });
|
|
64
|
+
const outage = lookupFailed({});
|
|
65
|
+
const rejectedKey = lookupFailed({ status: 401 });
|
|
66
|
+
|
|
67
|
+
describe('isBridgeNotDelivered', () => {
|
|
68
|
+
it('counts only the ambiguous relay miss', () => {
|
|
69
|
+
expect(isBridgeNotDelivered(notDelivered)).toBe(true);
|
|
70
|
+
expect(isBridgeNotDelivered(outage)).toBe(false);
|
|
71
|
+
expect(isBridgeNotDelivered(rejectedKey)).toBe(false);
|
|
72
|
+
expect(isBridgeNotDelivered(relayed)).toBe(false);
|
|
73
|
+
expect(isBridgeNotDelivered(backend('relaying'))).toBe(false);
|
|
74
|
+
expect(isBridgeNotDelivered(undefined)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('getBridgeDetailedStatusRefetchInterval', () => {
|
|
79
|
+
it('keeps polling an in-flight backend record', () => {
|
|
80
|
+
for (const status of ['pending', 'relaying', 'relayed']) {
|
|
81
|
+
expect(getBridgeDetailedStatusRefetchInterval(backend(status), 0)).toBe(STATUS_POLL_MS);
|
|
82
|
+
}
|
|
83
|
+
expect(getBridgeDetailedStatusRefetchInterval(undefined, 0)).toBe(STATUS_POLL_MS);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('stops on the two terminal backend statuses and on an abandoned record', () => {
|
|
87
|
+
expect(getBridgeDetailedStatusRefetchInterval(backend('executed'), 0)).toBe(false);
|
|
88
|
+
expect(getBridgeDetailedStatusRefetchInterval(backend('failed'), 0)).toBe(false);
|
|
89
|
+
expect(getBridgeDetailedStatusRefetchInterval(backend('relayed', '2026-09-18T00:00:00.000Z'), 0)).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('stops on a relay packet without waiting for a budget — that arm is terminal on arrival', () => {
|
|
93
|
+
expect(getBridgeDetailedStatusRefetchInterval(relayed, 0)).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('stops on a rejected API key, at the first read', () => {
|
|
97
|
+
expect(getBridgeDetailedStatusRefetchInterval(rejectedKey, 0)).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('stops a not-delivered read only once the budget is spent', () => {
|
|
101
|
+
expect(getBridgeDetailedStatusRefetchInterval(notDelivered, MAX_NOT_FOUND_POLLS - 1)).toBe(STATUS_POLL_MS);
|
|
102
|
+
expect(getBridgeDetailedStatusRefetchInterval(notDelivered, MAX_NOT_FOUND_POLLS)).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('keeps polling an outage forever — that is how the read recovers', () => {
|
|
106
|
+
expect(getBridgeDetailedStatusRefetchInterval(outage, MAX_NOT_FOUND_POLLS)).toBe(STATUS_POLL_MS);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('the not-delivered budget, driven as the hook drives it', () => {
|
|
111
|
+
/** One query update per read, the way `refetchInterval` advances it. */
|
|
112
|
+
const advance = (reads: Read[], key = KEY_A, from = 1) =>
|
|
113
|
+
reads.reduce(
|
|
114
|
+
(acc, read, i) => advanceNotFoundStreak(acc, key, isBridgeNotDelivered(read), from + i),
|
|
115
|
+
INITIAL_NOT_FOUND_STREAK,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
it('a backend-pending stretch never exhausts the budget', () => {
|
|
119
|
+
// The reset is what makes this safe: pending reads are not misses, so they zero the counter.
|
|
120
|
+
const pending = advance(Array.from({ length: MAX_NOT_FOUND_POLLS + 5 }, () => backend('pending')));
|
|
121
|
+
expect(pending.consecutiveNotFound).toBe(0);
|
|
122
|
+
expect(getBridgeDetailedStatusRefetchInterval(backend('pending'), pending.consecutiveNotFound)).toBe(
|
|
123
|
+
STATUS_POLL_MS,
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('an outage in the middle resets a run of misses', () => {
|
|
128
|
+
const state = advance([notDelivered, notDelivered, outage, notDelivered]);
|
|
129
|
+
expect(state.consecutiveNotFound).toBe(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('stops only after MAX_NOT_FOUND_POLLS consecutive misses', () => {
|
|
133
|
+
const state = advance(Array.from({ length: MAX_NOT_FOUND_POLLS }, () => notDelivered));
|
|
134
|
+
expect(state.consecutiveNotFound).toBe(MAX_NOT_FOUND_POLLS);
|
|
135
|
+
expect(getBridgeDetailedStatusRefetchInterval(notDelivered, state.consecutiveNotFound)).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('starts a fresh budget for a new source tx', () => {
|
|
139
|
+
const spent = advance(Array.from({ length: MAX_NOT_FOUND_POLLS }, () => notDelivered));
|
|
140
|
+
const next = advanceNotFoundStreak(spent, KEY_B, true, 1);
|
|
141
|
+
expect(next.consecutiveNotFound).toBe(1);
|
|
142
|
+
expect(next.pollKey).toBe(KEY_B);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DETAILED_STATUS_NOT_DELIVERED,
|
|
3
|
+
isAuthFailure,
|
|
4
|
+
type BridgeDetailedStatusError,
|
|
5
|
+
type DetailedBridgeStatus,
|
|
6
|
+
type Result,
|
|
7
|
+
} from '@sodax/sdk';
|
|
8
|
+
import { MAX_NOT_FOUND_POLLS, STATUS_POLL_MS } from '../shared/notFoundStreak.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Structural mirror of `useBridgeDetailedStatus`'s data type. Declared locally rather than imported
|
|
12
|
+
* so this module does not own the hook's public contract.
|
|
13
|
+
*/
|
|
14
|
+
type DetailedBridgeStatusRead = Result<DetailedBridgeStatus, BridgeDetailedStatusError> | undefined;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The read the budget counts: the relay has no packet for this source tx *and* the backend answered,
|
|
18
|
+
* so the miss is real rather than unprovable. Every other `LOOKUP_FAILED` is a dependency failing
|
|
19
|
+
* right now — the relay unreachable, a malformed response — and those keep polling, because that is
|
|
20
|
+
* how the read recovers when the outage ends.
|
|
21
|
+
*/
|
|
22
|
+
export function isBridgeNotDelivered(data: DetailedBridgeStatusRead): boolean {
|
|
23
|
+
return data !== undefined && !data.ok && data.error?.context?.reason === DETAILED_STATUS_NOT_DELIVERED;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Polling interval for `useBridgeDetailedStatus`.
|
|
28
|
+
*
|
|
29
|
+
* Unlike the swap analog there is no in-flight second arm to wait on: the relay arm only ever
|
|
30
|
+
* carries a matched `executed` packet, so it is terminal on arrival. That leaves three stops — a
|
|
31
|
+
* terminal or abandoned backend record, a rejected API key, and the ambiguous relay miss once it has
|
|
32
|
+
* run `MAX_NOT_FOUND_POLLS` times in a row.
|
|
33
|
+
*
|
|
34
|
+
* Kept in its own pure module (no React/context imports) so it is unit-testable in dapp-kit's `node`
|
|
35
|
+
* test environment — importing the hook itself pulls in `useSodaxContext`.
|
|
36
|
+
*/
|
|
37
|
+
export function getBridgeDetailedStatusRefetchInterval(
|
|
38
|
+
data: DetailedBridgeStatusRead,
|
|
39
|
+
consecutiveNotFound: number,
|
|
40
|
+
): number | false {
|
|
41
|
+
if (data?.ok) {
|
|
42
|
+
if (data.value.source === 'relay') return false;
|
|
43
|
+
// The two terminal states of the bridge wire contract, plus a record the backend abandoned
|
|
44
|
+
// while its status was still non-terminal.
|
|
45
|
+
const { status, abandonedAt } = data.value.data;
|
|
46
|
+
return status === 'executed' || status === 'failed' || abandonedAt ? false : STATUS_POLL_MS;
|
|
47
|
+
}
|
|
48
|
+
// A rejected key is terminal — only a corrected key changes the answer, so stop asking.
|
|
49
|
+
if (data && !data.ok && isAuthFailure(data.error)) return false;
|
|
50
|
+
if (isBridgeNotDelivered(data) && consecutiveNotFound >= MAX_NOT_FOUND_POLLS) return false;
|
|
51
|
+
return STATUS_POLL_MS;
|
|
52
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export * from './getBridgeDetailedStatusRefetchInterval.js'; // poll policy (drives the detailed-status hook)
|
|
1
2
|
export * from './useBridge.js';
|
|
3
|
+
export * from './useBridgeDetailedStatus.js';
|
|
2
4
|
export * from './useBridgeAllowance.js';
|
|
3
5
|
export * from './useBridgeApprove.js';
|
|
4
6
|
export * from './useGetBridgeableAmount.js';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { MAX_NOT_FOUND_POLLS, STATUS_POLL_MS } from '../shared/notFoundStreak.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pins the WIRING the pure-policy test cannot see: the hook must pass the read into the policy and
|
|
6
|
+
* advance the budget once per query update. Feeding the policy the wrong argument reproduces a
|
|
7
|
+
* poll that never stops, with the policy's own tests still green.
|
|
8
|
+
*
|
|
9
|
+
* No renderer, per the package convention (see `_apiKeyWire.test.ts`): the React Query wrapper is
|
|
10
|
+
* mocked so the captured options bag can be driven directly.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// The captured options bag is opaque, like the real wrapper's.
|
|
14
|
+
let captured: any;
|
|
15
|
+
|
|
16
|
+
const refSlots: Array<{ current: unknown }> = [];
|
|
17
|
+
let refCursor = 0;
|
|
18
|
+
|
|
19
|
+
// Only useRef is needed, and call order is stable, so this replays React's slot semantics well
|
|
20
|
+
// enough to observe the budget across refetchInterval calls — the same shape
|
|
21
|
+
// `shared/useRequestTrustline.test.ts` uses.
|
|
22
|
+
vi.mock('react', () => ({
|
|
23
|
+
useRef: (initial: unknown) => {
|
|
24
|
+
const slot = refSlots[refCursor] ?? { current: initial };
|
|
25
|
+
refSlots[refCursor] = slot;
|
|
26
|
+
refCursor += 1;
|
|
27
|
+
return slot;
|
|
28
|
+
},
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
const getDetailedStatus = vi.fn();
|
|
32
|
+
vi.mock('../shared/useSodaxContext.js', () => ({
|
|
33
|
+
useSodaxContext: () => ({ sodax: { bridge: { getDetailedStatus } } }),
|
|
34
|
+
}));
|
|
35
|
+
vi.mock('@tanstack/react-query', () => ({
|
|
36
|
+
useQuery: (options: any) => {
|
|
37
|
+
captured = options;
|
|
38
|
+
return {};
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const { useBridgeDetailedStatus } = await import('./useBridgeDetailedStatus.js');
|
|
43
|
+
|
|
44
|
+
const SRC_CHAIN = '0xa4b1.arbitrum';
|
|
45
|
+
const SRC_TX = '0xaaa';
|
|
46
|
+
const render = (apiConfig?: { apiKey: string }) => {
|
|
47
|
+
refCursor = 0;
|
|
48
|
+
return useBridgeDetailedStatus({ params: { srcChainKey: SRC_CHAIN, srcTxHash: SRC_TX, apiConfig } });
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const notDeliveredRead = {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: { code: 'LOOKUP_FAILED', context: { reason: 'relay_not_delivered' } },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
describe('useBridgeDetailedStatus wiring', () => {
|
|
57
|
+
it('keys the query on the source tx and runs only when both identifiers are present', () => {
|
|
58
|
+
render();
|
|
59
|
+
expect(captured.queryKey).toEqual(['bridge', 'detailedStatus', SRC_CHAIN, SRC_TX]);
|
|
60
|
+
expect(captured.enabled).toBe(true);
|
|
61
|
+
|
|
62
|
+
refCursor = 0;
|
|
63
|
+
useBridgeDetailedStatus({ params: { srcChainKey: SRC_CHAIN, srcTxHash: undefined } });
|
|
64
|
+
expect(captured.enabled).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('threads a per-request override into the SDK call', async () => {
|
|
68
|
+
render({ apiKey: 'per-action-key' });
|
|
69
|
+
await captured.queryFn();
|
|
70
|
+
expect(getDetailedStatus).toHaveBeenCalledWith(
|
|
71
|
+
{ srcChainKey: SRC_CHAIN, srcTxHash: SRC_TX },
|
|
72
|
+
{ apiKey: 'per-action-key' },
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('feeds the read into the policy, so a terminal answer stops the poll', () => {
|
|
77
|
+
render();
|
|
78
|
+
const relayed = { ok: true, value: { source: 'relay', data: { dst_tx_hash: '0xdst' } } };
|
|
79
|
+
expect(captured.refetchInterval({ state: { data: relayed, dataUpdateCount: 1 } })).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('advances the not-delivered budget once per update, and stops when it is spent', () => {
|
|
83
|
+
refSlots.length = 0;
|
|
84
|
+
render();
|
|
85
|
+
// Same dataUpdateCount twice must not double-count, so the cap is reached on update 40, not 20.
|
|
86
|
+
for (let update = 1; update <= MAX_NOT_FOUND_POLLS - 1; update++) {
|
|
87
|
+
captured.refetchInterval({ state: { data: notDeliveredRead, dataUpdateCount: update } });
|
|
88
|
+
captured.refetchInterval({ state: { data: notDeliveredRead, dataUpdateCount: update } });
|
|
89
|
+
}
|
|
90
|
+
expect(captured.refetchInterval({ state: { data: notDeliveredRead, dataUpdateCount: 39 } })).toBe(STATUS_POLL_MS);
|
|
91
|
+
expect(captured.refetchInterval({ state: { data: notDeliveredRead, dataUpdateCount: 40 } })).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeDetailedStatusError,
|
|
3
|
+
DetailedBridgeStatus,
|
|
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
|
+
getBridgeDetailedStatusRefetchInterval,
|
|
15
|
+
isBridgeNotDelivered,
|
|
16
|
+
} from './getBridgeDetailedStatusRefetchInterval.js';
|
|
17
|
+
|
|
18
|
+
export type UseBridgeDetailedStatusResult = Result<DetailedBridgeStatus, BridgeDetailedStatusError> | undefined;
|
|
19
|
+
|
|
20
|
+
export type UseBridgeDetailedStatusParams = ReadHookParams<
|
|
21
|
+
UseBridgeDetailedStatusResult,
|
|
22
|
+
{
|
|
23
|
+
srcChainKey: SpokeChainKey | undefined;
|
|
24
|
+
srcTxHash: string | undefined;
|
|
25
|
+
apiConfig?: RequestOverrideConfig;
|
|
26
|
+
}
|
|
27
|
+
>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Hook for monitoring a bridge from its source-chain transaction.
|
|
31
|
+
*
|
|
32
|
+
* `useBridgeApiSubmitTxStatus` needs the backend to still hold a usable record; this takes
|
|
33
|
+
* `(srcChainKey, srcTxHash)` — the pair you hold after `bridge()` — and routes to whichever source
|
|
34
|
+
* can answer, so it also covers a bridge the client-side fallback completed. `data.value` is
|
|
35
|
+
* discriminated on `source`, and each variant carries that source's payload unmodified.
|
|
36
|
+
*
|
|
37
|
+
* Polls every 3s. Stops once the answering source is terminal (a relay packet always is), once the
|
|
38
|
+
* backend rejects the API key, and — like `useDetailedStatus` — after 40 consecutive *ambiguous*
|
|
39
|
+
* reads, meaning a `LOOKUP_FAILED` whose relay has no packet for the tx. A dependency outage keeps
|
|
40
|
+
* polling instead, so the read recovers by itself; any real answer resets the budget. Override with
|
|
41
|
+
* `queryOptions.refetchInterval`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const { data } = useBridgeDetailedStatus({ params: { srcChainKey, srcTxHash } });
|
|
46
|
+
* if (data?.ok && data.value.source === 'relay') console.log(data.value.data.dst_tx_hash);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const useBridgeDetailedStatus = ({
|
|
50
|
+
params,
|
|
51
|
+
queryOptions,
|
|
52
|
+
}: UseBridgeDetailedStatusParams = {}): UseQueryResult<UseBridgeDetailedStatusResult> => {
|
|
53
|
+
const { sodax } = useSodaxContext();
|
|
54
|
+
const srcChainKey = params?.srcChainKey;
|
|
55
|
+
const srcTxHash = params?.srcTxHash;
|
|
56
|
+
const apiConfig = params?.apiConfig;
|
|
57
|
+
// Composite identity: a new bridge must start its own not-delivered budget.
|
|
58
|
+
const pollKey = srcChainKey && srcTxHash ? `${srcChainKey}:${srcTxHash}` : undefined;
|
|
59
|
+
const notFoundStreakRef = useRef({ ...INITIAL_NOT_FOUND_STREAK });
|
|
60
|
+
|
|
61
|
+
return useQuery({
|
|
62
|
+
queryKey: ['bridge', 'detailedStatus', srcChainKey, srcTxHash],
|
|
63
|
+
queryFn: async () => {
|
|
64
|
+
if (!srcChainKey || !srcTxHash) return undefined;
|
|
65
|
+
return sodax.bridge.getDetailedStatus({ srcChainKey, srcTxHash }, apiConfig);
|
|
66
|
+
},
|
|
67
|
+
enabled: !!srcChainKey && !!srcTxHash,
|
|
68
|
+
refetchInterval: query => {
|
|
69
|
+
notFoundStreakRef.current = advanceNotFoundStreak(
|
|
70
|
+
notFoundStreakRef.current,
|
|
71
|
+
pollKey,
|
|
72
|
+
isBridgeNotDelivered(query.state.data),
|
|
73
|
+
query.state.dataUpdateCount,
|
|
74
|
+
);
|
|
75
|
+
return getBridgeDetailedStatusRefetchInterval(query.state.data, notFoundStreakRef.current.consecutiveNotFound);
|
|
76
|
+
},
|
|
77
|
+
...queryOptions,
|
|
78
|
+
});
|
|
79
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { SodaxError, type BridgeSubmitTxStatusDataV2 } from '@sodax/sdk';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
BRIDGE_SUBMIT_TX_STATUS_POLL_MS,
|
|
5
|
+
getBridgeSubmitTxStatusRefetchInterval,
|
|
6
|
+
} from './getBridgeSubmitTxStatusRefetchInterval.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Guards the polling-stop invariant for `useBridgeApiSubmitTxStatus.refetchInterval`: keep polling
|
|
10
|
+
* a live record, stop on a terminal or abandoned one, and stop on a rejected API key. Tested as a
|
|
11
|
+
* pure function because dapp-kit's vitest runs in the `node` environment.
|
|
12
|
+
*/
|
|
13
|
+
const record = (data: Partial<BridgeSubmitTxStatusDataV2>): BridgeSubmitTxStatusDataV2 => ({
|
|
14
|
+
txHash: '0xabc',
|
|
15
|
+
srcChainKey: '0xa4b1.arbitrum',
|
|
16
|
+
status: 'pending',
|
|
17
|
+
processingAttempts: 1,
|
|
18
|
+
...data,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/** A backend failure as `BridgeApiService` surfaces it: the HTTP status lifted onto `context`. */
|
|
22
|
+
const apiError = (status?: number): SodaxError =>
|
|
23
|
+
new SodaxError('EXTERNAL_API_ERROR', `responded with ${status}`, {
|
|
24
|
+
feature: 'backend',
|
|
25
|
+
context: { api: 'bridge', endpoint: '/bridge/submit-tx/status', status },
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('getBridgeSubmitTxStatusRefetchInterval', () => {
|
|
29
|
+
it('polls at the 1s cadence the docs and skills advertise', () => {
|
|
30
|
+
expect(BRIDGE_SUBMIT_TX_STATUS_POLL_MS).toBe(1000);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('keeps polling every in-flight status', () => {
|
|
34
|
+
for (const status of ['pending', 'relaying', 'relayed']) {
|
|
35
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(undefined, record({ status }))).toBe(
|
|
36
|
+
BRIDGE_SUBMIT_TX_STATUS_POLL_MS,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('keeps polling before the first response has landed', () => {
|
|
42
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(undefined, undefined)).toBe(BRIDGE_SUBMIT_TX_STATUS_POLL_MS);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('stops on the two terminal statuses', () => {
|
|
46
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(undefined, record({ status: 'executed' }))).toBe(false);
|
|
47
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(undefined, record({ status: 'failed' }))).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('stops on an abandoned record even while its status is still non-terminal', () => {
|
|
51
|
+
// The backend gave up mid-flight; the record never self-heals, so polling it is dead weight.
|
|
52
|
+
expect(
|
|
53
|
+
getBridgeSubmitTxStatusRefetchInterval(
|
|
54
|
+
undefined,
|
|
55
|
+
record({ status: 'relayed', abandonedAt: '2026-09-17T00:00:00.000Z' }),
|
|
56
|
+
),
|
|
57
|
+
).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('stops on a terminal API-key rejection — the defect `retry` alone cannot fix', () => {
|
|
61
|
+
for (const status of [401, 403]) {
|
|
62
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(apiError(status), undefined)).toBe(false);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("keeps polling the apiguard's transient 503 and other non-auth failures", () => {
|
|
67
|
+
// 503 is verification being down, not a rejected key — the SDK excludes it deliberately.
|
|
68
|
+
for (const status of [408, 429, 500, 502, 503, 504]) {
|
|
69
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(apiError(status), undefined)).toBe(BRIDGE_SUBMIT_TX_STATUS_POLL_MS);
|
|
70
|
+
}
|
|
71
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(new Error('ECONNRESET'), undefined)).toBe(
|
|
72
|
+
BRIDGE_SUBMIT_TX_STATUS_POLL_MS,
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('stops on an auth failure even when a live record is still cached', () => {
|
|
77
|
+
// Order matters: the key was rejected on a later poll, so the stale in-flight record must not
|
|
78
|
+
// keep the interval alive.
|
|
79
|
+
expect(getBridgeSubmitTxStatusRefetchInterval(apiError(401), record({ status: 'relaying' }))).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isAuthFailure, type BridgeSubmitTxStatusDataV2 } from '@sodax/sdk';
|
|
2
|
+
|
|
3
|
+
export const BRIDGE_SUBMIT_TX_STATUS_POLL_MS = 1000;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Polling interval for {@link useBridgeApiSubmitTxStatus}. Stops on a terminal record, on a record
|
|
7
|
+
* the backend abandoned, and on a terminal API-key rejection.
|
|
8
|
+
*
|
|
9
|
+
* `abandonedAt` is terminal even when `status` is still non-terminal (e.g. `'relayed'`) — same rule
|
|
10
|
+
* as the SDK's `pollBackendSubmitTx`. The auth stop is separate from the hook's `retry` policy:
|
|
11
|
+
* `retry` bounds attempts *within* a tick, so without this the interval would keep firing a request
|
|
12
|
+
* a corrected key is the only fix for.
|
|
13
|
+
*
|
|
14
|
+
* Kept in its own pure module (no React/context imports) so it is unit-testable in dapp-kit's
|
|
15
|
+
* `node` test environment — importing the hook itself pulls in `useSodaxContext`. Same reason as
|
|
16
|
+
* `swapsApi/isTerminalSwapIntentStatus`.
|
|
17
|
+
*/
|
|
18
|
+
export function getBridgeSubmitTxStatusRefetchInterval(
|
|
19
|
+
error: unknown,
|
|
20
|
+
data: BridgeSubmitTxStatusDataV2 | undefined,
|
|
21
|
+
): number | false {
|
|
22
|
+
if (isAuthFailure(error)) return false;
|
|
23
|
+
if (data?.status === 'executed' || data?.status === 'failed' || data?.abandonedAt) return false;
|
|
24
|
+
return BRIDGE_SUBMIT_TX_STATUS_POLL_MS;
|
|
25
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BridgeAllowanceCheckResponseV2, CreateBridgeIntentParamsV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -36,7 +37,7 @@ export const useBridgeApiAllowance = ({
|
|
|
36
37
|
return unwrapResult(await sodax.api.bridge.checkAllowance(body, apiConfig));
|
|
37
38
|
},
|
|
38
39
|
enabled: !!body,
|
|
39
|
-
retry:
|
|
40
|
+
retry: retryUnlessAuthFailure,
|
|
40
41
|
...queryOptions,
|
|
41
42
|
});
|
|
42
43
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BridgeApproveResponseV2, CreateBridgeIntentParamsV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
2
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
3
4
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
4
5
|
import type { MutationHookParams } from '../shared/types.js';
|
|
5
6
|
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
@@ -37,7 +38,7 @@ export const useBridgeApiApprove = ({
|
|
|
37
38
|
|
|
38
39
|
return useSafeMutation<BridgeApproveResponseV2, Error, UseBridgeApiApproveVars>({
|
|
39
40
|
mutationKey: ['bridgeApi', 'approve'],
|
|
40
|
-
retry:
|
|
41
|
+
retry: retryUnlessAuthFailure,
|
|
41
42
|
...mutationOptions,
|
|
42
43
|
mutationFn: async ({ body, apiConfig }): Promise<BridgeApproveResponseV2> =>
|
|
43
44
|
unwrapResult(await sodax.api.bridge.approve(body, apiConfig)),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BridgeableAmountResponseV2, BridgeQuoteRequestV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -39,7 +40,7 @@ export const useBridgeApiBridgeableAmount = ({
|
|
|
39
40
|
return unwrapResult(await sodax.api.bridge.getBridgeableAmount(body, apiConfig));
|
|
40
41
|
},
|
|
41
42
|
enabled: !!body,
|
|
42
|
-
retry:
|
|
43
|
+
retry: retryUnlessAuthFailure,
|
|
43
44
|
...queryOptions,
|
|
44
45
|
});
|
|
45
46
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CreateBridgeIntentParamsV2, CreateBridgeIntentResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
2
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
3
4
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
4
5
|
import type { MutationHookParams } from '../shared/types.js';
|
|
5
6
|
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
@@ -34,7 +35,7 @@ export const useBridgeApiCreateBridgeIntent = ({
|
|
|
34
35
|
|
|
35
36
|
return useSafeMutation<CreateBridgeIntentResponseV2, Error, UseBridgeApiCreateBridgeIntentVars>({
|
|
36
37
|
mutationKey: ['bridgeApi', 'createBridgeIntent'],
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...mutationOptions,
|
|
39
40
|
mutationFn: async ({ body, apiConfig }): Promise<CreateBridgeIntentResponseV2> =>
|
|
40
41
|
unwrapResult(await sodax.api.bridge.createBridgeIntent(body, apiConfig)),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BridgeFeeRequestV2, BridgeFeeResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -38,7 +39,7 @@ export const useBridgeApiFee = ({
|
|
|
38
39
|
return unwrapResult(await sodax.api.bridge.getFee(body, apiConfig));
|
|
39
40
|
},
|
|
40
41
|
enabled: !!body,
|
|
41
|
-
retry:
|
|
42
|
+
retry: retryUnlessAuthFailure,
|
|
42
43
|
...queryOptions,
|
|
43
44
|
});
|
|
44
45
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BridgeableCheckResponseV2, BridgeQuoteRequestV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -39,7 +40,7 @@ export const useBridgeApiIsBridgeable = ({
|
|
|
39
40
|
return unwrapResult(await sodax.api.bridge.isBridgeable(body, apiConfig));
|
|
40
41
|
},
|
|
41
42
|
enabled: !!body,
|
|
42
|
-
retry:
|
|
43
|
+
retry: retryUnlessAuthFailure,
|
|
43
44
|
...queryOptions,
|
|
44
45
|
});
|
|
45
46
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BridgeSubmitTxRequestV2, BridgeSubmitTxResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
2
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
3
4
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
4
5
|
import type { MutationHookParams } from '../shared/types.js';
|
|
5
6
|
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
@@ -20,8 +21,9 @@ export type UseBridgeApiSubmitTxVars = {
|
|
|
20
21
|
* broadcasting the spoke-deposit tx, handing it (with the FULL `relayData { address, payload }`
|
|
21
22
|
* envelope) to the backend.
|
|
22
23
|
*
|
|
23
|
-
* Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`.
|
|
24
|
-
*
|
|
24
|
+
* Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`. Retries up to 3 times at the
|
|
25
|
+
* hook level, except on a terminal API-key rejection (401/403) — see `retryUnlessAuthFailure`.
|
|
26
|
+
* Consumers can override via `mutationOptions.retry`.
|
|
25
27
|
*
|
|
26
28
|
* @example
|
|
27
29
|
* const { mutateAsync: submitBridgeTx, isPending, error } = useBridgeApiSubmitTx();
|
|
@@ -42,7 +44,7 @@ export const useBridgeApiSubmitTx = ({
|
|
|
42
44
|
|
|
43
45
|
return useSafeMutation<BridgeSubmitTxResponseV2, Error, UseBridgeApiSubmitTxVars>({
|
|
44
46
|
mutationKey: ['bridgeApi', 'submitTx'],
|
|
45
|
-
retry:
|
|
47
|
+
retry: retryUnlessAuthFailure,
|
|
46
48
|
...mutationOptions,
|
|
47
49
|
mutationFn: async ({ request, apiConfig }): Promise<BridgeSubmitTxResponseV2> =>
|
|
48
50
|
unwrapResult(await sodax.api.bridge.submitTx(request, apiConfig)),
|