Seedelf-Wallet

Chunk 37 · A pool’s return on stake

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.

What Lace does (2.4.2, _reference/lace at e431933)

What this wallet does

Koios’s pool_history, and its holes

The data layer (mainnet)

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:

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:

Measured (2026-10-15, the owner’s run on home)

Epoch 661; the largest pool pool1gaztx97t53k47fr7282d70tje8323vvzx8pshgts30t9krw62tm (35,041 delegators); the reward table 457.5M rows, 33 GB.

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).

The measurements (the queries that were run)

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.

Tests