@sodax/dapp-kit 1.5.7-beta → 2.0.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.
Files changed (202) hide show
  1. package/README.md +300 -422
  2. package/ai-exported/AGENTS.md +134 -0
  3. package/ai-exported/integration/README.md +49 -0
  4. package/ai-exported/integration/ai-rules.md +79 -0
  5. package/ai-exported/integration/architecture.md +274 -0
  6. package/ai-exported/integration/features/README.md +29 -0
  7. package/ai-exported/integration/features/auxiliary-services.md +169 -0
  8. package/ai-exported/integration/features/bitcoin.md +87 -0
  9. package/ai-exported/integration/features/bridge.md +91 -0
  10. package/ai-exported/integration/features/dex.md +152 -0
  11. package/ai-exported/integration/features/migration.md +118 -0
  12. package/ai-exported/integration/features/money-market.md +116 -0
  13. package/ai-exported/integration/features/staking.md +123 -0
  14. package/ai-exported/integration/features/swap.md +101 -0
  15. package/ai-exported/integration/quickstart.md +187 -0
  16. package/ai-exported/integration/recipes/README.md +136 -0
  17. package/ai-exported/integration/recipes/backend-queries.md +157 -0
  18. package/ai-exported/integration/recipes/bitcoin.md +193 -0
  19. package/ai-exported/integration/recipes/bridge.md +174 -0
  20. package/ai-exported/integration/recipes/dex.md +204 -0
  21. package/ai-exported/integration/recipes/invalidations.md +115 -0
  22. package/ai-exported/integration/recipes/migration.md +212 -0
  23. package/ai-exported/integration/recipes/money-market.md +206 -0
  24. package/ai-exported/integration/recipes/mutation-error-handling.md +118 -0
  25. package/ai-exported/integration/recipes/observability.md +93 -0
  26. package/ai-exported/integration/recipes/setup.md +144 -0
  27. package/ai-exported/integration/recipes/staking.md +202 -0
  28. package/ai-exported/integration/recipes/swap.md +272 -0
  29. package/ai-exported/integration/recipes/wallet-connectivity.md +101 -0
  30. package/ai-exported/integration/reference/README.md +12 -0
  31. package/ai-exported/integration/reference/glossary.md +188 -0
  32. package/ai-exported/integration/reference/hooks-index.md +194 -0
  33. package/ai-exported/integration/reference/public-api.md +110 -0
  34. package/ai-exported/integration/reference/querykey-conventions.md +179 -0
  35. package/ai-exported/migration/README.md +60 -0
  36. package/ai-exported/migration/ai-rules.md +81 -0
  37. package/ai-exported/migration/breaking-changes/hook-signatures.md +233 -0
  38. package/ai-exported/migration/breaking-changes/querykey-conventions.md +108 -0
  39. package/ai-exported/migration/breaking-changes/result-handling.md +211 -0
  40. package/ai-exported/migration/breaking-changes/sdk-leakage.md +165 -0
  41. package/ai-exported/migration/checklist.md +89 -0
  42. package/ai-exported/migration/features/README.md +34 -0
  43. package/ai-exported/migration/features/auxiliary-services.md +114 -0
  44. package/ai-exported/migration/features/bitcoin.md +88 -0
  45. package/ai-exported/migration/features/bridge.md +123 -0
  46. package/ai-exported/migration/features/dex.md +101 -0
  47. package/ai-exported/migration/features/migration.md +120 -0
  48. package/ai-exported/migration/features/money-market.md +97 -0
  49. package/ai-exported/migration/features/staking.md +109 -0
  50. package/ai-exported/migration/features/swap.md +118 -0
  51. package/ai-exported/migration/recipes.md +188 -0
  52. package/ai-exported/migration/reference/README.md +15 -0
  53. package/ai-exported/migration/reference/deleted-hooks.md +110 -0
  54. package/ai-exported/migration/reference/error-shape-crosswalk.md +144 -0
  55. package/ai-exported/migration/reference/renamed-hooks.md +66 -0
  56. package/dist/index.cjs +2642 -0
  57. package/dist/index.cjs.map +1 -0
  58. package/dist/index.d.cts +1550 -0
  59. package/dist/index.d.ts +1020 -2051
  60. package/dist/index.mjs +1581 -1531
  61. package/dist/index.mjs.map +1 -1
  62. package/package.json +20 -10
  63. package/src/contexts/index.ts +0 -3
  64. package/src/hooks/_mutationContract.test.ts +99 -0
  65. package/src/hooks/backend/README.md +2 -2
  66. package/src/hooks/backend/index.ts +13 -13
  67. package/src/hooks/backend/unwrapResult.ts +1 -0
  68. package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +13 -45
  69. package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +29 -59
  70. package/src/hooks/backend/useBackendIntentByHash.ts +21 -47
  71. package/src/hooks/backend/useBackendIntentByTxHash.ts +23 -50
  72. package/src/hooks/backend/useBackendMoneyMarketAsset.ts +21 -54
  73. package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +30 -57
  74. package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +31 -58
  75. package/src/hooks/backend/useBackendMoneyMarketPosition.ts +22 -38
  76. package/src/hooks/backend/useBackendOrderbook.ts +27 -49
  77. package/src/hooks/backend/useBackendSubmitSwapTx.ts +30 -36
  78. package/src/hooks/backend/useBackendSubmitSwapTxStatus.ts +38 -58
  79. package/src/hooks/backend/useBackendUserIntents.ts +25 -63
  80. package/src/hooks/bitcoin/index.ts +9 -8
  81. package/src/hooks/bitcoin/useBitcoinBalance.ts +20 -5
  82. package/src/hooks/bitcoin/useExpiredUtxos.ts +26 -16
  83. package/src/hooks/bitcoin/useFundTradingWallet.ts +33 -30
  84. package/src/hooks/bitcoin/useRadfiAuth.ts +43 -40
  85. package/src/hooks/bitcoin/useRadfiSession.ts +53 -59
  86. package/src/hooks/bitcoin/useRadfiWithdraw.ts +35 -53
  87. package/src/hooks/bitcoin/useRenewUtxos.ts +30 -50
  88. package/src/hooks/bitcoin/useTradingWallet.ts +1 -1
  89. package/src/hooks/bitcoin/useTradingWalletBalance.ts +25 -14
  90. package/src/hooks/bridge/index.ts +5 -5
  91. package/src/hooks/bridge/useBridge.ts +29 -55
  92. package/src/hooks/bridge/useBridgeAllowance.ts +38 -38
  93. package/src/hooks/bridge/useBridgeApprove.ts +32 -57
  94. package/src/hooks/bridge/useGetBridgeableAmount.ts +23 -37
  95. package/src/hooks/bridge/useGetBridgeableTokens.ts +27 -50
  96. package/src/hooks/dex/index.ts +16 -16
  97. package/src/hooks/dex/useClaimRewards.ts +35 -54
  98. package/src/hooks/dex/useCreateDecreaseLiquidityParams.ts +7 -20
  99. package/src/hooks/dex/useCreateDepositParams.ts +7 -21
  100. package/src/hooks/dex/useCreateSupplyLiquidityParams.ts +13 -28
  101. package/src/hooks/dex/useCreateWithdrawParams.ts +7 -20
  102. package/src/hooks/dex/useDecreaseLiquidity.ts +40 -66
  103. package/src/hooks/dex/useDexAllowance.ts +29 -75
  104. package/src/hooks/dex/useDexApprove.ts +32 -43
  105. package/src/hooks/dex/useDexDeposit.ts +42 -49
  106. package/src/hooks/dex/useDexWithdraw.ts +32 -43
  107. package/src/hooks/dex/useLiquidityAmounts.ts +13 -82
  108. package/src/hooks/dex/usePoolBalances.ts +50 -72
  109. package/src/hooks/dex/usePoolData.ts +17 -43
  110. package/src/hooks/dex/usePools.ts +11 -38
  111. package/src/hooks/dex/usePositionInfo.ts +27 -62
  112. package/src/hooks/dex/useSupplyLiquidity.ts +80 -75
  113. package/src/hooks/index.ts +12 -10
  114. package/src/hooks/migrate/index.ts +13 -4
  115. package/src/hooks/migrate/useMigrateBaln.ts +42 -0
  116. package/src/hooks/migrate/useMigrateIcxToSoda.ts +44 -0
  117. package/src/hooks/migrate/useMigratebnUSD.ts +47 -0
  118. package/src/hooks/migrate/useMigrationAllowance.ts +76 -0
  119. package/src/hooks/migrate/useMigrationApprove.ts +66 -0
  120. package/src/hooks/migrate/useRevertMigrateSodaToIcx.ts +39 -0
  121. package/src/hooks/mm/index.ts +14 -12
  122. package/src/hooks/mm/useAToken.ts +25 -41
  123. package/src/hooks/mm/useATokensBalances.ts +29 -60
  124. package/src/hooks/mm/useBorrow.ts +38 -56
  125. package/src/hooks/mm/useMMAllowance.ts +37 -73
  126. package/src/hooks/mm/useMMApprove.ts +36 -43
  127. package/src/hooks/mm/useRepay.ts +33 -53
  128. package/src/hooks/mm/useReservesData.ts +12 -38
  129. package/src/hooks/mm/useReservesHumanized.ts +12 -31
  130. package/src/hooks/mm/useReservesList.ts +11 -31
  131. package/src/hooks/mm/useReservesUsdFormat.ts +15 -35
  132. package/src/hooks/mm/useSupply.ts +45 -51
  133. package/src/hooks/mm/useUserFormattedSummary.ts +32 -84
  134. package/src/hooks/mm/useUserReservesData.ts +27 -77
  135. package/src/hooks/mm/useWithdraw.ts +38 -54
  136. package/src/hooks/partner/index.ts +6 -0
  137. package/src/hooks/partner/useApproveToken.ts +42 -0
  138. package/src/hooks/partner/useFeeClaimSwap.ts +38 -0
  139. package/src/hooks/partner/useFetchAssetsBalances.ts +37 -0
  140. package/src/hooks/partner/useGetAutoSwapPreferences.ts +37 -0
  141. package/src/hooks/partner/useIsTokenApproved.ts +39 -0
  142. package/src/hooks/partner/useSetSwapPreference.ts +50 -0
  143. package/src/hooks/provider/index.ts +1 -2
  144. package/src/hooks/provider/useHubProvider.ts +1 -1
  145. package/src/hooks/recovery/index.ts +2 -0
  146. package/src/hooks/recovery/useHubAssetBalances.ts +43 -0
  147. package/src/hooks/recovery/useWithdrawHubAsset.ts +48 -0
  148. package/src/hooks/shared/index.ts +10 -6
  149. package/src/hooks/shared/types.ts +77 -0
  150. package/src/hooks/shared/unwrapResult.ts +19 -0
  151. package/src/hooks/shared/useDeriveUserWalletAddress.ts +22 -40
  152. package/src/hooks/shared/useEstimateGas.ts +18 -15
  153. package/src/hooks/shared/useGetUserHubWalletAddress.ts +25 -26
  154. package/src/hooks/shared/useRequestTrustline.ts +28 -61
  155. package/src/hooks/shared/useSafeMutation.test.ts +43 -0
  156. package/src/hooks/shared/useSafeMutation.ts +68 -0
  157. package/src/hooks/shared/useSodaxContext.ts +1 -1
  158. package/src/hooks/shared/useStellarTrustlineCheck.ts +30 -64
  159. package/src/hooks/shared/useXBalances.test.ts +113 -0
  160. package/src/hooks/shared/useXBalances.ts +61 -0
  161. package/src/hooks/staking/index.ts +18 -18
  162. package/src/hooks/staking/useCancelUnstake.ts +30 -41
  163. package/src/hooks/staking/useClaim.ts +27 -36
  164. package/src/hooks/staking/useConvertedAssets.ts +24 -34
  165. package/src/hooks/staking/useInstantUnstake.ts +33 -40
  166. package/src/hooks/staking/useInstantUnstakeAllowance.ts +37 -45
  167. package/src/hooks/staking/useInstantUnstakeApprove.ts +42 -42
  168. package/src/hooks/staking/useInstantUnstakeRatio.ts +24 -41
  169. package/src/hooks/staking/useStake.ts +32 -37
  170. package/src/hooks/staking/useStakeAllowance.ts +30 -43
  171. package/src/hooks/staking/useStakeApprove.ts +40 -40
  172. package/src/hooks/staking/useStakeRatio.ts +24 -40
  173. package/src/hooks/staking/useStakingConfig.ts +14 -27
  174. package/src/hooks/staking/useStakingInfo.ts +30 -38
  175. package/src/hooks/staking/useUnstake.ts +29 -43
  176. package/src/hooks/staking/useUnstakeAllowance.ts +37 -44
  177. package/src/hooks/staking/useUnstakeApprove.ts +40 -43
  178. package/src/hooks/staking/useUnstakingInfo.ts +29 -41
  179. package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +31 -47
  180. package/src/hooks/swap/index.ts +8 -8
  181. package/src/hooks/swap/useCancelLimitOrder.ts +24 -41
  182. package/src/hooks/swap/useCancelSwap.ts +24 -33
  183. package/src/hooks/swap/useCreateLimitOrder.ts +29 -62
  184. package/src/hooks/swap/useQuote.ts +17 -43
  185. package/src/hooks/swap/useStatus.ts +22 -29
  186. package/src/hooks/swap/useSwap.ts +31 -49
  187. package/src/hooks/swap/useSwapAllowance.ts +38 -35
  188. package/src/hooks/swap/useSwapApprove.ts +48 -57
  189. package/src/index.ts +5 -3
  190. package/src/providers/SodaxProvider.tsx +17 -11
  191. package/src/providers/createSodaxQueryClient.ts +96 -0
  192. package/src/providers/index.ts +2 -1
  193. package/src/utils/dex-utils.ts +27 -5
  194. package/src/utils/index.ts +1 -1
  195. package/dist/index.d.mts +0 -2581
  196. package/dist/index.js +0 -2574
  197. package/dist/index.js.map +0 -1
  198. package/src/hooks/migrate/types.ts +0 -15
  199. package/src/hooks/migrate/useMigrate.tsx +0 -110
  200. package/src/hooks/migrate/useMigrationAllowance.tsx +0 -79
  201. package/src/hooks/migrate/useMigrationApprove.tsx +0 -129
  202. package/src/hooks/provider/useSpokeProvider.ts +0 -172
@@ -1,15 +0,0 @@
1
- import type { XToken } from '@sodax/types';
2
-
3
- export const MIGRATION_MODE_ICX_SODA = 'icxsoda';
4
- export const MIGRATION_MODE_BNUSD = 'bnusd';
5
-
6
- export type MigrationMode = typeof MIGRATION_MODE_ICX_SODA | typeof MIGRATION_MODE_BNUSD;
7
-
8
- export interface MigrationIntentParams {
9
- token: XToken | undefined;
10
- amount: string | undefined;
11
- sourceAddress: string | undefined;
12
- migrationMode?: MigrationMode;
13
- toToken?: XToken;
14
- destinationAddress?: string;
15
- }
@@ -1,110 +0,0 @@
1
- import { parseUnits } from 'viem';
2
- import {
3
- spokeChainConfig,
4
- type UnifiedBnUSDMigrateParams,
5
- type IconSpokeProvider,
6
- type SonicSpokeProvider,
7
- isLegacybnUSDToken,
8
- type SpokeProvider,
9
- } from '@sodax/sdk';
10
- import { type ChainId, ICON_MAINNET_CHAIN_ID } from '@sodax/types';
11
- import { useSodaxContext } from '../shared/useSodaxContext';
12
- import { useMutation, type UseMutationResult } from '@tanstack/react-query';
13
- import { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
14
-
15
- /**
16
- * Hook for executing migration operations between chains.
17
- *
18
- * This hook handles ICX/SODA and bnUSD migrations by accepting a spoke provider
19
- * and returning a mutation function that accepts migration parameters.
20
- *
21
- * @param {SpokeProvider} spokeProvider - The spoke provider to use for the migration
22
- * @returns {UseMutationResult} Mutation result object containing migration function and state
23
- *
24
- * @example
25
- * ```typescript
26
- * const { mutateAsync: migrate, isPending } = useMigrate(spokeProvider);
27
- *
28
- * const result = await migrate({
29
- * token: { address: "0x...", decimals: 18 },
30
- * amount: "100",
31
- * migrationMode: MIGRATION_MODE_ICX_SODA,
32
- * toToken: { address: "0x...", decimals: 18 },
33
- * destinationAddress: "0x..."
34
- * });
35
- * ```
36
- */
37
- export function useMigrate(
38
- spokeProvider: SpokeProvider | undefined,
39
- ): UseMutationResult<{ spokeTxHash: string; hubTxHash: `0x${string}` }, Error, MigrationIntentParams> {
40
- const { sodax } = useSodaxContext();
41
-
42
- return useMutation({
43
- mutationFn: async (params: MigrationIntentParams) => {
44
- const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
45
- const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
46
-
47
- if (!spokeProvider) {
48
- throw new Error('Spoke provider not found');
49
- }
50
-
51
- if (migrationMode === MIGRATION_MODE_ICX_SODA) {
52
- // ICX->SODA migration logic
53
- if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
54
- const params = {
55
- address: spokeChainConfig[ICON_MAINNET_CHAIN_ID].nativeToken,
56
- amount: amountToMigrate,
57
- to: destinationAddress as `0x${string}`,
58
- };
59
- const result = await sodax.migration.migrateIcxToSoda(params, spokeProvider as IconSpokeProvider, 30000);
60
- if (result.ok) {
61
- const [spokeTxHash, hubTxHash] = result.value;
62
- return { spokeTxHash, hubTxHash };
63
- }
64
- throw new Error('ICX to SODA migration failed. Please try again.');
65
- }
66
-
67
- // SODA->ICX migration
68
- const revertParams = {
69
- amount: amountToMigrate,
70
- to: destinationAddress as `hx${string}`,
71
- };
72
- const result = await sodax.migration.revertMigrateSodaToIcx(
73
- revertParams,
74
- spokeProvider as SonicSpokeProvider,
75
- 30000,
76
- );
77
- if (result.ok) {
78
- const [hubTxHash, spokeTxHash] = result.value;
79
- return { spokeTxHash, hubTxHash };
80
- }
81
- throw new Error('SODA to ICX migration failed. Please try again.');
82
- }
83
-
84
- if (migrationMode === MIGRATION_MODE_BNUSD) {
85
- // bnUSD migration logic - handle dynamic source/destination chains
86
- const params = {
87
- srcChainId: token?.xChainId as ChainId,
88
- dstChainId: toToken?.xChainId as ChainId,
89
- srcbnUSD: token?.address as string,
90
- dstbnUSD: toToken?.address as string,
91
- amount: amountToMigrate,
92
- to: destinationAddress as `hx${string}` | `0x${string}`,
93
- } satisfies UnifiedBnUSDMigrateParams;
94
-
95
- const result = await sodax.migration.migratebnUSD(params, spokeProvider, 30000);
96
- if (result.ok) {
97
- const [spokeTxHash, hubTxHash] = result.value;
98
- return { spokeTxHash, hubTxHash };
99
- }
100
-
101
- const errorMessage = isLegacybnUSDToken(token?.address as string)
102
- ? 'bnUSD migration failed. Please try again.'
103
- : 'bnUSD reverse migration failed. Please try again.';
104
- throw new Error(errorMessage);
105
- }
106
-
107
- throw new Error('Invalid migration mode');
108
- },
109
- });
110
- }
@@ -1,79 +0,0 @@
1
- import { useQuery, type UseQueryResult } from '@tanstack/react-query';
2
- import type { ChainId } from '@sodax/types';
3
- import { useSodaxContext } from '../shared/useSodaxContext';
4
- import { parseUnits } from 'viem';
5
- import type { IcxCreateRevertMigrationParams, UnifiedBnUSDMigrateParams, SpokeProvider } from '@sodax/sdk';
6
- import { ICON_MAINNET_CHAIN_ID } from '@sodax/types';
7
- import { MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
8
-
9
- /**
10
- * Hook for checking token allowance for migration operations.
11
- *
12
- * This hook verifies if the user has approved enough tokens for migration operations.
13
- * It handles both ICX/SODA and bnUSD migration allowance checks.
14
- *
15
- * @param {MigrationIntentParams} params - The parameters for the migration allowance check
16
- * @param {SpokeProvider} spokeProvider - The spoke provider to use for allowance checks
17
- *
18
- * @returns {UseQueryResult<boolean, Error>} A React Query result containing:
19
- * - data: Boolean indicating if allowance is sufficient
20
- * - isLoading: Loading state indicator
21
- * - error: Any error that occurred during the check
22
- *
23
- * @example
24
- * ```typescript
25
- * const { data: hasAllowed, isLoading } = useMigrationAllowance(params, spokeProvider);
26
- * ```
27
- */
28
- export function useMigrationAllowance(
29
- params: MigrationIntentParams | undefined,
30
- spokeProvider: SpokeProvider | undefined,
31
- ): UseQueryResult<boolean, Error> {
32
- const { sodax } = useSodaxContext();
33
-
34
- return useQuery({
35
- queryKey: ['migration-allowance', params],
36
- queryFn: async () => {
37
- if (!spokeProvider || !params) {
38
- return false;
39
- }
40
-
41
- const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
42
-
43
- // For ICON chain, no allowance is required (forward migrations)
44
- if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
45
- return true;
46
- }
47
-
48
- if (!spokeProvider) throw new Error('Spoke provider is required');
49
- const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
50
-
51
- let migrationParams: IcxCreateRevertMigrationParams | UnifiedBnUSDMigrateParams;
52
- if (migrationMode === MIGRATION_MODE_ICX_SODA) {
53
- migrationParams = {
54
- amount: amountToMigrate,
55
- to: destinationAddress as `hx${string}`,
56
- } satisfies IcxCreateRevertMigrationParams;
57
- } else {
58
- if (!toToken) throw new Error('Destination token is required for bnUSD migration');
59
-
60
- migrationParams = {
61
- srcChainId: token?.xChainId as ChainId,
62
- dstChainId: toToken?.xChainId as ChainId,
63
- srcbnUSD: token?.address as string,
64
- dstbnUSD: toToken?.address as string,
65
- amount: amountToMigrate,
66
- to: destinationAddress as `hx${string}` | `0x${string}`,
67
- } satisfies UnifiedBnUSDMigrateParams;
68
- }
69
-
70
- const allowance = await sodax.migration.isAllowanceValid(migrationParams, 'revert', spokeProvider);
71
- if (allowance.ok) {
72
- return allowance.value;
73
- }
74
- return false;
75
- },
76
- enabled: !!spokeProvider && !!params,
77
- refetchInterval: 2000,
78
- });
79
- }
@@ -1,129 +0,0 @@
1
- import { parseUnits } from 'viem';
2
- import { useCallback, useState, useRef, useEffect } from 'react';
3
- import type {
4
- IcxCreateRevertMigrationParams,
5
- UnifiedBnUSDMigrateParams,
6
- SpokeProvider,
7
- Result,
8
- ChainId,
9
- } from '@sodax/sdk';
10
- import { useSodaxContext } from '../shared/useSodaxContext';
11
- import { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
12
- import { useQueryClient } from '@tanstack/react-query';
13
-
14
- interface UseApproveReturn {
15
- approve: ({ params }: { params: MigrationIntentParams }) => Promise<boolean>;
16
- isLoading: boolean;
17
- error: Error | null;
18
- resetError: () => void;
19
- isApproved: boolean;
20
- }
21
-
22
- /**
23
- * Hook for approving token spending for migration actions
24
- * @param params The parameters for the migration approval
25
- * @param spokeProvider The spoke provider instance for the chain
26
- * @returns Object containing approve function, loading state, error state and reset function
27
- * @example
28
- * ```tsx
29
- * const { approve, isLoading, error } = useMigrationApprove(params, spokeProvider);
30
- *
31
- * // Approve tokens for migration
32
- * await approve({ params });
33
- * ```
34
- */
35
-
36
- export function useMigrationApprove(
37
- params: MigrationIntentParams | undefined,
38
- spokeProvider: SpokeProvider | undefined,
39
- ): UseApproveReturn {
40
- const { sodax } = useSodaxContext();
41
- const [isLoading, setIsLoading] = useState(false);
42
- const [error, setError] = useState<Error | null>(null);
43
- const [isApproved, setIsApproved] = useState(false);
44
- const queryClient = useQueryClient();
45
-
46
- // Track previous values to reset approval state when needed
47
- const prevTokenAddress = useRef<string | undefined>(undefined);
48
- const prevAmount = useRef<string | undefined>(undefined);
49
-
50
- useEffect(() => {
51
- if (prevTokenAddress.current !== params?.token?.address || prevAmount.current !== params?.amount) {
52
- setIsApproved(false);
53
- prevTokenAddress.current = params?.token?.address;
54
- prevAmount.current = params?.amount;
55
- }
56
- }, [params?.token?.address, params?.amount]);
57
-
58
- const approve = useCallback(
59
- async ({ params: approveParams }: { params: MigrationIntentParams }) => {
60
- try {
61
- setIsLoading(true);
62
- setError(null);
63
-
64
- if (!spokeProvider) {
65
- throw new Error('Spoke provider not found');
66
- }
67
- if (!approveParams) {
68
- throw new Error('Migration intent not found');
69
- }
70
-
71
- const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = approveParams;
72
- const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
73
-
74
- let result: Result<string, unknown>;
75
- if (migrationMode === MIGRATION_MODE_ICX_SODA) {
76
- // ICX/SODA migration approval
77
- const revertParams = {
78
- amount: amountToMigrate,
79
- to: destinationAddress as `hx${string}`,
80
- } satisfies IcxCreateRevertMigrationParams;
81
-
82
- result = await sodax.migration.approve(revertParams, 'revert', spokeProvider, false);
83
- } else if (migrationMode === MIGRATION_MODE_BNUSD) {
84
- // bnUSD migration approval
85
- if (!toToken) throw new Error('Destination token is required for bnUSD migration');
86
-
87
- const migrationParams = {
88
- srcChainId: token?.xChainId as ChainId,
89
- dstChainId: toToken?.xChainId as ChainId,
90
- srcbnUSD: token?.address as string,
91
- dstbnUSD: toToken?.address as string,
92
- amount: amountToMigrate,
93
- to: destinationAddress as `hx${string}` | `0x${string}`,
94
- } satisfies UnifiedBnUSDMigrateParams;
95
-
96
- result = await sodax.migration.approve(migrationParams, 'revert', spokeProvider, false);
97
- } else {
98
- throw new Error('Invalid migration mode');
99
- }
100
- if (!result.ok) {
101
- throw new Error('Failed to approve tokens');
102
- }
103
-
104
- setIsApproved(true);
105
- queryClient.invalidateQueries({ queryKey: ['migration-allowance', params] });
106
- return result.ok;
107
- } catch (err) {
108
- const error = err instanceof Error ? err : new Error('An unknown error occurred');
109
- setError(error);
110
- throw error;
111
- } finally {
112
- setIsLoading(false);
113
- }
114
- },
115
- [spokeProvider, sodax, queryClient, params],
116
- );
117
-
118
- const resetError = useCallback(() => {
119
- setError(null);
120
- }, []);
121
-
122
- return {
123
- approve,
124
- isLoading,
125
- error,
126
- resetError,
127
- isApproved,
128
- };
129
- }
@@ -1,172 +0,0 @@
1
- import { useSodaxContext } from '@/index';
2
- import {
3
- BitcoinSpokeProvider,
4
- EvmSpokeProvider,
5
- spokeChainConfig,
6
- type SuiSpokeChainConfig,
7
- SuiSpokeProvider,
8
- type EvmSpokeChainConfig,
9
- IconSpokeProvider,
10
- type IconSpokeChainConfig,
11
- InjectiveSpokeProvider,
12
- type InjectiveSpokeChainConfig,
13
- StellarSpokeProvider,
14
- type StellarSpokeChainConfig,
15
- type SpokeProvider,
16
- type IWalletProvider,
17
- SolanaSpokeProvider,
18
- type SolanaChainConfig,
19
- SONIC_MAINNET_CHAIN_ID,
20
- SonicSpokeProvider,
21
- type SonicSpokeChainConfig,
22
- NearSpokeProvider,
23
- type NearSpokeChainConfig,
24
- StacksSpokeProvider,
25
- type StacksSpokeChainConfig,
26
- } from '@sodax/sdk';
27
- import type {
28
- BitcoinSpokeChainConfig,
29
- BitcoinRpcConfig,
30
- IBitcoinWalletProvider,
31
- IEvmWalletProvider,
32
- IIconWalletProvider,
33
- ISuiWalletProvider,
34
- SpokeChainId,
35
- IInjectiveWalletProvider,
36
- IStellarWalletProvider,
37
- ISolanaWalletProvider,
38
- INearWalletProvider,
39
- IStacksWalletProvider,
40
- } from '@sodax/types';
41
- import { useMemo } from 'react';
42
-
43
- /**
44
- * Hook to get the appropriate spoke provider based on the chain type.
45
- * Supports EVM, SUI, ICON and INJECTIVE chains.
46
- *
47
- * @param {SpokeChainId | undefined} spokeChainId - The spoke chain ID to get the provider for
48
- * @param {IWalletProvider | undefined} walletProvider - The wallet provider to use
49
- * @returns {SpokeProvider | undefined} The appropriate spoke provider instance for the given chain ID, or undefined if invalid/unsupported
50
- *
51
- * @example
52
- * ```tsx
53
- * // Using a specific SpokeChainId and wallet provider
54
- * const spokeProvider = useSpokeProvider(spokeChainId, walletProvider);
55
- * ```
56
- */
57
- export function useSpokeProvider(
58
- spokeChainId: SpokeChainId | undefined,
59
- walletProvider?: IWalletProvider | undefined,
60
- ): SpokeProvider | undefined {
61
- const { rpcConfig } = useSodaxContext();
62
- const xChainType = spokeChainId ? spokeChainConfig[spokeChainId]?.chain.type : undefined;
63
-
64
- const spokeProvider = useMemo(() => {
65
- if (!walletProvider) return undefined;
66
- if (!spokeChainId) return undefined;
67
- if (!xChainType) return undefined;
68
- if (!rpcConfig) return undefined;
69
-
70
- if (xChainType === 'BITCOIN') {
71
- const bitcoinConfig = spokeChainConfig[spokeChainId] as BitcoinSpokeChainConfig;
72
- const btcRpcOverride = rpcConfig[spokeChainId] as BitcoinRpcConfig | undefined;
73
- return new BitcoinSpokeProvider(
74
- walletProvider as IBitcoinWalletProvider,
75
- bitcoinConfig,
76
- {
77
- url: btcRpcOverride?.radfiApiUrl || bitcoinConfig.radfiApiUrl,
78
- apiKey: bitcoinConfig.radfiApiKey,
79
- umsUrl: btcRpcOverride?.radfiUmsUrl || bitcoinConfig.radfiUmsUrl,
80
- },
81
- 'TRADING',
82
- btcRpcOverride?.rpcUrl || bitcoinConfig.rpcUrl,
83
- );
84
- }
85
-
86
- if (xChainType === 'EVM') {
87
- if (spokeChainId === SONIC_MAINNET_CHAIN_ID) {
88
- return new SonicSpokeProvider(
89
- walletProvider as IEvmWalletProvider,
90
- spokeChainConfig[spokeChainId] as SonicSpokeChainConfig,
91
- );
92
- }
93
- // EVM RPC: flat RpcConfig keyed by chain id (same shape as app's rpcConfig)
94
- const evmRpcUrl = rpcConfig[spokeChainId];
95
- return new EvmSpokeProvider(
96
- walletProvider as IEvmWalletProvider,
97
- spokeChainConfig[spokeChainId] as EvmSpokeChainConfig,
98
- typeof evmRpcUrl === 'string' ? evmRpcUrl : undefined,
99
- );
100
- }
101
-
102
- if (xChainType === 'SUI') {
103
- return new SuiSpokeProvider(
104
- spokeChainConfig[spokeChainId] as SuiSpokeChainConfig,
105
- walletProvider as ISuiWalletProvider,
106
- );
107
- }
108
-
109
- if (xChainType === 'ICON') {
110
- return new IconSpokeProvider(
111
- walletProvider as IIconWalletProvider,
112
- spokeChainConfig[spokeChainId] as IconSpokeChainConfig,
113
- );
114
- }
115
-
116
- if (xChainType === 'INJECTIVE') {
117
- return new InjectiveSpokeProvider(
118
- spokeChainConfig[spokeChainId] as InjectiveSpokeChainConfig,
119
- walletProvider as IInjectiveWalletProvider,
120
- );
121
- }
122
-
123
- if (xChainType === 'STELLAR') {
124
- const stellarConfig = spokeChainConfig[spokeChainId] as StellarSpokeChainConfig;
125
- return new StellarSpokeProvider(
126
- walletProvider as IStellarWalletProvider,
127
- stellarConfig,
128
- rpcConfig.stellar
129
- ? rpcConfig.stellar
130
- : {
131
- horizonRpcUrl: stellarConfig.horizonRpcUrl,
132
- sorobanRpcUrl: stellarConfig.sorobanRpcUrl,
133
- },
134
- );
135
- }
136
-
137
- if (xChainType === 'SOLANA') {
138
- return new SolanaSpokeProvider(
139
- walletProvider as ISolanaWalletProvider,
140
- rpcConfig.solana
141
- ? ({
142
- ...spokeChainConfig[spokeChainId],
143
- rpcUrl: rpcConfig.solana,
144
- } as SolanaChainConfig)
145
- : (spokeChainConfig[spokeChainId] as SolanaChainConfig),
146
- );
147
- }
148
-
149
- if (xChainType === 'NEAR') {
150
- return new NearSpokeProvider(
151
- walletProvider as INearWalletProvider,
152
- spokeChainConfig[spokeChainId] as NearSpokeChainConfig,
153
- );
154
- }
155
-
156
- if (xChainType === 'STACKS') {
157
- return new StacksSpokeProvider(
158
- rpcConfig[spokeChainId]
159
- ? ({
160
- ...spokeChainConfig[spokeChainId],
161
- rpcUrl: rpcConfig[spokeChainId],
162
- } as StacksSpokeChainConfig)
163
- : (spokeChainConfig[spokeChainId] as StacksSpokeChainConfig),
164
- walletProvider as IStacksWalletProvider,
165
- );
166
- }
167
-
168
- return undefined;
169
- }, [spokeChainId, xChainType, walletProvider, rpcConfig]);
170
-
171
- return spokeProvider;
172
- }