@sodax/dapp-kit 2.1.0-rc.9 → 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,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useActivateStellarAccount` wire test for the per-request API key: `requestConfig.apiKey` in the
|
|
3
|
+
* mutation vars must reach the sponsoring `/accounts` POST as `x-api-key`, beating the
|
|
4
|
+
* instance-level key.
|
|
5
|
+
*
|
|
6
|
+
* Follows the package convention of testing hooks without a renderer (captured `mutationFn`),
|
|
7
|
+
* driving a real keyed `Sodax` end to end the way packages/sdk/src/sponsoring/SponsoringService.test.ts
|
|
8
|
+
* does: Horizon is stubbed on the instance, the wallet signs with a real throwaway keypair, and the
|
|
9
|
+
* config GET + account POST run call-through to a stubbed global fetch.
|
|
10
|
+
*
|
|
11
|
+
* `@stellar/stellar-sdk` is not a dapp-kit dependency, so it is loaded through `@sodax/sdk`'s own
|
|
12
|
+
* module graph — the same copy the SDK builds and verifies the transaction with.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createRequire } from 'node:module';
|
|
16
|
+
import { Sodax } from '@sodax/sdk';
|
|
17
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
18
|
+
|
|
19
|
+
const sdkRequire = createRequire(createRequire(import.meta.url).resolve('@sodax/sdk/package.json'));
|
|
20
|
+
const { Keypair, Networks, TransactionBuilder } = sdkRequire('@stellar/stellar-sdk');
|
|
21
|
+
|
|
22
|
+
// biome-ignore lint/suspicious/noExplicitAny: the captured mutation options are driven directly.
|
|
23
|
+
let captured: any;
|
|
24
|
+
|
|
25
|
+
const sodax = new Sodax({ apiKey: 'instance-key', logger: 'silent' });
|
|
26
|
+
|
|
27
|
+
vi.mock('../shared/useSodaxContext.js', () => ({ useSodaxContext: () => ({ sodax }) }));
|
|
28
|
+
vi.mock('@tanstack/react-query', () => ({ useQueryClient: () => ({ invalidateQueries: vi.fn() }) }));
|
|
29
|
+
vi.mock('../shared/useSafeMutation.js', () => ({
|
|
30
|
+
// biome-ignore lint/suspicious/noExplicitAny: mirrors the real wrapper's opaque options bag.
|
|
31
|
+
useSafeMutation: (options: any) => {
|
|
32
|
+
captured = options;
|
|
33
|
+
return {};
|
|
34
|
+
},
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
const { useActivateStellarAccount } = await import('./useActivateStellarAccount.js');
|
|
38
|
+
|
|
39
|
+
const SPONSOR = Keypair.random();
|
|
40
|
+
const USER = Keypair.random();
|
|
41
|
+
|
|
42
|
+
const SPONSOR_CONFIG = {
|
|
43
|
+
sponsorAccount: SPONSOR.publicKey(),
|
|
44
|
+
networkPassphrase: Networks.PUBLIC,
|
|
45
|
+
minTotalFeeStroops: '3000',
|
|
46
|
+
maxTotalFeeStroops: '10000',
|
|
47
|
+
operationCount: 3,
|
|
48
|
+
minPerOperationFeeStroops: '1000',
|
|
49
|
+
maxPerOperationFeeStroops: '3333',
|
|
50
|
+
recommendedPerOperationFeeStroops: '1000',
|
|
51
|
+
maxTimeboundSeconds: 3600,
|
|
52
|
+
requiredStartingBalance: '0',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const fetchMock = vi.fn();
|
|
56
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
57
|
+
|
|
58
|
+
const jsonOk = (data: unknown): Response =>
|
|
59
|
+
new Response(JSON.stringify(data), { status: 200, headers: { 'content-type': 'application/json' } });
|
|
60
|
+
|
|
61
|
+
// The account being created signs; the wallet parses and re-emits the SDK-built envelope as XDR.
|
|
62
|
+
const wallet = {
|
|
63
|
+
chainType: 'STELLAR',
|
|
64
|
+
getWalletAddress: vi.fn(async () => USER.publicKey()),
|
|
65
|
+
waitForTransactionReceipt: vi.fn(),
|
|
66
|
+
signTransaction: vi.fn(async (xdr: string) => {
|
|
67
|
+
const tx = TransactionBuilder.fromXDR(xdr, Networks.PUBLIC);
|
|
68
|
+
tx.sign(USER);
|
|
69
|
+
return tx.toXDR();
|
|
70
|
+
}),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
fetchMock.mockReset();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('useActivateStellarAccount — requestConfig.apiKey on the sponsoring wire', () => {
|
|
78
|
+
it('lands the per-request key on the /accounts POST, beating the instance key', async () => {
|
|
79
|
+
// Horizon uses its own HTTP client, so stub it on the instance rather than through fetch.
|
|
80
|
+
vi.spyOn(sodax.spoke.stellar.server, 'loadAccount').mockImplementation(async (address: string) => {
|
|
81
|
+
if (address === SPONSOR.publicKey()) return { sequenceNumber: () => '100' } as never;
|
|
82
|
+
throw Object.assign(new Error('Not Found'), { response: { status: 404 } });
|
|
83
|
+
});
|
|
84
|
+
fetchMock
|
|
85
|
+
.mockResolvedValueOnce(jsonOk(SPONSOR_CONFIG))
|
|
86
|
+
.mockResolvedValueOnce(jsonOk({ hash: 'abc123', alreadyActive: false }));
|
|
87
|
+
|
|
88
|
+
useActivateStellarAccount();
|
|
89
|
+
const result = await captured.mutationFn({
|
|
90
|
+
address: USER.publicKey(),
|
|
91
|
+
walletProvider: wallet,
|
|
92
|
+
requestConfig: { apiKey: 'hook-key' },
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(result).toEqual({ status: 'submitted', hash: 'abc123', attempts: 1 });
|
|
96
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
97
|
+
|
|
98
|
+
const post = fetchMock.mock.calls.find(([url]) => String(url).endsWith('/sponsorships/stellar/accounts'));
|
|
99
|
+
expect(post).toBeDefined();
|
|
100
|
+
expect(post?.[1]?.method).toBe('POST');
|
|
101
|
+
const headers = post?.[1]?.headers as Record<string, string>;
|
|
102
|
+
expect(Object.keys(headers).filter(name => name.toLowerCase() === 'x-api-key')).toHaveLength(1);
|
|
103
|
+
expect(new Headers(headers).get('x-api-key')).toBe('hook-key');
|
|
104
|
+
|
|
105
|
+
// The same requestConfig rides the preceding sponsor-config GET.
|
|
106
|
+
const configGet = fetchMock.mock.calls.find(([url]) => String(url).endsWith('/sponsorships/stellar/config'));
|
|
107
|
+
expect(new Headers(configGet?.[1]?.headers).get('x-api-key')).toBe('hook-key');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useSwap` 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 2-step tests use: `createIntent` is spied on a
|
|
8
|
+
* real keyed `Sodax` so no wallet or chain is touched, while `POST /swaps/submit-tx` and
|
|
9
|
+
* `GET /swaps/submit-tx/status` run call-through to a stubbed global fetch serving the schema-valid
|
|
10
|
+
* fixtures from packages/sdk/src/swap/SwapService.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 { useSwap } = await import('./useSwap.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
|
+
// Only the six IntentRequestV2 bigint fields carry bigints — the wire serializer rejects any other.
|
|
40
|
+
const INTENT = {
|
|
41
|
+
intentId: 1n,
|
|
42
|
+
creator: '0x3333333333333333333333333333333333333333',
|
|
43
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
44
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
45
|
+
inputAmount: 1_000_000n,
|
|
46
|
+
minOutputAmount: 900_000n,
|
|
47
|
+
deadline: 0n,
|
|
48
|
+
allowPartialFill: false,
|
|
49
|
+
srcChain: 4n,
|
|
50
|
+
dstChain: 44n,
|
|
51
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
52
|
+
dstAddress: '0x2222222222222222222222222222222222222222',
|
|
53
|
+
solver: '0x0000000000000000000000000000000000000000',
|
|
54
|
+
data: '0x',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const SWAP_PARAMS = {
|
|
58
|
+
inputToken: '0x4444444444444444444444444444444444444444',
|
|
59
|
+
outputToken: '0x5555555555555555555555555555555555555555',
|
|
60
|
+
inputAmount: 1_000_000n,
|
|
61
|
+
minOutputAmount: 900_000n,
|
|
62
|
+
deadline: 0n,
|
|
63
|
+
allowPartialFill: false,
|
|
64
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
65
|
+
dstChainKey: ChainKeys.ARBITRUM_MAINNET,
|
|
66
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
67
|
+
dstAddress: '0x2222222222222222222222222222222222222222',
|
|
68
|
+
solver: '0x0000000000000000000000000000000000000000',
|
|
69
|
+
data: '0x',
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const SUBMIT_ACCEPTED = { success: true, data: { status: 'inserted', message: 'accepted' } };
|
|
73
|
+
const STATUS_SOLVED = {
|
|
74
|
+
success: true,
|
|
75
|
+
data: {
|
|
76
|
+
txHash: '0xspokeTx',
|
|
77
|
+
srcChainKey: ChainKeys.BSC_MAINNET,
|
|
78
|
+
status: 'solved',
|
|
79
|
+
processingAttempts: 1,
|
|
80
|
+
result: { dstIntentTxHash: '0xDST', intent_hash: '0xHASH' },
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
beforeEach(() => {
|
|
85
|
+
vi.restoreAllMocks();
|
|
86
|
+
fetchMock.mockReset();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('useSwap — extras.apiKey on the backend submit-tx wire', () => {
|
|
90
|
+
it('lands the per-action key on the submit-tx POST and the status poll, beating the instance key', async () => {
|
|
91
|
+
vi.spyOn(sodax.swaps, 'createIntent').mockResolvedValueOnce({
|
|
92
|
+
ok: true,
|
|
93
|
+
value: {
|
|
94
|
+
tx: '0xspokeTx',
|
|
95
|
+
intent: { ...INTENT, feeAmount: 0n },
|
|
96
|
+
relayData: { address: INTENT.creator, payload: '0xpay' },
|
|
97
|
+
},
|
|
98
|
+
} as never);
|
|
99
|
+
fetchMock.mockImplementation(async (url: string | URL, init?: RequestInit) => {
|
|
100
|
+
const { pathname } = new URL(String(url));
|
|
101
|
+
if (pathname === '/v1/swaps/submit-tx/status' && init?.method === 'GET') return jsonOk(STATUS_SOLVED);
|
|
102
|
+
if (pathname === '/v1/swaps/submit-tx' && init?.method === 'POST') return jsonOk(SUBMIT_ACCEPTED);
|
|
103
|
+
throw new Error(`unexpected fetch: ${String(url)}`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
useSwap();
|
|
107
|
+
const result = await captured.mutationFn({
|
|
108
|
+
params: SWAP_PARAMS,
|
|
109
|
+
extras: { apiKey: 'action-key' },
|
|
110
|
+
walletProvider: {},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Completed on the backend path: reconstructed from the solved status, no relay fallback fetches.
|
|
114
|
+
expect(result.intentDeliveryInfo.dstTxHash).toBe('0xDST');
|
|
115
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
116
|
+
|
|
117
|
+
const submit = fetchMock.mock.calls.find(
|
|
118
|
+
([url, init]) => new URL(String(url)).pathname === '/v1/swaps/submit-tx' && init?.method === 'POST',
|
|
119
|
+
);
|
|
120
|
+
const poll = fetchMock.mock.calls.find(
|
|
121
|
+
([url, init]) => new URL(String(url)).pathname === '/v1/swaps/submit-tx/status' && init?.method === 'GET',
|
|
122
|
+
);
|
|
123
|
+
expect(submit).toBeDefined();
|
|
124
|
+
expect(poll).toBeDefined();
|
|
125
|
+
expect(new Headers(submit?.[1]?.headers).get('x-api-key')).toBe('action-key');
|
|
126
|
+
expect(new Headers(poll?.[1]?.headers).get('x-api-key')).toBe('action-key');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { AllowanceCheckResponseV2, CreateIntentParamsV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiAllowance = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.checkAllowance(body, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!body,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ApproveResponseV2, CreateIntentParamsV2, 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 useSwapsApiApprove = ({
|
|
|
34
35
|
|
|
35
36
|
return useSafeMutation<ApproveResponseV2, Error, UseSwapsApiApproveVars>({
|
|
36
37
|
mutationKey: ['swapsApi', 'approve'],
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...mutationOptions,
|
|
39
40
|
mutationFn: async ({ body, apiConfig }): Promise<ApproveResponseV2> =>
|
|
40
41
|
unwrapResult(await sodax.api.swaps.approve(body, apiConfig)),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CancelIntentRequestV2, CancelIntentResponseV2, 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';
|
|
@@ -33,7 +34,7 @@ export const useSwapsApiCancelIntent = ({
|
|
|
33
34
|
|
|
34
35
|
return useSafeMutation<CancelIntentResponseV2, Error, UseSwapsApiCancelIntentVars>({
|
|
35
36
|
mutationKey: ['swapsApi', 'cancelIntent'],
|
|
36
|
-
retry:
|
|
37
|
+
retry: retryUnlessAuthFailure,
|
|
37
38
|
...mutationOptions,
|
|
38
39
|
mutationFn: async ({ body, apiConfig }): Promise<CancelIntentResponseV2> =>
|
|
39
40
|
unwrapResult(await sodax.api.swaps.cancelIntent(body, apiConfig)),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CreateIntentParamsV2, CreateIntentResponseV2, 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';
|
|
@@ -33,7 +34,7 @@ export const useSwapsApiCreateIntent = ({
|
|
|
33
34
|
|
|
34
35
|
return useSafeMutation<CreateIntentResponseV2, Error, UseSwapsApiCreateIntentVars>({
|
|
35
36
|
mutationKey: ['swapsApi', 'createIntent'],
|
|
36
|
-
retry:
|
|
37
|
+
retry: retryUnlessAuthFailure,
|
|
37
38
|
...mutationOptions,
|
|
38
39
|
mutationFn: async ({ body, apiConfig }): Promise<CreateIntentResponseV2> =>
|
|
39
40
|
unwrapResult(await sodax.api.swaps.createIntent(body, apiConfig)),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CreateLimitOrderParamsV2, CreateLimitOrderResponseV2, 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';
|
|
@@ -33,7 +34,7 @@ export const useSwapsApiCreateLimitOrder = ({
|
|
|
33
34
|
|
|
34
35
|
return useSafeMutation<CreateLimitOrderResponseV2, Error, UseSwapsApiCreateLimitOrderVars>({
|
|
35
36
|
mutationKey: ['swapsApi', 'createLimitOrder'],
|
|
36
|
-
retry:
|
|
37
|
+
retry: retryUnlessAuthFailure,
|
|
37
38
|
...mutationOptions,
|
|
38
39
|
mutationFn: async ({ body, apiConfig }): Promise<CreateLimitOrderResponseV2> =>
|
|
39
40
|
unwrapResult(await sodax.api.swaps.createLimitOrderIntent(body, apiConfig)),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { DeadlineQueryV2, DeadlineResponseV2, 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
|
|
|
@@ -30,7 +31,7 @@ export const useSwapsApiDeadline = ({
|
|
|
30
31
|
return useQuery<DeadlineResponseV2, Error>({
|
|
31
32
|
queryKey: ['swapsApi', 'deadline', query?.offsetSeconds ?? null],
|
|
32
33
|
queryFn: async (): Promise<DeadlineResponseV2> => unwrapResult(await sodax.api.swaps.getDeadline(query, apiConfig)),
|
|
33
|
-
retry:
|
|
34
|
+
retry: retryUnlessAuthFailure,
|
|
34
35
|
...queryOptions,
|
|
35
36
|
});
|
|
36
37
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GasEstimateRequestV2, GasEstimateResponseV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiEstimateGas = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.estimateGas(body, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!body,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { IntentStateV2, 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
|
|
|
@@ -35,7 +36,7 @@ export const useSwapsApiFilledIntent = ({
|
|
|
35
36
|
return unwrapResult(await sodax.api.swaps.getFilledIntent(txHash, apiConfig));
|
|
36
37
|
},
|
|
37
38
|
enabled: !!txHash && txHash.length > 0,
|
|
38
|
-
retry:
|
|
39
|
+
retry: retryUnlessAuthFailure,
|
|
39
40
|
...queryOptions,
|
|
40
41
|
});
|
|
41
42
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GetIntentResponseV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiIntent = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.getIntent(txHash, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!txHash && txHash.length > 0,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { IntentExtraDataRequestV2, IntentExtraDataResponseV2, 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
|
|
|
@@ -35,7 +36,7 @@ export const useSwapsApiIntentExtraData = ({
|
|
|
35
36
|
return unwrapResult(await sodax.api.swaps.getIntentSubmitTxExtraData(body, apiConfig));
|
|
36
37
|
},
|
|
37
38
|
enabled: !!body && (!!body.txHash || !!body.intent),
|
|
38
|
-
retry:
|
|
39
|
+
retry: retryUnlessAuthFailure,
|
|
39
40
|
...queryOptions,
|
|
40
41
|
});
|
|
41
42
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { IntentHashResponseV2, IntentRequestV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiIntentHash = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.getIntentHash({ intent }, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!intent,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { IntentPacketRequestV2, IntentPacketResponseV2, 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
|
|
|
@@ -37,7 +38,7 @@ export const useSwapsApiIntentPacket = ({
|
|
|
37
38
|
return unwrapResult(await sodax.api.swaps.getSolvedIntentPacket(body, apiConfig));
|
|
38
39
|
},
|
|
39
40
|
enabled: !!body,
|
|
40
|
-
retry:
|
|
41
|
+
retry: retryUnlessAuthFailure,
|
|
41
42
|
...queryOptions,
|
|
42
43
|
});
|
|
43
44
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { FeeResponseV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiPartnerFee = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.getPartnerFee({ amount }, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!amount && amount.length > 0,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { QuoteQueryV2, QuoteRequestV2, QuoteResponseV2, 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
|
|
|
@@ -27,6 +28,9 @@ export type UseSwapsApiQuoteParams = ReadHookParams<
|
|
|
27
28
|
* amount: '1000000', quoteType: 'exact_input',
|
|
28
29
|
* partnerFee: { address: '0xSonicFeeReceiver', percentage: 10 },
|
|
29
30
|
* },
|
|
31
|
+
* // Optional per-request override of the configured backend API key (`x-api-key`);
|
|
32
|
+
* // usually set once instead via new Sodax({ apiKey }).
|
|
33
|
+
* apiConfig: { apiKey: 'partner-api-key' },
|
|
30
34
|
* },
|
|
31
35
|
* });
|
|
32
36
|
*/
|
|
@@ -50,7 +54,7 @@ export const useSwapsApiQuote = ({
|
|
|
50
54
|
return unwrapResult(await sodax.api.swaps.getQuote(body, query, apiConfig));
|
|
51
55
|
},
|
|
52
56
|
enabled: !!body,
|
|
53
|
-
retry:
|
|
57
|
+
retry: retryUnlessAuthFailure,
|
|
54
58
|
...queryOptions,
|
|
55
59
|
});
|
|
56
60
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { FeeResponseV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiSolverFee = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.getSolverFee({ amount }, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!amount && amount.length > 0,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
-
import type
|
|
2
|
+
import { isAuthFailure, type RequestOverrideConfig, type StatusResponseV2 } 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
|
import { isTerminalSwapIntentStatus } from './isTerminalSwapIntentStatus.js';
|
|
@@ -21,7 +22,8 @@ export type UseSwapsApiStatusParams = ReadHookParams<
|
|
|
21
22
|
* const { data } = useSwapsApiStatus({ params: { intentTxHash: '0x123...' } });
|
|
22
23
|
*
|
|
23
24
|
* @remarks
|
|
24
|
-
* - Default refetch interval is 1 second; stops once `status` is `3` (SOLVED) or `4` (FAILED)
|
|
25
|
+
* - Default refetch interval is 1 second; stops once `status` is `3` (SOLVED) or `4` (FAILED), or
|
|
26
|
+
* once the backend rejects the API key (401/403 is terminal — a retry cannot fix it).
|
|
25
27
|
*/
|
|
26
28
|
export const useSwapsApiStatus = ({
|
|
27
29
|
params,
|
|
@@ -38,8 +40,12 @@ export const useSwapsApiStatus = ({
|
|
|
38
40
|
return unwrapResult(await sodax.api.swaps.getStatus({ intentTxHash }, apiConfig));
|
|
39
41
|
},
|
|
40
42
|
enabled: !!intentTxHash && intentTxHash.length > 0,
|
|
41
|
-
retry:
|
|
42
|
-
refetchInterval: query =>
|
|
43
|
+
retry: retryUnlessAuthFailure,
|
|
44
|
+
refetchInterval: query => {
|
|
45
|
+
// `retry` bounds attempts within a tick, not the interval itself — so stop it here too.
|
|
46
|
+
if (isAuthFailure(query.state.error)) return false;
|
|
47
|
+
return isTerminalSwapIntentStatus(query.state.data?.status) ? false : 1000;
|
|
48
|
+
},
|
|
43
49
|
...queryOptions,
|
|
44
50
|
});
|
|
45
51
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RequestOverrideConfig, SubmitTxRequestV2, SubmitTxResponseV2 } 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';
|
|
@@ -18,8 +19,9 @@ export type UseSwapsApiSubmitTxVars = {
|
|
|
18
19
|
* React hook for submitting a swap transaction to be processed (relay, post execution to the
|
|
19
20
|
* solver, etc.) via the swaps API — `sodax.api.swaps.submitTx`.
|
|
20
21
|
*
|
|
21
|
-
* Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`.
|
|
22
|
-
*
|
|
22
|
+
* Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`. Retries up to 3 times at the hook
|
|
23
|
+
* level, except on a terminal API-key rejection (401/403) — see `retryUnlessAuthFailure`. Consumers
|
|
24
|
+
* can override via `mutationOptions.retry`.
|
|
23
25
|
*
|
|
24
26
|
* @example
|
|
25
27
|
* const { mutateAsync: submitSwapTx, isPending, error } = useSwapsApiSubmitTx();
|
|
@@ -40,7 +42,7 @@ export const useSwapsApiSubmitTx = ({
|
|
|
40
42
|
|
|
41
43
|
return useSafeMutation<SubmitTxResponseV2, Error, UseSwapsApiSubmitTxVars>({
|
|
42
44
|
mutationKey: ['swapsApi', 'submitTx'],
|
|
43
|
-
retry:
|
|
45
|
+
retry: retryUnlessAuthFailure,
|
|
44
46
|
...mutationOptions,
|
|
45
47
|
mutationFn: async ({ request, apiConfig }): Promise<SubmitTxResponseV2> =>
|
|
46
48
|
unwrapResult(await sodax.api.swaps.submitTx(request, apiConfig)),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
-
import type
|
|
2
|
+
import { isAuthFailure, type RequestOverrideConfig, type SubmitTxStatusResponseV2 } 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
|
|
|
@@ -24,8 +25,9 @@ export type UseSwapsApiSubmitTxStatusParams = ReadHookParams<
|
|
|
24
25
|
* });
|
|
25
26
|
*
|
|
26
27
|
* @remarks
|
|
27
|
-
* - Default refetch interval is 1 second; stops on 'solved' or 'failed' status,
|
|
28
|
-
* backend marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal
|
|
28
|
+
* - Default refetch interval is 1 second; stops on 'solved' or 'failed' status, when the
|
|
29
|
+
* backend marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal, or
|
|
30
|
+
* once the backend rejects the API key (401/403 is terminal — a retry cannot fix it).
|
|
29
31
|
*/
|
|
30
32
|
export const useSwapsApiSubmitTxStatus = ({
|
|
31
33
|
params,
|
|
@@ -45,8 +47,10 @@ export const useSwapsApiSubmitTxStatus = ({
|
|
|
45
47
|
return unwrapResult(await sodax.api.swaps.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
|
|
46
48
|
},
|
|
47
49
|
enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
|
|
48
|
-
retry:
|
|
50
|
+
retry: retryUnlessAuthFailure,
|
|
49
51
|
refetchInterval: query => {
|
|
52
|
+
// `retry` bounds attempts within a tick, not the interval itself — so stop it here too.
|
|
53
|
+
if (isAuthFailure(query.state.error)) return false;
|
|
50
54
|
const data = query.state.data?.data;
|
|
51
55
|
// `abandonedAt` is terminal even when `status` is still non-terminal — same rule as the
|
|
52
56
|
// SDK's pollBackendSubmitTx and the bridge-api status hook.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GetSwapTokensResponseV2, 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
|
|
|
@@ -28,7 +29,7 @@ export const useSwapsApiTokens = ({
|
|
|
28
29
|
return useQuery<GetSwapTokensResponseV2, Error>({
|
|
29
30
|
queryKey: ['swapsApi', 'tokens'],
|
|
30
31
|
queryFn: async (): Promise<GetSwapTokensResponseV2> => unwrapResult(await sodax.api.swaps.getTokens(apiConfig)),
|
|
31
|
-
retry:
|
|
32
|
+
retry: retryUnlessAuthFailure,
|
|
32
33
|
...queryOptions,
|
|
33
34
|
});
|
|
34
35
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GetSwapTokensByChainResponseV2, 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
|
|
|
@@ -34,7 +35,7 @@ export const useSwapsApiTokensByChain = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.swaps.getTokensByChain(chainKey, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!chainKey && chainKey.length > 0,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|