Query editor
withpairs as (select * from (values('1INCH/WBTC', 0x2260fac5e5542a773aa44fbcfedf7c193bc2c599),('1INCH/UNI', 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984),('1INCH/AAVE', 0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9),('1INCH/ONDO', 0xfaba6f8e4a5e8ab82f62fe7c39859fa577269be3)) t(pair, other)),inch as (select 0x111111111117dc0aa78b770fa6a738034120c302 as a),fills as (select block_time, bytearray_substring(data,45,20) as maker,bytearray_substring(data,109,20) as token_in, bytearray_substring(data,141,20) as token_out,cast(bytearray_to_uint256(bytearray_substring(data,161,32)) as double) as amount_in,cast(bytearray_to_uint256(bytearray_substring(data,193,32)) as double) as amount_outfrom ethereum.logswhere topic0 = 0x54bc5c027d15d7aa8ae083f994ab4411d2f223291672ecd3a344f3d92dcaf8b2and contract_address = 0x111111338c5091e8440b67b168bae16a668ac0deand block_time >= now() - interval '{{days}}' dayand bytearray_substring(data,45,20) = {{maker}} -- uncomment to focus one wallet),tagged as (select f.block_time, p.pair,case when f.token_in = (select a from inch) then 'buy_1INCH' else 'sell_1INCH' end as side,f.token_in, f.token_out, f.amount_in, f.amount_outfrom fills fjoin pairs p on p.other in (f.token_in, f.token_out)where (select a from inch) in (f.token_in, f.token_out)),dcm as (select contract_address, max(decimals) as decimals from tokens.erc20 where blockchain='ethereum' group by 1),priced as (select t.pair, t.side, t.block_time,t.amount_in /power(10, coalesce(d_in.decimals,18)) * pin.price as usd_in,t.amount_out/power(10, coalesce(d_out.decimals,18)) * pout.price as usd_outfrom tagged tleft join dcm d_in on d_in.contract_address = t.token_inleft join dcm d_out on d_out.contract_address = t.token_outleft join prices.usd pin on pin.blockchain='ethereum' and pin.contract_address=t.token_inand pin.minute = date_trunc('minute', t.block_time)left join prices.usd pout on pout.blockchain='ethereum' and pout.contract_address=t.token_outand pout.minute = date_trunc('minute', t.block_time)),pf as (select pair, side,(usd_in - usd_out) as edge_usd,(usd_in + usd_out)/2 as notional,(usd_in - usd_out)/nullif((usd_in+usd_out)/2,0)*10000 as edge_bpsfrom priced where usd_in is not null and usd_out is not null)selectpair, side,count(*) as fills,round(sum(edge_usd),2) as total_edge,round(sum(edge_usd)/nullif(sum(notional),0)*10000,2) as avg_bps,round(approx_percentile(edge_bps,0.05),1) as p05,round(approx_percentile(edge_bps,0.25),1) as p25,round(approx_percentile(edge_bps,0.50),1) as p50_median,round(approx_percentile(edge_bps,0.75),1) as p75,round(approx_percentile(edge_bps,0.95),1) as p95,round(sum(case when edge_bps < -10 then edge_usd end),2) as loss_beyond_10bps,count(case when edge_bps < -10 then 1 end) as n_beyond_10bpsfrom pf