near-toll-audit / pipeline/sql/15_stake_history.sql · raw
-- Total stake at month ends, rebuilt from account_changes because the RPC keeps validator info only for recent epochs.-- An account's `staked_balance` is its protocol-locked NEAR. Rewards touch every active validator each epoch, so one day is enough.WITH c AS ( SELECT block_date, affected_account_id AS a, ARRAY_AGG(affected_account_staked_balance ORDER BY block_height DESC, index_in_block DESC LIMIT 1)[OFFSET(0)] AS s FROM `bigquery-public-data.crypto_near_mainnet_us.account_changes` WHERE block_date IN ('2021-03-31','2021-06-30','2021-09-30','2021-12-31','2022-03-31','2022-06-30','2022-09-30','2022-12-31','2023-03-31','2023-06-30','2023-09-30','2023-12-31','2024-03-31','2024-06-30','2024-09-30','2024-12-31','2025-03-31','2025-06-30','2025-07-31','2025-08-31','2025-09-30','2025-10-31','2025-11-30','2025-12-31','2026-01-31','2026-02-28','2026-03-31','2026-04-30','2026-05-31','2026-06-30','2026-07-31','2026-08-31','2026-09-30','2026-10-31') AND affected_account_staked_balance > 0 GROUP BY 1, 2)SELECT block_date, COUNT(*) AS accounts, SUM(s) / 1e24 AS staked_near, COUNTIF(a LIKE '%.poolv1.near' OR a LIKE '%.pool.near' OR a LIKE '%.pool.%') AS pool_accounts, SUM(IF(a LIKE '%.poolv1.near' OR a LIKE '%.pool.near' OR a LIKE '%.pool.%', s, 0)) / 1e24 AS pool_staked_nearFROM c GROUP BY 1 ORDER BY 1