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
| Parameter | Type | Required | Description |
|---|---|---|---|
address | PublicKey | null | undefined | No | The 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:
| Property | Type | Description |
|---|---|---|
data | number | undefined | Balance in SOL (not lamports) |
isLoading | boolean | True on the first fetch |
isFetching | boolean | True whenever a fetch is in progress |
isError | boolean | True if the last fetch failed |
error | Error | null | The error, if any |
refetch | () => void | Manually 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
addressisnullorundefined, so it's safe to passpublicKeyfromuseWalletdirectly — 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.