Query editor
withstatic as (selecttimestamp '{{displayed period from for pairs charts}}' as ts_from, '{{timebox for pair charts}}' as timebox), pairs as (selectdate_trunc((select timebox from static), block_time) as time, if({{take into account the direction of the pair}}, src_token || '->' || dst_token, least(src_token, dst_token) || '-' || greatest(src_token, dst_token)) as pair, max_by((blockchain, if({{take into account the direction of the pair}}, src_token_link || '->' || dst_token_link, least(src_token_link, dst_token_link) || '-' || greatest(src_token_link, dst_token_link))), amount_usd) as pair_link, count_if(first_execution) as swaps, count(distinct user) as users, sum(amount_usd) as volume-- , approx_percentile(amount_usd / (case-- when dst_token_address = from_hex(rpad(regexp_extract(lower('{{token address}}'), '[a-fx\d]+'), 42, '0')) or '{{token address}}' = 'dst' then ((dst_token_amount + 1) / pow(10, dst_token_decimals))-- else ((src_token_amount + 1) / pow(10, src_token_decimals))-- end), 0.50) as median_price, approx_percentile(amount_usd / (casewhen dst_token_address = from_hex(rpad(regexp_extract(lower('{{token address}}'), '[a-fx\d]+'), 42, '0')) or '{{token address}}' = 'dst' then ((dst_token_amount + 1) / pow(10, cast(complement['dst_decimals'] as int)))else ((src_token_amount + 1) / pow(10, cast(complement['src_decimals'] as int)))end), 0.50) as median_price, approx_percentile(amount_usd, 0.5) as median_amount, avg(amount_usd) as mean_amount, max_by(concat(tx_link, ' ', split_part(blockchain, '_', 1), ' ', format('$%,.0f', amount_usd)), amount_usd) as tx_hash_max_amount_usd, max_by(concat(tx_link, ' ', split_part(blockchain, '_', 1), ' ', format('%1$tY-%1$tm-%1$td %tT', block_time)), block_time) as tx_hash_last, array_agg(distinct blockchain) as blockchainsfrom (selectblock_time, blockchain, src_token_address, dst_token_address, amount_usd, src_token_amount, dst_token_amount, complement, src_executed_amount_usd, dst_executed_amount_usd, tx_hash, "user", flags, coalesce(src_executed_symbol, cast(substr(src_token_address, 1, 2) as varchar) || '…' || substr(cast(src_token_address as varchar), length(src_token_address) * 2 - 1)) as src_token, coalesce(dst_executed_symbol, cast(substr(dst_token_address, 1, 2) as varchar) || '…' || substr(cast(dst_token_address as varchar), length(dst_token_address) * 2 - 1)) as dst_token, get_href(get_chain_explorer_address(blockchain, cast(src_token_address as varchar)), coalesce(src_executed_symbol, cast(substr(src_token_address, 1, 2) as varchar) || '…' || substr(cast(src_token_address as varchar), length(src_token_address) * 2 - 1))) as src_token_link, get_href(get_chain_explorer_address(blockchain, cast(dst_token_address as varchar)), coalesce(dst_executed_symbol, cast(substr(dst_token_address, 1, 2) as varchar) || '…' || substr(cast(dst_token_address as varchar), length(dst_token_address) * 2 - 1))) as dst_token_link, get_href(get_chain_explorer_tx_hash(blockchain, (cast(tx_hash as varchar))), concat(cast(substr(tx_hash, 1, 2) as varchar), '…', substr(cast(tx_hash as varchar), 63))) as tx_link, coalesce(element_at(flags, 'first'), true) as first_executionfrom oneinch.swapswhereblock_time > (select ts_from from static)and if('{{blockchain}}' = 'all', true, blockchain = '{{blockchain}}')and (if('{{token address}}' = 'all', true, src_token_address = from_hex('{{token address}}'))or if('{{token address}}' = 'all', true, dst_token_address = from_hex('{{token address}}')))andcase '{{swap mode}}'