Query editor
with liq as (select chain as blockchain, 'v3' as version, evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidatorfrom aave_v3_multichain.pool_evt_liquidationcallunion all select 'arbitrum','v3', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v3_arbitrum.l2pool_evt_liquidationcallunion all select 'linea','v3', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v3_linea.l2pool_evt_liquidationcallunion all select 'scroll','v3', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v3_scroll.l2pool_evt_liquidationcallunion all select 'plasma','v3', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v3_plasma.poolinstance_evt_liquidationcallunion all select 'ink','v3', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v3_ink.l2pool_evt_liquidationcallunion all select 'ethereum','v2', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v2_ethereum.lendingpool_evt_liquidationcallunion all select 'polygon','v2', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v2_polygon.lendingpool_evt_liquidationcallunion all select 'avalanche_c','v2', evt_block_time, evt_tx_hash, evt_index, collateralAsset, debtAsset, debtToCover, liquidatedCollateralAmount, liquidator from aave_v2_avalanche_c.lendingpool_evt_liquidationcall),priced as (select l.*,cast(l.debtToCover as double) / power(10, pd.decimals) * pd.price as debt_usd,cast(l.liquidatedCollateralAmount as double) / power(10, pc.decimals) * pc.price as coll_usdfrom liq lleft join prices.day pd on pd.blockchain = l.blockchain and pd.contract_address = l.debtAsset and pd.timestamp = date_trunc('day', l.evt_block_time)left join prices.day pc on pc.blockchain = l.blockchain and pc.contract_address = l.collateralAsset and pc.timestamp = date_trunc('day', l.evt_block_time)),flagged as (select *,case when debt_usd > 0 and coll_usd is not null and (coll_usd - debt_usd) / debt_usd between -0.05 and 0.30 then true else false end as cleanfrom priced)selectdate_trunc('month', evt_block_time) as month,count(*) as liquidations,count_if(debt_usd is null or coll_usd is null) as unpriced_events,count_if(debt_usd is not null and coll_usd is not null and not clean) as outlier_events,sum(debt_usd) as debt_repaid_usd,sum(coll_usd) as collateral_seized_usd,sum(case when clean then coll_usd - debt_usd end) as liquidator_bonus_usd,sum(case when clean then debt_usd end) as clean_debt_usd,sum(case when version = 'v3' then debt_usd end) as v3_debt_repaid_usd,sum(case when version = 'v2' then debt_usd end) as v2_debt_repaid_usdfrom flaggedgroup by 1order by 1