Hooks
useSolBalance

useSolBalance

Fetch the SOL balance of any Solana address.

Import

import { useSolBalance } from '@wankmi/wankmi'

Usage

import { useWallet, useSolBalance } from '@wankmi/wankmi'
 
function Balance() {
  const { publicKey } = useWallet()
  const { data: balance, isLoading, isError } = useSolBalance({
    address: publicKey,
  })
 
  if (!publicKey) return <p>Not connected</p>
  if (isLoading) return <p>Loading...</p>
  if (isError) return <p>Failed to fetch balance</p>
 
  return <p>{balance} SOL</p>
}

Parameters

ParameterTypeRequiredDescription
addressPublicKey | null | undefinedNoThe address to fetch the balance for. Query is disabled when null or undefined.

Return Value

This hook returns a TanStack Query result object. The most useful fields:

PropertyTypeDescription
datanumber | undefinedBalance in SOL (not lamports)
isLoadingbooleanTrue on the first fetch
isFetchingbooleanTrue whenever a fetch is in progress
isErrorbooleanTrue if the last fetch failed
errorError | nullThe error, if any
refetch() => voidManually trigger a refetch

The balance is returned in SOL, not lamports. To get lamports, multiply by 1_000_000_000 or use LAMPORTS_PER_SOL from @solana/web3.js.

Behaviour

  • The query is disabled when address is null or undefined, so it's safe to pass publicKey from useWallet directly — it won't fire until the wallet is connected.
  • Data is automatically refetched in the background by TanStack Query according to its default stale time.