library / Blockchain ideas / Dashboards and explorers / BI0125
Epoch progress countdown
A Solana epoch counter is two RPC reads and some arithmetic. You get the epoch number, a progress bar, the slots left and an estimated end time, with the slot time measured from recent samples. A runnable main.ts prints it every 10 seconds.
Who uses it
An epoch counter answers one question: how long until the epoch changes. Validator operators plan restarts and reports around the boundary. Teams that run a job once per epoch, such as a snapshot, a leaderboard reset or an accounting cut, need to know when to fire it. A community page can show a bar that fills and a clock that runs down.
Explorers show the epoch number, but a countdown you own can sit in a status page, a chat command or a terminal tab, and it can trigger your own work when the number changes. It takes two reads and a little arithmetic, which makes it a good first build for a hackathon team that wants a working piece before lunch and the rest of the day for what the countdown starts.
What it shows
One line per cycle, built from two calls on Carbium's RPC at services.carbium.sh. The first call says where the epoch stands. The second says how fast slots are passing right now.
| Output | Source | Meaning |
|---|---|---|
| Epoch number | getEpochInfo, field epoch | the epoch the cluster is in |
| Progress | slotIndex divided by slotsInEpoch | how far through the epoch, as a bar and a percent |
| Slots left | slotsInEpoch minus slotIndex | how many slots remain before the next epoch begins |
| Slot time | getRecentPerformanceSamples | seconds per slot, from samplePeriodSecs over numSlots |
| Time left | slots left times slot time | an estimate, printed as hours, minutes and seconds |
| End time | now plus time left | the estimated boundary as a UTC timestamp |
The call to getEpochInfo passes confirmed as its commitment. A call that leaves commitment out is filled in with confirmed by @solana/kit 8.6.0 while the server's own default is finalized, so naming it keeps the level in the code where a reader can see it. At this scale the two levels differ by a slot or so, far below what a bar of 30 cells can show.
How the estimate works
slotIndex counts the slots since the epoch began and slotsInEpoch is the epoch's length, 432,000 on mainnet. Progress is the first divided by the second. The countdown multiplies the slots left by the seconds a slot takes.
Seconds per slot is where countdowns go wrong. Since the Alpenglow switch on mainnet (2026-10-21) a slot takes about 200 ms, so 432,000 slots run for about 24 hours. The older figure of 400 ms gives about two days, and a constant copied from an older tutorial doubles the answer. The script measures instead. It adds up samplePeriodSecs and numSlots across the ten newest performance samples and divides one by the other, so the clock follows the cluster as it is now.
At 0.2 seconds a slot, 1% of an epoch is 4,320 slots, about 14 minutes and 24 seconds. A bar of 30 cells therefore gains one cell about every 48 minutes, and most refreshes show the same bar with a smaller number of slots left.
The build
One file, with @solana/kit and what Node 24 ships. It only reads: no private key, nothing signed, nothing sent.
- Read CARBIUM_KEY and exit with a message when it is unset. Build one RPC client with the key and keep it for the life of the process.
- Each cycle, send getEpochInfo and getRecentPerformanceSamples together and settle both, so a failure of one still lets the other print.
- Compute progress, slots left and, when samples came back, seconds per slot, time left and the end time in UTC.
- When the samples call fails or returns nothing, print the slots left without a clock and report the error on stderr.
- Repeat every 10 seconds. A failed cycle logs its reason and the next cycle tries again.
// BI0125 · Epoch progress countdown// https://carbium.sh/library/blockchainideas/epoch-progress-countdown// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { 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 rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);const WIDTH = 30;const why = (e: unknown) => (e instanceof Error ? e.message : String(e));const two = (n: number) => String(Math.floor(n)).padStart(2, "0");const clock = (secs: number) => `${Math.floor(secs / 3600)}h ${two((secs % 3600) / 60)}m ${two(secs % 60)}s`;async function cycle(): Promise<void> { // Settle both reads so one failing call still lets the other print. const [info, perf] = await Promise.allSettled([ rpc.getEpochInfo({ commitment: "confirmed" }).send(), rpc.getRecentPerformanceSamples(10).send(), ]); if (info.status === "rejected") { console.error("getEpochInfo failed:", why(info.reason)); return; } const { epoch, slotIndex, slotsInEpoch } = info.value; const left = Number(slotsInEpoch) - Number(slotIndex); const pct = (Number(slotIndex) / Number(slotsInEpoch)) * 100; const filled = Math.round((pct / 100) * WIDTH); let line = `epoch ${epoch} [${"#".repeat(filled)}${"-".repeat(WIDTH - filled)}] ${pct.toFixed(2)}% | ${left} slots left`; if (perf.status === "rejected") { console.error("getRecentPerformanceSamples failed:", why(perf.reason)); } else { const slots = perf.value.reduce((n, s) => n + Number(s.numSlots), 0); const secs = perf.value.reduce((n, s) => n + Number(s.samplePeriodSecs), 0); if (slots > 0) { const perSlot = secs / slots; // measured seconds per slot, never a constant const eta = left * perSlot; line += ` | ${perSlot.toFixed(3)} s/slot | about ${clock(eta)} left | ends ${new Date(Date.now() + eta * 1000).toISOString()}`; } } console.log(line);}const tick = () => cycle().catch((e) => console.error("cycle failed:", why(e)));tick();setInterval(tick, 10_000);
run: CARBIUM_KEY=<YOUR_KEY> node main.ts
The epoch fields arrive as bigint, and the script turns them into plain numbers before dividing. Slot counts stay far below the size where that loses precision. When the epoch rolls over, the epoch number rises by one and the slots left jump back to nearly a full epoch, so the next cycle shows the new epoch with no special case.
For a web page, run this read on a small server, keep the latest line in memory and let every visitor read that copy. The key stays on the server and the number of viewers does not change the number of calls.
What it costs
Two calls every 10 seconds is 0.2 calls a second, and one server doing that covers any number of viewers. Limits belong to the account, so other tools on the same keys draw from the same budget as this one.
A page that wants a smoothly ticking number can read getSlot once a second between epoch reads. It sits among the cheap cached reads, which cost 1 cell each. The rate depends on the plan.
| Plan | Price a month | Cheap cached reads (req/s) |
|---|---|---|
| Free | $0 | 5 |
| Dev | $20 | 30 |
| Business | $250 | 100 |
| Pro | $500 | 500 |
A WebSocket adds nothing here. A subscription costs credits to open and then 20 credits per MiB sent, and an epoch value that changes once a day gains nothing from a push.
What to add next
- A ticking clock: read getSlot every second, subtract it from the last known end slot, and re-read getEpochInfo once a minute to correct drift.
- An epoch hook: remember the last epoch number in a file and run your job when the number changes, so a restart never fires it twice.
- Threshold alerts at one hour and ten minutes before the end, sent once each per epoch.
- A wall-clock view of the past half of the story: Slot to timestamp conversion turns a slot that has already happened into a time.
- A wider status panel: Public chain stats page puts the countdown beside other network numbers.
Where it ends
- The end time is a forecast. No call returns the time of a future slot, so the clock is slots left times a recent average and drifts when slot time does.
- getEpochInfo describes the present and returns no times for earlier epochs. To log how long each epoch ran, have your own job note the time each change was seen.
- The script says nothing about what changes at the boundary for a given account or program. Read those accounts yourself after the epoch rolls.
- The slot-time window is the ten newest samples. A sudden change in load shows in the clock a few samples late.
- One cluster only: the numbers are mainnet's.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| The countdown reads about two days. | A fixed 400 ms slot time is in play. Use the measured seconds per slot from the performance samples, which now average about 0.2 s. |
| Only slots left prints and the clock is missing. | The performance samples call failed or came back empty. The script reports the reason on stderr and the next cycle tries again. |
| Progress drops to near 0 and the epoch number rises by one. | The epoch rolled over. Compare the epoch numbers between cycles and treat a change as an event, not as an error. |
| The first call answers 401. | The key is unknown at the door. Check that CARBIUM_KEY holds the whole key with nothing cut off. |
| Arithmetic throws a TypeError about mixing BigInt and other types. | The epoch fields are bigint. Convert them with Number before dividing, as the script does. |
Questions
How long is a Solana epoch now?
It is 432,000 slots on mainnet. At about 200 ms a slot since the Alpenglow switch, that is about 24 hours. Read slotsInEpoch from the reply and leave the 432,000 out of your code.
Can I skip the performance samples?
Yes, with a fixed 0.2 seconds a slot. It costs one call less a cycle, and the clock drifts whenever the cluster's slot time moves, so the measured figure is the better default.
Does a countdown need the WebSocket?
No. The epoch changes slowly, and one read every 10 seconds is plenty. A socket would add an opening charge and then credits for every MiB sent.
Which commitment should the read use?
Use confirmed. Finalized sits 0 to 1 slots behind processed on mainnet now, so a bar of this resolution cannot show the difference.