services
Get a key

library / How to use Solana / HS0039

gRPC account data-change filter

One TypeScript file watches an account over a Carbium WebSocket and prints a line only when the account's data bytes differ from the last copy. Writes that leave the data alone, such as lamport-only changes, are counted and skipped.

By Carbium · Yellowstone gRPC · HS0039 · updated 2026-11-22

01

Why an update is not a change

An account update on a Solana stream says the account was written in a slot. It does not say the data differs from before. A transaction can list an account as writable, run, and leave every byte as it was. A transfer into an account moves lamports and leaves the data field alone. Both can produce an update, and a consumer that reacts to each one redoes work for nothing: a database write, a recompute, a message to a user.

Yellowstone's account filters choose which accounts you hear about: by address, by owner program, by data size, by a memcmp on some bytes, and among others by lamports comparisons. None of them compares an update with the previous one, so no subscription setting says "only when the data changed". That comparison belongs to the consumer. It is small: keep a digest of each account's last data and drop an update whose digest matches.

What the write didData bytesmain.ts
Lamports moved in or out, data untouchedSameCounted, skipped
Program rewrote the account with identical bytesSameCounted, skipped
Program changed a fieldDifferentPrinted with the slot

The file below runs this check over a WebSocket, because that needs nothing beyond Node 24 and @solana/kit. The check itself is a few lines and moves unchanged to a gRPC consumer.

02

The code

Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24, which strips the types on load. The first argument is the account to watch and the second is how many seconds to listen. With no arguments it watches the Orca SOL/USDC Whirlpool, an account that swaps write to many times a minute, for 20 seconds. Pass any other account that gets written often to see the filter work on it.

main.ts
// HS0039 · gRPC account data-change filter// https://carbium.sh/library/howsolana/grpc-account-data-change-filter// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createHash } from "node:crypto";import { address, createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) {  console.error("Set CARBIUM_KEY to your Carbium API key.");  process.exit(1);}const rpcUrl = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const wsUrl = process.env.CARBIUM_WS_URL ?? "wss://services.carbium.sh";// Default: the Orca SOL/USDC Whirlpool, written on nearly every swap.const target = address(process.argv[2] ?? "Czfq3xZZDmsdGdUyrNLtRhGc47cXcZtLG4crryfu44zE");const seconds = Number(process.argv[3] ?? 20);const rpc = createSolanaRpcFromTransport(  createDefaultRpcTransport({ url: rpcUrl, headers: { "x-api-key": key } }),);// A short digest of the data bytes: equal digests mean the data did not change.const digest = (b64: string) => createHash("sha256").update(Buffer.from(b64, "base64")).digest("hex").slice(0, 12);let last = "";let seen = 0;let changed = 0;try {  const { value } = await rpc.getAccountInfo(target, { encoding: "base64", commitment: "confirmed" }).send();  if (value) {    last = digest(value.data[0]);    console.log(`baseline data ${last}, ${value.lamports} lamports`);  } else {    console.log("account not found at confirmed");  }} catch (e) {  console.error(`getAccountInfo failed: ${e}`);}const ws = new WebSocket(`${wsUrl}/?api-key=${key}`);ws.addEventListener("open", () => {  ws.send(JSON.stringify({    jsonrpc: "2.0",    id: 1,    method: "accountSubscribe",    params: [target, { encoding: "base64", commitment: "confirmed" }],  }));});ws.addEventListener("message", (event) => {  const msg = JSON.parse(String(event.data));  if (msg.id === 1) {    console.log(msg.error ? `subscribe refused: ${JSON.stringify(msg.error)}` : `subscribed, id ${msg.result}`);    return;  }  if (msg.method !== "accountNotification") return;  const { context, value } = msg.params.result;  seen++;  const now = digest(value.data[0]);  if (now === last) return; // same data bytes: lamports-only or identical rewrite  last = now;  changed++;  console.log(`slot ${context.slot} data changed -> ${now} (${value.lamports} lamports)`);});ws.addEventListener("error", () => console.error("websocket error"));setTimeout(() => {  console.log(`${seen} notifications, ${changed} with new data in ${seconds}s`);  ws.close();  process.exit(0);}, seconds * 1000);

run: node main.ts [address] [seconds]

The file takes one baseline copy of the account with getAccountInfo at confirmed, then opens a WebSocket and sends accountSubscribe at confirmed with base64 encoding. Each notification carries the account's lamports and data. The file hashes the data bytes with SHA-256 and compares the first 12 hex characters of the result with the last one it kept.

Only a new digest prints. Every notification adds to a counter, and a final line reports how many arrived and how many carried new data. A failed baseline read is printed and the run goes on, since the subscription does not depend on it.

03

Reading the output

The run starts with a baseline data line holding the digest of the account as it stands and its lamports. Then comes subscribed, id N, the subscription id the server returns. After that, each data changed line gives the slot of the write, the new digest and the lamports at that moment.

The closing line compares two numbers. When notifications and changes are equal, every write in the window altered the data, which is common for a pool whose price fields move on each swap. A gap between them is the work the filter saved you from. A pool shows equal counts, and an account with lamport-only or identical writes shows the gap. An account with no writes in the window gives zero of both, and the run is still fine.

$A digest shows that the bytes differ and says nothing about which field moved. To react to one field only, hash a slice of the data at the offsets you care about.
04

The same check on the gRPC stream

A gRPC consumer receives account updates that carry the account's public key, lamports and data. Keep a map from public key to the last digest. When an update arrives, hash its data, look up the key, drop the update if the digest matches and store it otherwise. That is the whole filter, and it is the same few lines as in the file above.

Carbium's gRPC stream is at https://services.carbium.sh:9443, takes the key in the x-token header and opens on Dev, Business and Pro. Free is off. The client side needs the geyser.proto definitions and a generated client, which the file avoids by using the WebSocket. Keep the map across reconnects.

Pick the owner or address filters first so that fewer accounts reach the map at all. The digest check removes repeats among the accounts you chose, and the stream filter decides which accounts you chose.

05

On Carbium

The WebSocket is wss://services.carbium.sh and takes the key as `?api-key=` in the URL.

Opening an `accountSubscribe` costs 25 credits, the weight Carbium gives to account subscriptions, and each MiB sent over the socket then costs 20 credits. The bytes are metered as they arrive, before your code compares them, so a skipped duplicate has already been billed. To cut credits, watch fewer accounts. A subscription that never receives a notification is refunded at close, and a refused one costs nothing.

The plan also caps WebSocket connections and subscriptions per Carbium account, and Free carries 5 sockets. The baseline getAccountInfo is an account read at 10 cells per call on the RPC endpoint.

accountSubscribe sends account state on change for streamed accounts and sends nothing for a plain SOL wallet. For a wallet, use logsSubscribe with a mention and read the balance with getBalance when a log arrives, as in Multi-wallet log watcher. For the wider picture of feeding a stream into your own store, see Chain data streaming.

06

Where it ends

The digest covers the data field only. A change to lamports, the owner or the executable flag with the same data bytes is skipped. If those matter to you, add them to the string you hash.

The last digest lives in memory. A restart starts from a fresh baseline, so the first update after a restart is compared only with the new baseline, never with state from the earlier run. A change that landed between the baseline read and the subscription reaching the server shows only as the next write, which can be far off for a quiet account. The file listens for a fixed number of seconds and exits, so a long-running consumer needs a reconnect loop around it.

Hashing every update costs CPU in proportion to the account size. For multi-megabyte accounts at high write rates, hash a data slice or compare lengths first.

The file is read-only and never signs or sends anything. It reads at confirmed.

07

When it goes wrong

what goes wrongwhat to do
The run ends with 0 notifications and prints no data changed line.The account was not written in the window, or it sends nothing, as a plain SOL wallet does. Pass a busy account, raise the seconds, or use logsSubscribe with a mention plus getBalance for a wallet.
The line subscribe refused appears with an error object.Read the code in the object. Close sockets you no longer use if you are at the plan's connection cap, and check the key has the access the plan allows. A refused subscription costs no credits.
The socket closes at once with a websocket error and no subscribed line.An unknown key gets 401 at the door. Attach the whole key, with nothing cut off, in the ?api-key= part of the URL.
Every notification prints as a change.The account's data really does change on every write, as with a busy pool's price fields. The filter is working. To ignore a field that always moves, hash only the bytes you care about.
The file stops with Set CARBIUM_KEY to your Carbium API key.Export CARBIUM_KEY in the shell that runs node main.ts. The file reads it from the environment only.
08

Questions

Does Yellowstone have a switch for sending only changed data?

Its account filters pick accounts by address, owner, size and bytes, and none compares an update with the previous copy. Keep the last digest per account in your consumer, as the file does.

Does dropping duplicates lower my credit use?

No. The bytes have already been sent and metered when your code compares them. Watching fewer accounts is what lowers the bill.

Why a hash and not the whole previous data?

Comparing the stored bytes works too. A digest of 12 characters keeps memory flat when you track thousands of accounts with large data.

Can I react to one field instead of the whole account?

Yes. Decode the bytes at the field's offset, or hash just that slice, and compare that. A timestamp field that changes on every write then stops triggering updates.