Skip to content

Commit d7708f9

Browse files
authored
Merge pull request #156 from codexhange/feat/use-wallet-balance
feat(react): add useWalletBalance query hook
2 parents f066933 + ac35fdc commit d7708f9

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

packages/react/src/index.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import type {
7272
TransactionListParams,
7373
TransferInput,
7474
Wallet,
75+
WalletBalance,
7576
WebhookEventEnvelope,
7677
WebhookEventName,
7778
} from '@astroid/types';
@@ -238,9 +239,54 @@ export function useWallet(
238239
}
239240

240241
/**
241-
* List agents.
242-
* @param params Filter params.
243-
* @param options Custom query options (`queryKey`, `staleTime`, `refetchInterval`, `gcTime`, etc.).
242+
* Fetch the live on-chain balance of a single wallet, with a sensible
243+
* stale-time default so balances stay fresh without hammering the API.
244+
*
245+
* The query is automatically **disabled** when `walletId` is `undefined` or
246+
* empty. Balances are inherently volatile, so by default the result is marked
247+
* stale after 15s; pass `options.staleTime` to tune, or set
248+
* `refetchInterval` (e.g. `30_000`) to poll while mounted.
249+
*
250+
* @param walletId The wallet to read balances for. Pass `undefined` to skip.
251+
* @param options Extra TanStack Query options (`enabled`, `staleTime`,
252+
* `refetchInterval`, etc.). The `queryKey`/`queryFn` are set
253+
* internally and cannot be overridden.
254+
* @returns A TanStack Query result with `data` (a {@link WalletBalance}),
255+
* `isLoading`, `isError`, `error`, etc.
256+
*
257+
* @example
258+
* ```tsx
259+
* const { data: balance, isStale } = useWalletBalance(activeWalletId, {
260+
* refetchInterval: 30_000,
261+
* });
262+
* ```
263+
*/
264+
export function useWalletBalance(
265+
walletId: string | undefined,
266+
options?: ReadOptions<WalletBalance>,
267+
): UseQueryResult<WalletBalance, Error> {
268+
const astroid = useAstroid();
269+
return useQuery({
270+
queryKey: queryKeys.wallets.balance(walletId ?? ''),
271+
queryFn: () => astroid.wallets.balance(walletId as string),
272+
enabled: Boolean(walletId) && options?.enabled !== false,
273+
staleTime: 15_000, // balances go stale quickly; refresh on refocus/interval
274+
...options,
275+
});
276+
}
277+
278+
/**
279+
* Fetch a paginated list of AI agents.
280+
*
281+
* @param params Optional filters: `page`, `pageSize`, `walletId`, etc.
282+
* @param options Extra TanStack Query options.
283+
* @returns A TanStack Query result with `data` (a {@link Paginated} of
284+
* {@link Agent}), `isLoading`, `isError`, `error`, etc.
285+
*
286+
* @example
287+
* ```tsx
288+
* const { data } = useAgents({ walletId: 'wal_123' });
289+
* ```
244290
*/
245291
export function useAgents(
246292
params?: AgentListParams,

0 commit comments

Comments
 (0)