Query editor
withbalances_daily as (/* Daily pre-aggregate maintained as two matviews (see queries/prod/matviews):history refreshed weekly, the last 30 days refreshed daily. History onlyfills days older than the recent part, so a day is never counted twice. */select * from dune.dune.result_stablecoin_frontend_balances_daily_historywhere day < (select min(day) from dune.dune.result_stablecoin_frontend_balances_daily_recent)union allselect * from dune.dune.result_stablecoin_frontend_balances_daily_recent),multichain_daily as (selectday,blockchain,token_symbol,address_category,sum(amnt_usd) as amnt_usdfrom balances_dailywhere day < current_dategroup by 1, 2, 3, 4),/*Pricing-lag guard. Balance rows land in the enriched tables hours before theUSD pricing join, so the newest `day` can carry rows whose `balance_usd` isstill null for most chains. Left in, that day sums to a fraction of the realfigure and the newest bucket collapses. Cut the series off at the last daythat prices as many chains as the recent norm.*/daily_chain_coverage as (/* Bounded to recent days on purpose: Trino inlines CTEs, so an unboundedpass here would re-scan all history on the month/year queries. The cutoffnever lands more than a few days back. */selectday,count(distinct blockchain) as chains_pricedfrom multichain_dailywhere amnt_usd is not nulland day >= current_date - interval '60' daygroup by 1),expected_chains as (/* Excludes the two newest days so a chain onboarded today cannot raise thebar above what any settled day can clear. */select max(chains_priced) as chains_pricedfrom daily_chain_coveragewhere day between current_date - interval '30' dayand current_date - interval '2' day),last_complete_day as (/* Falls back to the raw max day so the guard can never empty the result. */select coalesce((select max(c.day)from daily_chain_coverage cwhere c.chains_priced >= (select chains_priced from expected_chains)