forked from solana-foundation/connectorkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-wallet-assets.ts
More file actions
319 lines (283 loc) · 9.86 KB
/
use-wallet-assets.ts
File metadata and controls
319 lines (283 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use client';
import { useCallback, useMemo } from 'react';
import { address as toAddress } from '@solana/addresses';
import { useWallet } from '../use-wallet';
import { useSolanaClient } from '../use-kit-solana-client';
import { useSharedQuery } from './use-shared-query';
import type { SharedQueryOptions } from './use-shared-query';
import type { SolanaClient } from '../../lib/kit';
/**
* Token Program IDs
*/
export const TOKEN_PROGRAM_ID = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
export const TOKEN_2022_PROGRAM_ID = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';
/**
* Native SOL mint address (wrapped SOL)
*/
export const NATIVE_SOL_MINT = 'So11111111111111111111111111111111111111112';
/**
* Information about a single token account
*/
export interface TokenAccountInfo {
/** Token account public key */
pubkey: string;
/** Token mint address */
mint: string;
/** Owner address */
owner: string;
/** Raw balance in smallest unit */
amount: bigint;
/** Token decimals */
decimals: number;
/** Whether the account is frozen */
isFrozen: boolean;
/** Which token program this account belongs to */
programId: 'token' | 'token-2022';
}
/**
* Combined wallet assets data
*/
export interface WalletAssetsData {
/** SOL balance in lamports */
lamports: bigint;
/** All token accounts (Token Program + Token-2022) */
tokenAccounts: TokenAccountInfo[];
}
/**
* Options for useWalletAssets hook
*/
export interface UseWalletAssetsOptions<TSelected = WalletAssetsData> extends Omit<
SharedQueryOptions<WalletAssetsData, TSelected>,
'select'
> {
/** Override the Solana client from provider */
client?: SolanaClient | null;
/** Transform/select a subset of data (reduces rerenders) */
select?: (data: WalletAssetsData | undefined) => TSelected;
}
/**
* Return type for useWalletAssets hook
*/
export interface UseWalletAssetsReturn<TSelected = WalletAssetsData> {
/** The wallet assets data (or selected subset) */
data: TSelected;
/** Whether data is loading */
isLoading: boolean;
/** Whether a fetch is in progress */
isFetching: boolean;
/** Error if fetch failed */
error: Error | null;
/** Refetch assets */
refetch: (options?: { signal?: AbortSignal }) => Promise<unknown>;
/** Abort in-flight request */
abort: () => void;
/** Last updated timestamp */
updatedAt: number | null;
}
/**
* Parse a token account from RPC response
*/
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
function parseBigInt(value: unknown): bigint {
if (typeof value === 'bigint') return value;
if (typeof value === 'number' && Number.isSafeInteger(value)) return BigInt(value);
if (typeof value === 'string') {
try {
return BigInt(value);
} catch {
return 0n;
}
}
return 0n;
}
interface ParsedTokenAmount {
amount?: unknown;
decimals?: unknown;
}
interface ParsedTokenAccountInfo {
mint?: unknown;
owner?: unknown;
tokenAmount?: unknown;
state?: unknown;
}
function getParsedTokenAccountInfo(data: unknown): ParsedTokenAccountInfo | null {
if (!isRecord(data)) return null;
const parsed = data.parsed;
if (!isRecord(parsed)) return null;
const info = parsed.info;
if (!isRecord(info)) return null;
return info as ParsedTokenAccountInfo;
}
function parseTokenAccount(
account: { pubkey: unknown; account: { data: unknown } },
programId: 'token' | 'token-2022',
): TokenAccountInfo | null {
const info = getParsedTokenAccountInfo(account.account.data);
if (!info) return null;
const mint = typeof info.mint === 'string' ? info.mint : null;
const owner = typeof info.owner === 'string' ? info.owner : null;
if (!mint || !owner) return null;
const tokenAmount = isRecord(info.tokenAmount) ? (info.tokenAmount as ParsedTokenAmount) : null;
const amount = parseBigInt(tokenAmount?.amount);
const decimals = typeof tokenAmount?.decimals === 'number' ? tokenAmount.decimals : 0;
const state = typeof info.state === 'string' ? info.state : undefined;
return {
pubkey: String(account.pubkey),
mint,
owner,
amount,
decimals,
isFrozen: state === 'frozen',
programId,
};
}
/**
* Generate the query key for wallet assets.
* Use this to invalidate the cache externally.
*
* @param rpcUrl - The RPC URL being used
* @param address - The wallet address
* @returns The stringified query key, or null if params are invalid
*
* @example
* ```tsx
* // Invalidate wallet assets after a transaction
* const key = getWalletAssetsQueryKey(rpcUrl, address);
* if (key) invalidateSharedQuery(key);
* ```
*/
export function getWalletAssetsQueryKey(rpcUrl: string | null, address: string | null): string | null {
if (!rpcUrl || !address) return null;
return JSON.stringify(['wallet-assets', rpcUrl, address]);
}
/**
* Internal hook that fetches both SOL balance and all token accounts.
* Queries both Token Program and Token-2022 in parallel.
*
* This hook is used internally by `useBalance` and `useTokens` to share
* a single RPC query, preventing duplicate requests.
*
* @internal
*
* @example Basic usage
* ```tsx
* const { data, isLoading } = useWalletAssets();
* // data.lamports - SOL balance
* // data.tokenAccounts - all token accounts
* ```
*
* @example With select (reduces rerenders)
* ```tsx
* const { data: lamports } = useWalletAssets({
* select: (assets) => assets?.lamports ?? 0n,
* });
* ```
*/
export function useWalletAssets<TSelected = WalletAssetsData>(
options: UseWalletAssetsOptions<TSelected> = {},
): UseWalletAssetsReturn<TSelected> {
const {
enabled = true,
staleTimeMs = 0,
cacheTimeMs = 5 * 60 * 1000, // 5 minutes
refetchOnMount = 'stale',
refetchIntervalMs = false,
client: clientOverride,
select,
} = options;
const { account, isConnected } = useWallet();
const address = account ? String(account) : null;
const { client: providerClient } = useSolanaClient();
// Use override client if provided, otherwise use provider client
const rpcClient = clientOverride ?? providerClient;
// Generate cache key based on RPC URL and address only
// This ensures useBalance and useTokens share the same cache entry
const key = useMemo(() => {
if (!enabled || !isConnected || !address || !rpcClient) return null;
const rpcUrl =
rpcClient.urlOrMoniker instanceof URL ? rpcClient.urlOrMoniker.toString() : String(rpcClient.urlOrMoniker);
return getWalletAssetsQueryKey(rpcUrl, address);
}, [enabled, isConnected, address, rpcClient]);
// Query function that fetches SOL balance and all token accounts
const queryFn = useCallback(
async (signal: AbortSignal): Promise<WalletAssetsData> => {
if (!isConnected || !address || !rpcClient) {
return { lamports: 0n, tokenAccounts: [] };
}
// Throw on abort - fetchSharedQuery will preserve previous data
if (signal.aborted) {
throw new DOMException('Query aborted', 'AbortError');
}
const rpc = rpcClient.rpc;
const walletAddress = toAddress(address);
const tokenProgramId = toAddress(TOKEN_PROGRAM_ID);
const token2022ProgramId = toAddress(TOKEN_2022_PROGRAM_ID);
// Fetch SOL balance and both token programs in parallel
const [balanceResult, tokenAccountsResult, token2022AccountsResult] = await Promise.all([
rpc.getBalance(walletAddress).send(),
rpc
.getTokenAccountsByOwner(walletAddress, { programId: tokenProgramId }, { encoding: 'jsonParsed' })
.send(),
rpc
.getTokenAccountsByOwner(
walletAddress,
{ programId: token2022ProgramId },
{ encoding: 'jsonParsed' },
)
.send(),
]);
// Check abort after async operations
if (signal.aborted) {
throw new DOMException('Query aborted', 'AbortError');
}
// Parse Token Program accounts
const tokenAccounts: TokenAccountInfo[] = [];
for (const account of tokenAccountsResult.value) {
const parsed = parseTokenAccount(account, 'token');
if (parsed) {
tokenAccounts.push(parsed);
}
}
// Parse Token-2022 accounts
for (const account of token2022AccountsResult.value) {
const parsed = parseTokenAccount(account, 'token-2022');
if (parsed) {
tokenAccounts.push(parsed);
}
}
return {
lamports: balanceResult.value,
tokenAccounts,
};
},
[isConnected, address, rpcClient],
);
// Use shared query with optional select
const { data, error, status, updatedAt, isFetching, refetch, abort } = useSharedQuery<WalletAssetsData, TSelected>(
key,
queryFn,
{
enabled,
staleTimeMs,
cacheTimeMs,
refetchOnMount,
refetchIntervalMs,
select: select as ((data: WalletAssetsData | undefined) => TSelected) | undefined,
},
);
const isLoading = status === 'loading' || status === 'idle';
return useMemo(
() => ({
data: data as TSelected,
isLoading,
isFetching,
error,
refetch,
abort,
updatedAt,
}),
[data, isLoading, isFetching, error, refetch, abort, updatedAt],
);
}