Query editor
WITHlimit_order_type as (select t.tx_hash, t.order_uid, t.block_date,case when d.order_class is null and ( r.data.quote_buy_amount!='None' and r.data.quote_sell_amount!='None' and( (limit_sell_amount- r.data.quote_gas_cost/ r.data.quote_sell_token_price)/limit_buy_amount< ( cast(r.data.quote_sell_amount as double)/ cast(r.data.quote_buy_amount as double))and fee_atoms=0 and r.block_number > 19165490)or ( fee_atoms=0 and r.block_number <= 19165490) )-- and d.order_class!='twap'then 'limit'when d.order_class is null and r.data.quote_buy_amount!='None'and r.data.quote_sell_amount!='None' and (limit_sell_amount -( r.data.quote_gas_cost / r.data.quote_sell_token_price ))/limit_buy_amount >= ( cast(r.data.quote_sell_amount as double) / cast(r.data.quote_buy_amount as double) )and fee_atoms=0 and r.block_number > 19165490then 'market'when d.order_class is null and fee_atoms!=0 then 'market'else d.order_class end as order_classfromcow_protocol_{{blockchain}}.trades AS tleft joindune.cowprotocol.result_cow_protocol_{{blockchain}}_app_data as don t.app_data=d.app_hashleft join cowswap.raw_order_rewards ron r.order_uid=cast(t.order_uid as varchar)and cast(t.tx_hash as varchar)=r.tx_hash-- where r.data.quote_buy_amount!='None'-- and r.data.quote_sell_amount!='None'),annotated_trades AS (SELECTt.tx_hash, t.block_date,fee,usd_value,-- Surplus value is computed by taking the sum of ratios of price improvements (extra buy and fewer sell amounts compared to limit).-- This gives relative surplus which can be multiplied by the total trade amount to get absolute surplus.-- E.g. if I wanted to sell 100 for 50, but ended up getting 90, my surplus is (1 - (100/100) + 1 - (50/90)) = 44.44%---- (1 - (limit_buy_amount / atoms_bought) + 1 - (atoms_sold / limit_sell_amount)) * usd_value as surplus,usd_value * (cast(atoms_bought * limit_sell_amount as int256) - cast(atoms_sold * limit_buy_amount as int256)) / (atoms_bought * limit_sell_amount) as surplus,max(order_class) as order_class_fixFROM cow_protocol_{{blockchain}}.trades tleft join limit_order_type lon t.tx_hash=l.tx_hash and t.order_uid=l.order_uidand t.block_date=l.block_dateWHERE t.block_date >= date(timestamp '2022-12-01')group by 1,2,3,4,5)SELECTDATE_TRUNC('{{Aggregate by}}', block_date) AS aggregate_by,COUNT(CASE WHEN order_class_fix='limit' THEN 1 END) as limit_orders_count,COUNT(CASE WHEN order_class_fix='twap' THEN 1 END) as twap_orders_count,COUNT(CASE when order_class_fix is null or order_class_fix='market' then 1 end) as swap_orders_count,SUM(CASE WHEN order_class_fix='limit' THEN usd_value END) as limit_orders_volume,SUM(CASE WHEN order_class_fix='twap' THEN usd_value END) as twap_orders_volume,SUM(CASE when order_class_fix is null or order_class_fix='market' then usd_value end) as swap_orders_volume,-- SUM(CASE WHEN order_class is null THEN usd_value END) as other_orders_volume,SUM(CASE WHEN order_class_fix='limit' THEN surplus END) as limit_orders_surplus,SUM(CASE WHEN order_class_fix='twap' THEN surplus END) as twap_orders_surplus,SUM(CASE when order_class_fix is null or order_class_fix='market' THEN surplus END) as swap_orders_surplus