Skip to content

useWatchBalance Hook

The useWatchBalance hook watches an address balance on a given chain and re-fetches it on every new block. It is a thin wrapper around wagmi's useBalance that invalidates the underlying query whenever the block number changes.

Import

import { useWatchBalance } from "@scaffold-ui/hooks";

Usage

const { data, isLoading, isError } = useWatchBalance({
  address: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
  chain: mainnet,
});

Parameters

useWatchBalance accepts every parameter wagmi's useBalance accepts, plus an optional chain.

ParameterTypeDefaultDescription
addressAddress (optional)-The address to watch. When omitted, no balance is fetched.
chainChain (optional)-The viem chain to fetch the balance from. Internally maps to wagmi's chainId option.
...restUseBalanceParameters-Any other useBalance parameter.

Return Values

The hook returns wagmi's useBalance result, without the internal queryKey (it's used internally to invalidate on each new block).

PropertyTypeDescription
dataGetBalanceReturnType | undefinedThe balance payload (value, decimals, formatted, symbol).
isLoadingbooleanLoading state for the balance fetch.
isErrorbooleanError state for the balance fetch.
...restUseBalanceReturnTypeAll other wagmi useBalance return values (refetch, status, error).

Live Examples

Basic Usage

Display a balance that automatically updates each block.

Fetching balance…

import React from "react";
import { useWatchBalance } from "@scaffold-ui/hooks";
import { mainnet } from "viem/chains";
import { formatEther } from "viem";
 
function LiveBalance() {
  const address = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
  const { data, isLoading, isError } = useWatchBalance({ address, chain: mainnet });
 
  if (isLoading) return <p>Fetching balance…</p>;
  if (isError || !data) return <p>Could not fetch balance</p>;
 
  return (
    <p>
      Balance: {Number(formatEther(data.value)).toLocaleString()} {data.symbol}
    </p>
  );
}