Query editor
with top100_pnl as (with token_pnl as (with buys as (select taker as address, token_bought_address, min(block_date) as first_buy_date,sum(coalesce(amount_usd, token_sold_amount*p.price))/sum(coalesce(token_bought_amount, token_bought_amount_raw/pow(10, {{shitcoin_decimals}}))) as unit_price, --backup logic to determine shitcoin unit pricesum(coalesce(token_bought_amount, token_bought_amount_raw/pow(10, {{shitcoin_decimals}}))) as token_bought_amountfrom dex.trades djoin prices.usd p on d.token_sold_address = p.contract_address and p.blockchain = '{{blockchain}}' and p.minute = date_trunc('minute', d.block_time)left join {{blockchain}}.creation_traces c on d.taker = c.addresswhere token_bought_address = {{token_address}} and p.blockchain = '{{blockchain}}'and c.address is null and d.block_time > now() - interval '{{last_n_days}}' daygroup by 1, 2),sells as (select taker as address, token_sold_address, max(block_date) as last_sell_date,sum(coalesce(amount_usd, token_bought_amount*p.price))/sum(coalesce(token_sold_amount, token_sold_amount_raw/pow(10, {{shitcoin_decimals}}))) as unit_price, --backup logic to determine shitcoin unit pricesum(coalesce(token_sold_amount, token_sold_amount_raw/pow(10, {{shitcoin_decimals}}))) as token_sold_amountfrom dex.trades djoin prices.usd p on d.token_bought_address = p.contract_address and p.blockchain = '{{blockchain}}' and p.minute = date_trunc('minute', d.block_time)where token_sold_address = {{token_address}} and p.blockchain = '{{blockchain}}'and block_time > now() - interval '{{last_n_days}}' daygroup by 1, 2),combined as (select b.address, b.first_buy_date, s.last_sell_date, b.unit_price as avg_buy_price, s.unit_price as avg_sell_price, token_bought_amount,case when token_sold_amount > token_bought_amount then token_bought_amount else token_sold_amount end as token_sold_amountfrom buys bleft join sells s on b.address = s.address)selectaddress, first_buy_date, last_sell_date, avg_buy_price, token_bought_amount,coalesce(avg_sell_price, 0) as avg_sell_price, coalesce(token_sold_amount, 0) as token_sold_amount,coalesce(((avg_sell_price - avg_buy_price) * token_sold_amount), 0) as realized_pnl,coalesce((token_bought_amount - token_sold_amount), 0) as current_holding_from_buys,coalesce((token_bought_amount - token_sold_amount) * (select price from prices.usd_latest where contract_address = {{token_address}}), 0) as unrealized_pnlfrom combined)select *from token_pnlorder by realized_pnl desclimit 100),top_flows as (select d.blockchain, coalesce(d.token_bought_symbol, cast(d.token_bought_address as varchar)) as token_bought, sum(coalesce(amount_usd, token_sold_amount*p.price)) as amount_usdfrom dex.trades djoin prices.usd p on d.token_sold_address = p.contract_address and p.blockchain = d.blockchain and p.minute = date_trunc('minute', d.block_time)where taker IN (select address from top100_pnl)and block_time > now() - interval '7' daygroup by 1, 2order by 3 desc)select * from top_flows where amount_usd < 10000000000