Query editor
withL as (select * from dune.fadz2323.result_base_liq_open_race_events),S as (select * from dune.fadz2323.result_base_liq_open_race_svr),X as (select * from dune.fadz2323.result_base_liq_open_race_txs),tb as (select tx, sum(bonus_usd) tx_bonus from L group by 1),mkt_atlas as (select mkey, sum(case when is_atlas then bonus_usd else 0 end) atlas_bonus, sum(bonus_usd) all_bonus from L group by 1),e as (select L.*, S.svr_agg, S.map_m_agg, X.prio_usd / nullif(tb.tx_bonus,0) prio_pct, X.tx_type, X.n_loser_senders, X.n_loser_same_block,casewhen (S.last_atlas_ts is not null and L.ts - S.last_atlas_ts <= 10) or (S.last_s_ts is not null and L.ts - S.last_s_ts <= 10) then 'leftover'when S.lag_fb_mapped is not null or (S.svr_agg is null and S.lag_fb_any <= 3) then 'fallback'when S.nonsvr_hit >= 0 then 'non_svr'else 'drift' end cls,casewhen (S.last_atlas_ts is not null and L.ts - S.last_atlas_ts <= 10) then (case when S.last_atlas_b = L.b then 1 else 0 end)when (S.last_s_ts is not null and L.ts - S.last_s_ts <= 10) then (case when L.ts = S.last_s_ts then 1 else 0 end)when S.lag_fb_mapped is not null then (case when S.lag_fb_mapped < (case when L.b >= 53664122 then 0.2 else 2 end) then 1 else 0 end)when S.svr_agg is null and S.lag_fb_any <= 3 then (case when S.lag_fb_any < (case when L.b >= 53664122 then 0.2 else 2 end) then 1 else 0 end)when S.nonsvr_hit >= 0 then S.nonsvr_hitelse 0 end same_blkfrom L join S on S.tx = L.tx and S.ei = L.eileft join X on X.tx = L.txleft join tb on tb.tx = L.txwhere not L.is_atlas),win as (select mkey, winner, sum(bonus_usd) wb from e group by 1,2),top1 as (select mkey, max(wb)/nullif(sum(wb),0) top1_share from win group by 1),agg as (select e.proto, e.mkey, max(e.coll) coll, max(e.loan) loan, max(e.lltv) lltv,count(*) n, sum(bonus_usd) bonus, approx_percentile(repaid_usd, 0.5) med_repaid,count(distinct winner) winners, approx_percentile(prio_pct, 0.5) med_prio_pct, approx_percentile(txi, 0.5) med_txi,avg(same_blk) same_blk_share,sum(case when cls='leftover' then bonus_usd else 0 end)/nullif(sum(bonus_usd),0) sh_leftover,sum(case when cls='fallback' then bonus_usd else 0 end)/nullif(sum(bonus_usd),0) sh_fallback,sum(case when cls='non_svr' then bonus_usd else 0 end)/nullif(sum(bonus_usd),0) sh_nonsvr,sum(case when cls='drift' then bonus_usd else 0 end)/nullif(sum(bonus_usd),0) sh_drift,approx_percentile(n_loser_senders, 0.5) med_losers, max(case when svr_agg is not null then 1 else 0 end) svr_mappedfrom e group by 1,2)select agg.proto, coalesce(tc.symbol, '?') || '/' || coalesce(tl.symbol, '?') || coalesce(' ' || cast(round(agg.lltv*100,1) as varchar), '') market,agg.n, round(agg.bonus) bonus_usd, round(agg.med_repaid) med_repaid_usd, agg.winners, round(t.top1_share,2) top1,round(agg.med_prio_pct,3) med_prio_pct, agg.med_txi, round(agg.same_blk_share,2) same_blk,round(agg.sh_leftover,2) leftover, round(agg.sh_fallback,2) fallback, round(agg.sh_nonsvr,2) nonsvr, round(agg.sh_drift,2) drift,agg.med_losers, agg.svr_mapped, round(ma.atlas_bonus/nullif(ma.all_bonus,0),2) mkt_atlas_share,round(agg.bonus / nullif(agg.winners,0)) bonus_per_winner, agg.mkeyfrom aggleft join top1 t on t.mkey = agg.mkeyleft join mkt_atlas ma on ma.mkey = agg.mkeyleft join tokens.erc20 tc on tc.blockchain = 'base' and tc.contract_address = agg.collleft join tokens.erc20 tl on tl.blockchain = 'base' and tl.contract_address = agg.loanorder by agg.bonus desc nulls lastlimit 40