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),A as (select tx, mkey, b, txi, ts, borrower, winner, repaid_usd from L where is_atlas),atx as (select tx, count(*) n_liq, count(distinct borrower) n_borr, count(distinct mkey) n_mkt from A group by 1),solvers as (select distinct winner from A),lo as (select L.*, S.last_atlas_ts, S.last_atlas_b, S.same_borrower, L.ts - S.last_atlas_ts gap,X.prio_usd / nullif(tb.tx_bonus,0) prio_pctfrom L join S on S.tx = L.tx and S.ei = L.eileft join X on X.tx = L.tx left join tb on tb.tx = L.txwhere not L.is_atlas and S.last_atlas_ts is not null and L.ts - S.last_atlas_ts <= 10),-- the Atlas tx immediately preceding each leftover in the same marketprev as (select lo.tx, lo.ei, max_by(A.tx, A.ts*1e6 + A.txi) atlas_txfrom lo join A on A.mkey = lo.mkey and A.ts = lo.last_atlas_ts and A.b = lo.last_atlas_bgroup by 1,2),-- did the Atlas-liquidated borrower get liquidated again later (partial in Atlas)?apart as (select A.tx, A.borrower, count(*) n_againfrom A join L l2 on l2.mkey = A.mkey and l2.borrower = A.borrower and not l2.is_atlas and l2.ts > A.ts and l2.ts - A.ts <= 10group by 1,2),mk_med as (select mkey, approx_percentile(repaid_usd, 0.5) atlas_med_repaid from A group by 1),rows1 as (select lo.*, atx.n_liq atlas_n_liq, atx.n_borr atlas_n_borr,case when lo.same_borrower = 1 then 'A_partial_remainder (same borrower)'when lo.gap = 0 then 'B_other_borrower same block as Atlas tx'when lo.gap <= 2 then 'C_other_borrower <=2s after'else 'D_other_borrower 2-10s after' end why,case when s.winner is not null then 1 else 0 end winner_is_atlas_solver,m.atlas_med_repaidfrom lo left join prev on prev.tx = lo.tx and prev.ei = lo.eileft join atx on atx.tx = prev.atlas_txleft join solvers s on s.winner = lo.winnerleft join mk_med m on m.mkey = lo.mkey)select why, count(*) n, round(sum(bonus_usd)) bonus, round(sum(bonus_usd)/sum(sum(bonus_usd)) over (),3) bonus_share,round(approx_percentile(repaid_usd,0.5)) med_repaid, round(approx_percentile(atlas_med_repaid,0.5)) mkt_atlas_med_repaid,approx_percentile(atlas_n_liq,0.5) med_atlas_tx_nliq, round(avg(atlas_n_liq),1) avg_atlas_tx_nliq,round(avg(winner_is_atlas_solver),2) sh_winner_is_solver,round(sum(case when winner_is_atlas_solver=1 then bonus_usd end)/sum(bonus_usd),2) bonus_sh_solver_winners,count(distinct winner) winners, round(approx_percentile(prio_pct,0.5),3) med_prio_pct, approx_percentile(txi,0.5) med_txi,round(approx_percentile(gap,0.5),2) med_gap_sfrom rows1 group by 1union allselect 'Z_Atlas txs: liq per tx dist', count(*), null, null, approx_percentile(n_liq,0.5), approx_percentile(n_liq,0.9), approx_percentile(n_borr,0.5), avg(n_liq),avg(case when n_mkt > 1 then 1.0 else 0 end), null, null, null, null, nullfrom atxunion allselect 'Y_Atlas borrowers re-liquidated in open race <=10s (partial)', count(*), null, null, null, null, null, null, null, null, null, null, null, null from apartunion allselect 'X_Atlas liquidation events total', count(*), round(sum(L.bonus_usd)), null, null, null, null, null, null, null, null, null, null, null from L where is_atlasorder by 1