Query editor
-- Metronome Recovery Dashboard: Daily Morpho Debt (Section 2 / Section 5 base query)-- On-chain, live. Cumulative outstanding debt per synth per day across Morpho Blue markets-- whose loanToken is msUSD/msETH, from Borrow (+), Repay (-), AccrueInterest (+interest),-- and Liquidate (-repaidAssets, -badDebtAssets) events.-- NOTE: excludes legacy pre-Morpho CDP debt, which has no historical time series available-- (see the "Known data-source limitation" note on the Exec Summary Composite query).WITH cm AS (SELECT 'ethereum' AS chain, id, json_extract_scalar(marketParams, '$.loanToken') AS loanFROM morpho_blue_ethereum.morphoblue_evt_createmarketUNION ALLSELECT 'base', id, json_extract_scalar(marketParams, '$.loanToken')FROM morpho_blue_base.morphoblue_evt_createmarket),mkt AS (SELECT cm.chain, cm.id, s.synthFROM cmJOIN query_8222703 s ON s.chain = cm.chain AND LOWER(cm.loan) = LOWER('0x' || to_hex(s.token))),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, SUM(CAST(f.delta AS double)) / 1e18 AS net_deltaFROM flows fJOIN mkt m ON m.chain = f.chain AND m.id = f.idGROUP BY 1, 2),spine AS (SELECT d AS day, syn.synthFROM UNNEST(SEQUENCE((SELECT MIN(day) FROM daily_net), CURRENT_DATE)) AS t(d)CROSS JOIN (SELECT DISTINCT synth FROM daily_net) syn)SELECTsp.day,sp.synth,SUM(COALESCE(dn.net_delta, 0)) OVER (PARTITION BY sp.synth ORDER BY sp.day) AS debtFROM spine spLEFT JOIN daily_net dn ON dn.day = sp.day AND dn.synth = sp.synthORDER BY 1, 2;