Query editor
-- Metronome Recovery Dashboard: Daily Debt by Collateral (Section 5)-- On-chain, live. Cumulative Morpho debt per synth per collateral label (Morpho Markets-- Registry, query_8223464), per day. Sums to the same total as Daily Morpho Debt-- (query_8222771) once collapsed across collateral_label -- this is that same debt broken-- out by collateral type instead of collapsed to a synth total.WITH flows AS (SELECT 'ethereum' AS chain, id, evt_block_date AS day, CAST(assets AS int256) AS delta FROM morpho_blue_ethereum.morphoblue_evt_borrowUNION ALL SELECT 'ethereum', id, evt_block_date, -CAST(assets AS int256) FROM morpho_blue_ethereum.morphoblue_evt_repayUNION ALL SELECT 'ethereum', id, evt_block_date, CAST(interest AS int256) FROM morpho_blue_ethereum.morphoblue_evt_accrueinterestUNION ALL SELECT 'ethereum', id, evt_block_date, -CAST(repaidAssets AS int256) FROM morpho_blue_ethereum.morphoblue_evt_liquidateUNION ALL SELECT 'ethereum', id, evt_block_date, -CAST(badDebtAssets AS int256) FROM morpho_blue_ethereum.morphoblue_evt_liquidateUNION ALL SELECT 'base', id, evt_block_date, CAST(assets AS int256) FROM morpho_blue_base.morphoblue_evt_borrowUNION ALL SELECT 'base', id, evt_block_date, -CAST(assets AS int256) FROM morpho_blue_base.morphoblue_evt_repayUNION ALL SELECT 'base', id, evt_block_date, CAST(interest AS int256) FROM morpho_blue_base.morphoblue_evt_accrueinterestUNION ALL SELECT 'base', id, evt_block_date, -CAST(repaidAssets AS int256) FROM morpho_blue_base.morphoblue_evt_liquidateUNION ALL SELECT 'base', id, evt_block_date, -CAST(badDebtAssets AS int256) FROM morpho_blue_base.morphoblue_evt_liquidate),daily_net AS (SELECT f.day, m.synth, m.collateral_label, SUM(CAST(f.delta AS double)) / 1e18 AS net_deltaFROM flows fJOIN query_8223464 m ON m.chain = f.chain AND m.id = f.idGROUP BY 1, 2, 3),spine AS (SELECT d AS day, combo.synth, combo.collateral_labelFROM UNNEST(SEQUENCE((SELECT MIN(day) FROM daily_net), CURRENT_DATE)) AS t(d)CROSS JOIN (SELECT DISTINCT synth, collateral_label FROM daily_net) combo)SELECTsp.day,sp.synth,sp.collateral_label,SUM(COALESCE(dn.net_delta, 0)) OVER (PARTITION BY sp.synth, sp.collateral_label ORDER BY sp.day) AS debtFROM spine spLEFT JOIN daily_net dn ON dn.day = sp.day AND dn.synth = sp.synth AND dn.collateral_label = sp.collateral_labelORDER BY 1, 2, 3;