@sodax/dapp-kit 2.1.0 → 2.2.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.
- package/dist/index.d.ts +25 -6
- package/dist/index.mjs +29 -23
- package/package.json +2 -2
- package/src/hooks/_apiKeyWire.test.ts +487 -0
- package/src/hooks/bridge/useBridge.test.ts +104 -0
- package/src/hooks/shared/index.ts +1 -0
- package/src/hooks/shared/retryUnlessAuthFailure.test.ts +38 -0
- package/src/hooks/shared/retryUnlessAuthFailure.ts +18 -0
- package/src/hooks/sponsoring/useActivateStellarAccount.wire.test.ts +109 -0
- package/src/hooks/swap/useSwap.test.ts +128 -0
- package/src/hooks/swapsApi/useSwapsApiAllowance.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiApprove.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiCancelIntent.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiCreateIntent.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiCreateLimitOrder.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiDeadline.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiEstimateGas.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiFilledIntent.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiIntent.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiIntentExtraData.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiIntentHash.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiIntentPacket.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiPartnerFee.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiQuote.ts +5 -1
- package/src/hooks/swapsApi/useSwapsApiSolverFee.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiStatus.ts +10 -4
- package/src/hooks/swapsApi/useSwapsApiSubmitTx.ts +5 -3
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +8 -4
- package/src/hooks/swapsApi/useSwapsApiTokens.ts +2 -1
- package/src/hooks/swapsApi/useSwapsApiTokensByChain.ts +2 -1
- package/src/providers/SodaxProvider.test.ts +41 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executable API-key wire manifest for the swapsApi + bridgeApi hook families.
|
|
3
|
+
*
|
|
4
|
+
* Every hook that accepts a per-request `apiConfig` must land it on the outgoing HTTP request as
|
|
5
|
+
* `x-api-key`. Each row renders its hook without a renderer (the package convention: the React
|
|
6
|
+
* Query wrapper is mocked so `queryFn`/`mutationFn` can be captured and driven directly) against a
|
|
7
|
+
* REAL `Sodax` instance whose config carries a DIFFERENT key — so a hook that silently drops its
|
|
8
|
+
* `apiConfig` still makes a request, still sends a key, and fails its row. The stubbed fetch serves
|
|
9
|
+
* a minimal 200 body; post-request validation rejections are expected and tolerated, because the
|
|
10
|
+
* request that already went out is the whole subject. For the approveAndBroadcast hooks that
|
|
11
|
+
* rejection also means the wallet machinery is never touched.
|
|
12
|
+
*
|
|
13
|
+
* To add a new swapsApi/bridgeApi hook, add its row here. The friction is intentional — it forces
|
|
14
|
+
* key-on-wire coverage from day one.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { ChainKeys, Sodax, type RequestOverrideConfig } from '@sodax/sdk';
|
|
18
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
19
|
+
|
|
20
|
+
// biome-ignore lint/suspicious/noExplicitAny: the captured query/mutation options are driven directly.
|
|
21
|
+
let captured: any;
|
|
22
|
+
|
|
23
|
+
const sodax = new Sodax({ apiKey: 'instance-key', logger: 'silent' });
|
|
24
|
+
|
|
25
|
+
vi.mock('./shared/useSodaxContext.js', () => ({ useSodaxContext: () => ({ sodax }) }));
|
|
26
|
+
vi.mock('@tanstack/react-query', () => ({
|
|
27
|
+
// biome-ignore lint/suspicious/noExplicitAny: mirrors the real wrapper's opaque options bag.
|
|
28
|
+
useQuery: (options: any) => {
|
|
29
|
+
captured = options;
|
|
30
|
+
return {};
|
|
31
|
+
},
|
|
32
|
+
useQueryClient: () => ({ invalidateQueries: vi.fn() }),
|
|
33
|
+
}));
|
|
34
|
+
vi.mock('./shared/useSafeMutation.js', () => ({
|
|
35
|
+
// biome-ignore lint/suspicious/noExplicitAny: mirrors the real wrapper's opaque options bag.
|
|
36
|
+
useSafeMutation: (options: any) => {
|
|
37
|
+
captured = options;
|
|
38
|
+
return {};
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const swapsApi = await import('./swapsApi/index.js');
|
|
43
|
+
const bridgeApi = await import('./bridgeApi/index.js');
|
|
44
|
+
|
|
45
|
+
const fetchMock = vi.fn(
|
|
46
|
+
async () => new Response('{}', { status: 200, headers: { 'content-type': 'application/json' } }),
|
|
47
|
+
);
|
|
48
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
49
|
+
|
|
50
|
+
// --- wire-DTO fixtures (string amounts; the six intent bigint fields are serialized client-side) ---
|
|
51
|
+
|
|
52
|
+
const INTENT = {
|
|
53
|
+
intentId: 1n,
|
|
54
|
+
creator: '0x3333333333333333333333333333333333333333',
|
|
55
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
56
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
57
|
+
inputAmount: 1_000_000n,
|
|
58
|
+
minOutputAmount: 900_000n,
|
|
59
|
+
deadline: 0n,
|
|
60
|
+
allowPartialFill: false,
|
|
61
|
+
srcChain: 4n,
|
|
62
|
+
dstChain: 44n,
|
|
63
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
64
|
+
dstAddress: '0x2222222222222222222222222222222222222222',
|
|
65
|
+
solver: '0x0000000000000000000000000000000000000000',
|
|
66
|
+
data: '0x',
|
|
67
|
+
} as const;
|
|
68
|
+
|
|
69
|
+
const CREATE_INTENT_BODY = {
|
|
70
|
+
srcChainKey: ChainKeys.SONIC_MAINNET,
|
|
71
|
+
dstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
72
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
73
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
74
|
+
inputAmount: '1000000',
|
|
75
|
+
minOutputAmount: '1',
|
|
76
|
+
deadline: '0',
|
|
77
|
+
allowPartialFill: false,
|
|
78
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
79
|
+
dstAddress: '0x2222222222222222222222222222222222222222',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const QUOTE_BODY = {
|
|
83
|
+
tokenSrc: '0x4444444444444444444444444444444444444444',
|
|
84
|
+
tokenSrcChainKey: ChainKeys.SONIC_MAINNET,
|
|
85
|
+
tokenDst: '0x5555555555555555555555555555555555555555',
|
|
86
|
+
tokenDstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
87
|
+
amount: '1000000',
|
|
88
|
+
quoteType: 'exact_input' as const,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const SUBMIT_TX_REQUEST = {
|
|
92
|
+
txHash: '0xabc',
|
|
93
|
+
srcChainKey: ChainKeys.SONIC_MAINNET,
|
|
94
|
+
walletAddress: '0x1111111111111111111111111111111111111111',
|
|
95
|
+
intent: INTENT,
|
|
96
|
+
relayData: '0x',
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const BRIDGE_INTENT_BODY = {
|
|
100
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
101
|
+
dstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
102
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
103
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
104
|
+
inputAmount: '1000000',
|
|
105
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
106
|
+
dstAddress: '0x2222222222222222222222222222222222222222',
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const BRIDGE_QUOTE_BODY = {
|
|
110
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
111
|
+
dstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
112
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
113
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const BRIDGE_SUBMIT_REQUEST = {
|
|
117
|
+
txHash: '0xabc',
|
|
118
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
119
|
+
walletAddress: '0x1111111111111111111111111111111111111111',
|
|
120
|
+
relayData: { address: '0x1111111111111111111111111111111111111111', payload: '0x' },
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// --- manifest ---------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
type Row = {
|
|
126
|
+
hook: string;
|
|
127
|
+
/** Exact pathname (below the `/v1` gateway root) the row's single request must hit. */
|
|
128
|
+
path: string;
|
|
129
|
+
/** Exact HTTP method of that request — with `path`, pins the row to one endpoint. */
|
|
130
|
+
method: 'GET' | 'POST';
|
|
131
|
+
/** Renders the hook with `apiConfig` threaded in, then drives the captured queryFn/mutationFn. */
|
|
132
|
+
run: (apiConfig: RequestOverrideConfig) => Promise<unknown>;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const ROWS: Row[] = [
|
|
136
|
+
// swapsApi read hooks
|
|
137
|
+
{
|
|
138
|
+
hook: 'useSwapsApiTokens',
|
|
139
|
+
path: '/swaps/tokens',
|
|
140
|
+
method: 'GET',
|
|
141
|
+
run: apiConfig => {
|
|
142
|
+
swapsApi.useSwapsApiTokens({ params: { apiConfig } });
|
|
143
|
+
return captured.queryFn();
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
hook: 'useSwapsApiTokensByChain',
|
|
148
|
+
path: '/swaps/tokens/sonic',
|
|
149
|
+
method: 'GET',
|
|
150
|
+
run: apiConfig => {
|
|
151
|
+
swapsApi.useSwapsApiTokensByChain({ params: { chainKey: ChainKeys.SONIC_MAINNET, apiConfig } });
|
|
152
|
+
return captured.queryFn();
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
hook: 'useSwapsApiQuote',
|
|
157
|
+
path: '/swaps/quote',
|
|
158
|
+
method: 'POST',
|
|
159
|
+
run: apiConfig => {
|
|
160
|
+
swapsApi.useSwapsApiQuote({ params: { body: QUOTE_BODY, apiConfig } });
|
|
161
|
+
return captured.queryFn();
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
hook: 'useSwapsApiDeadline',
|
|
166
|
+
path: '/swaps/deadline',
|
|
167
|
+
method: 'GET',
|
|
168
|
+
run: apiConfig => {
|
|
169
|
+
swapsApi.useSwapsApiDeadline({ params: { apiConfig } });
|
|
170
|
+
return captured.queryFn();
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
hook: 'useSwapsApiAllowance',
|
|
175
|
+
path: '/swaps/allowance/check',
|
|
176
|
+
method: 'POST',
|
|
177
|
+
run: apiConfig => {
|
|
178
|
+
swapsApi.useSwapsApiAllowance({ params: { body: CREATE_INTENT_BODY, apiConfig } });
|
|
179
|
+
return captured.queryFn();
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
hook: 'useSwapsApiStatus',
|
|
184
|
+
path: '/swaps/intents/status',
|
|
185
|
+
method: 'POST',
|
|
186
|
+
run: apiConfig => {
|
|
187
|
+
swapsApi.useSwapsApiStatus({ params: { intentTxHash: '0xabc', apiConfig } });
|
|
188
|
+
return captured.queryFn();
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
hook: 'useSwapsApiSubmitTxStatus',
|
|
193
|
+
path: '/swaps/submit-tx/status',
|
|
194
|
+
method: 'GET',
|
|
195
|
+
run: apiConfig => {
|
|
196
|
+
swapsApi.useSwapsApiSubmitTxStatus({
|
|
197
|
+
params: { txHash: '0xabc', srcChainKey: ChainKeys.SONIC_MAINNET, apiConfig },
|
|
198
|
+
});
|
|
199
|
+
return captured.queryFn();
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
hook: 'useSwapsApiFilledIntent',
|
|
204
|
+
path: '/swaps/intents/0xabc/fill',
|
|
205
|
+
method: 'GET',
|
|
206
|
+
run: apiConfig => {
|
|
207
|
+
swapsApi.useSwapsApiFilledIntent({ params: { txHash: '0xabc', apiConfig } });
|
|
208
|
+
return captured.queryFn();
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
hook: 'useSwapsApiIntent',
|
|
213
|
+
path: '/swaps/intents/0xabc',
|
|
214
|
+
method: 'GET',
|
|
215
|
+
run: apiConfig => {
|
|
216
|
+
swapsApi.useSwapsApiIntent({ params: { txHash: '0xabc', apiConfig } });
|
|
217
|
+
return captured.queryFn();
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
hook: 'useSwapsApiPartnerFee',
|
|
222
|
+
path: '/swaps/fees/partner',
|
|
223
|
+
method: 'GET',
|
|
224
|
+
run: apiConfig => {
|
|
225
|
+
swapsApi.useSwapsApiPartnerFee({ params: { amount: '1000', apiConfig } });
|
|
226
|
+
return captured.queryFn();
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
hook: 'useSwapsApiSolverFee',
|
|
231
|
+
path: '/swaps/fees/solver',
|
|
232
|
+
method: 'GET',
|
|
233
|
+
run: apiConfig => {
|
|
234
|
+
swapsApi.useSwapsApiSolverFee({ params: { amount: '1000', apiConfig } });
|
|
235
|
+
return captured.queryFn();
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
hook: 'useSwapsApiEstimateGas',
|
|
240
|
+
path: '/swaps/gas/estimate',
|
|
241
|
+
method: 'POST',
|
|
242
|
+
run: apiConfig => {
|
|
243
|
+
swapsApi.useSwapsApiEstimateGas({
|
|
244
|
+
params: { body: { chainKey: ChainKeys.SONIC_MAINNET, tx: { from: '0xf', to: '0xt', data: '0x' } }, apiConfig },
|
|
245
|
+
});
|
|
246
|
+
return captured.queryFn();
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
hook: 'useSwapsApiIntentExtraData',
|
|
251
|
+
path: '/swaps/intents/extra-data',
|
|
252
|
+
method: 'POST',
|
|
253
|
+
run: apiConfig => {
|
|
254
|
+
swapsApi.useSwapsApiIntentExtraData({ params: { body: { txHash: '0xabc' }, apiConfig } });
|
|
255
|
+
return captured.queryFn();
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
hook: 'useSwapsApiIntentHash',
|
|
260
|
+
path: '/swaps/intents/hash',
|
|
261
|
+
method: 'POST',
|
|
262
|
+
run: apiConfig => {
|
|
263
|
+
swapsApi.useSwapsApiIntentHash({ params: { intent: INTENT, apiConfig } });
|
|
264
|
+
return captured.queryFn();
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
hook: 'useSwapsApiIntentPacket',
|
|
269
|
+
path: '/swaps/intents/packet',
|
|
270
|
+
method: 'POST',
|
|
271
|
+
run: apiConfig => {
|
|
272
|
+
swapsApi.useSwapsApiIntentPacket({ params: { body: { chainId: '146', fillTxHash: '0xf' }, apiConfig } });
|
|
273
|
+
return captured.queryFn();
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
// swapsApi mutation hooks
|
|
277
|
+
{
|
|
278
|
+
hook: 'useSwapsApiApprove',
|
|
279
|
+
path: '/swaps/approve',
|
|
280
|
+
method: 'POST',
|
|
281
|
+
run: apiConfig => {
|
|
282
|
+
swapsApi.useSwapsApiApprove();
|
|
283
|
+
return captured.mutationFn({ body: CREATE_INTENT_BODY, apiConfig });
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
hook: 'useSwapsApiCancelIntent',
|
|
288
|
+
path: '/swaps/intents/cancel',
|
|
289
|
+
method: 'POST',
|
|
290
|
+
run: apiConfig => {
|
|
291
|
+
swapsApi.useSwapsApiCancelIntent();
|
|
292
|
+
return captured.mutationFn({ body: { srcChainKey: ChainKeys.SONIC_MAINNET, intent: INTENT }, apiConfig });
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
hook: 'useSwapsApiCreateIntent',
|
|
297
|
+
path: '/swaps/intents',
|
|
298
|
+
method: 'POST',
|
|
299
|
+
run: apiConfig => {
|
|
300
|
+
swapsApi.useSwapsApiCreateIntent();
|
|
301
|
+
return captured.mutationFn({ body: CREATE_INTENT_BODY, apiConfig });
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
hook: 'useSwapsApiCreateLimitOrder',
|
|
306
|
+
path: '/swaps/limit-orders',
|
|
307
|
+
method: 'POST',
|
|
308
|
+
run: apiConfig => {
|
|
309
|
+
swapsApi.useSwapsApiCreateLimitOrder();
|
|
310
|
+
return captured.mutationFn({ body: CREATE_INTENT_BODY, apiConfig });
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
hook: 'useSwapsApiSubmitIntent',
|
|
315
|
+
path: '/swaps/intents/submit',
|
|
316
|
+
method: 'POST',
|
|
317
|
+
run: apiConfig => {
|
|
318
|
+
swapsApi.useSwapsApiSubmitIntent();
|
|
319
|
+
return captured.mutationFn({ body: { chainId: '146', txHash: '0xabc' }, apiConfig });
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
hook: 'useSwapsApiSubmitTx',
|
|
324
|
+
path: '/swaps/submit-tx',
|
|
325
|
+
method: 'POST',
|
|
326
|
+
run: apiConfig => {
|
|
327
|
+
swapsApi.useSwapsApiSubmitTx();
|
|
328
|
+
return captured.mutationFn({ request: SUBMIT_TX_REQUEST, apiConfig });
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
// The `{}` 200 fails the approve schema, so the mutation rejects BEFORE any wallet work — the
|
|
333
|
+
// backend request this row asserts is the hook's first step.
|
|
334
|
+
hook: 'useSwapsApiApproveAndBroadcast',
|
|
335
|
+
path: '/swaps/approve',
|
|
336
|
+
method: 'POST',
|
|
337
|
+
run: apiConfig => {
|
|
338
|
+
swapsApi.useSwapsApiApproveAndBroadcast();
|
|
339
|
+
return captured.mutationFn({ body: CREATE_INTENT_BODY, walletProvider: {}, apiConfig });
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
// bridgeApi read hooks
|
|
343
|
+
{
|
|
344
|
+
hook: 'useBridgeApiTokens',
|
|
345
|
+
path: '/bridge/tokens',
|
|
346
|
+
method: 'GET',
|
|
347
|
+
run: apiConfig => {
|
|
348
|
+
bridgeApi.useBridgeApiTokens({ params: { apiConfig } });
|
|
349
|
+
return captured.queryFn();
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
hook: 'useBridgeApiTokensByChain',
|
|
354
|
+
path: '/bridge/tokens/0x38.bsc',
|
|
355
|
+
method: 'GET',
|
|
356
|
+
run: apiConfig => {
|
|
357
|
+
bridgeApi.useBridgeApiTokensByChain({ params: { chainKey: ChainKeys.BSC_MAINNET, apiConfig } });
|
|
358
|
+
return captured.queryFn();
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
hook: 'useBridgeApiAllowance',
|
|
363
|
+
path: '/bridge/allowance/check',
|
|
364
|
+
method: 'POST',
|
|
365
|
+
run: apiConfig => {
|
|
366
|
+
bridgeApi.useBridgeApiAllowance({ params: { body: BRIDGE_INTENT_BODY, apiConfig } });
|
|
367
|
+
return captured.queryFn();
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
hook: 'useBridgeApiBridgeableAmount',
|
|
372
|
+
path: '/bridge/bridgeable-amount',
|
|
373
|
+
method: 'POST',
|
|
374
|
+
run: apiConfig => {
|
|
375
|
+
bridgeApi.useBridgeApiBridgeableAmount({ params: { body: BRIDGE_QUOTE_BODY, apiConfig } });
|
|
376
|
+
return captured.queryFn();
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
hook: 'useBridgeApiFee',
|
|
381
|
+
path: '/bridge/fee',
|
|
382
|
+
method: 'POST',
|
|
383
|
+
run: apiConfig => {
|
|
384
|
+
bridgeApi.useBridgeApiFee({ params: { body: { inputAmount: '1000000' }, apiConfig } });
|
|
385
|
+
return captured.queryFn();
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
hook: 'useBridgeApiIsBridgeable',
|
|
390
|
+
path: '/bridge/bridgeable/check',
|
|
391
|
+
method: 'POST',
|
|
392
|
+
run: apiConfig => {
|
|
393
|
+
bridgeApi.useBridgeApiIsBridgeable({ params: { body: BRIDGE_QUOTE_BODY, apiConfig } });
|
|
394
|
+
return captured.queryFn();
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
{
|
|
398
|
+
hook: 'useBridgeApiSubmitTxStatus',
|
|
399
|
+
path: '/bridge/submit-tx/status',
|
|
400
|
+
method: 'GET',
|
|
401
|
+
run: apiConfig => {
|
|
402
|
+
bridgeApi.useBridgeApiSubmitTxStatus({
|
|
403
|
+
params: { txHash: '0xabc', srcChainKey: ChainKeys.BSC_MAINNET, apiConfig },
|
|
404
|
+
});
|
|
405
|
+
return captured.queryFn();
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
// bridgeApi mutation hooks
|
|
409
|
+
{
|
|
410
|
+
hook: 'useBridgeApiApprove',
|
|
411
|
+
path: '/bridge/approve',
|
|
412
|
+
method: 'POST',
|
|
413
|
+
run: apiConfig => {
|
|
414
|
+
bridgeApi.useBridgeApiApprove();
|
|
415
|
+
return captured.mutationFn({ body: BRIDGE_INTENT_BODY, apiConfig });
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
hook: 'useBridgeApiCreateBridgeIntent',
|
|
420
|
+
path: '/bridge/intents',
|
|
421
|
+
method: 'POST',
|
|
422
|
+
run: apiConfig => {
|
|
423
|
+
bridgeApi.useBridgeApiCreateBridgeIntent();
|
|
424
|
+
return captured.mutationFn({ body: BRIDGE_INTENT_BODY, apiConfig });
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
hook: 'useBridgeApiSubmitTx',
|
|
429
|
+
path: '/bridge/submit-tx',
|
|
430
|
+
method: 'POST',
|
|
431
|
+
run: apiConfig => {
|
|
432
|
+
bridgeApi.useBridgeApiSubmitTx();
|
|
433
|
+
return captured.mutationFn({ request: BRIDGE_SUBMIT_REQUEST, apiConfig });
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
// Same shape as the swaps row: the failed approve unwrap rejects before any wallet work.
|
|
438
|
+
hook: 'useBridgeApiApproveAndBroadcast',
|
|
439
|
+
path: '/bridge/approve',
|
|
440
|
+
method: 'POST',
|
|
441
|
+
run: apiConfig => {
|
|
442
|
+
bridgeApi.useBridgeApiApproveAndBroadcast();
|
|
443
|
+
return captured.mutationFn({ body: BRIDGE_INTENT_BODY, walletProvider: {}, apiConfig });
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
];
|
|
447
|
+
|
|
448
|
+
/** Fails loudly when a named row is missing, so a hook rename cannot silently drop coverage. */
|
|
449
|
+
const rowFor = (hook: string): Row => {
|
|
450
|
+
const row = ROWS.find(r => r.hook === hook);
|
|
451
|
+
if (!row) throw new Error(`_apiKeyWire manifest has no row named ${hook}`);
|
|
452
|
+
return row;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
beforeEach(() => {
|
|
456
|
+
fetchMock.mockClear();
|
|
457
|
+
captured = undefined;
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
describe('apiConfig.apiKey reaches the wire (hook-level key beats the instance key)', () => {
|
|
461
|
+
it.each(ROWS)('$hook → $method $path carries x-api-key: hook-key', async ({ path, method, run }) => {
|
|
462
|
+
await run({ apiKey: 'hook-key' }).catch(() => {}); // response may fail validation; the request is the subject
|
|
463
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
464
|
+
const [url, init] = fetchMock.mock.calls[0] ?? [];
|
|
465
|
+
expect(new URL(String(url)).pathname).toBe(`/v1${path}`);
|
|
466
|
+
expect(init?.method).toBe(method);
|
|
467
|
+
expect(new Headers(init?.headers).get('x-api-key')).toBe('hook-key');
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
describe('a raw x-api-key header override wins in any casing, without duplicating the header', () => {
|
|
472
|
+
const REPRESENTATIVES = [rowFor('useSwapsApiTokens'), rowFor('useBridgeApiTokens')];
|
|
473
|
+
|
|
474
|
+
it.each(REPRESENTATIVES)(
|
|
475
|
+
'$hook → $method $path carries exactly one x-api-key: raw-key',
|
|
476
|
+
async ({ path, method, run }) => {
|
|
477
|
+
await run({ headers: { 'X-Api-Key': 'raw-key' } }).catch(() => {});
|
|
478
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
479
|
+
const [url, init] = fetchMock.mock.calls[0] ?? [];
|
|
480
|
+
expect(new URL(String(url)).pathname).toBe(`/v1${path}`);
|
|
481
|
+
expect(init?.method).toBe(method);
|
|
482
|
+
const headers = init?.headers as Record<string, string>;
|
|
483
|
+
expect(Object.keys(headers).filter(name => name.toLowerCase() === 'x-api-key')).toHaveLength(1);
|
|
484
|
+
expect(new Headers(headers).get('x-api-key')).toBe('raw-key');
|
|
485
|
+
},
|
|
486
|
+
);
|
|
487
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useBridge` wire test for the per-action API key: `extras.apiKey` in the mutation vars must reach
|
|
3
|
+
* the backend submit-tx leg as `x-api-key` — on the POST and on the status poll — beating the
|
|
4
|
+
* instance-level key.
|
|
5
|
+
*
|
|
6
|
+
* Follows the package convention of testing hooks without a renderer (captured `mutationFn`), with
|
|
7
|
+
* the SDK stubbed at the same seams its own backend submit-tx tests use: `createBridgeIntent` is
|
|
8
|
+
* spied on a real keyed `Sodax` so no wallet or chain is touched, while `POST /bridge/submit-tx`
|
|
9
|
+
* and `GET /bridge/submit-tx/status` run call-through to a stubbed global fetch serving the
|
|
10
|
+
* schema-valid fixtures from packages/sdk/src/bridge/BridgeService.test.ts.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { ChainKeys, Sodax } from '@sodax/sdk';
|
|
14
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
15
|
+
|
|
16
|
+
// biome-ignore lint/suspicious/noExplicitAny: the captured mutation options are driven directly.
|
|
17
|
+
let captured: any;
|
|
18
|
+
|
|
19
|
+
const sodax = new Sodax({ apiKey: 'instance-key', logger: 'silent' });
|
|
20
|
+
|
|
21
|
+
vi.mock('../shared/useSodaxContext.js', () => ({ useSodaxContext: () => ({ sodax }) }));
|
|
22
|
+
vi.mock('@tanstack/react-query', () => ({ useQueryClient: () => ({ invalidateQueries: vi.fn() }) }));
|
|
23
|
+
vi.mock('../shared/useSafeMutation.js', () => ({
|
|
24
|
+
// biome-ignore lint/suspicious/noExplicitAny: mirrors the real wrapper's opaque options bag.
|
|
25
|
+
useSafeMutation: (options: any) => {
|
|
26
|
+
captured = options;
|
|
27
|
+
return {};
|
|
28
|
+
},
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
const { useBridge } = await import('./useBridge.js');
|
|
32
|
+
|
|
33
|
+
const fetchMock = vi.fn();
|
|
34
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
35
|
+
|
|
36
|
+
const jsonOk = (data: unknown): Response =>
|
|
37
|
+
new Response(JSON.stringify(data), { status: 200, headers: { 'content-type': 'application/json' } });
|
|
38
|
+
|
|
39
|
+
const BRIDGE_PARAMS = {
|
|
40
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
41
|
+
dstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
42
|
+
srcToken: '0x4444444444444444444444444444444444444444',
|
|
43
|
+
dstToken: '0x5555555555555555555555555555555555555555',
|
|
44
|
+
amount: 1_000_000n,
|
|
45
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
46
|
+
recipient: '0x2222222222222222222222222222222222222222',
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const SUBMIT_ACCEPTED = { success: true, data: { status: 'inserted', message: 'accepted' } };
|
|
50
|
+
const STATUS_EXECUTED = {
|
|
51
|
+
success: true,
|
|
52
|
+
data: {
|
|
53
|
+
txHash: '0xspokeTx',
|
|
54
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
55
|
+
status: 'executed',
|
|
56
|
+
processingAttempts: 1,
|
|
57
|
+
result: { dstIntentTxHash: '0xDST' },
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
vi.restoreAllMocks();
|
|
63
|
+
fetchMock.mockReset();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('useBridge — extras.apiKey on the backend submit-tx wire', () => {
|
|
67
|
+
it('lands the per-action key on the submit-tx POST and the status poll, beating the instance key', async () => {
|
|
68
|
+
vi.spyOn(sodax.bridge, 'createBridgeIntent').mockResolvedValueOnce({
|
|
69
|
+
ok: true,
|
|
70
|
+
value: {
|
|
71
|
+
tx: '0xspokeTx',
|
|
72
|
+
relayData: { address: '0x1111111111111111111111111111111111111111', payload: '0xpay' },
|
|
73
|
+
},
|
|
74
|
+
} as never);
|
|
75
|
+
fetchMock.mockImplementation(async (url: string | URL, init?: RequestInit) => {
|
|
76
|
+
const { pathname } = new URL(String(url));
|
|
77
|
+
if (pathname === '/v1/bridge/submit-tx/status' && init?.method === 'GET') return jsonOk(STATUS_EXECUTED);
|
|
78
|
+
if (pathname === '/v1/bridge/submit-tx' && init?.method === 'POST') return jsonOk(SUBMIT_ACCEPTED);
|
|
79
|
+
throw new Error(`unexpected fetch: ${String(url)}`);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
useBridge();
|
|
83
|
+
const result = await captured.mutationFn({
|
|
84
|
+
params: BRIDGE_PARAMS,
|
|
85
|
+
extras: { apiKey: 'action-key' },
|
|
86
|
+
walletProvider: {},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Completed on the backend path: TxHashPair from the executed status, no relay fallback fetches.
|
|
90
|
+
expect(result).toEqual({ srcChainTxHash: '0xspokeTx', dstChainTxHash: '0xDST' });
|
|
91
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
92
|
+
|
|
93
|
+
const submit = fetchMock.mock.calls.find(
|
|
94
|
+
([url, init]) => new URL(String(url)).pathname === '/v1/bridge/submit-tx' && init?.method === 'POST',
|
|
95
|
+
);
|
|
96
|
+
const poll = fetchMock.mock.calls.find(
|
|
97
|
+
([url, init]) => new URL(String(url)).pathname === '/v1/bridge/submit-tx/status' && init?.method === 'GET',
|
|
98
|
+
);
|
|
99
|
+
expect(submit).toBeDefined();
|
|
100
|
+
expect(poll).toBeDefined();
|
|
101
|
+
expect(new Headers(submit?.[1]?.headers).get('x-api-key')).toBe('action-key');
|
|
102
|
+
expect(new Headers(poll?.[1]?.headers).get('x-api-key')).toBe('action-key');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SodaxError } from '@sodax/sdk';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { retryUnlessAuthFailure } from './retryUnlessAuthFailure.js';
|
|
4
|
+
|
|
5
|
+
/** A backend failure as the swaps API surfaces it: `EXTERNAL_API_ERROR` with the status on context. */
|
|
6
|
+
const apiError = (status?: number): SodaxError =>
|
|
7
|
+
new SodaxError('EXTERNAL_API_ERROR', `responded with ${status}`, {
|
|
8
|
+
feature: 'backend',
|
|
9
|
+
context: { api: 'swaps', endpoint: '/swaps/quote', status },
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('retryUnlessAuthFailure', () => {
|
|
13
|
+
it('never retries a terminal API-key rejection, even on the first attempt', () => {
|
|
14
|
+
// The count check must not rescue a terminal error — order of the two conditions matters.
|
|
15
|
+
for (const status of [401, 403]) {
|
|
16
|
+
expect(retryUnlessAuthFailure(0, apiError(status))).toBe(false);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("retries the apiguard's transient 503 and other server failures", () => {
|
|
21
|
+
// 503 is the one guard outcome that is NOT terminal — the key may be fine, verification is down.
|
|
22
|
+
for (const status of [408, 429, 500, 502, 503, 504]) {
|
|
23
|
+
expect(retryUnlessAuthFailure(0, apiError(status))).toBe(true);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('retries failures that carry no HTTP status (network, timeout, non-SDK throws)', () => {
|
|
28
|
+
expect(retryUnlessAuthFailure(0, apiError(undefined))).toBe(true);
|
|
29
|
+
expect(retryUnlessAuthFailure(0, new Error('ECONNRESET'))).toBe(true);
|
|
30
|
+
expect(retryUnlessAuthFailure(0, undefined)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('stops after 3 retries of a repeatable failure', () => {
|
|
34
|
+
const err = apiError(500);
|
|
35
|
+
expect(retryUnlessAuthFailure(2, err)).toBe(true);
|
|
36
|
+
expect(retryUnlessAuthFailure(3, err)).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isAuthFailure } from '@sodax/sdk';
|
|
2
|
+
|
|
3
|
+
/** Retries a repeatable failure gets, matching the `retry: 3` these hooks used before. */
|
|
4
|
+
const MAX_RETRIES = 3;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default React Query `retry` policy for the backend swaps-API hooks: replay a transport blip, never a
|
|
8
|
+
* terminal API-key rejection (see `isAuthFailure`). Retrying a rejected key only multiplies doomed
|
|
9
|
+
* requests before the consumer sees the error.
|
|
10
|
+
*
|
|
11
|
+
* Override or compose it through `queryOptions` / `mutationOptions`:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* useSwapsApiQuote({ params: { body }, queryOptions: { retry: 5 } });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const retryUnlessAuthFailure = (failureCount: number, error: unknown): boolean =>
|
|
18
|
+
!isAuthFailure(error) && failureCount < MAX_RETRIES;
|