Query editor
withtrades as (SELECT amount_usd as volume, token_bought_amount as amount, 'buy' as action, block_time as block_time, taker as address, token_bought_symbol as token, token_sold_symbol as traded_token, amount_usd/ token_bought_amount as cost_basis,token_bought_amount * ((SELECT price FROM prices.usd_latest where contract_address = lower('{{token_address}}')) - amount_usd/ token_bought_amount) as estimated_profitFROM dex.tradeswhere token_bought_address = lower('{{token_address}}')and blockchain = '{{chain}}'and block_time > now() - interval '{{day}} day'UNIONSELECT amount_usd as volume, token_sold_amount as amount, 'sell' as action, block_time as block_time, taker as address, token_sold_symbol as token, token_bought_symbol as traded_token, amount_usd/ token_sold_amount as cost_basis,token_sold_amount * (amount_usd/ token_sold_amount - (SELECT price FROM prices.usd_latest where contract_address = lower('{{token_address}}'))) as estimated_profitFROM dex.tradeswhere token_sold_address = lower('{{token_address}}')and blockchain = '{{chain}}'and block_time > now() - interval '{{day}} day'),latest_price(price, priced) as (SELECT sum(amount_usd) / sum(token_bought_amount) as price, (SELECT price from prices.usd_latest where contract_address = lower('{{token_address}}') and blockchain = '{{chain}}') as pricedFROM dex.trades where token_bought_address = lower('{{token_address}}')and block_time > now() - interval '3 hour'),dex as (SELECT action, volume, block_time, cost_basis, amount * ((SELECT case when price is null then priced else price end from latest_price) - cost_basis) as estimated_profit,token, traded_token,case when '{{chain}}' = 'arbitrum' then '<a href=https://arbiscan.io/address/' || address || ' target=_blank">' || address || '</a>'when '{{chain}}' = 'polygon' then '<a href=https://polygonscan.com//address/' || address || ' target=_blank">' || address || '</a>'when '{{chain}}' = 'optimism' then '<a href=https://optimistic.etherscan.io//address/' || address || ' target=_blank">' || address || '</a>'when '{{chain}}' = 'ethereum' then '<a href=https://etherscan.io//address/' || address || ' target=_blank">' || address || '</a>' end as wallet_addressFROM trades),trading as (SELECT ROW_NUMBER() OVER (ORDER BY a.volume DESC) as ranking, a.* FROM dex a )SELECT * FROM tradingLIMIT 100