Query editor
WITH market_info AS (-- Get market parameters to identify the loan tokenSELECTid as market_id,JSON_EXTRACT_SCALAR(marketParams, '$.loanToken') as loan_token_addressFROM morpho_blue_ethereum.morphoblue_evt_createmarketWHERE id = {{market_id}}),market_flows AS (-- Supply events (positive flow into market)SELECTs.id as market_id,s.evt_block_time,s.evt_block_number,s.evt_tx_hash,'supply' as event_type,s.assets as amount,s.assets as net_flow, -- positive for suppliesm.loan_token_address,0 as bad_debt_assets,0 as repaid_assetsFROM morpho_blue_ethereum.morphoblue_evt_supply sJOIN market_info m ON s.id = m.market_idWHERE s.id = {{market_id}}UNION ALL-- Withdraw events (negative flow out of market)SELECTw.id as market_id,w.evt_block_time,w.evt_block_number,w.evt_tx_hash,'withdraw' as event_type,w.assets as amount,-w.assets as net_flow, -- negative for withdrawalsm.loan_token_address,0 as bad_debt_assets,0 as repaid_assetsFROM morpho_blue_ethereum.morphoblue_evt_withdraw wJOIN market_info m ON w.id = m.market_idWHERE w.id = {{market_id}}UNION ALL-- Liquidation events (affect TVL through bad debt and repayments)SELECTl.id as market_id,l.evt_block_time,l.evt_block_number,l.evt_tx_hash,'liquidation' as event_type,l.repaidAssets as amount,-- Net effect: repaid assets come back in, bad debt reduces TVLCAST(l.repaidAssets AS BIGINT) - CAST(l.badDebtAssets AS BIGINT) as net_flow,m.loan_token_address,l.badDebtAssets,l.repaidAssetsFROM morpho_blue_ethereum.morphoblue_evt_liquidate l