@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,91 +1,96 @@
1
- import { useMutation, useQueryClient, type UseMutationResult } from '@tanstack/react-query';
2
- import type { HubTxHash, SpokeTxHash, SpokeProvider } from '@sodax/sdk';
3
- import { useSodaxContext } from '../shared/useSodaxContext';
4
- import type { UseCreateSupplyLiquidityParamsResult } from './useCreateSupplyLiquidityParams';
1
+ // packages/dapp-kit/src/hooks/dex/useSupplyLiquidity.ts
2
+ import type {
3
+ ClIncreaseLiquidityParams,
4
+ ClSupplyParams,
5
+ GetWalletProviderType,
6
+ SpokeChainKey,
7
+ TxHashPair,
8
+ } from '@sodax/sdk';
9
+ import { useQueryClient } from '@tanstack/react-query';
10
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
11
+ import type { UseCreateSupplyLiquidityParamsResult } from './useCreateSupplyLiquidityParams.js';
12
+ import type { MutationHookParams } from '../shared/types.js';
13
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
14
+ import { unwrapResult } from '../shared/unwrapResult.js';
5
15
 
6
- export type UseSupplyLiquidityProps = {
7
- params: UseCreateSupplyLiquidityParamsResult;
8
- spokeProvider: SpokeProvider;
16
+ /**
17
+ * Mutation variables for {@link useSupplyLiquidity}. Generic over `K extends SpokeChainKey`
18
+ * (defaults to the full union). The hook fans out internally to either
19
+ * `clService.increaseLiquidity` (when `params.tokenId` + `params.isValidPosition` are present) or
20
+ * `clService.supplyLiquidity` (mint a new position).
21
+ */
22
+ export type UseSupplyLiquidityVars<K extends SpokeChainKey = SpokeChainKey> = {
23
+ params: UseCreateSupplyLiquidityParamsResult & { srcChainKey: K; srcAddress: string };
24
+ walletProvider: GetWalletProviderType<K>;
25
+ /** Optional relay timeout in ms (default 60_000) */
26
+ timeout?: number;
9
27
  };
10
28
 
11
29
  /**
12
- * Hook for supplying liquidity to a pool.
13
- *
14
- * This hook handles both minting new positions and increasing liquidity in existing positions.
15
- * It applies slippage tolerance before calculating liquidity and handles the complete transaction flow.
16
- *
17
- * @param {SpokeProvider} spokeProvider - The spoke provider for the chain
18
- * @returns {UseMutationResult<void, Error, SupplyLiquidityParams>} Mutation result with supply function
19
- *
20
- * @example
21
- * ```typescript
22
- * const { mutateAsync: supplyLiquidity, isPending, error } = useSupplyLiquidity(spokeProvider);
30
+ * React hook for supplying liquidity to a concentrated-liquidity pool. If the input vars include a
31
+ * valid `tokenId` for an existing position, the hook calls `increaseLiquidity`; otherwise it mints
32
+ * a new position via `supplyLiquidity`.
23
33
  *
24
- * await supplyLiquidity({
25
- * poolData,
26
- * poolKey,
27
- * minPrice: '100',
28
- * maxPrice: '200',
29
- * liquidityToken0Amount: '10',
30
- * liquidityToken1Amount: '20',
31
- * slippageTolerance: '0.5',
32
- * });
33
- * ```
34
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
35
+ * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success.
34
36
  */
35
- export function useSupplyLiquidity(): UseMutationResult<[SpokeTxHash, HubTxHash], Error, UseSupplyLiquidityProps> {
37
+ export function useSupplyLiquidity<K extends SpokeChainKey = SpokeChainKey>({
38
+ mutationOptions,
39
+ }: MutationHookParams<TxHashPair, UseSupplyLiquidityVars<K>> = {}): SafeUseMutationResult<
40
+ TxHashPair,
41
+ Error,
42
+ UseSupplyLiquidityVars<K>
43
+ > {
36
44
  const { sodax } = useSodaxContext();
37
45
  const queryClient = useQueryClient();
38
46
 
39
- return useMutation({
40
- mutationFn: async ({ params, spokeProvider }: UseSupplyLiquidityProps) => {
41
- // Check if we're increasing an existing position or minting a new one
42
- if (params.tokenId && params.isValidPosition) {
43
- // Increase liquidity in existing position
44
- const increaseResult = await sodax.dex.clService.increaseLiquidity({
45
- params: {
46
- poolKey: params.poolKey,
47
- tokenId: BigInt(params.tokenId),
48
- tickLower: params.tickLower,
49
- tickUpper: params.tickUpper,
50
- liquidity: params.liquidity,
51
- amount0Max: params.amount0Max,
52
- amount1Max: params.amount1Max,
53
- sqrtPriceX96: params.sqrtPriceX96,
54
- },
55
- spokeProvider,
56
- });
47
+ return useSafeMutation<TxHashPair, Error, UseSupplyLiquidityVars<K>>({
48
+ mutationKey: ['dex', 'supplyLiquidity'],
49
+ ...mutationOptions,
50
+ mutationFn: async ({ params, walletProvider, timeout }) => {
51
+ const sharedParams = {
52
+ srcChainKey: params.srcChainKey,
53
+ srcAddress: params.srcAddress as ClSupplyParams<K>['srcAddress'],
54
+ poolKey: params.poolKey,
55
+ tickLower: params.tickLower,
56
+ tickUpper: params.tickUpper,
57
+ liquidity: params.liquidity,
58
+ amount0Max: params.amount0Max,
59
+ amount1Max: params.amount1Max,
60
+ sqrtPriceX96: params.sqrtPriceX96,
61
+ } satisfies ClSupplyParams<K>;
57
62
 
58
- if (!increaseResult.ok) {
59
- throw new Error(`Increase liquidity failed: ${increaseResult.error?.code || 'Unknown error'}`);
60
- }
61
-
62
- return increaseResult.value;
63
+ if (params.tokenId !== undefined && params.isValidPosition) {
64
+ const increaseParams: ClIncreaseLiquidityParams<K> = {
65
+ ...sharedParams,
66
+ tokenId: typeof params.tokenId === 'bigint' ? params.tokenId : BigInt(params.tokenId),
67
+ };
68
+ return unwrapResult(
69
+ await sodax.dex.clService.increaseLiquidity({ params: increaseParams, raw: false, walletProvider, timeout }),
70
+ );
63
71
  }
64
72
 
65
- // Mint new position
66
- const supplyResult = await sodax.dex.clService.supplyLiquidity({
67
- params: {
68
- poolKey: params.poolKey,
69
- tickLower: params.tickLower,
70
- tickUpper: params.tickUpper,
71
- liquidity: params.liquidity,
72
- amount0Max: params.amount0Max,
73
- amount1Max: params.amount1Max,
74
- sqrtPriceX96: params.sqrtPriceX96,
75
- },
76
- spokeProvider,
77
- });
78
-
79
- if (!supplyResult.ok) {
80
- throw new Error(`Supply liquidity failed: ${supplyResult.error?.code || 'Unknown error'}`);
81
- }
82
-
83
- return supplyResult.value;
73
+ return unwrapResult(
74
+ await sodax.dex.clService.supplyLiquidity({ params: sharedParams, raw: false, walletProvider, timeout }),
75
+ );
84
76
  },
85
- onSuccess: () => {
86
- // Invalidate relevant queries
87
- queryClient.invalidateQueries({ queryKey: ['dex', 'poolBalances'] });
88
- queryClient.invalidateQueries({ queryKey: ['dex', 'positionInfo'] });
77
+ onSuccess: async (data, vars, ctx) => {
78
+ const { params } = vars;
79
+ // Increase-liquidity branch knows the affected position — scope to it. Mint-new-position
80
+ // branch creates a fresh tokenId that's only known after the tx, so a bare invalidation is
81
+ // the right fallback (refetches all positions, including the new one once it lands).
82
+ if (params.tokenId !== undefined && params.isValidPosition) {
83
+ // `usePositionInfo` keys by string tokenId — coerce here to match its shape regardless of
84
+ // whether the caller passed bigint or string.
85
+ const tokenIdStr = String(params.tokenId);
86
+ queryClient.invalidateQueries({ queryKey: ['dex', 'positionInfo', tokenIdStr, params.poolKey] });
87
+ } else {
88
+ queryClient.invalidateQueries({ queryKey: ['dex', 'positionInfo'] });
89
+ }
90
+ queryClient.invalidateQueries({ queryKey: ['dex', 'poolData', params.poolKey] });
91
+ queryClient.invalidateQueries({ queryKey: ['dex', 'poolBalances', params.srcChainKey, params.srcAddress] });
92
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', params.srcChainKey] });
93
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
89
94
  },
90
95
  });
91
96
  }
@@ -1,10 +1,12 @@
1
- export * from './shared';
2
- export * from './provider';
3
- export * from './bitcoin';
4
- export * from './mm';
5
- export * from './swap';
6
- export * from './backend';
7
- export * from './bridge';
8
- export * from './staking';
9
- export * from './migrate';
10
- export * from './dex';
1
+ export * from './shared/index.js';
2
+ export * from './provider/index.js';
3
+ export * from './bitcoin/index.js';
4
+ export * from './mm/index.js';
5
+ export * from './swap/index.js';
6
+ export * from './backend/index.js';
7
+ export * from './bridge/index.js';
8
+ export * from './staking/index.js';
9
+ export * from './partner/index.js';
10
+ export * from './recovery/index.js';
11
+ export * from './migrate/index.js';
12
+ export * from './dex/index.js';
@@ -1,4 +1,13 @@
1
- export { useMigrate } from './useMigrate';
2
- export { useMigrationAllowance } from './useMigrationAllowance';
3
- export { useMigrationApprove } from './useMigrationApprove';
4
- export * from './types';
1
+ export { useMigrateIcxToSoda, type UseMigrateIcxToSodaVars } from './useMigrateIcxToSoda.js';
2
+ export {
3
+ useRevertMigrateSodaToIcx,
4
+ type UseRevertMigrateSodaToIcxVars,
5
+ } from './useRevertMigrateSodaToIcx.js';
6
+ export { useMigratebnUSD, type UseMigratebnUSDVars } from './useMigratebnUSD.js';
7
+ export { useMigrateBaln, type UseMigrateBalnVars } from './useMigrateBaln.js';
8
+ export { useMigrationApprove, type UseMigrationApproveVars } from './useMigrationApprove.js';
9
+ export {
10
+ useMigrationAllowance,
11
+ type UseMigrationAllowanceInputs,
12
+ type UseMigrationAllowanceParams,
13
+ } from './useMigrationAllowance.js';
@@ -0,0 +1,42 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useMigrateBaln.ts
2
+ import type { BalnMigrateAction, TxHashPair } from '@sodax/sdk';
3
+ import { ChainKeys } from '@sodax/sdk';
4
+ import { useQueryClient } from '@tanstack/react-query';
5
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
6
+ import type { MutationHookParams } from '../shared/types.js';
7
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
8
+ import { unwrapResult } from '../shared/unwrapResult.js';
9
+
10
+ export type UseMigrateBalnVars = Omit<BalnMigrateAction<false>, 'raw'>;
11
+
12
+ /**
13
+ * React hook for migrating BALN → SODA on the Sonic hub. Source chain is always Icon. Supports
14
+ * lockup periods (0–24 months) which multiply the SODA reward (0.5x–1.5x).
15
+ *
16
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
17
+ * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success.
18
+ */
19
+ export function useMigrateBaln({
20
+ mutationOptions,
21
+ }: MutationHookParams<TxHashPair, UseMigrateBalnVars> = {}): SafeUseMutationResult<
22
+ TxHashPair,
23
+ Error,
24
+ UseMigrateBalnVars
25
+ > {
26
+ const { sodax } = useSodaxContext();
27
+ const queryClient = useQueryClient();
28
+
29
+ return useSafeMutation<TxHashPair, Error, UseMigrateBalnVars>({
30
+ mutationKey: ['migrate', 'baln'],
31
+ ...mutationOptions,
32
+ mutationFn: async vars => unwrapResult(await sodax.migration.migrateBaln({ ...vars, raw: false })),
33
+ onSuccess: async (data, vars, ctx) => {
34
+ queryClient.invalidateQueries({ queryKey: ['migrate', 'allowance'] });
35
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', ChainKeys.ICON_MAINNET] });
36
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', ChainKeys.SONIC_MAINNET] });
37
+ // BALN with stake=true affects staking info too; cheap broad invalidation.
38
+ queryClient.invalidateQueries({ queryKey: ['staking', 'info'] });
39
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
40
+ },
41
+ });
42
+ }
@@ -0,0 +1,44 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useMigrateIcxToSoda.ts
2
+ import type { IcxMigrateAction, TxHashPair } from '@sodax/sdk';
3
+ import { ChainKeys } from '@sodax/sdk';
4
+ import { useQueryClient } from '@tanstack/react-query';
5
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
6
+ import type { MutationHookParams } from '../shared/types.js';
7
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
8
+ import { unwrapResult } from '../shared/unwrapResult.js';
9
+
10
+ /**
11
+ * Mutation variables for {@link useMigrateIcxToSoda}. Wraps `IcxMigrateAction<false>` with `raw`
12
+ * stripped — the hook always submits non-raw and unwraps the SDK Result.
13
+ */
14
+ export type UseMigrateIcxToSodaVars = Omit<IcxMigrateAction<false>, 'raw'>;
15
+
16
+ /**
17
+ * React hook for migrating ICX/wICX → SODA on the Sonic hub. Forward-only direction; for the
18
+ * reverse (SODA → ICX) use {@link useRevertMigrateSodaToIcx}.
19
+ *
20
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
21
+ * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success.
22
+ */
23
+ export function useMigrateIcxToSoda({
24
+ mutationOptions,
25
+ }: MutationHookParams<TxHashPair, UseMigrateIcxToSodaVars> = {}): SafeUseMutationResult<
26
+ TxHashPair,
27
+ Error,
28
+ UseMigrateIcxToSodaVars
29
+ > {
30
+ const { sodax } = useSodaxContext();
31
+ const queryClient = useQueryClient();
32
+
33
+ return useSafeMutation<TxHashPair, Error, UseMigrateIcxToSodaVars>({
34
+ mutationKey: ['migrate', 'icxToSoda'],
35
+ ...mutationOptions,
36
+ mutationFn: async vars => unwrapResult(await sodax.migration.migrateIcxToSoda({ ...vars, raw: false })),
37
+ onSuccess: async (data, vars, ctx) => {
38
+ queryClient.invalidateQueries({ queryKey: ['migrate', 'allowance'] });
39
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', vars.params.srcChainKey] });
40
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', ChainKeys.SONIC_MAINNET] });
41
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
42
+ },
43
+ });
44
+ }
@@ -0,0 +1,47 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useMigratebnUSD.ts
2
+ import type { SpokeChainKey, TxHashPair, UnifiedBnUSDMigrateAction } from '@sodax/sdk';
3
+ import { useQueryClient } from '@tanstack/react-query';
4
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
5
+ import type { MutationHookParams } from '../shared/types.js';
6
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
7
+ import { unwrapResult } from '../shared/unwrapResult.js';
8
+
9
+ /**
10
+ * Mutation variables for {@link useMigratebnUSD}. Generic over `K extends SpokeChainKey` (defaults
11
+ * to the full union). Bidirectional — handles both legacy → new and new → legacy bnUSD migration
12
+ * via the same SDK call; the SDK detects direction from the token addresses.
13
+ */
14
+ export type UseMigratebnUSDVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
15
+ UnifiedBnUSDMigrateAction<K, false>,
16
+ 'raw'
17
+ >;
18
+
19
+ /**
20
+ * React hook for migrating bnUSD between legacy and new formats across spoke chains via Sonic.
21
+ *
22
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
23
+ * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success.
24
+ */
25
+ export function useMigratebnUSD<K extends SpokeChainKey = SpokeChainKey>({
26
+ mutationOptions,
27
+ }: MutationHookParams<TxHashPair, UseMigratebnUSDVars<K>> = {}): SafeUseMutationResult<
28
+ TxHashPair,
29
+ Error,
30
+ UseMigratebnUSDVars<K>
31
+ > {
32
+ const { sodax } = useSodaxContext();
33
+ const queryClient = useQueryClient();
34
+
35
+ return useSafeMutation<TxHashPair, Error, UseMigratebnUSDVars<K>>({
36
+ mutationKey: ['migrate', 'bnUSD'],
37
+ ...mutationOptions,
38
+ mutationFn: async vars => unwrapResult(await sodax.migration.migratebnUSD({ ...vars, raw: false })),
39
+ onSuccess: async (data, vars, ctx) => {
40
+ const { params } = vars;
41
+ queryClient.invalidateQueries({ queryKey: ['migrate', 'allowance'] });
42
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', params.srcChainKey] });
43
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', params.dstChainKey] });
44
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
45
+ },
46
+ });
47
+ }
@@ -0,0 +1,76 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useMigrationAllowance.ts
2
+ import type {
3
+ BalnMigrateParams,
4
+ IcxCreateRevertMigrationParams,
5
+ IcxMigrateParams,
6
+ MigrationAction,
7
+ SpokeChainKey,
8
+ UnifiedBnUSDMigrateParams,
9
+ } from '@sodax/sdk';
10
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
11
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
12
+ import type { ReadHookParams } from '../shared/types.js';
13
+
14
+ /**
15
+ * Domain inputs for {@link useMigrationAllowance}. `params` is the underlying migration params
16
+ * (NOT the action wrapper — no `walletProvider`/`raw`); `action` is the `'migrate' | 'revert'`
17
+ * discriminator the SDK uses to pick the right allowance check path.
18
+ *
19
+ * For ICX forward migration (`migrate`, params={IcxMigrateParams}) and BALN migration the SDK
20
+ * always returns `true` (Icon has no ERC-20-style allowance) — but the hook still works there
21
+ * for code-uniformity at the call site.
22
+ */
23
+ export type UseMigrationAllowanceInputs<K extends SpokeChainKey = SpokeChainKey> = {
24
+ params:
25
+ | IcxMigrateParams
26
+ | IcxCreateRevertMigrationParams
27
+ | UnifiedBnUSDMigrateParams<K>
28
+ | BalnMigrateParams
29
+ | undefined;
30
+ action: MigrationAction | undefined;
31
+ };
32
+
33
+ export type UseMigrationAllowanceParams<K extends SpokeChainKey = SpokeChainKey> = ReadHookParams<
34
+ boolean,
35
+ UseMigrationAllowanceInputs<K>
36
+ >;
37
+
38
+ const REFETCH_INTERVAL_MS = 2_000;
39
+
40
+ /**
41
+ * React hook to check if migration spending is approved.
42
+ *
43
+ * Returns `false` when params are missing (so the call site can disable the action button).
44
+ * Hook lifecycle (`enabled`, `queryKey`, `queryFn`) is owned internally; consumers can override
45
+ * other React Query knobs via `queryOptions`.
46
+ */
47
+ export function useMigrationAllowance<K extends SpokeChainKey = SpokeChainKey>({
48
+ params,
49
+ queryOptions,
50
+ }: UseMigrationAllowanceParams<K> = {}): UseQueryResult<boolean, Error> {
51
+ const { sodax } = useSodaxContext();
52
+ const migrationParams = params?.params;
53
+ const action = params?.action;
54
+
55
+ return useQuery<boolean, Error>({
56
+ // Extract the (chain, owner, token, amount) tuple that actually scopes the allowance —
57
+ // raw-object keys break per Rule 4 (bigints) and churn on every render.
58
+ queryKey: [
59
+ 'migrate',
60
+ 'allowance',
61
+ action,
62
+ migrationParams?.srcChainKey,
63
+ migrationParams?.srcAddress,
64
+ migrationParams && 'srcbnUSD' in migrationParams ? migrationParams.srcbnUSD : undefined,
65
+ migrationParams?.amount?.toString(),
66
+ ],
67
+ queryFn: async () => {
68
+ if (!migrationParams || !action) return false;
69
+ const result = await sodax.migration.isAllowanceValid<K>(migrationParams, action);
70
+ return result.ok ? result.value : false;
71
+ },
72
+ enabled: !!migrationParams && !!action,
73
+ refetchInterval: REFETCH_INTERVAL_MS,
74
+ ...queryOptions,
75
+ });
76
+ }
@@ -0,0 +1,66 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useMigrationApprove.ts
2
+ import type {
3
+ IcxRevertMigrationAction,
4
+ MigrationAction,
5
+ SpokeChainKey,
6
+ TxReturnType,
7
+ UnifiedBnUSDMigrateAction,
8
+ } from '@sodax/sdk';
9
+ import { useQueryClient } from '@tanstack/react-query';
10
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
11
+ import type { MutationHookParams } from '../shared/types.js';
12
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
13
+ import { unwrapResult } from '../shared/unwrapResult.js';
14
+
15
+ /**
16
+ * Mutation variables for {@link useMigrationApprove}. The SDK's `approve` accepts a union of
17
+ * action wrappers (Icx-revert OR bnUSD bidirectional) plus a separate `'migrate' | 'revert'`
18
+ * discriminator. Both ride along in `TVars` here so the hook stays a pure pass-through.
19
+ */
20
+ export type UseMigrationApproveVars<K extends SpokeChainKey = SpokeChainKey> = (
21
+ | Omit<IcxRevertMigrationAction<false>, 'raw'>
22
+ | Omit<UnifiedBnUSDMigrateAction<K, false>, 'raw'>
23
+ ) & { action: MigrationAction };
24
+
25
+ /**
26
+ * React hook for approving token spending on a migration intent. Required before:
27
+ * - SODA → ICX revert (consumes SODA on Sonic via the user's hub router)
28
+ * - bnUSD migrations on EVM/Stellar source chains (consumes the source bnUSD via asset manager)
29
+ *
30
+ * NOT required for ICX → SODA forward migrations or BALN migrations (both originate on Icon
31
+ * which doesn't use ERC-20 allowances).
32
+ *
33
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
34
+ * `onError`, `retry`). Returns the unwrapped tx return value on success.
35
+ */
36
+ export function useMigrationApprove<K extends SpokeChainKey = SpokeChainKey>({
37
+ mutationOptions,
38
+ }: MutationHookParams<TxReturnType<K, false>, UseMigrationApproveVars<K>> = {}): SafeUseMutationResult<
39
+ TxReturnType<K, false>,
40
+ Error,
41
+ UseMigrationApproveVars<K>
42
+ > {
43
+ const { sodax } = useSodaxContext();
44
+ const queryClient = useQueryClient();
45
+
46
+ return useSafeMutation<TxReturnType<K, false>, Error, UseMigrationApproveVars<K>>({
47
+ mutationKey: ['migrate', 'approve'],
48
+ ...mutationOptions,
49
+ mutationFn: async ({ action, ...actionParams }) =>
50
+ unwrapResult(
51
+ await sodax.migration.approve(
52
+ { ...actionParams, raw: false } as
53
+ | IcxRevertMigrationAction<false>
54
+ | UnifiedBnUSDMigrateAction<K, false>,
55
+ action,
56
+ ),
57
+ ),
58
+ onSuccess: async (data, vars, ctx) => {
59
+ // Broad — wipes all migrate allowance variants. The mutation can't know exactly which
60
+ // (action, params) tuple the consumer was reading, so refetch all and let the active
61
+ // queries reconcile.
62
+ queryClient.invalidateQueries({ queryKey: ['migrate', 'allowance'] });
63
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
64
+ },
65
+ });
66
+ }
@@ -0,0 +1,39 @@
1
+ // packages/dapp-kit/src/hooks/migrate/useRevertMigrateSodaToIcx.ts
2
+ import type { IcxRevertMigrationAction, TxHashPair } from '@sodax/sdk';
3
+ import { ChainKeys } from '@sodax/sdk';
4
+ import { useQueryClient } from '@tanstack/react-query';
5
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
6
+ import type { MutationHookParams } from '../shared/types.js';
7
+ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
8
+ import { unwrapResult } from '../shared/unwrapResult.js';
9
+
10
+ export type UseRevertMigrateSodaToIcxVars = Omit<IcxRevertMigrationAction<false>, 'raw'>;
11
+
12
+ /**
13
+ * React hook for reverting SODA → ICX (the inverse of {@link useMigrateIcxToSoda}).
14
+ *
15
+ * Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
16
+ * `onError`, `retry`). Returns the unwrapped `TxHashPair` on success.
17
+ */
18
+ export function useRevertMigrateSodaToIcx({
19
+ mutationOptions,
20
+ }: MutationHookParams<TxHashPair, UseRevertMigrateSodaToIcxVars> = {}): SafeUseMutationResult<
21
+ TxHashPair,
22
+ Error,
23
+ UseRevertMigrateSodaToIcxVars
24
+ > {
25
+ const { sodax } = useSodaxContext();
26
+ const queryClient = useQueryClient();
27
+
28
+ return useSafeMutation<TxHashPair, Error, UseRevertMigrateSodaToIcxVars>({
29
+ mutationKey: ['migrate', 'revertSodaToIcx'],
30
+ ...mutationOptions,
31
+ mutationFn: async vars => unwrapResult(await sodax.migration.revertMigrateSodaToIcx({ ...vars, raw: false })),
32
+ onSuccess: async (data, vars, ctx) => {
33
+ queryClient.invalidateQueries({ queryKey: ['migrate', 'allowance'] });
34
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', vars.params.srcChainKey] });
35
+ queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', ChainKeys.ICON_MAINNET] });
36
+ await mutationOptions?.onSuccess?.(data, vars, ctx);
37
+ },
38
+ });
39
+ }
@@ -1,12 +1,14 @@
1
- export * from './useBorrow';
2
- export * from './useRepay';
3
- export * from './useSupply';
4
- export * from './useWithdraw';
5
- export * from './useUserReservesData';
6
- export * from './useReservesData';
7
- export * from './useMMAllowance';
8
- export * from './useMMApprove';
9
- export * from './useAToken';
10
- export * from './useATokensBalances';
11
- export * from './useReservesUsdFormat';
12
- export * from './useUserFormattedSummary';
1
+ export * from './useBorrow.js';
2
+ export * from './useRepay.js';
3
+ export * from './useSupply.js';
4
+ export * from './useWithdraw.js';
5
+ export * from './useUserReservesData.js';
6
+ export * from './useReservesData.js';
7
+ export * from './useReservesHumanized.js';
8
+ export * from './useReservesList.js';
9
+ export * from './useMMAllowance.js';
10
+ export * from './useMMApprove.js';
11
+ export * from './useAToken.js';
12
+ export * from './useATokensBalances.js';
13
+ export * from './useReservesUsdFormat.js';
14
+ export * from './useUserFormattedSummary.js';
@@ -1,53 +1,35 @@
1
- import { isAddress } from 'viem';
2
- import type { XToken } from '@sodax/sdk';
3
- import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query';
4
- import { useSodaxContext } from '../shared/useSodaxContext';
1
+ import type { Erc20Token } from '@sodax/sdk';
2
+ import type { ChainKey } from '@sodax/sdk';
3
+ import { useQuery, type UseQueryResult } from '@tanstack/react-query';
4
+ import { type Address, isAddress } from 'viem';
5
+ import { useSodaxContext } from '../shared/useSodaxContext.js';
6
+ import type { ReadHookParams } from '../shared/types.js';
5
7
 
6
- export type UseATokenParams = {
7
- aToken: string;
8
- queryOptions?: UseQueryOptions<XToken, Error>;
9
- };
8
+ export type ATokenData = Erc20Token & { chainKey: ChainKey };
9
+
10
+ export type UseATokenParams = ReadHookParams<
11
+ ATokenData,
12
+ {
13
+ aToken: Address | string | undefined;
14
+ }
15
+ >;
10
16
 
11
17
  /**
12
- * React hook to fetch and cache metadata for a given aToken address.
13
- *
14
- * Accepts an aToken address and React Query options to control query behavior.
15
- * Returns the aToken's ERC20-style metadata from the Sodax money market, with querying/caching
16
- * powered by React Query.
17
- *
18
- * @param {UseATokenParams} params - Required params object:
19
- * @property {Address} aToken - The aToken contract address to query.
20
- * @property {UseQueryOptions<XToken, Error>} queryOptions - React Query options to control query (e.g., staleTime, refetch, etc.).
21
- *
22
- * @returns {UseQueryResult<XToken, Error>} React Query result object:
23
- * - data: XToken metadata, if available
24
- * - isLoading: Boolean loading state
25
- * - error: Error, if API call fails
26
- *
27
- * @example
28
- * const { data: xToken, isLoading, error } = useAToken({ aToken: aTokenAddress, queryOptions: {} });
29
- * if (xToken) {
30
- * console.log(xToken.symbol);
31
- * }
18
+ * React hook to fetch ERC-20 metadata for a given aToken address on the hub chain.
19
+ * Returns `Erc20Token` (`name`, `symbol`, `decimals`, `address`) augmented with the hub
20
+ * `chainKey`. Note: the returned shape is a subset of `XToken` `hubAsset` and `vault` fields
21
+ * must be resolved separately if needed.
32
22
  */
33
- export function useAToken({ aToken, queryOptions }: UseATokenParams): UseQueryResult<XToken, Error> {
23
+ export function useAToken({ params, queryOptions }: UseATokenParams = {}): UseQueryResult<ATokenData, Error> {
34
24
  const { sodax } = useSodaxContext();
35
- const defaultQueryOptions = {
36
- queryKey: ['mm', 'aToken', aToken],
37
- enabled: !!aToken,
38
- };
39
- queryOptions = {
40
- ...defaultQueryOptions,
41
- ...queryOptions, // override default query options if provided
42
- };
25
+ const aToken = params?.aToken;
43
26
 
44
27
  return useQuery({
45
- ...queryOptions,
28
+ queryKey: ['mm', 'aToken', aToken],
46
29
  queryFn: async () => {
47
30
  if (!aToken) {
48
- throw new Error('aToken address or hub provider is not defined');
31
+ throw new Error('aToken address is required');
49
32
  }
50
-
51
33
  if (!isAddress(aToken)) {
52
34
  throw new Error('aToken address is not a valid address');
53
35
  }
@@ -55,8 +37,10 @@ export function useAToken({ aToken, queryOptions }: UseATokenParams): UseQueryRe
55
37
  const aTokenData = await sodax.moneyMarket.data.getATokenData(aToken);
56
38
  return {
57
39
  ...aTokenData,
58
- xChainId: sodax.hubProvider.chainConfig.chain.id,
40
+ chainKey: sodax.hubProvider.chainConfig.chain.key,
59
41
  };
60
42
  },
43
+ enabled: !!aToken && isAddress(aToken ?? ''),
44
+ ...queryOptions,
61
45
  });
62
46
  }