Query editor
-- Top-holder discovery for AdapterStablecoinBalances (saved on Dune, query_id in adapter_stables_balances.py). Meant to run monthly.-- Per stablecoin: the top_n addresses by approximate balance (all-time transfers in minus out).-- The approximation is only used to rank addresses; balances themselves are always read via balanceOf().-- Params (text): blockchain (Dune alias, e.g. 'ethereum'), token_addresses (comma separated 0x...), top_nWITH tokens AS (SELECT from_hex(substr(trim(t), 3)) AS tokenFROM UNNEST(split('{{token_addresses}}', ',')) AS u(t)),flows AS (SELECT tr.contract_address AS token_address, tr."to" AS address, CAST(tr.value AS double) AS vFROM erc20_{{blockchain}}.evt_Transfer trJOIN tokens tk ON tk.token = tr.contract_addressUNION ALLSELECT tr.contract_address, tr."from", -CAST(tr.value AS double)FROM erc20_{{blockchain}}.evt_Transfer trJOIN tokens tk ON tk.token = tr.contract_address),balances AS (SELECT token_address, address, SUM(v) AS balance_rawFROM flowsWHERE address != 0x0000000000000000000000000000000000000000GROUP BY 1, 2HAVING SUM(v) > 0),ranked AS (SELECT *, row_number() OVER (PARTITION BY token_address ORDER BY balance_raw DESC) AS rnFROM balances)SELECT address, token_address, balance_raw, rnFROM rankedWHERE rn <= {{top_n}}