Query editor
/**Query outputs the following daily DeFi metrics for a fungible token on EVM:1. Trades by token pair2. Median trading volume by token pairNOTE! There might be no official pools for a pair if user trades it via an aggregator like 1inch.For example, user can send USDC to 1inch which will swap vault USDT for MOCA in an official liquidity pool (e.g. Uni V3) and send MOCA to the user.It will be recorded as USDC<>MOCA trade despite the official liquidity pool used was USDT<>MOCA.**/WITH-- 1. Calculate the daily trading volume and number of trades per token pair with the target tokendaily_pair_trades AS (SELECTSUM(amount_usd) AS pair_trading_vol_usd,COUNT(tx_hash) AS number_of_trades,AVG(amount_usd) AS avg_trade_value,APPROX_PERCENTILE(amount_usd, 0.5) AS median_trade_value,DATE_TRUNC('day', block_date) AS day,token_pairFROM dex.tradesWHERE blockchain = '{{blockchain}}'AND (token_bought_address = {{token_address}}OR token_sold_address = {{token_address}})GROUP BY DATE_TRUNC('day', block_date), token_pair),-- 2. Calculate the total daily trading volume and number of trades across all token pairs with the target tokendaily_total_trades AS (SELECTSUM(amount_usd) AS pair_trading_vol_usd,COUNT(tx_hash) AS number_of_trades,AVG(amount_usd) AS avg_trade_value,APPROX_PERCENTILE(amount_usd, 0.5) AS median_trade_value,DATE_TRUNC('day', block_date) AS day,'Total' AS token_pairFROM dex.tradesWHERE blockchain = '{{blockchain}}'AND (token_bought_address = {{token_address}}OR token_sold_address = {{token_address}})GROUP BY DATE_TRUNC('day', block_date))-- Finally, combine daily trades and volume by pair with daily Total trades and volume across all pairsSELECT * FROM daily_pair_tradesUNION ALLSELECT * FROM daily_total_tradesORDER BY day DESC, pair_trading_vol_usd DESC