@sodax/dapp-kit 2.2.0-rc.2 → 2.2.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/index.d.ts +505 -2
  2. package/dist/index.mjs +608 -3
  3. package/package.json +2 -2
  4. package/src/hooks/_apiKeyWire.test.ts +383 -17
  5. package/src/hooks/_mutationContract.test.ts +7 -0
  6. package/src/hooks/index.ts +1 -0
  7. package/src/hooks/leverageYieldApi/index.ts +55 -0
  8. package/src/hooks/leverageYieldApi/useLeverageYieldApiAllowance.ts +50 -0
  9. package/src/hooks/leverageYieldApi/useLeverageYieldApiApprove.ts +37 -0
  10. package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.test.ts +176 -0
  11. package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.ts +83 -0
  12. package/src/hooks/leverageYieldApi/useLeverageYieldApiApr.ts +41 -0
  13. package/src/hooks/leverageYieldApi/useLeverageYieldApiAsset.ts +41 -0
  14. package/src/hooks/leverageYieldApi/useLeverageYieldApiCancelIntent.ts +37 -0
  15. package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateDepositIntent.ts +37 -0
  16. package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateWithdrawIntent.ts +37 -0
  17. package/src/hooks/leverageYieldApi/useLeverageYieldApiDeadline.ts +38 -0
  18. package/src/hooks/leverageYieldApi/useLeverageYieldApiDepositQuote.ts +57 -0
  19. package/src/hooks/leverageYieldApi/useLeverageYieldApiEffectiveApr.ts +41 -0
  20. package/src/hooks/leverageYieldApi/useLeverageYieldApiEstimateGas.ts +41 -0
  21. package/src/hooks/leverageYieldApi/useLeverageYieldApiFilledIntent.ts +40 -0
  22. package/src/hooks/leverageYieldApi/useLeverageYieldApiIntent.ts +40 -0
  23. package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentExtraData.ts +42 -0
  24. package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentHash.ts +41 -0
  25. package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentPacket.ts +41 -0
  26. package/src/hooks/leverageYieldApi/useLeverageYieldApiLsdApr.ts +41 -0
  27. package/src/hooks/leverageYieldApi/useLeverageYieldApiMaxWithdraw.ts +43 -0
  28. package/src/hooks/leverageYieldApi/useLeverageYieldApiPartnerFee.ts +41 -0
  29. package/src/hooks/leverageYieldApi/useLeverageYieldApiPosition.ts +41 -0
  30. package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewDeposit.ts +43 -0
  31. package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewRedeem.ts +43 -0
  32. package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewWithdraw.ts +43 -0
  33. package/src/hooks/leverageYieldApi/useLeverageYieldApiShareBalance.ts +43 -0
  34. package/src/hooks/leverageYieldApi/useLeverageYieldApiSolverFee.ts +41 -0
  35. package/src/hooks/leverageYieldApi/useLeverageYieldApiStatus.ts +48 -0
  36. package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitIntent.ts +36 -0
  37. package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTx.ts +37 -0
  38. package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTxStatus.ts +60 -0
  39. package/src/hooks/leverageYieldApi/useLeverageYieldApiTotalAssets.ts +41 -0
  40. package/src/hooks/leverageYieldApi/useLeverageYieldApiVault.ts +41 -0
  41. package/src/hooks/leverageYieldApi/useLeverageYieldApiVaults.ts +36 -0
  42. package/src/hooks/leverageYieldApi/useLeverageYieldApiWithdrawQuote.ts +56 -0
  43. package/src/hooks/swapsApi/useSwapsApiApproveAndBroadcast.test.ts +5 -1
  44. package/src/utils/approvalPlan.ts +4 -1
@@ -0,0 +1,36 @@
1
+ import type { SubmitIntentRequestV2, SubmitIntentResponseV2, RequestOverrideConfig } from '@sodax/sdk';
2
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
3
+ import { unwrapResult } from '../shared/unwrapResult.js';
4
+ import type { MutationHookParams } from '../shared/types.js';
5
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
6
+
7
+ /**
8
+ * Mutation variables for {@link useLeverageYieldApiSubmitIntent}. The per-request `apiConfig` override belongs here
9
+ * rather than at the hook level — different calls in the same component can target different
10
+ * endpoints without re-rendering.
11
+ */
12
+ export type UseLeverageYieldApiSubmitIntentVars = {
13
+ body: SubmitIntentRequestV2;
14
+ apiConfig?: RequestOverrideConfig;
15
+ };
16
+
17
+ /**
18
+ * React hook to submit the broadcast intent tx to the relay via the leverage-yield API — `sodax.api.leverageYield.submitIntent`.
19
+ */
20
+ export const useLeverageYieldApiSubmitIntent = ({
21
+ mutationOptions,
22
+ }: MutationHookParams<SubmitIntentResponseV2, UseLeverageYieldApiSubmitIntentVars> = {}): SafeUseMutationResult<
23
+ SubmitIntentResponseV2,
24
+ Error,
25
+ UseLeverageYieldApiSubmitIntentVars
26
+ > => {
27
+ const { sodax } = useSodaxContext();
28
+
29
+ return useSafeMutation<SubmitIntentResponseV2, Error, UseLeverageYieldApiSubmitIntentVars>({
30
+ mutationKey: ['leverageYieldApi', 'submitIntent'],
31
+ retry: false,
32
+ ...mutationOptions,
33
+ mutationFn: async ({ body, apiConfig }): Promise<SubmitIntentResponseV2> =>
34
+ unwrapResult(await sodax.api.leverageYield.submitIntent(body, apiConfig)),
35
+ });
36
+ };
@@ -0,0 +1,37 @@
1
+ import type { LeverageYieldSubmitTxRequestV2, SubmitTxResponseV2, RequestOverrideConfig } from '@sodax/sdk';
2
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
3
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
4
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
+ import type { MutationHookParams } from '../shared/types.js';
6
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
7
+
8
+ /**
9
+ * Mutation variables for {@link useLeverageYieldApiSubmitTx}. The per-request `apiConfig` override belongs here
10
+ * rather than at the hook level — different calls in the same component can target different
11
+ * endpoints without re-rendering.
12
+ */
13
+ export type UseLeverageYieldApiSubmitTxVars = {
14
+ request: LeverageYieldSubmitTxRequestV2;
15
+ apiConfig?: RequestOverrideConfig;
16
+ };
17
+
18
+ /**
19
+ * React hook to submit a vault-swap transaction to be processed (relay, post-execution, etc.) via the leverage-yield API — `sodax.api.leverageYield.submitTx`.
20
+ */
21
+ export const useLeverageYieldApiSubmitTx = ({
22
+ mutationOptions,
23
+ }: MutationHookParams<SubmitTxResponseV2, UseLeverageYieldApiSubmitTxVars> = {}): SafeUseMutationResult<
24
+ SubmitTxResponseV2,
25
+ Error,
26
+ UseLeverageYieldApiSubmitTxVars
27
+ > => {
28
+ const { sodax } = useSodaxContext();
29
+
30
+ return useSafeMutation<SubmitTxResponseV2, Error, UseLeverageYieldApiSubmitTxVars>({
31
+ mutationKey: ['leverageYieldApi', 'submitTx'],
32
+ retry: retryUnlessAuthFailure,
33
+ ...mutationOptions,
34
+ mutationFn: async ({ request, apiConfig }): Promise<SubmitTxResponseV2> =>
35
+ unwrapResult(await sodax.api.leverageYield.submitTx(request, apiConfig)),
36
+ });
37
+ };
@@ -0,0 +1,60 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import { isAuthFailure, type RequestOverrideConfig, type SubmitTxStatusResponseV2 } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
5
+ import { unwrapResult } from '../shared/unwrapResult.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
7
+
8
+ export type UseLeverageYieldApiSubmitTxStatusParams = ReadHookParams<
9
+ SubmitTxStatusResponseV2 | undefined,
10
+ {
11
+ txHash: string | undefined;
12
+ srcChainKey?: string;
13
+ apiConfig?: RequestOverrideConfig;
14
+ }
15
+ >;
16
+
17
+ /**
18
+ * React hook for polling the processing status of a submitted vault-swap transaction via the
19
+ * leverage-yield API — `sodax.api.leverageYield.getSubmitTxStatus`. Both `txHash` and `srcChainKey`
20
+ * are required for the query to run.
21
+ *
22
+ * @example
23
+ * const { data: status } = useLeverageYieldApiSubmitTxStatus({
24
+ * params: { txHash: '0x123...', srcChainKey: 'sonic' },
25
+ * });
26
+ *
27
+ * @remarks
28
+ * - Default refetch interval is 1 second; stops on 'solved' or 'failed' status, when the backend
29
+ * marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal, or once the
30
+ * backend rejects the API key (401/403 is terminal — a retry cannot fix it).
31
+ */
32
+ export const useLeverageYieldApiSubmitTxStatus = ({
33
+ params,
34
+ queryOptions,
35
+ }: UseLeverageYieldApiSubmitTxStatusParams = {}): UseQueryResult<SubmitTxStatusResponseV2 | undefined, Error> => {
36
+ const { sodax } = useSodaxContext();
37
+ const txHash = params?.txHash;
38
+ const srcChainKey = params?.srcChainKey;
39
+ const apiConfig = params?.apiConfig;
40
+
41
+ return useQuery({
42
+ queryKey: ['leverageYieldApi', 'submitTx', 'status', txHash, srcChainKey],
43
+ queryFn: async (): Promise<SubmitTxStatusResponseV2 | undefined> => {
44
+ if (!txHash || !srcChainKey) return undefined;
45
+ return unwrapResult(await sodax.api.leverageYield.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
46
+ },
47
+ enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
48
+ retry: retryUnlessAuthFailure,
49
+ refetchInterval: query => {
50
+ // `retry` bounds attempts within a tick, not the interval itself — so stop it here too.
51
+ if (isAuthFailure(query.state.error)) return false;
52
+ const data = query.state.data?.data;
53
+ // `abandonedAt` is terminal even when `status` is still non-terminal — same rule as the
54
+ // SDK's pollBackendSubmitTx and the swaps-api status hook.
55
+ if (data?.status === 'solved' || data?.status === 'failed' || data?.abandonedAt) return false;
56
+ return 1000;
57
+ },
58
+ ...queryOptions,
59
+ });
60
+ };
@@ -0,0 +1,41 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { VaultTotalAssetsResponseV2, RequestOverrideConfig } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
5
+ import { unwrapResult } from '../shared/unwrapResult.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
7
+
8
+ export type UseLeverageYieldApiTotalAssetsParams = ReadHookParams<
9
+ VaultTotalAssetsResponseV2 | undefined,
10
+ {
11
+ vault: string | undefined;
12
+ apiConfig?: RequestOverrideConfig;
13
+ }
14
+ >;
15
+
16
+ /**
17
+ * React hook to fetch the total assets managed by the vault for a leverage-yield vault via the leverage-yield API —
18
+ * `sodax.api.leverageYield.getTotalAssets`.
19
+ *
20
+ * @example
21
+ * const { data } = useLeverageYieldApiTotalAssets({ params: { vault: '0x...' } });
22
+ */
23
+ export const useLeverageYieldApiTotalAssets = ({
24
+ params,
25
+ queryOptions,
26
+ }: UseLeverageYieldApiTotalAssetsParams = {}): UseQueryResult<VaultTotalAssetsResponseV2 | undefined, Error> => {
27
+ const { sodax } = useSodaxContext();
28
+ const vault = params?.vault;
29
+ const apiConfig = params?.apiConfig;
30
+
31
+ return useQuery({
32
+ queryKey: ['leverageYieldApi', 'totalAssets', vault],
33
+ queryFn: async (): Promise<VaultTotalAssetsResponseV2 | undefined> => {
34
+ if (!vault) return undefined;
35
+ return unwrapResult(await sodax.api.leverageYield.getTotalAssets({ vault }, apiConfig));
36
+ },
37
+ enabled: !!vault && vault.length > 0,
38
+ retry: retryUnlessAuthFailure,
39
+ ...queryOptions,
40
+ });
41
+ };
@@ -0,0 +1,41 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GetLeverageVaultResponseV2, RequestOverrideConfig } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
5
+ import { unwrapResult } from '../shared/unwrapResult.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
7
+
8
+ export type UseLeverageYieldApiVaultParams = ReadHookParams<
9
+ GetLeverageVaultResponseV2 | undefined,
10
+ {
11
+ name: string | undefined;
12
+ apiConfig?: RequestOverrideConfig;
13
+ }
14
+ >;
15
+
16
+ /**
17
+ * React hook to fetch a single vault descriptor by its lsoda* share-token name via the
18
+ * leverage-yield API — `sodax.api.leverageYield.getVault`.
19
+ *
20
+ * @example
21
+ * const { data: vault } = useLeverageYieldApiVault({ params: { name: 'lsodaWEETH' } });
22
+ */
23
+ export const useLeverageYieldApiVault = ({
24
+ params,
25
+ queryOptions,
26
+ }: UseLeverageYieldApiVaultParams = {}): UseQueryResult<GetLeverageVaultResponseV2 | undefined, Error> => {
27
+ const { sodax } = useSodaxContext();
28
+ const name = params?.name;
29
+ const apiConfig = params?.apiConfig;
30
+
31
+ return useQuery({
32
+ queryKey: ['leverageYieldApi', 'vault', name],
33
+ queryFn: async (): Promise<GetLeverageVaultResponseV2 | undefined> => {
34
+ if (!name) return undefined;
35
+ return unwrapResult(await sodax.api.leverageYield.getVault(name, apiConfig));
36
+ },
37
+ enabled: !!name && name.length > 0,
38
+ retry: retryUnlessAuthFailure,
39
+ ...queryOptions,
40
+ });
41
+ };
@@ -0,0 +1,36 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type { GetLeverageVaultsResponseV2, RequestOverrideConfig } from '@sodax/sdk';
3
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
4
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
5
+ import { unwrapResult } from '../shared/unwrapResult.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
7
+
8
+ export type UseLeverageYieldApiVaultsParams = ReadHookParams<
9
+ GetLeverageVaultsResponseV2,
10
+ {
11
+ apiConfig?: RequestOverrideConfig;
12
+ }
13
+ >;
14
+
15
+ /**
16
+ * React hook to fetch the registry of deployed leverage-yield vaults via the leverage-yield API —
17
+ * `sodax.api.leverageYield.getVaults`.
18
+ *
19
+ * @example
20
+ * const { data: vaults } = useLeverageYieldApiVaults();
21
+ */
22
+ export const useLeverageYieldApiVaults = ({
23
+ params,
24
+ queryOptions,
25
+ }: UseLeverageYieldApiVaultsParams = {}): UseQueryResult<GetLeverageVaultsResponseV2, Error> => {
26
+ const { sodax } = useSodaxContext();
27
+ const apiConfig = params?.apiConfig;
28
+
29
+ return useQuery<GetLeverageVaultsResponseV2, Error>({
30
+ queryKey: ['leverageYieldApi', 'vaults'],
31
+ queryFn: async (): Promise<GetLeverageVaultsResponseV2> =>
32
+ unwrapResult(await sodax.api.leverageYield.getVaults(apiConfig)),
33
+ retry: retryUnlessAuthFailure,
34
+ ...queryOptions,
35
+ });
36
+ };
@@ -0,0 +1,56 @@
1
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
+ import type {
3
+ LeverageYieldWithdrawQuoteRequestV2,
4
+ QuoteQueryV2,
5
+ QuoteResponseV2,
6
+ RequestOverrideConfig,
7
+ } from '@sodax/sdk';
8
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
9
+ import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
10
+ import { unwrapResult } from '../shared/unwrapResult.js';
11
+ import type { ReadHookParams } from '../shared/types.js';
12
+
13
+ export type UseLeverageYieldApiWithdrawQuoteParams = ReadHookParams<
14
+ QuoteResponseV2 | undefined,
15
+ {
16
+ body: LeverageYieldWithdrawQuoteRequestV2 | undefined;
17
+ query?: QuoteQueryV2;
18
+ apiConfig?: RequestOverrideConfig;
19
+ }
20
+ >;
21
+
22
+ /**
23
+ * React hook to quote a swap-style leverage-yield withdraw (lsoda* shares → any token) via the
24
+ * leverage-yield API — `sodax.api.leverageYield.getWithdrawQuote`. Pass `query.includeTxData = true`
25
+ * to also build the unsigned transaction (`srcAddress`/`dstAddress` then required in the body).
26
+ *
27
+ * @example
28
+ * const { data: quote } = useLeverageYieldApiWithdrawQuote({
29
+ * params: {
30
+ * body: {
31
+ * vault: '0x...', srcChainKey: 'sonic', tokenDst: '0x...', tokenDstChainKey: '0xa4b1.arbitrum',
32
+ * amount: '1000000000000000000', quoteType: 'exact_input',
33
+ * },
34
+ * },
35
+ * });
36
+ */
37
+ export const useLeverageYieldApiWithdrawQuote = ({
38
+ params,
39
+ queryOptions,
40
+ }: UseLeverageYieldApiWithdrawQuoteParams = {}): UseQueryResult<QuoteResponseV2 | undefined, Error> => {
41
+ const { sodax } = useSodaxContext();
42
+ const body = params?.body;
43
+ const query = params?.query;
44
+ const apiConfig = params?.apiConfig;
45
+
46
+ return useQuery({
47
+ queryKey: ['leverageYieldApi', 'withdrawQuote', body, query?.includeTxData ?? false],
48
+ queryFn: async (): Promise<QuoteResponseV2 | undefined> => {
49
+ if (!body) return undefined;
50
+ return unwrapResult(await sodax.api.leverageYield.getWithdrawQuote(body, query, apiConfig));
51
+ },
52
+ enabled: !!body,
53
+ retry: retryUnlessAuthFailure,
54
+ ...queryOptions,
55
+ });
56
+ };
@@ -8,7 +8,7 @@
8
8
  * mocked so `mutationFn` can be captured and driven directly.
9
9
  */
10
10
 
11
- import { ChainKeys } from '@sodax/sdk';
11
+ import { ChainKeys, getEvmViemChain } from '@sodax/sdk';
12
12
  import type { ApprovalProgress } from '../../utils/approvalPlan.js';
13
13
  import { beforeEach, describe, expect, it, vi } from 'vitest';
14
14
 
@@ -129,6 +129,10 @@ describe('useSwapsApiApproveAndBroadcast', () => {
129
129
 
130
130
  expect(result).toEqual({ approveTxHash: APPROVE_HASH });
131
131
  expect(walletProvider.sendTransaction).toHaveBeenCalledTimes(1);
132
+ // Backend-built approvals are chain-bound: the runner must pass the source chain's id.
133
+ expect(walletProvider.sendTransaction).toHaveBeenCalledWith(expect.anything(), {
134
+ expectedChainId: getEvmViemChain(ChainKeys.ARBITRUM_MAINNET).id,
135
+ });
132
136
  });
133
137
 
134
138
  it('works on the hub chain, not just EVM spokes', async () => {
@@ -9,6 +9,7 @@ import type {
9
9
  StellarRawTransaction,
10
10
  } from '@sodax/sdk';
11
11
  import {
12
+ getEvmViemChain,
12
13
  isApprovalSupportedChainKeyType,
13
14
  isEvmSpokeOnlyChainKeyType,
14
15
  isHubChainKeyType,
@@ -91,7 +92,9 @@ function resolveChainSender(
91
92
  const evm = walletProvider as IEvmWalletProvider;
92
93
 
93
94
  return {
94
- send: raw => evm.sendTransaction(raw as EvmRawTransaction),
95
+ // Chain-bind the send: the wallet broadcasts on ITS active chain, so refuse when that differs
96
+ // from the srcChainKey the user asked for. (Verifying the tx CONTENT is a separate concern.)
97
+ send: raw => evm.sendTransaction(raw as EvmRawTransaction, { expectedChainId: getEvmViemChain(srcChainKey).id }),
95
98
  confirm: async (hash, step) => {
96
99
  const receipt = await evm.waitForTransactionReceipt(hash as Hex);
97
100
  // Mined is not succeeded: a revert here would let the approve go out over an unmoved allowance.