@sodax/dapp-kit 2.4.0-rc.1 → 2.4.0-rc.3
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 +11 -3
- package/dist/index.d.ts +395 -2413
- package/dist/index.mjs +206 -31
- package/package.json +2 -2
- package/src/hooks/_apiKeyWire.test.ts +4 -29
- package/src/hooks/_mutationContract.test.ts +2 -8
- package/src/hooks/backend/index.ts +0 -13
- package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +0 -6
- package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +0 -10
- package/src/hooks/backend/useBackendIntentByHash.ts +0 -6
- package/src/hooks/backend/useBackendIntentByTxHash.ts +1 -10
- package/src/hooks/backend/useBackendMoneyMarketAsset.ts +0 -6
- package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +0 -8
- package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +0 -8
- package/src/hooks/backend/useBackendMoneyMarketPosition.ts +0 -6
- package/src/hooks/backend/useBackendOracleCandles.ts +1 -9
- package/src/hooks/backend/useBackendOracleMarkets.ts +1 -7
- package/src/hooks/backend/useBackendOrderbook.ts +0 -6
- package/src/hooks/backend/useBackendUserIntents.ts +0 -8
- package/src/hooks/bitcoin/resolveBtcReadAddress.test.ts +1 -3
- package/src/hooks/bitcoin/resolveBtcReadAddress.ts +2 -12
- package/src/hooks/bitcoin/useBitcoinBalance.ts +1 -7
- package/src/hooks/bitcoin/useBitcoinTradingSetup.ts +2 -4
- package/src/hooks/bitcoin/useEnsureRadfiAccessToken.ts +2 -13
- package/src/hooks/bitcoin/useFundTradingWallet.ts +0 -6
- package/src/hooks/bitcoin/useRadfiAuth.ts +1 -6
- package/src/hooks/bitcoin/useRadfiSession.ts +0 -1
- package/src/hooks/bitcoin/useRadfiWithdraw.ts +0 -6
- package/src/hooks/bitcoin/useRenewUtxos.ts +0 -5
- package/src/hooks/bitcoin/useTradingWallet.ts +1 -4
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.test.ts +1 -7
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.ts +7 -21
- package/src/hooks/bridge/useBridge.test.ts +1 -11
- package/src/hooks/bridge/useBridge.ts +2 -12
- package/src/hooks/bridge/useBridgeAllowance.ts +1 -2
- package/src/hooks/bridge/useBridgeApprovalStrategy.ts +2 -6
- package/src/hooks/bridge/useBridgeApprove.ts +2 -14
- package/src/hooks/bridge/useBridgeDetailedStatus.test.ts +2 -11
- package/src/hooks/bridge/useBridgeDetailedStatus.ts +3 -23
- package/src/hooks/bridge/useBridgeWithApproval.ts +2 -11
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.test.ts +2 -7
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.ts +2 -13
- package/src/hooks/bridgeApi/index.ts +11 -14
- package/src/hooks/bridgeApi/useBridgeApiAllowance.ts +1 -9
- package/src/hooks/bridgeApi/useBridgeApiApprove.ts +2 -17
- package/src/hooks/bridgeApi/useBridgeApiApproveAndBroadcast.test.ts +2 -13
- package/src/hooks/bridgeApi/useBridgeApiApproveAndBroadcast.ts +3 -20
- package/src/hooks/bridgeApi/useBridgeApiBridgeableAmount.ts +1 -12
- package/src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts +1 -14
- package/src/hooks/bridgeApi/useBridgeApiFee.ts +2 -11
- package/src/hooks/bridgeApi/useBridgeApiIsBridgeable.ts +1 -12
- package/src/hooks/bridgeApi/useBridgeApiSubmitTx.ts +2 -23
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.test.ts +1 -9
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.ts +1 -19
- package/src/hooks/bridgeApi/useBridgeApiTokens.ts +0 -8
- package/src/hooks/bridgeApi/useBridgeApiTokensByChain.ts +0 -7
- package/src/hooks/dex/useClaimRewards.ts +1 -12
- package/src/hooks/dex/useCreateDecreaseLiquidityParams.ts +1 -5
- package/src/hooks/dex/useCreateDepositParams.ts +1 -5
- package/src/hooks/dex/useCreateSupplyLiquidityParams.ts +1 -10
- package/src/hooks/dex/useCreateWithdrawParams.ts +1 -5
- package/src/hooks/dex/useDecreaseLiquidity.ts +2 -15
- package/src/hooks/dex/useDexAllowance.ts +1 -5
- package/src/hooks/dex/useDexApprove.ts +1 -12
- package/src/hooks/dex/useDexDeposit.ts +1 -23
- package/src/hooks/dex/useDexWithdraw.ts +1 -13
- package/src/hooks/dex/useLiquidityAmounts.ts +1 -4
- package/src/hooks/dex/usePoolBalances.ts +0 -4
- package/src/hooks/dex/usePoolData.ts +1 -4
- package/src/hooks/dex/usePools.ts +1 -4
- package/src/hooks/dex/usePositionInfo.ts +1 -4
- package/src/hooks/dex/useSupplyLiquidity.ts +3 -20
- package/src/hooks/leverageYield/useApproveLeveragePositionFunding.ts +2 -8
- package/src/hooks/leverageYield/useLeveragePositionAccount.ts +2 -13
- package/src/hooks/leverageYield/useLeveragePositionCollateral.ts +2 -14
- package/src/hooks/leverageYield/useLeveragePositionFundingAllowance.ts +2 -9
- package/src/hooks/leverageYield/useLeveragePositionInfo.ts +1 -13
- package/src/hooks/leverageYield/useLeveragePositionPayoutAddress.ts +3 -15
- package/src/hooks/leverageYield/useLeveragePositionPending.ts +2 -16
- package/src/hooks/leverageYield/useLeveragePositions.ts +1 -15
- package/src/hooks/leverageYield/useLeveragePositionsForUser.ts +1 -15
- package/src/hooks/leverageYield/useLeverageYieldApprovalStrategy.ts +1 -6
- package/src/hooks/leverageYield/useLeverageYieldDeposit.ts +1 -17
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.test.ts +3 -14
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.ts +2 -19
- package/src/hooks/leverageYield/useLeverageYieldEffectiveApr.ts +1 -12
- package/src/hooks/leverageYield/useLeverageYieldNotifySolver.ts +1 -11
- package/src/hooks/leverageYield/useLeverageYieldPosition.ts +1 -13
- package/src/hooks/leverageYield/useLeverageYieldPreviewRedeem.ts +1 -12
- package/src/hooks/leverageYield/useLeverageYieldQuote.test.ts +3 -13
- package/src/hooks/leverageYield/useLeverageYieldQuote.ts +5 -54
- package/src/hooks/leverageYield/useLeverageYieldShareBalances.ts +2 -29
- package/src/hooks/leverageYield/useLeverageYieldTotalAssets.ts +1 -9
- package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +2 -25
- package/src/hooks/leverageYield/useLeverageYieldVaultSwapWithApproval.ts +1 -11
- package/src/hooks/leverageYield/useLeverageYieldWithdraw.ts +1 -17
- package/src/hooks/leverageYield/useOpenLeveragePosition.ts +5 -18
- package/src/hooks/leverageYield/useRunLeveragePositionOperation.ts +4 -20
- package/src/hooks/leverageYield/useSubmitLeveragePositionIntent.ts +3 -15
- package/src/hooks/leverageYieldApi/index.ts +0 -15
- package/src/hooks/leverageYieldApi/useLeverageYieldApiAllowance.ts +1 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApprove.ts +3 -6
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.test.ts +4 -14
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.ts +4 -26
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApr.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiAsset.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCancelIntent.ts +2 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateDepositIntent.ts +2 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateWithdrawIntent.ts +2 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiDeadline.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiDepositQuote.ts +1 -15
- package/src/hooks/leverageYieldApi/useLeverageYieldApiEffectiveApr.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiEstimateGas.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiFilledIntent.ts +1 -6
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntent.ts +1 -6
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentExtraData.ts +1 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentHash.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentPacket.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiLsdApr.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiMaxWithdraw.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPartnerFee.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPosition.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewDeposit.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewRedeem.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewWithdraw.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiShareBalance.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSolverFee.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiStatus.ts +2 -12
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitIntent.ts +2 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTx.ts +2 -8
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTxStatus.ts +2 -17
- package/src/hooks/leverageYieldApi/useLeverageYieldApiTotalAssets.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiVault.ts +1 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiVaults.ts +0 -7
- package/src/hooks/leverageYieldApi/useLeverageYieldApiWithdrawQuote.ts +1 -15
- package/src/hooks/migrate/useMigrateBaln.ts +1 -8
- package/src/hooks/migrate/useMigrateIcxToSoda.ts +1 -12
- package/src/hooks/migrate/useMigratebnUSD.ts +1 -12
- package/src/hooks/migrate/useMigrationAllowance.ts +3 -19
- package/src/hooks/migrate/useMigrationApprove.ts +2 -20
- package/src/hooks/migrate/useRevertMigrateSodaToIcx.ts +0 -7
- package/src/hooks/mm/useAToken.ts +1 -6
- package/src/hooks/mm/useATokensBalances.ts +0 -5
- package/src/hooks/mm/useBorrow.ts +1 -13
- package/src/hooks/mm/useEModes.ts +2 -13
- package/src/hooks/mm/useMMAllowance.test.ts +55 -0
- package/src/hooks/mm/useMMAllowance.ts +6 -16
- package/src/hooks/mm/useMMApprove.ts +1 -14
- package/src/hooks/mm/useRepay.ts +1 -12
- package/src/hooks/mm/useReservesData.ts +0 -4
- package/src/hooks/mm/useReservesHumanized.ts +0 -4
- package/src/hooks/mm/useReservesList.ts +0 -4
- package/src/hooks/mm/useReservesUsdFormat.ts +0 -4
- package/src/hooks/mm/useSupply.ts +1 -24
- package/src/hooks/mm/useUserFormattedSummary.ts +0 -7
- package/src/hooks/mm/useUserReservesData.ts +0 -4
- package/src/hooks/mm/useWithdraw.ts +1 -13
- package/src/hooks/partner/useApproveToken.ts +1 -7
- package/src/hooks/partner/useFeeClaimSwap.ts +1 -7
- package/src/hooks/partner/useFeeClaimWithdraw.ts +3 -20
- package/src/hooks/partner/useFetchAssetsBalances.ts +0 -4
- package/src/hooks/partner/useGetAutoSwapPreferences.ts +0 -4
- package/src/hooks/partner/useGetIntentDetails.ts +1 -8
- package/src/hooks/partner/useGetUserIntent.ts +1 -8
- package/src/hooks/partner/useIsTokenApproved.ts +0 -4
- package/src/hooks/partner/usePartnerCancelIntent.ts +2 -9
- package/src/hooks/partner/useSetSwapPreference.ts +1 -14
- package/src/hooks/recovery/useHubAssetBalances.ts +1 -6
- package/src/hooks/recovery/useWithdrawHubAsset.ts +1 -13
- package/src/hooks/shared/index.ts +4 -0
- package/src/hooks/shared/invalidateBalances.ts +2 -10
- package/src/hooks/shared/isUserRejectedError.test.ts +0 -3
- package/src/hooks/shared/isUserRejectedError.ts +2 -23
- package/src/hooks/shared/notFoundStreak.ts +5 -22
- package/src/hooks/shared/retryUnlessAuthFailure.test.ts +1 -2
- package/src/hooks/shared/retryUnlessAuthFailure.ts +1 -12
- package/src/hooks/shared/solverStatusPolicy.test.ts +0 -5
- package/src/hooks/shared/solverStatusPolicy.ts +8 -58
- package/src/hooks/shared/types.ts +4 -55
- package/src/hooks/shared/unwrapResult.ts +1 -8
- package/src/hooks/shared/useBalances.ts +6 -39
- package/src/hooks/shared/useEstablishTrustline.test.ts +1 -5
- package/src/hooks/shared/useEstablishTrustline.ts +3 -13
- package/src/hooks/shared/useEstablishXrpTrustline.test.ts +112 -0
- package/src/hooks/shared/useEstablishXrpTrustline.ts +63 -0
- package/src/hooks/shared/useEstimateGas.ts +0 -1
- package/src/hooks/shared/useGetUserHubWalletAddress.ts +1 -18
- package/src/hooks/shared/useNearStorageCheck.ts +2 -9
- package/src/hooks/shared/useNearStorageGate.ts +5 -15
- package/src/hooks/shared/useRegisterNearStorage.ts +2 -10
- package/src/hooks/shared/useRequestTrustline.test.ts +1 -7
- package/src/hooks/shared/useRequestTrustline.ts +4 -14
- package/src/hooks/shared/useSafeMutation.test.ts +0 -3
- package/src/hooks/shared/useSafeMutation.ts +2 -26
- package/src/hooks/shared/useSodaxContext.ts +0 -6
- package/src/hooks/shared/useStellarGate.test.ts +2 -9
- package/src/hooks/shared/useStellarGate.ts +3 -9
- package/src/hooks/shared/useStellarTrustlineCheck.test.ts +0 -2
- package/src/hooks/shared/useStellarTrustlineCheck.ts +1 -4
- package/src/hooks/shared/useXBalances.ts +2 -20
- package/src/hooks/shared/useXrpDestinationCheck.ts +71 -0
- package/src/hooks/shared/useXrpGate.test.ts +116 -0
- package/src/hooks/shared/useXrpGate.ts +80 -0
- package/src/hooks/shared/useXrpTrustlineReserveCheck.ts +45 -0
- package/src/hooks/shared/waitForApiAllowance.test.ts +0 -1
- package/src/hooks/shared/waitForApiAllowance.ts +2 -6
- package/src/hooks/sponsoring/useActivateStellarAccount.test.ts +1 -2
- package/src/hooks/sponsoring/useActivateStellarAccount.ts +1 -5
- package/src/hooks/sponsoring/useActivateStellarAccount.wire.test.ts +2 -13
- package/src/hooks/sponsoring/useSponsorConfig.ts +0 -1
- package/src/hooks/sponsoring/useStellarAccountActive.ts +0 -1
- package/src/hooks/sponsoring/useStellarAccountStatus.ts +1 -4
- package/src/hooks/staking/index.ts +0 -1
- package/src/hooks/staking/useCancelUnstake.ts +1 -7
- package/src/hooks/staking/useClaim.ts +1 -7
- package/src/hooks/staking/useConvertedAssets.ts +1 -4
- package/src/hooks/staking/useInstantUnstake.ts +1 -7
- package/src/hooks/staking/useInstantUnstakeAllowance.ts +1 -5
- package/src/hooks/staking/useInstantUnstakeApprove.ts +1 -11
- package/src/hooks/staking/useInstantUnstakeRatio.ts +1 -4
- package/src/hooks/staking/useStake.ts +1 -12
- package/src/hooks/staking/useStakeAllowance.ts +1 -5
- package/src/hooks/staking/useStakeApprove.ts +1 -11
- package/src/hooks/staking/useStakeRatio.ts +1 -4
- package/src/hooks/staking/useStakingConfig.ts +1 -4
- package/src/hooks/staking/useStakingInfo.ts +1 -5
- package/src/hooks/staking/useUnstake.ts +2 -9
- package/src/hooks/staking/useUnstakeAllowance.ts +1 -5
- package/src/hooks/staking/useUnstakeApprove.ts +1 -10
- package/src/hooks/staking/useUnstakingInfo.ts +1 -4
- package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +1 -4
- package/src/hooks/swap/useCancelLimitOrder.ts +1 -7
- package/src/hooks/swap/useCancelSwap.ts +1 -7
- package/src/hooks/swap/useCreateLimitOrder.ts +2 -12
- package/src/hooks/swap/useDetailedStatus.test.ts +6 -18
- package/src/hooks/swap/useDetailedStatus.ts +2 -19
- package/src/hooks/swap/useQuote.test.ts +2 -8
- package/src/hooks/swap/useQuote.ts +5 -32
- package/src/hooks/swap/useStatus.ts +2 -10
- package/src/hooks/swap/useSwap.test.ts +1 -11
- package/src/hooks/swap/useSwap.ts +2 -12
- package/src/hooks/swap/useSwapAllowance.ts +1 -2
- package/src/hooks/swap/useSwapApprovalStrategy.ts +2 -7
- package/src/hooks/swap/useSwapApprove.ts +2 -14
- package/src/hooks/swap/useSwapLifecycle.test.ts +47 -2
- package/src/hooks/swap/useSwapLifecycle.ts +36 -30
- package/src/hooks/swap/useSwapWithApproval.ts +2 -11
- package/src/hooks/swapsApi/index.ts +2 -14
- package/src/hooks/swapsApi/isTerminalSwapIntentStatus.test.ts +1 -5
- package/src/hooks/swapsApi/isTerminalSwapIntentStatus.ts +1 -7
- package/src/hooks/swapsApi/useSwapsApiAllowance.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiApprove.ts +1 -14
- package/src/hooks/swapsApi/useSwapsApiApproveAndBroadcast.test.ts +5 -20
- package/src/hooks/swapsApi/useSwapsApiApproveAndBroadcast.ts +3 -24
- package/src/hooks/swapsApi/useSwapsApiBroadcastWithApproval.test.ts +2 -8
- package/src/hooks/swapsApi/useSwapsApiBroadcastWithApproval.ts +6 -41
- package/src/hooks/swapsApi/useSwapsApiCancelIntent.ts +0 -13
- package/src/hooks/swapsApi/useSwapsApiCreateIntent.ts +0 -13
- package/src/hooks/swapsApi/useSwapsApiCreateLimitOrder.ts +1 -13
- package/src/hooks/swapsApi/useSwapsApiDeadline.ts +1 -7
- package/src/hooks/swapsApi/useSwapsApiEstimateGas.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiFilledIntent.ts +0 -8
- package/src/hooks/swapsApi/useSwapsApiIntent.ts +1 -7
- package/src/hooks/swapsApi/useSwapsApiIntentExtraData.ts +1 -8
- package/src/hooks/swapsApi/useSwapsApiIntentHash.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiIntentPacket.ts +1 -10
- package/src/hooks/swapsApi/useSwapsApiPartnerFee.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiQuote.ts +2 -24
- package/src/hooks/swapsApi/useSwapsApiSolverFee.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiStatus.ts +1 -11
- package/src/hooks/swapsApi/useSwapsApiSubmitIntent.ts +1 -15
- package/src/hooks/swapsApi/useSwapsApiSubmitTx.ts +0 -21
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +2 -19
- package/src/hooks/swapsApi/useSwapsApiTokens.ts +0 -7
- package/src/hooks/swapsApi/useSwapsApiTokensByChain.ts +0 -7
- package/src/index.ts +1 -1
- package/src/providers/SodaxProvider.test.ts +2 -9
- package/src/providers/SodaxProvider.tsx +1 -12
- package/src/providers/createSodaxQueryClient.ts +4 -56
- package/src/utils/approvalPlan.ts +9 -32
- package/src/utils/dex-utils.ts +5 -20
- package/src/utils/index.ts +1 -0
- package/src/utils/nearStorageGate.ts +3 -14
- package/src/utils/stellarGate.ts +3 -16
- package/src/utils/swapLifecycle.test.ts +35 -0
- package/src/utils/swapLifecycle.ts +21 -28
- package/src/utils/xrpGate.test.ts +112 -0
- package/src/utils/xrpGate.ts +69 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// packages/dapp-kit/src/hooks/partner/useSetSwapPreference.ts
|
|
2
1
|
import type { HubChainKey, SetSwapPreferenceAction, SpokeChainKey, TxReturnType } from '@sodax/sdk';
|
|
3
2
|
import { useQueryClient } from '@tanstack/react-query';
|
|
4
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
@@ -6,23 +5,12 @@ import type { MutationHookParams } from '../shared/types.js';
|
|
|
6
5
|
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
7
6
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
8
7
|
|
|
9
|
-
/**
|
|
10
|
-
* Mutation variables for {@link useSetSwapPreference}. Generic over `K extends SpokeChainKey`
|
|
11
|
-
* for compatibility with the SDK signature; at runtime the SDK enforces hub-only
|
|
12
|
-
* (`isHubChainKeyType(srcChainKey)`).
|
|
13
|
-
*/
|
|
8
|
+
/** Generic only for SDK signature compatibility; the SDK enforces hub-only at runtime. */
|
|
14
9
|
export type UseSetSwapPreferenceVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
|
|
15
10
|
SetSwapPreferenceAction<K, false>,
|
|
16
11
|
'raw'
|
|
17
12
|
>;
|
|
18
13
|
|
|
19
|
-
/**
|
|
20
|
-
* React hook to set the partner's auto-swap preferences (output token + destination chain +
|
|
21
|
-
* destination address) on the protocol-intents contract.
|
|
22
|
-
*
|
|
23
|
-
* Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
|
|
24
|
-
* `onError`, `retry`). Returns the unwrapped tx return value on success.
|
|
25
|
-
*/
|
|
26
14
|
export function useSetSwapPreference<K extends SpokeChainKey = SpokeChainKey>({
|
|
27
15
|
mutationOptions,
|
|
28
16
|
}: MutationHookParams<TxReturnType<K, false>, UseSetSwapPreferenceVars<K>> = {}): SafeUseMutationResult<
|
|
@@ -46,5 +34,4 @@ export function useSetSwapPreference<K extends SpokeChainKey = SpokeChainKey>({
|
|
|
46
34
|
});
|
|
47
35
|
}
|
|
48
36
|
|
|
49
|
-
// Helper alias for the common Sonic-only call shape.
|
|
50
37
|
export type UseSetSwapPreferenceVarsHub = UseSetSwapPreferenceVars<HubChainKey>;
|
|
@@ -8,16 +8,11 @@ export type UseHubAssetBalancesParams = ReadHookParams<
|
|
|
8
8
|
HubAssetBalance[],
|
|
9
9
|
{
|
|
10
10
|
chainKey: SpokeChainKey | undefined;
|
|
11
|
-
/**
|
|
11
|
+
/** Spoke address; the SDK derives the hub wallet from it. */
|
|
12
12
|
srcAddress: string | undefined;
|
|
13
13
|
}
|
|
14
14
|
>;
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* React hook to fetch the hub-side balances of every supported token on the given spoke chain
|
|
18
|
-
* for the user's hub wallet (derived internally from `srcAddress` + `chainKey`). Disabled when
|
|
19
|
-
* either input is missing. Throws on `!ok`.
|
|
20
|
-
*/
|
|
21
16
|
export function useHubAssetBalances({
|
|
22
17
|
params,
|
|
23
18
|
queryOptions,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// packages/dapp-kit/src/hooks/recovery/useWithdrawHubAsset.ts
|
|
2
1
|
import type { SpokeChainKey, SendMessageReturnType, WithdrawHubAssetAction } from '@sodax/sdk';
|
|
3
2
|
import { useQueryClient } from '@tanstack/react-query';
|
|
4
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
@@ -8,23 +7,12 @@ import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMu
|
|
|
8
7
|
import { retryBeforeSubmission } from '../shared/retryBeforeSubmission.js';
|
|
9
8
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
10
9
|
|
|
11
|
-
/**
|
|
12
|
-
* Mutation variables for {@link useWithdrawHubAsset}. Generic over `K extends SpokeChainKey`
|
|
13
|
-
* (defaults to the full union). Sophisticated callers can lock K at the hook call site to narrow
|
|
14
|
-
* the `walletProvider` and `params.srcChainKey` types.
|
|
15
|
-
*/
|
|
16
10
|
export type UseWithdrawHubAssetVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
|
|
17
11
|
WithdrawHubAssetAction<K, false>,
|
|
18
12
|
'raw'
|
|
19
13
|
>;
|
|
20
14
|
|
|
21
|
-
/**
|
|
22
|
-
* React hook for withdrawing a hub-side asset back to the user's spoke chain wallet.
|
|
23
|
-
*
|
|
24
|
-
* Throws on SDK failure so React Query's native error model engages (`isError`, `error`,
|
|
25
|
-
* `onError`, `retry`). Returns the spoke tx hash on success, or from an MPC-relay chain (Tron) the
|
|
26
|
-
* relay's `trackingId`, which is not a tx hash: never link it to an explorer.
|
|
27
|
-
*/
|
|
15
|
+
/** Resolves the spoke tx hash, or for an MPC-relay chain a relay `trackingId` that is not a tx hash: never link it to an explorer. */
|
|
28
16
|
export function useWithdrawHubAsset<K extends SpokeChainKey = SpokeChainKey>({
|
|
29
17
|
mutationOptions,
|
|
30
18
|
}: MutationHookParams<SendMessageReturnType<K, false>, UseWithdrawHubAssetVars<K>> = {}): SafeUseMutationResult<
|
|
@@ -13,6 +13,10 @@ export * from './useNearStorageCheck.js';
|
|
|
13
13
|
export * from './useRegisterNearStorage.js';
|
|
14
14
|
export * from './useNearStorageGate.js';
|
|
15
15
|
export * from './useStellarGate.js';
|
|
16
|
+
export * from './useXrpDestinationCheck.js';
|
|
17
|
+
export * from './useXrpTrustlineReserveCheck.js';
|
|
18
|
+
export * from './useEstablishXrpTrustline.js';
|
|
19
|
+
export * from './useXrpGate.js';
|
|
16
20
|
export * from './useGetUserHubWalletAddress.js';
|
|
17
21
|
export * from './useXBalances.js';
|
|
18
22
|
export * from './useBalances.js';
|
|
@@ -2,16 +2,8 @@ import type { QueryClient } from '@tanstack/react-query';
|
|
|
2
2
|
import type { SpokeChainKey } from '@sodax/sdk';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* Two balance hooks ship side by side — {@link useBalances} (SDK-backed, keyed under
|
|
8
|
-
* `['shared','balances', …]`) and {@link useXBalances} (wallet-layer, `['shared','xBalances', …]`) —
|
|
9
|
-
* and an app may mount either. React Query matches query keys element-wise, so one key never
|
|
10
|
-
* matches the other; a mutation hook that invalidates only one silently leaves the other stale.
|
|
11
|
-
* Route every post-transaction balance refresh through here so the two cannot drift apart again.
|
|
12
|
-
*
|
|
13
|
-
* `undefined` chain keys are skipped and duplicates collapse, so callers can pass
|
|
14
|
-
* `(src, dst)` without guarding either.
|
|
5
|
+
* Invalidates both `useBalances` and `useXBalances` keys; invalidating only one leaves the other stale.
|
|
6
|
+
* `undefined` keys are skipped and duplicates collapse.
|
|
15
7
|
*/
|
|
16
8
|
export function invalidateBalances(
|
|
17
9
|
queryClient: QueryClient,
|
|
@@ -20,9 +20,6 @@ describe('isUserRejectedError', () => {
|
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
it('does not match a plain Error whose message contains rejection prose', () => {
|
|
23
|
-
// The predicate trusts SDK classification; it does NOT re-scan message content. A bare
|
|
24
|
-
// Error from outside the SDK boundary (e.g. a non-canonical throw in a queryFn) is not a
|
|
25
|
-
// canonical USER_REJECTED — it should fall through to the generic failure UI.
|
|
26
23
|
expect(isUserRejectedError(new Error('User rejected the request'))).toBe(false);
|
|
27
24
|
});
|
|
28
25
|
|
|
@@ -1,29 +1,8 @@
|
|
|
1
1
|
import { isSodaxError } from '@sodax/sdk';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The SDK classifies wallet rejection at the source: `intentCreationFailed` / `approveFailed`
|
|
7
|
-
* wrappers emit `SodaxError<'USER_REJECTED'>` whenever the underlying cause matches a known
|
|
8
|
-
* rejection shape (viem `4001` / `UserRejectedRequestError`, ICON Hana `CANCEL_SIGNING`,
|
|
9
|
-
* Solana / Sui / Stellar / Stacks / NEAR / Injective name + message patterns). Consumers
|
|
10
|
-
* branch on this code to render a "Cancelled" UI instead of a "Failed" toast — user
|
|
11
|
-
* cancellation is a normal flow, not a failure.
|
|
12
|
-
*
|
|
13
|
-
* Use after `mutateAsyncSafe` (or inside an `onError` / `catch`):
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const result = await swap.mutateAsyncSafe({ params, walletProvider });
|
|
17
|
-
* if (!result.ok) {
|
|
18
|
-
* if (isUserRejectedError(result.error)) return; // silent — user cancelled
|
|
19
|
-
* toast.error(getErrorMessage(result.error)); // real failure
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* The check trusts the SDK's classification — it does NOT re-scan message content. Errors
|
|
24
|
-
* raised outside the SDK boundary (e.g. a non-SodaxError thrown from a `queryFn`) will not
|
|
25
|
-
* match, which is intentional: only the SDK's `USER_REJECTED` is the canonical "cancelled"
|
|
26
|
-
* signal.
|
|
4
|
+
* True only for the SDK's `USER_REJECTED` (wallet prompt cancelled); does not re-scan messages, so
|
|
5
|
+
* non-SodaxError rejections intentionally do not match.
|
|
27
6
|
*/
|
|
28
7
|
export function isUserRejectedError(error: unknown): boolean {
|
|
29
8
|
return isSodaxError(error) && error.code === 'USER_REJECTED';
|
|
@@ -1,29 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Some reads cannot distinguish "still in flight" from "will never resolve" — a solver that has not
|
|
5
|
-
* seen an intent, a relay with no packet for a source tx. Polling those forever is the failure mode
|
|
6
|
-
* a budget exists to stop, but only for that shape: a dependency failing *right now* must keep
|
|
7
|
-
* polling, or a two-minute outage would permanently stick a transfer that is fine.
|
|
8
|
-
*
|
|
9
|
-
* Each feature decides what counts as an ambiguous miss and passes a boolean; the counting,
|
|
10
|
-
* identity and de-duplication are the same everywhere and live here.
|
|
11
|
-
*
|
|
12
|
-
* Kept free of React and context imports so it is unit-testable in dapp-kit's `node` test
|
|
13
|
-
* environment — importing a hook pulls in `useSodaxContext`.
|
|
14
|
-
*/
|
|
1
|
+
// Budget only for ambiguous misses; a dependency failing right now must keep polling.
|
|
2
|
+
// Kept free of React/context imports so it is unit-testable in the `node` test env.
|
|
15
3
|
|
|
16
4
|
export const STATUS_POLL_MS = 3000;
|
|
17
5
|
/** Cap consecutive ambiguous polls (~2 min at 3s). The first one is a race, not a stop. */
|
|
18
6
|
export const MAX_NOT_FOUND_POLLS = 40;
|
|
19
|
-
/**
|
|
20
|
-
* The cap for a read the SDK tags `context.mpcRelay` (~5 min at 3s). An MPC-relay leg waits on confirmations,
|
|
21
|
-
* the NEAR signature and the hub tx, and the SDK itself allows it at least 5 minutes.
|
|
22
|
-
*/
|
|
7
|
+
/** Cap for reads tagged `context.mpcRelay` (~5 min at 3s); the SDK allows an MPC-relay leg at least 5 min. */
|
|
23
8
|
export const MAX_MPC_RELAY_NOT_FOUND_POLLS = 100;
|
|
24
9
|
|
|
25
10
|
export type NotFoundStreakState = {
|
|
26
|
-
/** Identity of what is being polled — an intent tx hash, or a composite source-chain/tx key. */
|
|
27
11
|
pollKey: string | undefined;
|
|
28
12
|
seenUpdates: number;
|
|
29
13
|
consecutiveNotFound: number;
|
|
@@ -40,9 +24,8 @@ export function nextNotFoundStreak(isAmbiguousMiss: boolean, previousStreak: num
|
|
|
40
24
|
}
|
|
41
25
|
|
|
42
26
|
/**
|
|
43
|
-
* Advances
|
|
44
|
-
*
|
|
45
|
-
* than once per fetch — same `dataUpdateCount` is a no-op.
|
|
27
|
+
* Advances once per query update; a new `pollKey` resets the streak. React Query may call
|
|
28
|
+
* `refetchInterval` more than once per fetch, so a repeated `dataUpdateCount` is a no-op.
|
|
46
29
|
*/
|
|
47
30
|
export function advanceNotFoundStreak(
|
|
48
31
|
state: NotFoundStreakState,
|
|
@@ -2,7 +2,6 @@ import { SodaxError } from '@sodax/sdk';
|
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
3
|
import { retryUnlessAuthFailure } from './retryUnlessAuthFailure.js';
|
|
4
4
|
|
|
5
|
-
/** A backend failure as the swaps API surfaces it: `EXTERNAL_API_ERROR` with the status on context. */
|
|
6
5
|
const apiError = (status?: number): SodaxError =>
|
|
7
6
|
new SodaxError('EXTERNAL_API_ERROR', `responded with ${status}`, {
|
|
8
7
|
feature: 'backend',
|
|
@@ -18,7 +17,7 @@ describe('retryUnlessAuthFailure', () => {
|
|
|
18
17
|
});
|
|
19
18
|
|
|
20
19
|
it("retries the apiguard's transient 503 and other server failures", () => {
|
|
21
|
-
// 503 is
|
|
20
|
+
// 503 is not terminal: the key may be fine while verification is down.
|
|
22
21
|
for (const status of [408, 429, 500, 502, 503, 504]) {
|
|
23
22
|
expect(retryUnlessAuthFailure(0, apiError(status))).toBe(true);
|
|
24
23
|
}
|
|
@@ -1,18 +1,7 @@
|
|
|
1
1
|
import { isAuthFailure } from '@sodax/sdk';
|
|
2
2
|
|
|
3
|
-
/** Retries a repeatable failure gets, matching the `retry: 3` these hooks used before. */
|
|
4
3
|
const MAX_RETRIES = 3;
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* Default React Query `retry` policy for the backend API hooks (swaps, leverage-yield, bridge): replay a transport blip, never a
|
|
8
|
-
* terminal API-key rejection (see `isAuthFailure`). Retrying a rejected key only multiplies doomed
|
|
9
|
-
* requests before the consumer sees the error.
|
|
10
|
-
*
|
|
11
|
-
* Override or compose it through `queryOptions` / `mutationOptions`:
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* useSwapsApiQuote({ params: { body }, queryOptions: { retry: 5 } });
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
5
|
+
/** Default `retry` for backend API hooks: retries transport failures, never a rejected API key. */
|
|
17
6
|
export const retryUnlessAuthFailure = (failureCount: number, error: unknown): boolean =>
|
|
18
7
|
!isAuthFailure(error) && failureCount < MAX_RETRIES;
|
|
@@ -9,11 +9,6 @@ import {
|
|
|
9
9
|
} from './notFoundStreak.js';
|
|
10
10
|
import { getSolverStatusRefetchInterval, isSolverNotFound } from './solverStatusPolicy.js';
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Guards the polling-stop invariant for `useStatus.refetchInterval`: keep polling until SOLVED/
|
|
14
|
-
* FAILED, or until NOT_FOUND has been consecutive for MAX_NOT_FOUND_POLLS successful fetches.
|
|
15
|
-
* Tested as a pure function because dapp-kit's vitest runs in the `node` environment.
|
|
16
|
-
*/
|
|
17
12
|
const HASH_A = '0xaaa';
|
|
18
13
|
const HASH_B = '0xbbb';
|
|
19
14
|
const ok = (status: SolverIntentStatusCode) => ({ ok: true as const, value: { status } });
|
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Two features now resolve a source tx to a solver answer — swap and leverage yield — and their
|
|
5
|
-
* status unions are structurally the same, so the terminality rules and the ambiguous-miss mapping
|
|
6
|
-
* are identical. They live here rather than in one feature's folder: `hooks/` has no cross-feature
|
|
7
|
-
* imports anywhere else, and a second verbatim copy is what this extraction exists to prevent.
|
|
8
|
-
* Bridge keeps its own policy beside its hook, because its second arm is a relay packet that is
|
|
9
|
-
* terminal on arrival and has no solver vocabulary at all.
|
|
10
|
-
*
|
|
11
|
-
* The read types are declared structurally rather than imported from either feature: this module
|
|
12
|
-
* must not own another hook's public contract, and both `DetailedSwapStatus` and
|
|
13
|
-
* `DetailedLeverageYieldStatus` satisfy the shape below.
|
|
14
|
-
*
|
|
15
|
-
* Kept free of React and context imports so it is unit-testable in dapp-kit's `node` test
|
|
16
|
-
* environment — importing a hook pulls in `useSodaxContext`.
|
|
17
|
-
*/
|
|
1
|
+
// Read types are structural so this module does not own a feature hook's public contract.
|
|
2
|
+
// Kept free of React/context imports so it is unit-testable in the `node` test env.
|
|
18
3
|
import {
|
|
19
4
|
DETAILED_STATUS_NOT_DELIVERED,
|
|
20
5
|
isAuthFailure,
|
|
@@ -27,15 +12,9 @@ import {
|
|
|
27
12
|
|
|
28
13
|
import { MAX_NOT_FOUND_POLLS, STATUS_POLL_MS } from './notFoundStreak.js';
|
|
29
14
|
|
|
30
|
-
/** A solver status read, as `useStatus` holds it. */
|
|
31
15
|
export type SolverStatusRead = Result<SolverIntentStatusResponse, SolverErrorResponse> | undefined;
|
|
32
16
|
|
|
33
|
-
/**
|
|
34
|
-
* The structural minimum a detailed-status read must satisfy to use this policy: a backend arm
|
|
35
|
-
* reporting terminality in the submit-tx vocabulary, and a solver arm carrying the solver's own
|
|
36
|
-
* response. A feature's arm may carry more (leverage yield and swap both hoist `dstTxHash`); only
|
|
37
|
-
* what is read here is required.
|
|
38
|
-
*/
|
|
17
|
+
/** Structural minimum of a detailed-status read; feature arms may carry more fields. */
|
|
39
18
|
export type SolverDetailedStatusRead =
|
|
40
19
|
| Result<
|
|
41
20
|
{ source: 'backend'; data: { status: string } } | { source: 'solver'; data: SolverIntentStatusResponse },
|
|
@@ -43,11 +22,7 @@ export type SolverDetailedStatusRead =
|
|
|
43
22
|
>
|
|
44
23
|
| undefined;
|
|
45
24
|
|
|
46
|
-
/**
|
|
47
|
-
* Polling interval for a plain solver status read (`useStatus`). Stops on SOLVED/FAILED immediately;
|
|
48
|
-
* stops on NOT_FOUND only after `MAX_NOT_FOUND_POLLS` consecutive successful fetches (solver forgot /
|
|
49
|
-
* never saw the intent). In-flight statuses keep polling with no global cap.
|
|
50
|
-
*/
|
|
25
|
+
/** Stops on SOLVED/FAILED, or after `MAX_NOT_FOUND_POLLS` consecutive NOT_FOUNDs; in-flight polls uncapped. */
|
|
51
26
|
export function getSolverStatusRefetchInterval(data: SolverStatusRead, consecutiveNotFound: number): number | false {
|
|
52
27
|
const status = data?.ok ? data.value.status : undefined;
|
|
53
28
|
if (status === SolverIntentStatusCode.SOLVED || status === SolverIntentStatusCode.FAILED) {
|
|
@@ -59,24 +34,13 @@ export function getSolverStatusRefetchInterval(data: SolverStatusRead, consecuti
|
|
|
59
34
|
return STATUS_POLL_MS;
|
|
60
35
|
}
|
|
61
36
|
|
|
62
|
-
/** The read shape the budget counts: a solver that has not seen this intent. */
|
|
63
37
|
export function isSolverNotFound(data: SolverStatusRead): boolean {
|
|
64
38
|
return (data?.ok ? data.value.status : undefined) === SolverIntentStatusCode.NOT_FOUND;
|
|
65
39
|
}
|
|
66
40
|
|
|
67
41
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* - solver arm → passes through, so its status drives the budget as it does for `useStatus`;
|
|
71
|
-
* - backend arm → `undefined`, which resets the budget; the solver is not the one being asked;
|
|
72
|
-
* - failed read → counts as `NOT_FOUND` **only** when the relay has no packet for this source tx
|
|
73
|
-
* (`DETAILED_STATUS_NOT_DELIVERED`). That miss is ambiguous — a transfer still in flight and one
|
|
74
|
-
* whose tx never relayed look the same — so a budget is the only way to stop the second case.
|
|
75
|
-
*
|
|
76
|
-
* Every other `LOOKUP_FAILED` is a dependency failing right now: the relay unreachable, a malformed
|
|
77
|
-
* response, the solver down. Those reset the budget and keep polling, because that is how the read
|
|
78
|
-
* recovers when the outage ends. Counting them would stop polling a healthy transfer after ~2 min of
|
|
79
|
-
* downtime and never resume.
|
|
42
|
+
* Only `DETAILED_STATUS_NOT_DELIVERED` failures count as NOT_FOUND; other lookup failures are outages
|
|
43
|
+
* and reset the budget so polling resumes when they end.
|
|
80
44
|
*/
|
|
81
45
|
export function toNotFoundBudgetRead(data: SolverDetailedStatusRead): SolverStatusRead {
|
|
82
46
|
if (data === undefined) return undefined;
|
|
@@ -88,28 +52,14 @@ export function toNotFoundBudgetRead(data: SolverDetailedStatusRead): SolverStat
|
|
|
88
52
|
: undefined;
|
|
89
53
|
}
|
|
90
54
|
|
|
91
|
-
/**
|
|
92
|
-
* Polling interval for a detailed-status read. Backend records report terminality in their own
|
|
93
|
-
* vocabulary; everything else reuses the solver policy verbatim, so `MAX_NOT_FOUND_POLLS` stays the
|
|
94
|
-
* single cutoff — for a forgotten intent and for a transfer no source can resolve.
|
|
95
|
-
*
|
|
96
|
-
* A rejected API key is the one stop that sits outside both: `retryUnlessAuthFailure` cannot catch
|
|
97
|
-
* it, because the SDK returns the 401/403 as a `Result` rather than throwing, so React Query never
|
|
98
|
-
* sees an error to withhold a retry from. Without this branch a bad key polls forever whenever the
|
|
99
|
-
* relay has also not delivered — the miss is unprovable behind an unanswered backend, so it never
|
|
100
|
-
* consumes the not-delivered budget either.
|
|
101
|
-
*/
|
|
102
55
|
export function getSolverDetailedStatusRefetchInterval(
|
|
103
56
|
data: SolverDetailedStatusRead,
|
|
104
57
|
consecutiveNotFound: number,
|
|
105
58
|
): number | false {
|
|
106
|
-
//
|
|
59
|
+
// The SDK returns 401/403 as a Result, so `retryUnlessAuthFailure` never sees it; stop here instead.
|
|
107
60
|
if (data && !data.ok && isAuthFailure(data.error)) return false;
|
|
108
61
|
if (data?.ok && data.value.source === 'backend') {
|
|
109
|
-
//
|
|
110
|
-
// records to the solver so only `'solved'` reaches us, but that is its routing rule, not this
|
|
111
|
-
// hook's contract — encoding the wire contract keeps a terminal record from being polled forever
|
|
112
|
-
// if it changes.
|
|
62
|
+
// SDK currently routes 'failed' to the solver; still treat it as terminal in case routing changes.
|
|
113
63
|
const { status } = data.value.data;
|
|
114
64
|
return status === 'solved' || status === 'failed' ? false : STATUS_POLL_MS;
|
|
115
65
|
}
|
|
@@ -1,77 +1,26 @@
|
|
|
1
|
-
// packages/dapp-kit/src/hooks/shared/types.ts
|
|
2
1
|
import type { UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
* Subset of `UseQueryOptions` consumers may override on dapp-kit read hooks.
|
|
6
|
-
*
|
|
7
|
-
* `queryKey`, `queryFn`, and `enabled` are owned by the hook (the latter is derived
|
|
8
|
-
* from required-input presence) and are intentionally stripped here.
|
|
9
|
-
*/
|
|
3
|
+
/** `enabled` is hook-owned: derived from required-input presence. */
|
|
10
4
|
export type ReadQueryOptions<TData, TError = Error> = Omit<
|
|
11
5
|
UseQueryOptions<TData, TError>,
|
|
12
6
|
'queryKey' | 'queryFn' | 'enabled'
|
|
13
7
|
>;
|
|
14
8
|
|
|
15
|
-
/**
|
|
16
|
-
* Canonical params shape for read-only `useQuery`-backed hooks in dapp-kit.
|
|
17
|
-
*
|
|
18
|
-
* Two top-level keys, always:
|
|
19
|
-
* - `params`: SDK-feature-domain inputs (the "what" being fetched)
|
|
20
|
-
* - `queryOptions`: React Query behavior knobs (the "how" the query behaves)
|
|
21
|
-
*
|
|
22
|
-
* For hooks with no required inputs, leave `TParams` at its default and accept the
|
|
23
|
-
* whole params object as optional at the call site.
|
|
24
|
-
*/
|
|
25
9
|
export type ReadHookParams<TData, TParams = Record<string, never>, TError = Error> = {
|
|
26
10
|
params?: TParams;
|
|
27
11
|
queryOptions?: ReadQueryOptions<TData, TError>;
|
|
28
12
|
};
|
|
29
13
|
|
|
30
14
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* `mutationFn` is owned by the hook (it's the SDK integration point) and is
|
|
34
|
-
* intentionally stripped here. Everything else — `mutationKey`, `retry`, `gcTime`,
|
|
35
|
-
* `networkMode`, `onMutate`, `onSuccess`, `onError`, `onSettled`, `meta`, etc. —
|
|
36
|
-
* is consumer-overridable.
|
|
37
|
-
*
|
|
38
|
-
* Conventions enforced by every dapp-kit mutation hook:
|
|
39
|
-
*
|
|
40
|
-
* 1. **`mutationFn` throws on SDK failure.** The SDK returns `Result<T>`; the hook
|
|
41
|
-
* unwraps `result.value` on `ok` and throws `result.error` on `!ok`. So `TData`
|
|
42
|
-
* here is the unwrapped success type, not `Result<T>`. React Query's native
|
|
43
|
-
* error model — `isError`, `error`, `onError`, `retry`, `throwOnError`, devtools —
|
|
44
|
-
* works exactly as documented.
|
|
45
|
-
*
|
|
46
|
-
* 2. **`onSuccess` is composed.** Hook-owned invalidations always run first; then
|
|
47
|
-
* the hook awaits `mutationOptions?.onSuccess?.(data, vars, ctx)`. Per-call
|
|
48
|
-
* `mutate(vars, { onSuccess })` runs after both, per TanStack Query's native
|
|
49
|
-
* ordering. Because invalidations now live inside `onSuccess` (not `onSettled`),
|
|
50
|
-
* they only fire on confirmed success — failed mutations never trigger them.
|
|
51
|
-
*
|
|
52
|
-
* 3. **A default `mutationKey` is set per hook** (e.g. `['mm', 'supply']`,
|
|
53
|
-
* `['swap']`) so consumers can use `useIsMutating(['mm'])` and
|
|
54
|
-
* `useMutationState` without guessing the key shape. The default is set
|
|
55
|
-
* *before* `...mutationOptions` is spread, so consumers can override it.
|
|
15
|
+
* `TData` is the unwrapped success type (hooks throw on SDK failure). A consumer `onSuccess` runs after
|
|
16
|
+
* the hook's own invalidations; the default `mutationKey` is overridable.
|
|
56
17
|
*/
|
|
57
18
|
export type MutationHookOptions<TData, TVars, TError = Error, TContext = unknown> = Omit<
|
|
58
19
|
UseMutationOptions<TData, TError, TVars, TContext>,
|
|
59
20
|
'mutationFn'
|
|
60
21
|
>;
|
|
61
22
|
|
|
62
|
-
/**
|
|
63
|
-
* Canonical params shape for `useMutation`-backed hooks in dapp-kit.
|
|
64
|
-
*
|
|
65
|
-
* One top-level key:
|
|
66
|
-
* - `mutationOptions`: React Query behavior knobs (the "how" the mutation behaves)
|
|
67
|
-
*
|
|
68
|
-
* Domain inputs (params, walletProvider, etc.) are NOT here — they belong in
|
|
69
|
-
* `TVars` and flow through `mutate(vars)` so the call site can vary them per
|
|
70
|
-
* invocation without re-rendering the hook.
|
|
71
|
-
*
|
|
72
|
-
* Always accept the whole params object as optional at the call site:
|
|
73
|
-
* `useFoo({ mutationOptions } = {})`.
|
|
74
|
-
*/
|
|
23
|
+
/** Domain inputs belong in `TVars` and flow through `mutate(vars)`, not here. */
|
|
75
24
|
export type MutationHookParams<TData, TVars, TError = Error, TContext = unknown> = {
|
|
76
25
|
mutationOptions?: MutationHookOptions<TData, TVars, TError, TContext>;
|
|
77
26
|
};
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import type { Result } from '@sodax/sdk';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Translates the SDK's `Result<T>` contract into the React Query contract: returns `value` on
|
|
5
|
-
* success, throws `error` on failure. Non-`Error` throwables are wrapped so consumers always get
|
|
6
|
-
* an `Error` in `mutation.error` / catch blocks.
|
|
7
|
-
*
|
|
8
|
-
* Used by every dapp-kit mutation hook so the React Query error model (`isError`, `error`,
|
|
9
|
-
* `onError`, `retry`, `throwOnError`, devtools) engages uniformly.
|
|
10
|
-
*/
|
|
3
|
+
/** Returns `value` or throws `error`; non-`Error` failures are wrapped so consumers always get an `Error`. */
|
|
11
4
|
export function unwrapResult<T>(result: Result<T>): T {
|
|
12
5
|
if (!result.ok) {
|
|
13
6
|
if (result.error instanceof Error) throw result.error;
|
|
@@ -4,13 +4,7 @@ import { useSodaxContext } from './useSodaxContext.js';
|
|
|
4
4
|
import { unwrapResult } from './unwrapResult.js';
|
|
5
5
|
import type { ReadHookParams } from './types.js';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Domain inputs for {@link useBalances}. `chainKey` is optional so the hook can be mounted before a
|
|
9
|
-
* chain is selected; `enabled` gates execution on every required field being present.
|
|
10
|
-
*
|
|
11
|
-
* The SDK reads the chain named by `chainKey` and ignores `token.chainKey`, so a token that does
|
|
12
|
-
* not live on that chain reads as `0n` rather than erroring — keep the two in step.
|
|
13
|
-
*/
|
|
7
|
+
/** The SDK ignores `token.chainKey`: a token not on `chainKey` reads as `0n` rather than erroring. */
|
|
14
8
|
export interface BalancesInputs {
|
|
15
9
|
chainKey: SpokeChainKey | undefined;
|
|
16
10
|
tokens: readonly XToken[];
|
|
@@ -23,16 +17,10 @@ export type UseBalancesParams = ReadHookParams<Record<string, bigint>, BalancesI
|
|
|
23
17
|
|
|
24
18
|
const REFETCH_INTERVAL_MS = 5_000;
|
|
25
19
|
|
|
26
|
-
/**
|
|
27
|
-
* Pure builder for {@link useBalances} query options. Exported for unit tests and for advanced
|
|
28
|
-
* callers that compose their own `useQuery` wrapper. Takes the SDK instance explicitly (the hook
|
|
29
|
-
* supplies it from context).
|
|
30
|
-
*/
|
|
31
20
|
export function getBalancesQueryOptions(sodax: Sodax, { chainKey, tokens, address, accountAddress }: BalancesInputs) {
|
|
32
21
|
const tonAccount = chainKey === ChainKeys.TON_MAINNET && accountAddress ? { accountAddress } : undefined;
|
|
33
22
|
return {
|
|
34
|
-
//
|
|
35
|
-
// e.g. scam tokens copying a legitimate ticker).
|
|
23
|
+
// Symbol alone can collide (scam tokens copy tickers), so pair it with the address.
|
|
36
24
|
queryKey: [
|
|
37
25
|
'shared',
|
|
38
26
|
'balances',
|
|
@@ -49,35 +37,14 @@ export function getBalancesQueryOptions(sodax: Sodax, { chainKey, tokens, addres
|
|
|
49
37
|
},
|
|
50
38
|
enabled: !!chainKey && !!address && tokens.length > 0,
|
|
51
39
|
refetchInterval: REFETCH_INTERVAL_MS,
|
|
52
|
-
//
|
|
53
|
-
//
|
|
54
|
-
// token address, so its native balance would render as the new chain's until the fetch lands.
|
|
40
|
+
// No `keepPreviousData`: EVM chains share the native token address, so a chain switch would
|
|
41
|
+
// show the previous chain's native balance until the fetch lands.
|
|
55
42
|
};
|
|
56
43
|
}
|
|
57
44
|
|
|
58
45
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* object mapping each token's address to its balance in smallest units.
|
|
62
|
-
*
|
|
63
|
-
* This is the SDK-backed successor to {@link useXBalances} (which wraps a wallet-layer `xService`).
|
|
64
|
-
* Prefer this when the app already has a `SodaxProvider`.
|
|
65
|
-
*
|
|
66
|
-
* Failure model: a token that could not be read is logged by the SDK and reported as `0n`, so a
|
|
67
|
-
* flaky RPC is indistinguishable from an empty wallet — always the conservative direction, since
|
|
68
|
-
* under-reporting blocks a spend rather than permitting one. The query only errors when the whole
|
|
69
|
-
* batch is unusable: a shared round-trip every token depends on, or a batch in which no token
|
|
70
|
-
* could be read at all.
|
|
71
|
-
*
|
|
72
|
-
* Chain-specific notes: Stellar XLM reports the *spendable* amount (total minus the minimum reserve
|
|
73
|
-
* and selling liabilities); Bitcoin returns `0n` for Rune tokens, whose amounts are not exposed by
|
|
74
|
-
* the UTXO endpoint.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```tsx
|
|
78
|
-
* const { data: balances } = useBalances({ params: { chainKey, address, tokens } });
|
|
79
|
-
* const usdcBalance = balances?.[usdc.address] ?? 0n;
|
|
80
|
-
* ```
|
|
46
|
+
* Balances keyed by token address. An unreadable token reports `0n`; the query errors only when the
|
|
47
|
+
* whole batch fails. Stellar XLM is the spendable amount; Bitcoin Runes read `0n`.
|
|
81
48
|
*/
|
|
82
49
|
export function useBalances({ params, queryOptions }: UseBalancesParams = {}): UseQueryResult<Record<string, bigint>> {
|
|
83
50
|
const { sodax } = useSodaxContext();
|
|
@@ -4,8 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
4
4
|
const invalidateQueries = vi.fn();
|
|
5
5
|
const requestTrustline = vi.fn();
|
|
6
6
|
|
|
7
|
-
//
|
|
8
|
-
// React-side imports lets the real onSuccess run without a DOM.
|
|
7
|
+
// Stubbing the React-side imports lets the real onSuccess run without a DOM.
|
|
9
8
|
vi.mock('@tanstack/react-query', () => ({ useQueryClient: () => ({ invalidateQueries }) }));
|
|
10
9
|
vi.mock('./useSodaxContext.js', () => ({
|
|
11
10
|
useSodaxContext: () => ({ sodax: { spoke: { stellar: { requestTrustline } } } }),
|
|
@@ -53,15 +52,12 @@ describe('useEstablishTrustline — invalidation after a broadcast trustline', (
|
|
|
53
52
|
});
|
|
54
53
|
|
|
55
54
|
it('SURVIVES a wallet that fails right after signing, and still invalidates', async () => {
|
|
56
|
-
// The trustline is already on-chain here: throwing would report success as an
|
|
57
|
-
// error and leave the gate showing "needs a trustline" until a manual refetch.
|
|
58
55
|
const rejecting = vars(async () => {
|
|
59
56
|
throw new Error('wallet is locked');
|
|
60
57
|
});
|
|
61
58
|
|
|
62
59
|
await expect(optionsOf().onSuccess('hash', rejecting, undefined)).resolves.toBeUndefined();
|
|
63
60
|
|
|
64
|
-
// Falls back to the whole prefix, because the account is no longer identifiable.
|
|
65
61
|
expect(invalidatedKeys()).toEqual([
|
|
66
62
|
['shared', 'stellarTrustlineCheck'],
|
|
67
63
|
['sponsoring', 'stellarAccountStatus'],
|
|
@@ -13,13 +13,7 @@ export type UseEstablishTrustlineVars = {
|
|
|
13
13
|
walletProvider: IStellarWalletProvider;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* Establish a Stellar trustline. The account pays its fee and reserve; use
|
|
18
|
-
* {@link useStellarGate} to verify it can afford them.
|
|
19
|
-
*
|
|
20
|
-
* Supersedes the deprecated `useRequestTrustline`, which keeps the hand-rolled
|
|
21
|
-
* shape released in 2.0.0.
|
|
22
|
-
*/
|
|
16
|
+
/** The account pays the fee and reserve; use {@link useStellarGate} to verify it can afford them. */
|
|
23
17
|
export function useEstablishTrustline({
|
|
24
18
|
mutationOptions,
|
|
25
19
|
}: MutationHookParams<string, UseEstablishTrustlineVars> = {}): SafeUseMutationResult<
|
|
@@ -34,8 +28,7 @@ export function useEstablishTrustline({
|
|
|
34
28
|
mutationKey: ['shared', 'establishTrustline'],
|
|
35
29
|
...mutationOptions,
|
|
36
30
|
mutationFn: async ({ token, amount, srcChainKey, walletProvider }): Promise<string> => {
|
|
37
|
-
//
|
|
38
|
-
// sizes the trustline check, so zero is a caller bug rather than a request.
|
|
31
|
+
// The amount sizes the trustline check, so `0n` is a caller bug.
|
|
39
32
|
if (!token || !amount) {
|
|
40
33
|
throw new Error('Token and amount are required');
|
|
41
34
|
}
|
|
@@ -50,11 +43,8 @@ export function useEstablishTrustline({
|
|
|
50
43
|
});
|
|
51
44
|
},
|
|
52
45
|
onSuccess: async (data, vars, ctx) => {
|
|
53
|
-
//
|
|
54
|
-
// accounts mid-flow must not turn success into an error — nor skip the
|
|
55
|
-
// invalidations below, which is what an unguarded throw would do.
|
|
46
|
+
// Already broadcast: a locked/switched wallet must not turn success into an error or skip invalidation.
|
|
56
47
|
const srcAddress = await vars.walletProvider.getWalletAddress().catch(() => undefined);
|
|
57
|
-
// Without the address, invalidate the whole prefix rather than nothing.
|
|
58
48
|
queryClient.invalidateQueries({
|
|
59
49
|
queryKey: srcAddress
|
|
60
50
|
? ['shared', 'stellarTrustlineCheck', vars.srcChainKey, vars.token, srcAddress]
|