Query editor
with midnight_take as (select topic1 as id_,bytearray_to_int256(bytearray_substring(data, 385, 32)) as total_units_deltafrom base.logswhere contract_address = 0xadedd8ab6de832766fedf0fac4992e5c4d3ea18aand topic0 = 0xbd88656d0ee14d32d4f74f814f6ccb5c750a4c542d6fdf83ee00ddca4edebff0and block_time >= timestamp '2026-07-05'),tok as (select contract_address, max(symbol) as symbol, max(decimals) as decimalsfrom tokens.erc20 where blockchain = 'base' group by 1),mkt as (select id_,from_hex(json_extract_scalar(market, '$.loanToken')) as loan_token,cast(json_extract_scalar(market, '$.maturity') as bigint) as maturity_ts,evt_block_time as created_tsfrom morpho_midnight_base.midnight_evt_marketcreated),mkt_named as (select m.id_, m.loan_token, m.maturity_ts,cast(from_unixtime(m.maturity_ts) as date) as maturity_date,lt.decimals as loan_decfrom mkt mleft join tok lt on lt.contract_address = m.loan_token),market_collateral as (select mc.id_,coalesce(e.symbol, '?') as collat_sym_raw,case when coalesce(e.symbol, '?') like '%-collat%' then split_part(e.symbol, '-', 1) else coalesce(e.symbol, '?') end as collat_symfrom morpho_midnight_base.midnight_evt_marketcreated mccross join unnest(cast(json_extract(mc.market, '$.collateralParams') as array(varchar))) as t(cp)left join tok e on e.contract_address = from_hex(json_extract_scalar(cp, '$.token'))),collat_label as (select id_,array_join(array_agg(distinct collat_sym order by collat_sym), '+') as collateral_set,max(case when collat_sym_raw like '%-collat%' then 1 else 0 end) = 1 as is_tenorfrom market_collateral group by 1),px_universe as (select distinct loan_token as token from mkt where loan_token is not null),px_spot as (select contract_address, px from (select p.contract_address, p.price as px,row_number() over (partition by p.contract_address order by p.minute desc) as rnfrom prices.usd pwhere p.blockchain = 'base' and p.minute >= timestamp '2026-07-05'and p.contract_address in (select token from px_universe)) where rn = 1),mkt_full as (select m.id_, m.loan_token, m.maturity_ts, m.maturity_date, m.loan_dec,cl.collateral_set, cl.is_tenorfrom mkt_named mleft join collat_label cl on cl.id_ = m.id_),taken as (select id_, sum(total_units_delta) as net_units from midnight_take group by 1),repaid as (select id_, sum(units) as repay_units from morpho_midnight_base.midnight_evt_repay where evt_block_time >= timestamp '2026-07-05' group by 1),liquidated as (select id_, sum(repaidUnits) as liq_units from morpho_midnight_base.midnight_evt_liquidate where evt_block_time >= timestamp '2026-07-05' group by 1)select maturity_date, maturity_month, market_type, collateral_labeled, collateral_set, n_markets, outstanding_debt_usd