---
id: I0142
title: "Chrome extension balance badge"
url: https://carbium.sh/library/integrate/chrome-extension-balance-badge
collection: Integrations
category: Mobile and desktop
revision: 1
updated: 2026-11-23
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Chrome extension balance badge

A Chrome extension background script reads one wallet's SOL balance through Carbium every 30 seconds and sets the toolbar badge to a short figure. You get the formatter and the read in one module that also runs in Node.

## What the badge shows

A Chrome extension can set a short text on its toolbar icon with `chrome.action.setBadgeText`. The badge fits only a few characters, so the figure is shortened: two decimals under 10 SOL, one decimal up to 999 SOL, and thousands after that. The full balance and the read time go into the badge title, so a reader who hovers over the icon sees the exact figure.

The background script reads the balance with `getBalance` every 30 seconds. The answer is a count of lamports, the smallest unit, where 1 SOL is 1,000,000,000 lamports. The code keeps that count as a bigint and converts it only for display.

| State | When | Badge |
|---|---|---|
| fresh | The read answered. | The new balance text. |
| held | A 429 came back, with a retry-after hint. | The last text stays. |
| failed | Any other error, including a 401. | The last text stays and the log names the error. |

## The Solana read

The call goes to [the RPC endpoint](https://carbium.sh/docs/endpoints), `https://services.carbium.sh` by default, with the key in the [`x-api-key` header](https://carbium.sh/docs/auth). The key comes from `CARBIUM_KEY`, and the code exits with a message when it is unset.

The commitment is written into the call as `confirmed`. A wallet's balance moves as transactions land, so a read at `confirmed` can show a balance that is still settling. The address goes through `address()` first, which checks that the string is a base58 public key and throws on a typo, so a bad input fails before any request leaves the extension.

## The extension side

In the extension, `readBadge` runs in the background service worker. The browser can stop that worker between reads, so each read writes the badge and nothing needs to survive in memory. Only the key source and the badge calls change from the Node run: the extension reads the key from its own storage and passes the two values `readBadge` returns to `chrome.action.setBadgeText` and `chrome.action.setTitle`.

> Anything in an extension can be read by whoever installs it. A key shipped in extension code, or stored where the page can read it, is a key anyone can copy. Every key on an account shares one budget, so a copied key spends the budget your app runs on. Keep the key on a server you run and have the extension call that server.

## The badge module

The file has one job: read a balance and format it. It reads the key from the environment, exits with a message when the key or the address is missing, and prints the badge text, the title and the read time as JSON. The extension's background script calls the same `readBadge` function.

`main.ts` (run: `node main.ts JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4`)

```ts
// I0142 · Chrome extension balance badge
// https://carbium.sh/library/integrate/chrome-extension-balance-badge
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

import {
  address,
  createDefaultRpcTransport,
  createSolanaRpcFromTransport,
  isSolanaError,
  SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR,
} from "@solana/kit";

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium key.");
  process.exit(1);
}
const rpc = createSolanaRpcFromTransport(
  createDefaultRpcTransport({
    url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh",
    headers: { "x-api-key": key },
  }),
);

// Badge text fits the toolbar: two decimals under 10 SOL, one under 1,000, then thousands.
export function badgeText(lamports: bigint): string {
  const sol = Number(lamports) / 1e9;
  if (sol >= 1000) return `${Math.round(sol / 1000)}k`;
  return sol.toFixed(sol >= 10 ? 1 : 2);
}

// One read at confirmed commitment. A missing account reads as 0 lamports.
export async function readBadge(wallet: string) {
  const { value } = await rpc
    .getBalance(address(wallet), { commitment: "confirmed" })
    .send({ abortSignal: AbortSignal.timeout(5_000) });
  return {
    text: badgeText(value),
    title: `${(Number(value) / 1e9).toFixed(4)} SOL, read at confirmed ${new Date().toISOString()}`,
  };
}

const wallet = process.argv[2];
if (!wallet) {
  console.error("Usage: node main.ts <public address>");
  process.exit(1);
}
try {
  const badge = await readBadge(wallet);
  console.log(JSON.stringify({ address: wallet, ...badge }, null, 2));
} catch (err) {
  if (isSolanaError(err, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR) && err.context.statusCode === 429) {
    console.error("429: keep the last badge text and wait the retry-after hint.");
  } else {
    console.error("balance read failed:", err instanceof Error ? err.message : String(err));
  }
  process.exitCode = 1;
}
```

## Run it on one address

Install `@solana/kit@8.6.0`, set `"type": "module"` in `package.json`, set `CARBIUM_KEY` and pass a public address as the first argument. Node 24 strips the types on load. The example address is a busy program account that holds SOL. The output is one JSON object with the address, the badge text and the title.

A new wallet with no SOL reads as 0 and shows `0.00`. A missing account also reads as 0, so the badge cannot tell an empty wallet from one that was never funded.

## What polling costs against your limits

One badge reads one address every 30 seconds, which is two calls a minute. Limits are [per account](https://carbium.sh/docs/limits), so every key on the account shares one budget, and other apps on that account draw from the same budget as the badge does.

A call over its rate [waits once](https://carbium.sh/docs/limits) and is served if its budget refills within a second. Past that, the call gets 429 with a retry-after hint. The extension keeps the last badge text on a 429 and waits the hint before the next read, so a busy account only delays the badge.

## Where it ends

- It reads SOL for one address. Token balances need their own reads, and a wallet with several token accounts for one mint needs each one counted.
- The badge updates on the next poll, so a transfer shows up as much as 30 seconds late.
- The read is at `confirmed`, so a balance can show before it is final.
- The Node run proves the read and the formatting. It does not load the extension, so the manifest, the permissions and the badge calls need a check inside Chrome.
- The badge holds the latest read only and keeps no history.
- The file does not sign or send anything, and it has no key of its own: the key comes from the environment of whoever runs it.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| The badge keeps its last text and the log says `balance read failed` with `HTTP error (401)`. | The key is unknown to the service, usually because it was cut short or pasted with a space. Attach the whole key in the `x-api-key` header, and check that `CARBIUM_KEY` holds the key itself. |
| The badge stops changing and the log says `429: keep the last badge text`. | The account's budget is spent, often by app traffic on other keys. Lower that traffic or lengthen the poll interval, and wait the retry-after hint before the next read. |
| Every read returns `0.00` for a wallet that holds SOL. | Check the address first: a string can decode to a different key, which reads as an empty account. Copy the public key with no spaces or line breaks, and compare it with the address the wallet shows. |
| `readBadge` throws before any request, with an error about the address. | The argument is not a base58 public key. Remove spaces, quotes and line breaks from the value, and pass the address as the wallet displays it. |
| The badge text is cut off or shows the wrong unit. | Chrome shows only a few characters on the icon. Keep the text to the formatter's output and move detail into the title, which `readBadge` already fills. |

## Questions

### Can the badge show a token balance as well?

Yes. Use `getTokenAccountBalance` for each token account and show it in the title or in a second badge. Count those reads into the same account budget as the SOL read.

### Why use confirmed for the badge?

The commitment is one argument in the call, and the title states the level used. Change it in one place if a later read matters more to you than a fresh one.

### Does the badge update the moment a transfer lands?

No. It updates on the next poll, so the 30-second interval sets how late it can be. A shorter interval reads sooner and spends the account budget faster.

### Can I test the formatter without Chrome?

Yes. The file runs as a Node script and prints the same text and title the extension would set, so you can check the figures before you load the extension.

## Related

- [Wallet watch list at scale](https://carbium.sh/library/integrate/wallet-watch-list-at-scale) (Integrations, I0011)
- [Wallet token holdings](https://carbium.sh/library/usecase/wallet-token-holdings) (Use cases, UC0005)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
- [carbium.sh/docs/limits](https://carbium.sh/docs/limits)
