The owner asked for it on 2026-10-15: “branch and implement return on stake (ROS) for a pool. Lace shows it in the pool browser and on a pool’s details, with recent epochs’ rewards. Koios pool_history has a per-epoch ROS figure, so it would cost one request per pool opened. Small but worth it as a quick feature.” It was one of the five Lace items declined on 2026-10-14; delegation history, another, came back the same day as chunk 36.
Status: ✅ built 2026-10-15 on web-wallet/pool-ros, from main after #310: the wallet, the data layer’s pool_history route and the docs in one PR, so this file went straight to the archive, as chunk 36’s did. What still holds is in architecture.md (Staking: the request and the rate), flows.md (the pool page and Staking), privacy.md (What Koios learns) and seedelf-data’s README (the route). The privacy policy is unchanged: its staking row says a pool you look up is sent to the server.
The live test passed on 2026-10-15, run by the owner on the dev box (the auto-mode classifier now refuses reads of home’s db-sync, “Production Reads”): every pool’s 18 epochs worked out in 2.7 s, and three mainnet pools matched Koios’s recorded answers on every epoch, stake, blocks and what each paid. Its first run failed on OctasPool’s epoch 642, where the test wanted the largest of four values Koios’s servers gave: db-sync’s is 19,172 ₳, and the 19,269 ₳ one has about 98 ₳ more paid to members, a value cached before db-sync took back at payday what keys deregistered since were owed. So a cached Koios value can be high as well as low, and the test takes any value a server gave.
Deploy the API before a wallet with this ships: an API without the route answers 404, and any failure holds the wallet’s public part on Koios for 5 minutes. The usual order (merge, deploy the API, check it, then package) does it.
_reference/lace at e431933)estimateStakePoolROS (packages/contract/cardano-stake-pools/src/estimate-ros.ts) works a yearly rate out of the pool’s live stake, pledge, margin and cost and the network’s reserves, monetary expansion, treasury cut and a0 (the Shelley ledger spec’s maxPool, Fig. 46), assuming blocks in proportion to stake, so luck and downtime play no part. Zero when the live pledge is under the declared.~3.1%, sortable by it, from the bulk summary with the declared pledge standing in for the live one) and the pool’s details (ROS).pool_info, two requests), and Staking’s Pool details (read the first time it unfolds: opening Staking costs what it did). Back from a review shows it again without asking (PoolsView.history).epoch_params carry, plus totals’ reserves.pool_history, and its holespool_history?_pool_bech32=…&select=epoch_no,active_stake,block_cnt,deleg_rewards&order=epoch_no.desc&limit=18. Koios’s newest row is two epochs ago (an epoch’s rewards are paid two after it).deleg_rewards / active_stake (what the pool paid its stake after its fees, Koios’s own figure), yearly as Koios’s epoch_ros is, (1 + r)^73 − 1, which epoch 317 of LOGIC reproduces to 9 places; the pool’s, the mean of log(1 + r) over the known epochs, a year of them over.deleg_rewards, pool_fees and member_rewards null, epoch_ros 0, with blocks made). Its cache job, grest.pool_history_cache_update (koios-artifacts, files/grest/rpc/00_cached_tables/pool_history_cache.sql), does nothing while it holds up to the epoch before the current one, so it runs every two epochs, and then from the newest epoch it holds: the epoch between is written before its rewards exist and never again. Koios’s own pool_history carries the comment “temporary condition for testing, until cache table population fixed”. Seen 2026-10-15 for OctasPool on mainnet (8 or 9 of 18 null on four answers in five; the fifth had 2) and LOGIC on preprod (6 of 18; the largest preprod pool’s 18 were whole on one answer and had 8 null on another): a minute apart, the same request gave different holes.Needed before this wallet ships. Every read goes there first on mainnet (DataLayerKoios, and data-layer.test.ts’s guard that no Koios call is left unrouted), and the privacy policy says it: reads go through our server first, Koios for a part it can’t answer. Without the route the API answers 404, and any failure holds the wallet’s whole public part on Koios for 5 minutes. The wallet’s override is in already (poolHistory), as chunk 36’s was before its route.
The route: GET /api/v1/pool_history, exactly the query above (one pool, that select, that order, limit=18), Koios’s columns and JSON, one row an epoch the pool had stake in, from two epochs ago back 18. As built (design C, below): every pool’s epoch worked out once and kept for the server’s life (History), 18 after a start, behind the requests every minute (keep_filled), one more as an epoch turns; a request waits at most 3 s for epochs still being worked out, then answers with those done. The newest epoch worked out in its first hour of being paid is worked out again 10 minutes on, since db-sync inserts an epoch’s rewards bit by bit until then. Better than Koios: deleg_rewards is never null, and a pledge not met is "0". seedelf-data’s README has the rest.
What it reads, after Koios’s grest.get_pool_history_data_bulk (CC-BY-4.0), for every pool in one epoch, in one statement:
pool_stat.stake at epoch E − 1, which is epoch_stake’s sum for E (Measured);block by slot leader for the epoch (idx_block_epoch_no);reward earned in the epoch, member and leader, by pool;pool_update, the latest active by each epoch, for Koios’s deleg_rewards (members’ rewards plus the leader’s less the pool’s fees: fixed_cost + (total − fixed_cost) × margin, or the members’ alone when the leader’s is under the cost).The cost was the rewards, which chose the design. Home’s reward has idx_reward_pool_id and idx_reward_earned_epoch, each on its own, and no index on both: a large pool (35,000 delegators) has about 35,000 rows an epoch for every epoch since it started, and an epoch has a row for every delegator on the network. Three designs:
pool_info’s 3 s budget. Cheapest to write; a large pool’s first look may read gigabytes from a cold disk. Measure first.reward (pool_id, earned_epoch) include (type, amount), built concurrently from deploy/home/db-sync-indexes.sql by the owner: then A is a range scan. Its size and build time come from the measurements.idx_reward_earned_epoch, one epoch’s rows), kept in memory for the 18 epochs (3,000 pools × 18 rows), the oldest dropped as a new one comes. Each pool’s look is then a lookup; the cost is 18 epochs read once at each start, one an epoch after.Epoch 661; the largest pool pool1gaztx97t53k47fr7282d70tje8323vvzx8pshgts30t9krw62tm (35,041 delegators); the reward table 457.5M rows, 33 GB.
idx_reward_earned_epoch for all 18 epochs and filters the pool out: 21M rows read, 621,000 kept, 246,000 buffers (1.9 GB) from disk. So a pool’s look costs as much as every pool’s over the same epochs, and would cost that for each pool opened. Out.pool_stat is the snapshot a step ahead (one small pool, epochs 656–661, and the largest’s blocks): its stake at epoch N is epoch_stake’s sum at N + 1, which is Koios’s active_stake for N + 1, and its number_of_blocks at N is the blocks made in N − 1. So an epoch E’s stake is pool_stat’s at E − 1, and its blocks pool_stat’s at E + 1 (or block by idx_block_epoch_no, about 21,000 rows an epoch). The live test checks both against Koios for real pools.So: C. Each epoch’s every pool (stake, blocks, members’ and leader’s rewards, the terms active then) worked out once and kept for the API’s life, the 18 newest final epochs (two ago back 18), the oldest dropped as an epoch turns; a pool’s look is then a lookup. About 0.2–0.5 s an epoch, so 18 at a start in the background (single-flight, a few at a time), one more an epoch. It would also have every pool’s return at hand, should the owner want it on the browser’s rows on mainnet (preprod’s Koios has nothing like it).
Read-only, as seedelf_reader on cexplorer_new (the URL in seedelf-data/.env), each with set statement_timeout = '60s':
-- The largest pool by delegators, and the current epoch.
select ps.pool_hash_id, ph.view, ps.number_of_delegators from pool_stat ps join pool_hash ph on ph.id = ps.pool_hash_id
where ps.epoch_no = (select max(epoch_no) from pool_stat) order by ps.number_of_delegators desc limit 1;
select max(no) from epoch;
-- A: one pool's rewards over 18 epochs (put the pool's id and the epochs in).
explain (analyze, buffers) select earned_epoch, sum(amount) filter (where type = 'member'),
sum(amount) filter (where type = 'leader')
from reward where pool_id = <id> and earned_epoch between <cur - 19> and <cur - 2>
and type in ('member', 'leader') group by earned_epoch;
-- C: one epoch, every pool.
explain (analyze, buffers) select pool_id, sum(amount) filter (where type = 'member'),
sum(amount) filter (where type = 'leader')
from reward where earned_epoch = <cur - 2> and type in ('member', 'leader') group by pool_id;
-- B: how big the index would be.
select reltuples::bigint, pg_size_pretty(pg_relation_size('reward')) from pg_class where relname = 'reward';
-- The stake and blocks: does pool_stat line up with epoch_stake and block for the same epoch?
select ps.epoch_no, ps.stake, ps.number_of_blocks,
(select sum(amount) from epoch_stake where pool_id = ps.pool_hash_id and epoch_no = ps.epoch_no),
(select count(*) from block b join slot_leader sl on sl.id = b.slot_leader_id
where sl.pool_hash_id = ps.pool_hash_id and b.epoch_no = ps.epoch_no)
from pool_stat ps where ps.pool_hash_id = <id> and ps.epoch_no between <cur - 4> and <cur - 2> order by 1;
Each twice: the first from a cold disk, the second warm.
public_grammar.rs takes the one request and refuses every other (17 or 19 epochs, no limit, an offset, oldest first, no order, epoch_ros selected, the order of parameters, another filter, a DRep or a pool ID spelt otherwise); history.rs’s own tests, Koios’s deleg_rewards for OctasPool’s epoch 659 to the lovelace, the 18-epoch window as an epoch turns, a settling epoch worked out again, a pool’s rows; live_public.rs, three mainnet pools against Koios’s recorded answers (above), and a pool db-sync doesn’t know.staking.test.ts: the one request, in its order; 18 epochs newest first with their blocks; epoch 317’s rate against Koios’s own epoch_ros; Koios’s nulls left out, not counted as zero (which would take a point off LOGIC’s); mainnet’s OctasPool, every other epoch null, not halved; the ID as hex, and a DRep’s refused; rows that aren’t epochs left out, each epoch once, none past 18; no rate with no epoch known.data-layer.test.ts: the request on the data layer, in the order its grammar will take, or Koios when it fails.pool-return-screen.test.ts: the figures’ last row, the hint’s three wordings and none without a rate, the epochs folded, not known, reading and Try again, folded with the figures on Staking, and nowhere without a read.e2e/extension.spec.ts: opening Staking asks nothing, Pool details asks once (folded and opened again: still once), the epochs and the hint’s count match the recording, a pool’s page asks with its details, and Back from its review asks for neither again; the older staking test now sees pool_history beside pool_info. Ten runs each on 2 CPUs, dev and store builds: all passed.tests/fixtures/record-pool-history.mjs (preprod’s LOGIC and TPREP, mainnet’s OctasPool), pool-history.json, served by tests/fakes.ts. Koios’s servers disagree, so a new recording can differ: the one committed has mainnet’s every-other-epoch holes, which the OctasPool test needs.