@spacesops/wdk-react-native-core 1.0.0-beta.40
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 +551 -0
- package/dist/__mocks__/react.d.ts +40 -0
- package/dist/__mocks__/react.js +33 -0
- package/dist/__mocks__/react.js.map +1 -0
- package/dist/__mocks__/secureStorage.d.ts +26 -0
- package/dist/__mocks__/secureStorage.js +80 -0
- package/dist/__mocks__/secureStorage.js.map +1 -0
- package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
- package/dist/__tests__/hooks/useBalance.test.js +217 -0
- package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
- package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWallet.test.js +105 -0
- package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
- package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
- package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
- package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
- package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
- package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWorklet.test.js +168 -0
- package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
- package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
- package/dist/__tests__/services/accountService.test.d.ts +6 -0
- package/dist/__tests__/services/accountService.test.js +216 -0
- package/dist/__tests__/services/accountService.test.js.map +1 -0
- package/dist/__tests__/services/addressService.test.d.ts +6 -0
- package/dist/__tests__/services/addressService.test.js +246 -0
- package/dist/__tests__/services/addressService.test.js.map +1 -0
- package/dist/__tests__/services/balanceService.test.d.ts +6 -0
- package/dist/__tests__/services/balanceService.test.js +333 -0
- package/dist/__tests__/services/balanceService.test.js.map +1 -0
- package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSetupService.test.js +304 -0
- package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
- package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
- package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
- package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
- package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
- package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
- package/dist/__tests__/setup.d.ts +7 -0
- package/dist/__tests__/setup.js +49 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
- package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
- package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
- package/dist/__tests__/store/walletStore.test.d.ts +4 -0
- package/dist/__tests__/store/walletStore.test.js +86 -0
- package/dist/__tests__/store/walletStore.test.js.map +1 -0
- package/dist/__tests__/store/workletStore.test.d.ts +4 -0
- package/dist/__tests__/store/workletStore.test.js +255 -0
- package/dist/__tests__/store/workletStore.test.js.map +1 -0
- package/dist/__tests__/types/hrpc.test.d.ts +4 -0
- package/dist/__tests__/types/hrpc.test.js +99 -0
- package/dist/__tests__/types/hrpc.test.js.map +1 -0
- package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/balanceUtils.test.js +133 -0
- package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
- package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
- package/dist/__tests__/utils/errorHandling.test.js +119 -0
- package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
- package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/errorUtils.test.js +133 -0
- package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
- package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
- package/dist/__tests__/utils/initializationState.test.js +243 -0
- package/dist/__tests__/utils/initializationState.test.js.map +1 -0
- package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/jsonUtils.test.js +118 -0
- package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
- package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
- package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
- package/dist/__tests__/utils/raceConditions.test.js +239 -0
- package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
- package/dist/__tests__/utils/result.test.d.ts +4 -0
- package/dist/__tests__/utils/result.test.js +66 -0
- package/dist/__tests__/utils/result.test.js.map +1 -0
- package/dist/__tests__/utils/schemas.test.d.ts +4 -0
- package/dist/__tests__/utils/schemas.test.js +66 -0
- package/dist/__tests__/utils/schemas.test.js.map +1 -0
- package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
- package/dist/__tests__/utils/storeHelpers.test.js +227 -0
- package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
- package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
- package/dist/__tests__/utils/typeGuards.test.js +175 -0
- package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
- package/dist/__tests__/utils/validation.test.d.ts +4 -0
- package/dist/__tests__/utils/validation.test.js +126 -0
- package/dist/__tests__/utils/validation.test.js.map +1 -0
- package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/walletUtils.test.js +156 -0
- package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
- package/dist/hooks/useBalance.d.ts +170 -0
- package/dist/hooks/useBalance.js +442 -0
- package/dist/hooks/useBalance.js.map +1 -0
- package/dist/hooks/useWallet.d.ts +43 -0
- package/dist/hooks/useWallet.js +193 -0
- package/dist/hooks/useWallet.js.map +1 -0
- package/dist/hooks/useWalletManager.d.ts +100 -0
- package/dist/hooks/useWalletManager.js +526 -0
- package/dist/hooks/useWalletManager.js.map +1 -0
- package/dist/hooks/useWdkApp.d.ts +66 -0
- package/dist/hooks/useWdkApp.js +74 -0
- package/dist/hooks/useWdkApp.js.map +1 -0
- package/dist/hooks/useWorklet.d.ts +66 -0
- package/dist/hooks/useWorklet.js +42 -0
- package/dist/hooks/useWorklet.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/provider/WdkAppProvider.d.ts +112 -0
- package/dist/provider/WdkAppProvider.js +558 -0
- package/dist/provider/WdkAppProvider.js.map +1 -0
- package/dist/services/accountService.d.ts +52 -0
- package/dist/services/accountService.js +119 -0
- package/dist/services/accountService.js.map +1 -0
- package/dist/services/addressService.d.ts +22 -0
- package/dist/services/addressService.js +110 -0
- package/dist/services/addressService.js.map +1 -0
- package/dist/services/balanceService.d.ts +114 -0
- package/dist/services/balanceService.js +208 -0
- package/dist/services/balanceService.js.map +1 -0
- package/dist/services/walletSetupService.d.ts +217 -0
- package/dist/services/walletSetupService.js +535 -0
- package/dist/services/walletSetupService.js.map +1 -0
- package/dist/services/walletSwitchingService.d.ts +48 -0
- package/dist/services/walletSwitchingService.js +143 -0
- package/dist/services/walletSwitchingService.js.map +1 -0
- package/dist/services/workletLifecycleService.d.ts +117 -0
- package/dist/services/workletLifecycleService.js +433 -0
- package/dist/services/workletLifecycleService.js.map +1 -0
- package/dist/storage/mmkvStorage.d.ts +44 -0
- package/dist/storage/mmkvStorage.js +348 -0
- package/dist/storage/mmkvStorage.js.map +1 -0
- package/dist/store/walletStore.d.ts +130 -0
- package/dist/store/walletStore.js +204 -0
- package/dist/store/walletStore.js.map +1 -0
- package/dist/store/workletStore.d.ts +121 -0
- package/dist/store/workletStore.js +256 -0
- package/dist/store/workletStore.js.map +1 -0
- package/dist/types/hrpc.d.ts +60 -0
- package/dist/types/hrpc.js +26 -0
- package/dist/types/hrpc.js.map +1 -0
- package/dist/types.d.ts +168 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/balanceUtils.d.ts +30 -0
- package/dist/utils/balanceUtils.js +86 -0
- package/dist/utils/balanceUtils.js.map +1 -0
- package/dist/utils/constants.d.ts +66 -0
- package/dist/utils/constants.js +73 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/errorHandling.d.ts +47 -0
- package/dist/utils/errorHandling.js +54 -0
- package/dist/utils/errorHandling.js.map +1 -0
- package/dist/utils/errorUtils.d.ts +89 -0
- package/dist/utils/errorUtils.js +264 -0
- package/dist/utils/errorUtils.js.map +1 -0
- package/dist/utils/initializationState.d.ts +129 -0
- package/dist/utils/initializationState.js +220 -0
- package/dist/utils/initializationState.js.map +1 -0
- package/dist/utils/jsonUtils.d.ts +21 -0
- package/dist/utils/jsonUtils.js +67 -0
- package/dist/utils/jsonUtils.js.map +1 -0
- package/dist/utils/logger.d.ts +27 -0
- package/dist/utils/logger.js +50 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/mmkvKeyManager.d.ts +57 -0
- package/dist/utils/mmkvKeyManager.js +214 -0
- package/dist/utils/mmkvKeyManager.js.map +1 -0
- package/dist/utils/mnemonicUtils.d.ts +17 -0
- package/dist/utils/mnemonicUtils.js +28 -0
- package/dist/utils/mnemonicUtils.js.map +1 -0
- package/dist/utils/operationMutex.d.ts +69 -0
- package/dist/utils/operationMutex.js +150 -0
- package/dist/utils/operationMutex.js.map +1 -0
- package/dist/utils/result.d.ts +34 -0
- package/dist/utils/result.js +43 -0
- package/dist/utils/result.js.map +1 -0
- package/dist/utils/schemas.d.ts +365 -0
- package/dist/utils/schemas.js +152 -0
- package/dist/utils/schemas.js.map +1 -0
- package/dist/utils/storeHelpers.d.ts +112 -0
- package/dist/utils/storeHelpers.js +198 -0
- package/dist/utils/storeHelpers.js.map +1 -0
- package/dist/utils/typeGuards.d.ts +70 -0
- package/dist/utils/typeGuards.js +95 -0
- package/dist/utils/typeGuards.js.map +1 -0
- package/dist/utils/validation.d.ts +53 -0
- package/dist/utils/validation.js +152 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/walletStateHelpers.d.ts +62 -0
- package/dist/utils/walletStateHelpers.js +81 -0
- package/dist/utils/walletStateHelpers.js.map +1 -0
- package/dist/utils/walletUtils.d.ts +38 -0
- package/dist/utils/walletUtils.js +87 -0
- package/dist/utils/walletUtils.js.map +1 -0
- package/package.json +79 -0
- package/src/__mocks__/react.ts +34 -0
- package/src/__mocks__/secureStorage.ts +87 -0
- package/src/__tests__/hooks/useBalance.test.ts +286 -0
- package/src/__tests__/hooks/useWallet.test.ts +122 -0
- package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
- package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
- package/src/__tests__/hooks/useWorklet.test.ts +195 -0
- package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
- package/src/__tests__/services/accountService.test.ts +337 -0
- package/src/__tests__/services/addressService.test.ts +312 -0
- package/src/__tests__/services/balanceService.test.ts +411 -0
- package/src/__tests__/services/walletSetupService.test.ts +463 -0
- package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
- package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
- package/src/__tests__/setup.ts +54 -0
- package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
- package/src/__tests__/store/walletStore.test.ts +101 -0
- package/src/__tests__/store/workletStore.test.ts +329 -0
- package/src/__tests__/types/hrpc.test.ts +125 -0
- package/src/__tests__/utils/balanceUtils.test.ts +151 -0
- package/src/__tests__/utils/errorHandling.test.ts +147 -0
- package/src/__tests__/utils/errorUtils.test.ts +170 -0
- package/src/__tests__/utils/initializationState.test.ts +337 -0
- package/src/__tests__/utils/jsonUtils.test.ts +131 -0
- package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
- package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
- package/src/__tests__/utils/raceConditions.test.ts +292 -0
- package/src/__tests__/utils/result.test.ts +73 -0
- package/src/__tests__/utils/schemas.test.ts +82 -0
- package/src/__tests__/utils/storeHelpers.test.ts +323 -0
- package/src/__tests__/utils/typeGuards.test.ts +209 -0
- package/src/__tests__/utils/validation.test.ts +152 -0
- package/src/__tests__/utils/walletUtils.test.ts +195 -0
- package/src/hooks/useBalance.ts +597 -0
- package/src/hooks/useWallet.ts +288 -0
- package/src/hooks/useWalletManager.ts +654 -0
- package/src/hooks/useWdkApp.ts +78 -0
- package/src/hooks/useWorklet.ts +109 -0
- package/src/index.ts +112 -0
- package/src/provider/WdkAppProvider.tsx +764 -0
- package/src/services/accountService.ts +134 -0
- package/src/services/addressService.ts +125 -0
- package/src/services/balanceService.ts +291 -0
- package/src/services/walletSetupService.ts +695 -0
- package/src/services/walletSwitchingService.ts +157 -0
- package/src/services/workletLifecycleService.ts +561 -0
- package/src/storage/mmkvStorage.ts +417 -0
- package/src/store/walletStore.ts +301 -0
- package/src/store/workletStore.ts +340 -0
- package/src/types/hrpc.ts +78 -0
- package/src/types.ts +188 -0
- package/src/utils/balanceUtils.ts +96 -0
- package/src/utils/constants.ts +84 -0
- package/src/utils/errorHandling.md +163 -0
- package/src/utils/errorHandling.ts +61 -0
- package/src/utils/errorUtils.ts +300 -0
- package/src/utils/initializationState.ts +242 -0
- package/src/utils/jsonUtils.ts +75 -0
- package/src/utils/logger.ts +54 -0
- package/src/utils/mmkvKeyManager.ts +256 -0
- package/src/utils/mnemonicUtils.ts +35 -0
- package/src/utils/operationMutex.ts +181 -0
- package/src/utils/result.ts +57 -0
- package/src/utils/schemas.ts +190 -0
- package/src/utils/storeHelpers.ts +236 -0
- package/src/utils/typeGuards.ts +129 -0
- package/src/utils/validation.ts +174 -0
- package/src/utils/walletStateHelpers.ts +104 -0
- package/src/utils/walletUtils.ts +107 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balance Hooks with TanStack Query
|
|
3
|
+
*
|
|
4
|
+
* Provides React hooks for fetching and managing wallet balances using TanStack Query.
|
|
5
|
+
*
|
|
6
|
+
* ## Single Source of Truth Architecture
|
|
7
|
+
*
|
|
8
|
+
* This module uses Zustand as the single source of truth for balances, with TanStack Query
|
|
9
|
+
* as the fetching and caching layer.
|
|
10
|
+
*
|
|
11
|
+
* ### Zustand Store (Single Source of Truth)
|
|
12
|
+
* - **Purpose**: Store and persist balances across app restarts
|
|
13
|
+
* - **Lifetime**: Persisted to MMKV storage (survives app restarts)
|
|
14
|
+
* - **Features**: Single source of truth, persistence, immediate availability on app restart
|
|
15
|
+
* - **Usage**: All balance reads should go through TanStack Query hooks, which read from Zustand
|
|
16
|
+
*
|
|
17
|
+
* ### TanStack Query (Fetching & Caching Layer)
|
|
18
|
+
* - **Purpose**: Balance fetching, caching, refetching, and stale time management
|
|
19
|
+
* - **Lifetime**: Runtime-only (cache cleared on app restart, but reads initial data from Zustand)
|
|
20
|
+
* - **Features**: Automatic refetching, stale time management, cache invalidation, optimistic updates
|
|
21
|
+
* - **Usage**: Always use TanStack Query hooks (`useBalance`, `useBalances`) for balance operations
|
|
22
|
+
*
|
|
23
|
+
* ### Data Flow:
|
|
24
|
+
* 1. On app start: TanStack Query reads initial data from Zustand (via `initialData`)
|
|
25
|
+
* 2. If data is stale or missing: TanStack Query fetches balance from worklet (via `fetchBalance()`)
|
|
26
|
+
* 3. After fetch: Balance is updated in Zustand store (single source of truth update)
|
|
27
|
+
* 4. TanStack Query cache is updated with the fetched data
|
|
28
|
+
* 5. Components re-render with fresh data from TanStack Query
|
|
29
|
+
*
|
|
30
|
+
* ### Sync Guarantees:
|
|
31
|
+
* - **Zustand is always the source of truth** - TanStack Query reads from and writes to Zustand
|
|
32
|
+
* - **No sync logic needed** - TanStack Query updates Zustand directly after fetch
|
|
33
|
+
* - **Initial data consistency** - TanStack Query uses Zustand's persisted data as `initialData`
|
|
34
|
+
* - **No race conditions** - All updates go through Zustand, ensuring consistency
|
|
35
|
+
*
|
|
36
|
+
* ### Important Notes:
|
|
37
|
+
* - **Always use TanStack Query hooks** for balance operations - they handle Zustand integration
|
|
38
|
+
* - **Zustand is the single source of truth** - TanStack Query is a fetching/caching layer on top
|
|
39
|
+
* - **Addresses remain in Zustand only** (derived/computed state, deterministic, no refetching needed)
|
|
40
|
+
* - **Updates happen automatically** in `fetchBalance()` - Zustand is updated after each successful fetch
|
|
41
|
+
*/
|
|
42
|
+
import type { BalanceFetchResult, TokenConfigProvider } from '../types';
|
|
43
|
+
/**
|
|
44
|
+
* Balance query options
|
|
45
|
+
*/
|
|
46
|
+
export interface BalanceQueryOptions {
|
|
47
|
+
/** Whether the query is enabled */
|
|
48
|
+
enabled?: boolean;
|
|
49
|
+
/** Refetch interval in milliseconds (false to disable) */
|
|
50
|
+
refetchInterval?: number | false;
|
|
51
|
+
/** Stale time in milliseconds */
|
|
52
|
+
staleTime?: number;
|
|
53
|
+
/** Wallet identifier (defaults to activeWalletId) */
|
|
54
|
+
walletId?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Parameters for balance operations
|
|
58
|
+
*/
|
|
59
|
+
export interface BalanceParams {
|
|
60
|
+
/** Network name */
|
|
61
|
+
network: string;
|
|
62
|
+
/** Account index */
|
|
63
|
+
accountIndex: number;
|
|
64
|
+
/** Token address (null for native token) */
|
|
65
|
+
tokenAddress: string | null;
|
|
66
|
+
/** Optional wallet identifier */
|
|
67
|
+
walletId?: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Parameters for refreshing balances
|
|
71
|
+
*/
|
|
72
|
+
export interface RefreshBalanceParams {
|
|
73
|
+
/** Network name (required for 'token' or 'network' type) */
|
|
74
|
+
network?: string;
|
|
75
|
+
/** Account index */
|
|
76
|
+
accountIndex: number;
|
|
77
|
+
/** Token address (required for 'token' type) */
|
|
78
|
+
tokenAddress?: string | null;
|
|
79
|
+
/** Refresh type: 'token' (single), 'wallet' (all for wallet), 'network' (all for network), 'all' (everything) */
|
|
80
|
+
type?: 'token' | 'wallet' | 'network' | 'all';
|
|
81
|
+
/** Wallet identifier (defaults to activeWalletId) */
|
|
82
|
+
walletId?: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Query key factory for balance queries
|
|
86
|
+
*/
|
|
87
|
+
export declare const balanceQueryKeys: {
|
|
88
|
+
all: readonly ["balances"];
|
|
89
|
+
byWallet: (walletId: string, accountIndex: number) => readonly ["balances", "wallet", string, number];
|
|
90
|
+
byNetwork: (network: string) => readonly ["balances", "network", string];
|
|
91
|
+
byWalletAndNetwork: (walletId: string, accountIndex: number, network: string) => readonly ["balances", "wallet", string, number, "network", string];
|
|
92
|
+
byToken: (walletId: string, accountIndex: number, network: string, tokenAddress: string | null) => readonly ["balances", "wallet", string, number, "network", string, "token", string];
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Hook to fetch a single balance
|
|
96
|
+
*
|
|
97
|
+
* @param network - Network name
|
|
98
|
+
* @param accountIndex - Account index
|
|
99
|
+
* @param tokenAddress - Token address (null for native token)
|
|
100
|
+
* @param options - Query options (enabled, refetchInterval, identifier, etc.)
|
|
101
|
+
* @returns TanStack Query result with balance data
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```tsx
|
|
105
|
+
* const { data: balance, isLoading, error } = useBalance('ethereum', 0, null)
|
|
106
|
+
*
|
|
107
|
+
* // With specific wallet identifier
|
|
108
|
+
* const { data: balance } = useBalance('ethereum', 0, null, { walletId: 'user@example.com' })
|
|
109
|
+
*
|
|
110
|
+
* if (isLoading) return <Loading />
|
|
111
|
+
* if (error) return <Error message={error.message} />
|
|
112
|
+
* if (balance?.success) {
|
|
113
|
+
* return <Text>Balance: {balance.balance}</Text>
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function useBalance(network: string, accountIndex: number, tokenAddress: string | null, options?: BalanceQueryOptions): import("@tanstack/react-query").UseQueryResult<BalanceFetchResult, Error>;
|
|
118
|
+
/**
|
|
119
|
+
* Hook to fetch all balances for a wallet across all networks
|
|
120
|
+
*
|
|
121
|
+
* @param accountIndex - Account index
|
|
122
|
+
* @param tokenConfigs - Token configurations
|
|
123
|
+
* @param options - Query options (including identifier)
|
|
124
|
+
* @returns TanStack Query result with all balances
|
|
125
|
+
*/
|
|
126
|
+
export declare function useBalancesForWallet(accountIndex: number, tokenConfigs: TokenConfigProvider, options?: BalanceQueryOptions): import("@tanstack/react-query").UseQueryResult<BalanceFetchResult[], Error>;
|
|
127
|
+
/**
|
|
128
|
+
* Hook to fetch balances for multiple wallets
|
|
129
|
+
*
|
|
130
|
+
* This hook is designed to handle dynamic arrays of wallets without violating React's Rules of Hooks.
|
|
131
|
+
* It uses TanStack Query's useQueries which is specifically designed for this use case.
|
|
132
|
+
*
|
|
133
|
+
* @param wallets - Array of wallets with accountIndex and identifier
|
|
134
|
+
* @param tokenConfigs - Token configurations
|
|
135
|
+
* @param options - Query options (enabled, refetchInterval, etc.)
|
|
136
|
+
* @returns Array of TanStack Query results, one for each wallet
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```tsx
|
|
140
|
+
* const wallets = [
|
|
141
|
+
* { accountIndex: 0, identifier: 'user@example.com' },
|
|
142
|
+
* { accountIndex: 1, identifier: 'channel-123' },
|
|
143
|
+
* ]
|
|
144
|
+
* const balanceQueries = useBalancesForWallets(wallets, tokenConfigs, { enabled: true })
|
|
145
|
+
*
|
|
146
|
+
* const isLoading = balanceQueries.some(q => q.isLoading)
|
|
147
|
+
* const hasError = balanceQueries.some(q => q.isError)
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function useBalancesForWallets(wallets: Array<{
|
|
151
|
+
accountIndex: number;
|
|
152
|
+
identifier: string;
|
|
153
|
+
}>, tokenConfigs: TokenConfigProvider, options?: BalanceQueryOptions): import("@tanstack/react-query").DefinedUseQueryResult<BalanceFetchResult[], Error>[];
|
|
154
|
+
/**
|
|
155
|
+
* Hook to invalidate and refetch balances
|
|
156
|
+
*
|
|
157
|
+
* @returns Mutation function to refresh balances
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```tsx
|
|
161
|
+
* const { mutate: refreshBalance } = useRefreshBalance()
|
|
162
|
+
*
|
|
163
|
+
* // Refresh single balance
|
|
164
|
+
* refreshBalance({ network: 'ethereum', accountIndex: 0, tokenAddress: null })
|
|
165
|
+
*
|
|
166
|
+
* // Refresh all balances for a wallet
|
|
167
|
+
* refreshBalance({ accountIndex: 0, type: 'wallet' })
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare function useRefreshBalance(): import("@tanstack/react-query").UseMutationResult<void, Error, RefreshBalanceParams, unknown>;
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balance Hooks with TanStack Query
|
|
3
|
+
*
|
|
4
|
+
* Provides React hooks for fetching and managing wallet balances using TanStack Query.
|
|
5
|
+
*
|
|
6
|
+
* ## Single Source of Truth Architecture
|
|
7
|
+
*
|
|
8
|
+
* This module uses Zustand as the single source of truth for balances, with TanStack Query
|
|
9
|
+
* as the fetching and caching layer.
|
|
10
|
+
*
|
|
11
|
+
* ### Zustand Store (Single Source of Truth)
|
|
12
|
+
* - **Purpose**: Store and persist balances across app restarts
|
|
13
|
+
* - **Lifetime**: Persisted to MMKV storage (survives app restarts)
|
|
14
|
+
* - **Features**: Single source of truth, persistence, immediate availability on app restart
|
|
15
|
+
* - **Usage**: All balance reads should go through TanStack Query hooks, which read from Zustand
|
|
16
|
+
*
|
|
17
|
+
* ### TanStack Query (Fetching & Caching Layer)
|
|
18
|
+
* - **Purpose**: Balance fetching, caching, refetching, and stale time management
|
|
19
|
+
* - **Lifetime**: Runtime-only (cache cleared on app restart, but reads initial data from Zustand)
|
|
20
|
+
* - **Features**: Automatic refetching, stale time management, cache invalidation, optimistic updates
|
|
21
|
+
* - **Usage**: Always use TanStack Query hooks (`useBalance`, `useBalances`) for balance operations
|
|
22
|
+
*
|
|
23
|
+
* ### Data Flow:
|
|
24
|
+
* 1. On app start: TanStack Query reads initial data from Zustand (via `initialData`)
|
|
25
|
+
* 2. If data is stale or missing: TanStack Query fetches balance from worklet (via `fetchBalance()`)
|
|
26
|
+
* 3. After fetch: Balance is updated in Zustand store (single source of truth update)
|
|
27
|
+
* 4. TanStack Query cache is updated with the fetched data
|
|
28
|
+
* 5. Components re-render with fresh data from TanStack Query
|
|
29
|
+
*
|
|
30
|
+
* ### Sync Guarantees:
|
|
31
|
+
* - **Zustand is always the source of truth** - TanStack Query reads from and writes to Zustand
|
|
32
|
+
* - **No sync logic needed** - TanStack Query updates Zustand directly after fetch
|
|
33
|
+
* - **Initial data consistency** - TanStack Query uses Zustand's persisted data as `initialData`
|
|
34
|
+
* - **No race conditions** - All updates go through Zustand, ensuring consistency
|
|
35
|
+
*
|
|
36
|
+
* ### Important Notes:
|
|
37
|
+
* - **Always use TanStack Query hooks** for balance operations - they handle Zustand integration
|
|
38
|
+
* - **Zustand is the single source of truth** - TanStack Query is a fetching/caching layer on top
|
|
39
|
+
* - **Addresses remain in Zustand only** (derived/computed state, deterministic, no refetching needed)
|
|
40
|
+
* - **Updates happen automatically** in `fetchBalance()` - Zustand is updated after each successful fetch
|
|
41
|
+
*/
|
|
42
|
+
import { useQuery, useQueries, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
43
|
+
import { AccountService } from '../services/accountService';
|
|
44
|
+
import { BalanceService } from '../services/balanceService';
|
|
45
|
+
import { getWalletStore } from '../store/walletStore';
|
|
46
|
+
import { getWorkletStore } from '../store/workletStore';
|
|
47
|
+
import { resolveWalletId } from '../utils/storeHelpers';
|
|
48
|
+
import { convertBalanceToString } from '../utils/balanceUtils';
|
|
49
|
+
import { ACCOUNT_METHOD_GET_BALANCE, ACCOUNT_METHOD_GET_TOKEN_BALANCE, NATIVE_TOKEN_KEY, DEFAULT_QUERY_STALE_TIME_MS, DEFAULT_QUERY_GC_TIME_MS, } from '../utils/constants';
|
|
50
|
+
import { logError } from '../utils/logger';
|
|
51
|
+
import { validateWalletParams } from '../utils/validation';
|
|
52
|
+
/**
|
|
53
|
+
* Query key factory for balance queries
|
|
54
|
+
*/
|
|
55
|
+
export const balanceQueryKeys = {
|
|
56
|
+
all: ['balances'],
|
|
57
|
+
byWallet: (walletId, accountIndex) => ['balances', 'wallet', walletId, accountIndex],
|
|
58
|
+
byNetwork: (network) => ['balances', 'network', network],
|
|
59
|
+
byWalletAndNetwork: (walletId, accountIndex, network) => ['balances', 'wallet', walletId, accountIndex, 'network', network],
|
|
60
|
+
byToken: (walletId, accountIndex, network, tokenAddress) => ['balances', 'wallet', walletId, accountIndex, 'network', network, 'token', tokenAddress || NATIVE_TOKEN_KEY],
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Check if a query should be enabled
|
|
64
|
+
*/
|
|
65
|
+
function isQueryEnabled(enabledOption, isInitialized, additionalCondition = true) {
|
|
66
|
+
return (enabledOption !== false) && isInitialized && additionalCondition;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validate and parse a balance query key structure
|
|
70
|
+
*
|
|
71
|
+
* @param queryKey - Query key array from balanceQueryKeys.byToken()
|
|
72
|
+
* @returns Validated query key components
|
|
73
|
+
* @throws Error if query key structure is invalid
|
|
74
|
+
*/
|
|
75
|
+
function validateQueryKeyStructure(queryKey) {
|
|
76
|
+
// Validate queryKey structure before destructuring
|
|
77
|
+
if (!Array.isArray(queryKey) || queryKey.length < 8) {
|
|
78
|
+
throw new Error(`Invalid queryKey structure: ${JSON.stringify(queryKey)}`);
|
|
79
|
+
}
|
|
80
|
+
const [, , , accountIdx, , network, , tokenAddress] = queryKey;
|
|
81
|
+
// Validate types instead of using assertions
|
|
82
|
+
if (typeof accountIdx !== 'number' || accountIdx < 0) {
|
|
83
|
+
throw new Error(`Invalid accountIndex in queryKey: ${accountIdx}`);
|
|
84
|
+
}
|
|
85
|
+
if (typeof network !== 'string' || network.length === 0) {
|
|
86
|
+
throw new Error(`Invalid network in queryKey: ${network}`);
|
|
87
|
+
}
|
|
88
|
+
if (tokenAddress !== NATIVE_TOKEN_KEY && (typeof tokenAddress !== 'string' || tokenAddress.length === 0)) {
|
|
89
|
+
throw new Error(`Invalid tokenAddress in queryKey: ${tokenAddress}`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
accountIndex: accountIdx,
|
|
93
|
+
network,
|
|
94
|
+
tokenAddress: tokenAddress === NATIVE_TOKEN_KEY ? null : tokenAddress,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Fetch balance for a specific token (native or ERC20)
|
|
99
|
+
*
|
|
100
|
+
* @param network - Network name
|
|
101
|
+
* @param accountIndex - Account index
|
|
102
|
+
* @param tokenAddress - Token address (null for native token)
|
|
103
|
+
* @param walletId - Optional wallet identifier (defaults to activeWalletId)
|
|
104
|
+
* @returns Promise with balance fetch result
|
|
105
|
+
*/
|
|
106
|
+
async function fetchBalance(network, accountIndex, tokenAddress, walletId) {
|
|
107
|
+
validateWalletParams(network, accountIndex, tokenAddress);
|
|
108
|
+
const workletStore = getWorkletStore();
|
|
109
|
+
if (!workletStore.getState().isInitialized) {
|
|
110
|
+
return {
|
|
111
|
+
success: false,
|
|
112
|
+
network,
|
|
113
|
+
accountIndex,
|
|
114
|
+
tokenAddress,
|
|
115
|
+
balance: null,
|
|
116
|
+
error: 'Wallet not initialized',
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const isNative = tokenAddress === null;
|
|
121
|
+
const methodName = isNative ? ACCOUNT_METHOD_GET_BALANCE : ACCOUNT_METHOD_GET_TOKEN_BALANCE;
|
|
122
|
+
const methodArg = isNative ? null : tokenAddress;
|
|
123
|
+
const balanceResult = await AccountService.callAccountMethod(network, accountIndex, methodName, methodArg);
|
|
124
|
+
// Convert to string (handles BigInt values)
|
|
125
|
+
const balance = convertBalanceToString(balanceResult);
|
|
126
|
+
// Update Zustand store (single source of truth)
|
|
127
|
+
// IMPORTANT: Zustand is the single source of truth for balances.
|
|
128
|
+
// TanStack Query is a fetching/caching layer that reads from and updates Zustand.
|
|
129
|
+
// This update ensures balances are persisted and available immediately on app restart.
|
|
130
|
+
const targetWalletId = resolveWalletId(walletId);
|
|
131
|
+
BalanceService.updateBalance(accountIndex, network, tokenAddress, balance, targetWalletId);
|
|
132
|
+
BalanceService.updateLastBalanceUpdate(network, accountIndex, targetWalletId);
|
|
133
|
+
return {
|
|
134
|
+
success: true,
|
|
135
|
+
network,
|
|
136
|
+
accountIndex,
|
|
137
|
+
tokenAddress,
|
|
138
|
+
balance,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
143
|
+
const tokenInfo = tokenAddress ? `:${tokenAddress}` : '';
|
|
144
|
+
const balanceType = tokenAddress ? 'token' : 'native';
|
|
145
|
+
logError(`Failed to fetch ${balanceType} balance for ${network}:${accountIndex}${tokenInfo}:`, error);
|
|
146
|
+
return {
|
|
147
|
+
success: false,
|
|
148
|
+
network,
|
|
149
|
+
accountIndex,
|
|
150
|
+
tokenAddress,
|
|
151
|
+
balance: null,
|
|
152
|
+
error: errorMessage,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Hook to fetch a single balance
|
|
158
|
+
*
|
|
159
|
+
* @param network - Network name
|
|
160
|
+
* @param accountIndex - Account index
|
|
161
|
+
* @param tokenAddress - Token address (null for native token)
|
|
162
|
+
* @param options - Query options (enabled, refetchInterval, identifier, etc.)
|
|
163
|
+
* @returns TanStack Query result with balance data
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```tsx
|
|
167
|
+
* const { data: balance, isLoading, error } = useBalance('ethereum', 0, null)
|
|
168
|
+
*
|
|
169
|
+
* // With specific wallet identifier
|
|
170
|
+
* const { data: balance } = useBalance('ethereum', 0, null, { walletId: 'user@example.com' })
|
|
171
|
+
*
|
|
172
|
+
* if (isLoading) return <Loading />
|
|
173
|
+
* if (error) return <Error message={error.message} />
|
|
174
|
+
* if (balance?.success) {
|
|
175
|
+
* return <Text>Balance: {balance.balance}</Text>
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export function useBalance(network, accountIndex, tokenAddress, options) {
|
|
180
|
+
const workletStore = getWorkletStore();
|
|
181
|
+
const walletStore = getWalletStore();
|
|
182
|
+
// Check if wallet is initialized
|
|
183
|
+
const isInitialized = workletStore.getState().isInitialized;
|
|
184
|
+
// Get walletId from options or activeWalletId
|
|
185
|
+
const activeWalletId = walletStore.getState().activeWalletId;
|
|
186
|
+
const walletId = options?.walletId || activeWalletId || '__temporary__';
|
|
187
|
+
// Get initial data from Zustand (single source of truth)
|
|
188
|
+
// This ensures balances are available immediately on app restart before refetch
|
|
189
|
+
const initialBalance = BalanceService.getBalance(accountIndex, network, tokenAddress, walletId);
|
|
190
|
+
const initialData = initialBalance !== null
|
|
191
|
+
? {
|
|
192
|
+
success: true,
|
|
193
|
+
network,
|
|
194
|
+
accountIndex,
|
|
195
|
+
tokenAddress,
|
|
196
|
+
balance: initialBalance,
|
|
197
|
+
}
|
|
198
|
+
: undefined;
|
|
199
|
+
return useQuery({
|
|
200
|
+
queryKey: balanceQueryKeys.byToken(walletId, accountIndex, network, tokenAddress),
|
|
201
|
+
queryFn: () => fetchBalance(network, accountIndex, tokenAddress, walletId),
|
|
202
|
+
enabled: isQueryEnabled(options?.enabled, isInitialized),
|
|
203
|
+
refetchInterval: options?.refetchInterval,
|
|
204
|
+
staleTime: options?.staleTime ?? DEFAULT_QUERY_STALE_TIME_MS,
|
|
205
|
+
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
|
206
|
+
// Use Zustand as initial data source (single source of truth)
|
|
207
|
+
initialData,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Build query keys for all tokens across all networks
|
|
212
|
+
*/
|
|
213
|
+
function buildBalanceQueryKeys(walletId, accountIndex, tokenConfigs) {
|
|
214
|
+
const tokenConfigsObj = typeof tokenConfigs === 'function' ? tokenConfigs() : tokenConfigs;
|
|
215
|
+
const networks = Object.keys(tokenConfigsObj);
|
|
216
|
+
return networks.flatMap((network) => {
|
|
217
|
+
const networkTokens = tokenConfigsObj[network];
|
|
218
|
+
if (!networkTokens)
|
|
219
|
+
return [];
|
|
220
|
+
const tokens = [networkTokens.native, ...networkTokens.tokens];
|
|
221
|
+
return tokens.map((token) => balanceQueryKeys.byToken(walletId, accountIndex, network, token.address));
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Fetch balances for all query keys
|
|
226
|
+
*/
|
|
227
|
+
async function fetchBalancesForQueryKeys(queryKeys, walletId) {
|
|
228
|
+
return Promise.all(queryKeys.map(async (queryKey) => {
|
|
229
|
+
const validated = validateQueryKeyStructure(queryKey);
|
|
230
|
+
return fetchBalance(validated.network, validated.accountIndex, validated.tokenAddress, walletId);
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Hook to fetch all balances for a wallet across all networks
|
|
235
|
+
*
|
|
236
|
+
* @param accountIndex - Account index
|
|
237
|
+
* @param tokenConfigs - Token configurations
|
|
238
|
+
* @param options - Query options (including identifier)
|
|
239
|
+
* @returns TanStack Query result with all balances
|
|
240
|
+
*/
|
|
241
|
+
export function useBalancesForWallet(accountIndex, tokenConfigs, options) {
|
|
242
|
+
const workletStore = getWorkletStore();
|
|
243
|
+
const isInitialized = workletStore.getState().isInitialized;
|
|
244
|
+
// Resolve walletId from options or store
|
|
245
|
+
const walletId = resolveWalletId(options?.walletId);
|
|
246
|
+
// Create query keys for all tokens (with walletId)
|
|
247
|
+
const queryKeys = buildBalanceQueryKeys(walletId, accountIndex, tokenConfigs);
|
|
248
|
+
// Get initial data from Zustand (single source of truth)
|
|
249
|
+
// Build initial data array from persisted balances
|
|
250
|
+
const initialData = (() => {
|
|
251
|
+
const tokenConfigsObj = typeof tokenConfigs === 'function' ? tokenConfigs() : tokenConfigs;
|
|
252
|
+
const networks = Object.keys(tokenConfigsObj);
|
|
253
|
+
const initialBalances = [];
|
|
254
|
+
let hasAnyInitialData = false;
|
|
255
|
+
for (const network of networks) {
|
|
256
|
+
const networkTokens = tokenConfigsObj[network];
|
|
257
|
+
if (!networkTokens)
|
|
258
|
+
continue;
|
|
259
|
+
const tokens = [networkTokens.native, ...networkTokens.tokens];
|
|
260
|
+
for (const token of tokens) {
|
|
261
|
+
const balance = BalanceService.getBalance(accountIndex, network, token.address, walletId);
|
|
262
|
+
if (balance !== null) {
|
|
263
|
+
hasAnyInitialData = true;
|
|
264
|
+
initialBalances.push({
|
|
265
|
+
success: true,
|
|
266
|
+
network,
|
|
267
|
+
accountIndex,
|
|
268
|
+
tokenAddress: token.address,
|
|
269
|
+
balance,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
// Include placeholder for missing balances to maintain structure
|
|
274
|
+
initialBalances.push({
|
|
275
|
+
success: false,
|
|
276
|
+
network,
|
|
277
|
+
accountIndex,
|
|
278
|
+
tokenAddress: token.address,
|
|
279
|
+
balance: null,
|
|
280
|
+
error: 'Balance not available',
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// Only return initial data if we have at least one persisted balance
|
|
286
|
+
return hasAnyInitialData ? initialBalances : undefined;
|
|
287
|
+
})();
|
|
288
|
+
return useQuery({
|
|
289
|
+
queryKey: [...balanceQueryKeys.byWallet(walletId, accountIndex), 'all'],
|
|
290
|
+
queryFn: () => fetchBalancesForQueryKeys(queryKeys, walletId),
|
|
291
|
+
enabled: isQueryEnabled(options?.enabled, isInitialized, queryKeys.length > 0),
|
|
292
|
+
refetchInterval: options?.refetchInterval,
|
|
293
|
+
staleTime: options?.staleTime ?? DEFAULT_QUERY_STALE_TIME_MS,
|
|
294
|
+
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
|
295
|
+
// Use Zustand as initial data source (single source of truth)
|
|
296
|
+
initialData,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Hook to fetch balances for multiple wallets
|
|
301
|
+
*
|
|
302
|
+
* This hook is designed to handle dynamic arrays of wallets without violating React's Rules of Hooks.
|
|
303
|
+
* It uses TanStack Query's useQueries which is specifically designed for this use case.
|
|
304
|
+
*
|
|
305
|
+
* @param wallets - Array of wallets with accountIndex and identifier
|
|
306
|
+
* @param tokenConfigs - Token configurations
|
|
307
|
+
* @param options - Query options (enabled, refetchInterval, etc.)
|
|
308
|
+
* @returns Array of TanStack Query results, one for each wallet
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```tsx
|
|
312
|
+
* const wallets = [
|
|
313
|
+
* { accountIndex: 0, identifier: 'user@example.com' },
|
|
314
|
+
* { accountIndex: 1, identifier: 'channel-123' },
|
|
315
|
+
* ]
|
|
316
|
+
* const balanceQueries = useBalancesForWallets(wallets, tokenConfigs, { enabled: true })
|
|
317
|
+
*
|
|
318
|
+
* const isLoading = balanceQueries.some(q => q.isLoading)
|
|
319
|
+
* const hasError = balanceQueries.some(q => q.isError)
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
export function useBalancesForWallets(wallets, tokenConfigs, options) {
|
|
323
|
+
const workletStore = getWorkletStore();
|
|
324
|
+
const isInitialized = workletStore.getState().isInitialized;
|
|
325
|
+
return useQueries({
|
|
326
|
+
queries: wallets.map((wallet) => {
|
|
327
|
+
const { accountIndex, identifier } = wallet;
|
|
328
|
+
// Create query keys for all tokens (with walletId)
|
|
329
|
+
const queryKeys = buildBalanceQueryKeys(identifier, accountIndex, tokenConfigs);
|
|
330
|
+
// Get initial data from Zustand (single source of truth)
|
|
331
|
+
const initialData = (() => {
|
|
332
|
+
const tokenConfigsObj = typeof tokenConfigs === 'function' ? tokenConfigs() : tokenConfigs;
|
|
333
|
+
const networks = Object.keys(tokenConfigsObj);
|
|
334
|
+
const initialBalances = [];
|
|
335
|
+
let hasAnyInitialData = false;
|
|
336
|
+
for (const network of networks) {
|
|
337
|
+
const networkTokens = tokenConfigsObj[network];
|
|
338
|
+
if (!networkTokens)
|
|
339
|
+
continue;
|
|
340
|
+
const tokens = [networkTokens.native, ...networkTokens.tokens];
|
|
341
|
+
for (const token of tokens) {
|
|
342
|
+
const balance = BalanceService.getBalance(accountIndex, network, token.address, identifier);
|
|
343
|
+
if (balance !== null) {
|
|
344
|
+
hasAnyInitialData = true;
|
|
345
|
+
initialBalances.push({
|
|
346
|
+
success: true,
|
|
347
|
+
network,
|
|
348
|
+
accountIndex,
|
|
349
|
+
tokenAddress: token.address,
|
|
350
|
+
balance,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
// Include placeholder for missing balances to maintain structure
|
|
355
|
+
initialBalances.push({
|
|
356
|
+
success: false,
|
|
357
|
+
network,
|
|
358
|
+
accountIndex,
|
|
359
|
+
tokenAddress: token.address,
|
|
360
|
+
balance: null,
|
|
361
|
+
error: 'Balance not available',
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// Only return initial data if we have at least one persisted balance
|
|
367
|
+
return hasAnyInitialData ? initialBalances : undefined;
|
|
368
|
+
})();
|
|
369
|
+
return {
|
|
370
|
+
queryKey: [...balanceQueryKeys.byWallet(identifier, accountIndex), 'all'],
|
|
371
|
+
queryFn: () => fetchBalancesForQueryKeys(queryKeys, identifier),
|
|
372
|
+
enabled: isQueryEnabled(options?.enabled, isInitialized, queryKeys.length > 0),
|
|
373
|
+
refetchInterval: options?.refetchInterval,
|
|
374
|
+
staleTime: options?.staleTime ?? DEFAULT_QUERY_STALE_TIME_MS,
|
|
375
|
+
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
|
376
|
+
// Use Zustand as initial data source (single source of truth)
|
|
377
|
+
initialData,
|
|
378
|
+
};
|
|
379
|
+
}),
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Invalidate balance queries based on refresh type
|
|
384
|
+
*/
|
|
385
|
+
async function invalidateBalanceQueries(queryClient, params, walletId) {
|
|
386
|
+
const { network, accountIndex, tokenAddress, type = 'token' } = params;
|
|
387
|
+
switch (type) {
|
|
388
|
+
case 'all':
|
|
389
|
+
await queryClient.invalidateQueries({ queryKey: balanceQueryKeys.all });
|
|
390
|
+
break;
|
|
391
|
+
case 'wallet':
|
|
392
|
+
await queryClient.invalidateQueries({
|
|
393
|
+
queryKey: balanceQueryKeys.byWallet(walletId, accountIndex),
|
|
394
|
+
});
|
|
395
|
+
break;
|
|
396
|
+
case 'network':
|
|
397
|
+
if (network) {
|
|
398
|
+
await queryClient.invalidateQueries({
|
|
399
|
+
queryKey: balanceQueryKeys.byNetwork(network),
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
break;
|
|
403
|
+
case 'token':
|
|
404
|
+
default:
|
|
405
|
+
if (network && tokenAddress !== undefined) {
|
|
406
|
+
await queryClient.invalidateQueries({
|
|
407
|
+
queryKey: balanceQueryKeys.byToken(walletId, accountIndex, network, tokenAddress),
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Hook to invalidate and refetch balances
|
|
415
|
+
*
|
|
416
|
+
* @returns Mutation function to refresh balances
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```tsx
|
|
420
|
+
* const { mutate: refreshBalance } = useRefreshBalance()
|
|
421
|
+
*
|
|
422
|
+
* // Refresh single balance
|
|
423
|
+
* refreshBalance({ network: 'ethereum', accountIndex: 0, tokenAddress: null })
|
|
424
|
+
*
|
|
425
|
+
* // Refresh all balances for a wallet
|
|
426
|
+
* refreshBalance({ accountIndex: 0, type: 'wallet' })
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
export function useRefreshBalance() {
|
|
430
|
+
const queryClient = useQueryClient();
|
|
431
|
+
return useMutation({
|
|
432
|
+
mutationFn: async (params) => {
|
|
433
|
+
// Resolve walletId from params or store
|
|
434
|
+
const walletId = resolveWalletId(params.walletId);
|
|
435
|
+
// Invalidate queries based on type
|
|
436
|
+
await invalidateBalanceQueries(queryClient, params, walletId);
|
|
437
|
+
// Refetch the invalidated queries
|
|
438
|
+
await queryClient.refetchQueries();
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
//# sourceMappingURL=useBalance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBalance.js","sourceRoot":"","sources":["../../src/hooks/useBalance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAEzF,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAC9D,OAAO,EACL,0BAA0B,EAC1B,gCAAgC,EAChC,gBAAgB,EAChB,2BAA2B,EAC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BAAgB,CAAC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;aAC5D,CAAC,CAAA;YACF,MAAK;QACP,KAAK,SAAS;YACZ,IAAI,OAAO,EAAE,CAAC;gBACZ,MAAM,WAAW,CAAC,iBAAiB,CAAC;oBAClC,QAAQ,EAAE,gBAAgB,CAAC,SAAS,CAAC,OAAO,CAAC;iBAC9C,CAAC,CAAA;YACJ,CAAC;YACD,MAAK;QACP,KAAK,OAAO,CAAC;QACb;YACE,IAAI,OAAO,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;gBAC1C,MAAM,WAAW,CAAC,iBAAiB,CAAC;oBAClC,QAAQ,EAAE,gBAAgB,CAAC,OAAO,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,CAAC;iBAClF,CAAC,CAAA;YACJ,CAAC;YACD,MAAK;IACT,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,WAAW,GAAG,cAAc,EAAE,CAAA;IAEpC,OAAO,WAAW,CAAC;QACjB,UAAU,EAAE,KAAK,EAAE,MAA4B,EAAE,EAAE;YACjD,wCAAwC;YACxC,MAAM,QAAQ,GAAG,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YAEjD,mCAAmC;YACnC,MAAM,wBAAwB,CAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;YAE7D,kCAAkC;YAClC,MAAM,WAAW,CAAC,cAAc,EAAE,CAAA;QACpC,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to interact with wallet data (addresses and account methods)
|
|
3
|
+
*
|
|
4
|
+
* PURPOSE: Use this hook for wallet operations AFTER the wallet has been initialized.
|
|
5
|
+
* This hook provides access to wallet addresses and account methods.
|
|
6
|
+
*
|
|
7
|
+
* When to use which hook:
|
|
8
|
+
* - **App initialization state**: Use `useWdkApp()` to check if app is ready
|
|
9
|
+
* - **Wallet lifecycle** (create, load, import, delete): Use `useWalletManager()`
|
|
10
|
+
* - **Wallet operations** (addresses, account methods): Use this hook (`useWallet()`)
|
|
11
|
+
* - **Balance fetching**: Use `useBalance()` hook with TanStack Query
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* // First, check if app is ready
|
|
16
|
+
* const { isReady } = useWdkApp()
|
|
17
|
+
* if (!isReady) return <LoadingScreen />
|
|
18
|
+
*
|
|
19
|
+
* // Then use wallet operations
|
|
20
|
+
* const { addresses, getAddress, callAccountMethod } = useWallet()
|
|
21
|
+
*
|
|
22
|
+
* // Use specific wallet (automatically switches if needed)
|
|
23
|
+
* const { addresses, getAddress } = useWallet({ walletId: 'user@example.com' })
|
|
24
|
+
*
|
|
25
|
+
* // Note: For creating temporary wallets, use useWalletManager().createTemporaryWallet()
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export interface UseWalletResult {
|
|
29
|
+
addresses: Record<string, Record<number, string>>;
|
|
30
|
+
walletLoading: Record<string, boolean>;
|
|
31
|
+
isInitialized: boolean;
|
|
32
|
+
isSwitchingWallet: boolean;
|
|
33
|
+
switchingToWalletId: string | null;
|
|
34
|
+
switchWalletError: Error | null;
|
|
35
|
+
isTemporaryWallet: boolean;
|
|
36
|
+
getNetworkAddresses: (network: string) => Record<number, string>;
|
|
37
|
+
isLoadingAddress: (network: string, accountIndex?: number) => boolean;
|
|
38
|
+
getAddress: (network: string, accountIndex?: number) => Promise<string>;
|
|
39
|
+
callAccountMethod: <T = unknown>(network: string, accountIndex: number, methodName: string, args?: unknown) => Promise<T>;
|
|
40
|
+
}
|
|
41
|
+
export declare function useWallet(options?: {
|
|
42
|
+
walletId?: string;
|
|
43
|
+
}): UseWalletResult;
|