services
Get a key

library / Integrations / Mobile and desktop / I0141

Tauri tray balance monitor

A small Node process reads one wallet's SOL balance through Carbium every 30 seconds and writes balance.json with lamports, SOL, a slot and a UTC time. A Tauri tray app reads that file on its own timer, so the tray never holds the key.

By Carbium · Mobile and desktop · I0141 · updated 2026-11-23

01

What the process does

The process is one Node file whose only dependency is @solana/kit. It takes one wallet address as its argument. On every poll it asks Carbium for that wallet's balance and for the current slot, both at confirmed, then writes one JSON file in its working directory. The tray reads that file and never calls Carbium.

The file holds five fields. The last one tells the reader whether the value is fresh.

FieldHolds
walletThe address exactly as passed in.
lamportsThe balance as a string. A balance is a bigint, and JSON has no bigint.
solThe same balance divided by one billion, for display.
slotThe slot from the getSlot read, or null when that read failed.
atThe UTC time of the poll. A value whose at is old was not refreshed.

Each poll stands alone. The process keeps no state between polls except its timer, so a restart reads the wallet again and writes a new file at once. The two reads run together in Promise.allSettled, so a failed slot read does not throw away a balance that came back. The balance decides the tray's number, and the slot lets a reader place one snapshot against another when the balance has not moved between them.

02

Commitment and the reads

The reads pass commitment: confirmed in the code. @solana/kit uses confirmed when a call leaves the option out, but the JSON-RPC server's own default is finalized. Writing the level out means anyone reading the code sees which state the balance reflects.

Carbium's RPC endpoint takes both reads. getBalance returns lamports as a bigint, and the code keeps that integer until the display division. getSlot is one of the cheap cached reads, at one cell each. The price of getBalance is listed apart from the cheap reads, so check it on the RPC page before you pick a short poll interval.

A subscription does not replace the timer for a plain SOL wallet. Carbium's WebSocket sends nothing on accountSubscribe for a plain SOL wallet, so a balance change does not arrive that way. The documented route pairs logsSubscribe on the wallet with getBalance, and this process keeps to the timer.

A confirmed read can differ from a later finalized read of the same wallet, because the newest confirmed block may still be replaced. The tray shows what the file held at the last poll, and the next poll reads the current state. The slot field records the slot of that read, so a reader who needs the exact block can compare it with a later read.

03

The tray side

The tray lives in your Tauri app, outside this file. Its own code reads balance.json on a timer, puts the SOL value in the tray title, and marks the value stale when at is older than three poll intervals. The process writes under a temporary name and renames the file, so a read lands on the old file or the new one in full.

Keep the two intervals in step. With the default 30 second poll, a stale mark after 90 seconds means the process has stopped or its reads keep failing, and the log line says which.

A tray menu is small, so the title carries the SOL figure and one menu line carries the at time in local form. A reader who sees a number and a time can judge its age at a glance, without opening the file. The tray can refresh its view on a shorter timer than the poll, since a re-read of an unchanged local file costs nothing on Carbium.

04

The process

Install with npm install @solana/[email protected] and set "type": "module" in package.json. Node 24 strips the types on load. Set CARBIUM_KEY, then run the file with the wallet address. POLL_SECONDS is optional and defaults to 30.

main.ts
// I0141 · Tauri tray balance monitor// https://carbium.sh/library/integrate/tauri-tray-balance-monitor// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport {  address,  createDefaultRpcTransport,  createSolanaRpcFromTransport,  isSolanaError,  SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR,} from "@solana/kit";import { rename, writeFile } from "node:fs/promises";const key = process.env.CARBIUM_KEY;if (!key) {  console.error("Set CARBIUM_KEY to your Carbium key.");  process.exit(1);}const arg = process.argv[2];if (!arg) {  console.error("Usage: node main.ts <wallet-address>");  process.exit(1);}const wallet = address(arg);const rpc = createSolanaRpcFromTransport(  createDefaultRpcTransport({    url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh",    headers: { "x-api-key": key },  }),);const pollMs = Number(process.env.POLL_SECONDS ?? 30) * 1000;const commitment = "confirmed" as const;const why = (e: unknown): string =>  isSolanaError(e, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR)    ? `HTTP error (${e.context.statusCode})`    : e instanceof Error      ? e.message      : String(e);async function poll(): Promise<void> {  const [balance, slot] = await Promise.allSettled([    rpc.getBalance(wallet, { commitment }).send(),    rpc.getSlot({ commitment }).send(),  ]);  if (balance.status === "rejected") {    console.error("getBalance failed:", why(balance.reason));    return;  }  if (slot.status === "rejected") console.error("getSlot failed:", why(slot.reason));  const lamports = balance.value.value;  const snap = {    wallet: wallet.toString(),    lamports: lamports.toString(),    sol: Number(lamports) / 1e9,    slot: slot.status === "fulfilled" ? slot.value.toString() : null,    at: new Date().toISOString(),  };  await writeFile("balance.json.tmp", JSON.stringify(snap, null, 2));  await rename("balance.json.tmp", "balance.json");  console.log(`${snap.at} ${snap.sol.toFixed(4)} SOL, slot ${snap.slot ?? "n/a"}`);}for (;;) {  await poll().catch((err: unknown) => console.error("write failed:", why(err)));  await new Promise((r) => setTimeout(r, pollMs));}

run: node main.ts <wallet-address>

The process checks its inputs before the first poll. A missing key or a malformed address exits with a message, so a supervisor sees the failure at once instead of a loop that writes nothing. The wallet address is parsed with address() at start, and the poll interval is read as seconds and converted to milliseconds once.

05

What it costs against your limits

Each poll makes two calls, so the default 30 second interval makes four a minute. Limits are per account, so every key on the account shares one budget. A 429 on the balance read leaves the file as it was, and the next poll runs after the same interval. The file then keeps the last good balance, and its at shows the age.

The slot read costs one cell, and at the default interval it runs two times a minute, far below the 5 req/s that the Free plan allows for cheap reads.

06

Where it ends

  • It watches one wallet per process. Two processes in one directory write the same balance.json, so give each its own directory.
  • It is a timer. A change that lands and leaves between two polls is never seen, and the tray shows the value as of at.
  • It reads a balance and a slot. It checks no signatures and reads no history, so it cannot confirm that a transfer you expect has landed.
  • It does not restart itself. A crashed process leaves an old at, and only the tray's staleness mark shows it, so run it under a supervisor.
07

When it goes wrong

what goes wrongwhat to do
Every poll logs getBalance failed: HTTP error (401) and balance.json stops changing.The key is unknown at the door, which answers UNKNOWN_KEY. Set CARBIUM_KEY to the whole key and restart; the next poll writes again.
The log shows HTTP error (429) on some polls, and the tray marks the value stale.Your account's budget is spent, usually by other keys on the same account. Raise POLL_SECONDS, or move the other traffic to a plan with a higher rate.
The file shows "slot": null while the lamports keep changing.The slot read failed on its own and the balance is still correct. The log line names getSlot, so check that call's errors.
Startup stops with an error about the address and writes no file.Pass the base58 address exactly as a wallet shows it, with no quotes or spaces.
The tray shows one wallet's value under another wallet's name.Two processes share a directory and overwrite each other's file. Run one process per directory, each with its own balance.json.
08

Questions

Why not subscribe to the wallet instead of polling?

For a plain SOL wallet, accountSubscribe sends nothing, so a subscription on that account does not report a balance change. A timer is the simpler route for a tray that refreshes every 30 seconds.

Which commitment does the number use?

confirmed, written in the code. Both reads pass it, so a later reader can change the level in one place.

Can the tray run without this process?

Not with this design. The tray only reads a file, so nothing refreshes it while the process is down, and the staleness mark is the only sign of that.

Can it watch several wallets?

Run one process per wallet, each in its own directory with its own balance.json. A single loop over several addresses would need a change to the file name and the loop.