{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "balance",
  "title": "Balance",
  "description": "Native or ERC-20 balance of the connected account (or any address) formatted with TokenAmount. ERC-20 balance, decimals and symbol are read in one multicall. Refreshes on an interval. Loading skeleton (skullet), '—' on error, nothing when there is no address. Exports useTokenBalance with refetch. data-phase: idle | loading | ready | error.",
  "dependencies": [
    "wagmi",
    "viem",
    "skullet"
  ],
  "registryDependencies": [
    "@0xui/token-amount"
  ],
  "files": [
    {
      "path": "registry/default/components/balance.tsx",
      "content": "// Docs: https://0xui.io/components/balance.md\nimport { SkeletonBox } from \"skullet/react\";\nimport { erc20Abi, type Address } from \"viem\";\nimport { useBalance, useConnection, useReadContracts } from \"wagmi\";\nimport { TokenAmount, type TokenAmountProps } from \"@/components/token-amount\";\nimport { cn } from \"@/lib/utils\";\n\nexport type BalancePhase = \"idle\" | \"loading\" | \"ready\" | \"error\";\n\n/** SkeletonBox is a block by default. This one sits in a line of text. */\nconst inlineBox = { display: \"inline-block\", verticalAlign: \"middle\" } as const;\n\nexport type UseTokenBalanceParameters = {\n  /** Whose balance. Defaults to the connected account. */\n  address?: Address;\n  /** ERC-20 contract. Omit for the chain's native currency. */\n  token?: Address;\n  /** Chain to read. Defaults to the current chain. */\n  chainId?: number;\n  /** Refetch interval in ms. Pass false to fetch once. */\n  refreshInterval?: number | false;\n  /** Set to false to skip reading, e.g. until a token is chosen. */\n  enabled?: boolean;\n};\n\n/**\n * Native or ERC-20 balance with its decimals and symbol. ERC-20 reads are\n * batched into one multicall.\n */\nexport function useTokenBalance({\n  address: addressProp,\n  token,\n  chainId,\n  refreshInterval = 15_000,\n  enabled: enabledProp = true,\n}: UseTokenBalanceParameters = {}) {\n  const connection = useConnection();\n  const address = enabledProp ? (addressProp ?? connection.address) : undefined;\n\n  const native = useBalance({\n    address,\n    chainId,\n    query: { enabled: Boolean(address) && !token, refetchInterval: refreshInterval },\n  });\n\n  const erc20 = useReadContracts({\n    allowFailure: false,\n    contracts: [\n      { address: token, abi: erc20Abi, functionName: \"balanceOf\", args: [address!], chainId },\n      { address: token, abi: erc20Abi, functionName: \"decimals\", chainId },\n      { address: token, abi: erc20Abi, functionName: \"symbol\", chainId },\n    ],\n    query: {\n      enabled: Boolean(address) && Boolean(token),\n      refetchInterval: refreshInterval,\n    },\n  });\n\n  const query = token ? erc20 : native;\n  const data = token\n    ? erc20.data && {\n        value: erc20.data[0],\n        decimals: erc20.data[1],\n        symbol: erc20.data[2],\n      }\n    : native.data;\n\n  const phase: BalancePhase = !address\n    ? \"idle\"\n    : query.isError\n      ? \"error\"\n      : data\n        ? \"ready\"\n        : \"loading\";\n\n  return {\n    address,\n    value: data?.value,\n    decimals: data?.decimals,\n    symbol: data?.symbol,\n    phase,\n    error: query.error,\n    refetch: query.refetch,\n  };\n}\n\nexport type BalanceProps = UseTokenBalanceParameters &\n  Omit<TokenAmountProps, \"value\" | \"decimals\" | \"symbol\"> & {\n    /** Override the symbol read from the chain or contract. */\n    symbol?: string;\n  };\n\n/**\n * Balance of the connected account (or any address), native or ERC-20,\n * formatted with TokenAmount. Renders nothing when there is no address.\n */\nexport function Balance({\n  address,\n  token,\n  chainId,\n  refreshInterval,\n  symbol: symbolProp,\n  animate = true,\n  className,\n  ...format\n}: BalanceProps) {\n  const { value, decimals, symbol, phase } = useTokenBalance({\n    address,\n    token,\n    chainId,\n    refreshInterval,\n  });\n\n  if (phase === \"idle\") return null;\n\n  if (phase === \"loading\") {\n    return (\n      <span\n        data-slot=\"balance\"\n        data-phase={phase}\n        className={cn(\"inline-block align-middle\", className)}\n      >\n        {/* About the width of \"0.1234 ETH\" at the current font size. */}\n        <SkeletonBox width=\"5.5em\" height=\"1em\" style={inlineBox} />\n        <span className=\"sr-only\">Loading balance</span>\n      </span>\n    );\n  }\n\n  if (phase === \"error\") {\n    return (\n      <span\n        data-slot=\"balance\"\n        data-phase={phase}\n        title=\"Couldn't load balance\"\n        className={cn(\"text-muted-foreground\", className)}\n      >\n        —<span className=\"sr-only\">Couldn't load balance</span>\n      </span>\n    );\n  }\n\n  return (\n    <span\n      data-slot=\"balance\"\n      data-phase={phase}\n      className={cn(\"inline-flex\", className)}\n    >\n      <TokenAmount\n        value={value!}\n        decimals={decimals!}\n        symbol={symbolProp ?? symbol}\n        animate={animate}\n        {...format}\n      />\n    </span>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/balance.md",
  "categories": [
    "tokens"
  ],
  "type": "registry:component"
}