@sodax/dapp-kit 2.1.0 → 2.2.0-rc.2
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 +2 -0
- package/dist/index.d.ts +120 -8
- package/dist/index.mjs +126 -61
- package/package.json +3 -3
- package/src/hooks/_apiKeyWire.test.ts +487 -0
- package/src/hooks/_mutationContract.test.ts +6 -0
- package/src/hooks/backend/index.ts +4 -0
- package/src/hooks/backend/useBackendOracleCandles.ts +47 -0
- package/src/hooks/backend/useBackendOracleMarkets.ts +30 -0
- package/src/hooks/bitcoin/useFundTradingWallet.ts +2 -1
- package/src/hooks/bitcoin/useRadfiWithdraw.ts +2 -1
- package/src/hooks/bridge/useBridge.test.ts +104 -0
- package/src/hooks/bridge/useBridge.ts +2 -2
- package/src/hooks/dex/useDexDeposit.ts +2 -1
- package/src/hooks/dex/useDexWithdraw.ts +2 -1
- package/src/hooks/dex/useSupplyLiquidity.ts +2 -1
- package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +2 -2
- package/src/hooks/migrate/useMigrateBaln.ts +2 -2
- package/src/hooks/migrate/useMigrateIcxToSoda.ts +2 -2
- package/src/hooks/migrate/useMigratebnUSD.ts +2 -2
- package/src/hooks/migrate/useRevertMigrateSodaToIcx.ts +2 -2
- package/src/hooks/mm/useBorrow.ts +2 -4
- package/src/hooks/mm/useRepay.ts +2 -1
- package/src/hooks/mm/useSupply.ts +2 -1
- package/src/hooks/mm/useWithdraw.ts +2 -4
- package/src/hooks/partner/useFeeClaimWithdraw.ts +2 -1
- package/src/hooks/recovery/useWithdrawHubAsset.ts +2 -1
- package/src/hooks/shared/index.ts +3 -0
- package/src/hooks/shared/invalidateBalances.test.ts +71 -0
- package/src/hooks/shared/invalidateBalances.ts +25 -0
- package/src/hooks/shared/retryUnlessAuthFailure.test.ts +38 -0
- package/src/hooks/shared/retryUnlessAuthFailure.ts +18 -0
- package/src/hooks/shared/useBalances.test.ts +118 -0
- package/src/hooks/shared/useBalances.ts +81 -0
- package/src/hooks/shared/useEstablishTrustline.test.ts +2 -0
- package/src/hooks/shared/useEstablishTrustline.ts +2 -1
- package/src/hooks/shared/useStellarGate.test.ts +1 -1
- package/src/hooks/sponsoring/useActivateStellarAccount.test.ts +6 -4
- package/src/hooks/sponsoring/useActivateStellarAccount.ts +2 -1
- package/src/hooks/sponsoring/useActivateStellarAccount.wire.test.ts +109 -0
- package/src/hooks/sponsoring/useSponsorConfig.test.ts +8 -2
- package/src/hooks/sponsoring/useStellarAccountActive.test.ts +2 -2
- package/src/hooks/sponsoring/useStellarAccountStatus.test.ts +8 -2
- package/src/hooks/staking/useClaim.ts +2 -1
- package/src/hooks/staking/useInstantUnstake.ts +2 -1
- package/src/hooks/staking/useStake.ts +2 -1
- package/src/hooks/swap/useDetailedStatus.test.ts +2 -2
- package/src/hooks/swap/useSwap.test.ts +128 -0
- package/src/hooks/swap/useSwap.ts +2 -2
- 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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// packages/dapp-kit/src/hooks/swap/useSwap.ts
|
|
2
2
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { invalidateBalances } from '../shared/invalidateBalances.js';
|
|
3
4
|
import type { SpokeChainKey, SwapActionParams, SwapResponse } from '@sodax/sdk';
|
|
4
5
|
import { useQueryClient } from '@tanstack/react-query';
|
|
5
6
|
import type { MutationHookParams } from '../shared/types.js';
|
|
@@ -30,8 +31,7 @@ export function useSwap<K extends SpokeChainKey = SpokeChainKey>({
|
|
|
30
31
|
...mutationOptions,
|
|
31
32
|
mutationFn: async vars => unwrapResult(await sodax.swaps.swap({ ...vars, raw: false })),
|
|
32
33
|
onSuccess: async (data, vars, ctx) => {
|
|
33
|
-
queryClient
|
|
34
|
-
queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', vars.params.dstChainKey] });
|
|
34
|
+
invalidateBalances(queryClient, vars.params.srcChainKey, vars.params.dstChainKey);
|
|
35
35
|
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
36
36
|
},
|
|
37
37
|
});
|
|
@@ -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
|
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `SodaxProvider` wire test for the consumer-supplied API key: `config.apiKey` handed to the
|
|
3
|
+
* provider must reach the backend as `x-api-key` on requests made through the provided instance.
|
|
4
|
+
*
|
|
5
|
+
* Covers the one key-supply surface the hook manifests' hand-built `Sodax` does not: the provider's
|
|
6
|
+
* own `new Sodax(config)`. No renderer — `useMemo` is patched to evaluate its factory so the
|
|
7
|
+
* component can be called as a plain function and the provided `sodax` read off the element's
|
|
8
|
+
* context value.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Sodax } from '@sodax/sdk';
|
|
12
|
+
import type { ReactElement } from 'react';
|
|
13
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
14
|
+
|
|
15
|
+
vi.mock('react', async importOriginal => ({
|
|
16
|
+
...(await importOriginal<typeof import('react')>()),
|
|
17
|
+
useMemo: <T>(factory: () => T): T => factory(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
const { SodaxProvider } = await import('./SodaxProvider.js');
|
|
21
|
+
|
|
22
|
+
const fetchMock = vi.fn<typeof globalThis.fetch>(
|
|
23
|
+
async () => new Response('{}', { status: 200, headers: { 'content-type': 'application/json' } }),
|
|
24
|
+
);
|
|
25
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
26
|
+
|
|
27
|
+
describe('SodaxProvider — config.apiKey on the wire', () => {
|
|
28
|
+
it('provides a Sodax whose backend requests carry x-api-key from the provider config', async () => {
|
|
29
|
+
const element = SodaxProvider({ config: { apiKey: 'provider-key', logger: 'silent' }, children: null });
|
|
30
|
+
|
|
31
|
+
const { sodax } = (element as ReactElement<{ value: { sodax: Sodax } }>).props.value;
|
|
32
|
+
const result = await sodax.api.getAllConfig();
|
|
33
|
+
|
|
34
|
+
expect(result.ok).toBe(true);
|
|
35
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
36
|
+
const [url, init] = fetchMock.mock.calls[0] ?? [];
|
|
37
|
+
expect(new URL(String(url)).pathname).toBe('/v1/be/config/all');
|
|
38
|
+
expect(init?.method).toBe('GET');
|
|
39
|
+
expect(new Headers(init?.headers).get('x-api-key')).toBe('provider-key');
|
|
40
|
+
});
|
|
41
|
+
});
|