Query editor
WITH--get prices of specific jettons (tokens), for latervault_token_prices AS (SELECT timestamp , token_address , price_usd, CASE WHEN token_address = '0:B113A994B5024A16719F69139328EB759596C38A25F59028B146FECDC3621DFE' THEN 'USDT'WHEN token_address = '0:2F956143C461769579BAEF2E32CC2D7BC18283F40D20BB03E432CD603AC33FFC' THEN 'NOT'WHEN token_address = '0:0000000000000000000000000000000000000000000000000000000000000000' THEN 'TON'WHEN token_address = upper('0:f29d17a209e2bcc652916d802ba69e23cb366e17afad61f8453343b9ba53ace4') THEN 'jUSDT' END AS symbol, CASE WHEN token_address = '0:B113A994B5024A16719F69139328EB759596C38A25F59028B146FECDC3621DFE' THEN 6WHEN token_address = '0:2F956143C461769579BAEF2E32CC2D7BC18283F40D20BB03E432CD603AC33FFC' THEN 9WHEN token_address = '0:0000000000000000000000000000000000000000000000000000000000000000' THEN 9WHEN token_address = upper('0:f29d17a209e2bcc652916d802ba69e23cb366e17afad61f8453343b9ba53ace4') THEN 6 END AS decimals -- because of the way the prices' table is structured, we'll need this for laterFROM ton.prices_dailyWHERE token_address IN ('0:B113A994B5024A16719F69139328EB759596C38A25F59028B146FECDC3621DFE', '0:2F956143C461769579BAEF2E32CC2D7BC18283F40D20BB03E432CD603AC33FFC', '0:0000000000000000000000000000000000000000000000000000000000000000', upper('0:f29d17a209e2bcc652916d802ba69e23cb366e17afad61f8453343b9ba53ace4') ))SELECT block_date-- , asset_type, SUM(CASE WHEN asset_type = 'Crypto' then fee_usd END ) AS Crypto, SUM(CASE WHEN asset_type = 'Commodity' then fee_usd END ) AS Commodity, SUM(CASE WHEN asset_type = 'Forex' then fee_usd END ) AS Forex, SUM(CASE WHEN asset_type = 'Stock' then fee_usd END ) AS Stock, sum(fee_usd) as daily_fees_usd, sum(sum(fee_usd)) over (ORDER BY block_date) as cumulative_fees_usd-- , sum(sum(fee_usd)) over (PARTITION BY asset_type ORDER BY block_date) as asset_cumulative_fees_usd, AVG(sum(fee_usd)) OVER (ORDER BY block_date ASC ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS ma_7d, AVG(sum(fee_usd)) OVER (ORDER BY block_date ASC ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) AS ma_30dFROM(SELECTCOALESCE(free_amount, 0)/1e9 + COALESCE(executor_amount, 0)/1e9 + COALESCE(fee_to_stakers, 0)/1e9 AS fee -- add all the split fees (to stakers, executor, and free amount) to get the total fee per action/trade event, CASEWHEN p.price_usd IS NULL AND tn.vault_token IN ('jUSDT', 'USDT') THEN COALESCE(free_amount, 0)/1e9 + COALESCE(executor_amount, 0)/1e9 + COALESCE(fee_to_stakers, 0)/1e9 -- to make up for missing prices in the table (noticed for the usdt token)ELSE (COALESCE(free_amount, 0)/1e9 + COALESCE(executor_amount, 0)/1e9 + COALESCE(fee_to_stakers, 0)/1e9 ) * p.price_usd * pow(10, p.decimals)END AS fee_usd, ti.name as asset_name, ti.type as asset_type, tn.*FROM stormtrade_ton.trade_notification tnLEFT JOINvault_token_prices pON-- IF(tn.vault_token = 'jUSDT', 'USDT', tn.vault_token) = p.symbol(CASE WHEN tn.vault_token = 'jUSDT' THEN 'USDT' ELSE tn.vault_token END) = p.symbol --no 'jusdt' in the prices' table so we use usdt prices for itAND tn.block_date = p.timestampLEFT JOINdune.elo_viano.dataset_st_trading_instruments ti -- imported the asset data obtained from the repoONtn.asset_id = ti.index-- WHERE trader_addr = '0:65801657D1629A17A8A4AA47073C76C2E5AFCAAB0021C347DBE1DE9154C33572' QA checkORDER BY tx_now)group by 1order by 1