Query editor
-- ENS DAO — Treasury wallet balances (month-end ETH / USDC / ENS) v1-- Owner: coltron (public). Repo: ens-foundation-skill/pipeline/sql/ens_dao_balances.sql---- Month-end balances of the DAO's core wallets, derived from transfers (no dependency on a balances spell):-- ETH = cumulative (successful value traces in − out) from ethereum.traces-- USDC / ENS = cumulative erc20 Transfer in − out-- Gas is ignored: all four wallets are contracts (Timelock / Safes) that never pay gas themselves.-- Wallets: wallet.ensdao.eth (Timelock), endowment.ensdao.eth (avatar Safe — token-level view only, DeFi positions-- are in the yield query's NAV), main.mg.wg.ens.eth (MetaGov WG main Safe), stream.mg.wg.ens.eth (MetaGov stream pod).-- Balances are cumulative from genesis, so the query scans full history regardless of the parameters; the parameters-- only filter which month rows are returned. Validation: closes to the wei against Part A's Etherscan balance identity.-- Parameters: {{period_start}} / {{period_end}} (text, 'YYYY-MM-DD'; end exclusive).with params as (select timestamp '{{period_start}}' as t0, timestamp '{{period_end}}' as t1),wallets as (select * from (values(0xFe89cc7aBB2C4183683ab71653C4cdc9B02D44b7, 'wallet.ensdao.eth'),(0x4f2083f5fBede34c2714AfFb3105539775f7FE64, 'endowment.ensdao.eth'),(0x91c32893216dE3eA0a55ABb9851f581d4503d39b, 'main.mg.wg.ens.eth'),(0xB162Bf7A7fD64eF32b787719335d06B2780e31D1, 'stream.mg.wg.ens.eth')) as t(addr, label)),tokens as (select * from (values(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, 'USDC', 6),(0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72, 'ENS', 18)) as t(addr, symbol, decimals)),eth_flows as (select date_trunc('month', t.block_time) as month, w.label, 'ETH' as asset,sum(case when t."to" = w.addr then cast(t.value as double) else 0 end) / 1e18- sum(case when t."from" = w.addr then cast(t.value as double) else 0 end) / 1e18 as netfrom ethereum.traces tjoin wallets w on (t."to" = w.addr or t."from" = w.addr)where t.block_time >= timestamp '2021-11-01'and t.tx_success and t.success and t.value > uint256 '0' and t.call_type = 'call'group by 1, 2),erc20_flows as (select date_trunc('month', e.evt_block_time) as month, w.label, k.symbol as asset,sum(case when e."to" = w.addr then cast(e.value as double) / power(10, k.decimals) else 0 end)- sum(case when e."from" = w.addr then cast(e.value as double) / power(10, k.decimals) else 0 end) as netfrom erc20_ethereum.evt_transfer ejoin tokens k on e.contract_address = k.addrjoin wallets w on (e."to" = w.addr or e."from" = w.addr)where e.evt_block_time >= timestamp '2021-11-01'group by 1, 2, 3),flows as (select * from eth_flows union all select * from erc20_flows),months as (select cast(m as timestamp) as month from unnest(sequence(date '2021-11-01', cast(date_trunc('month', current_date) as date), interval '1' month)) as t(m)),grid as (select m.month, w.label, a.asset from months m cross join wallets w cross join (values ('ETH'), ('USDC'), ('ENS')) as a(asset)),running as (select g.month, g.label, g.asset,sum(coalesce(f.net, 0)) over (partition by g.label, g.asset order by g.month rows between unbounded preceding and current row) as balance