Query editor
WITH treasury AS (SELECT 0x3c54c68872550bbd5adfea96d8df7f95e24496b7 AS wallet), movements AS (SELECTCOALESCE(contract_address, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee) AS token_address,COALESCE(symbol, 'UNKNOWN') AS asset,COALESCE(token_standard, 'unknown') AS token_standard,SUM(CASEWHEN "to" = (SELECT wallet FROM treasury) THEN amountWHEN "from" = (SELECT wallet FROM treasury) THEN -amountELSE 0END) AS balanceFROM tokens.transfersWHERE blockchain = 'base'AND block_month >= DATE '2023-08-01'AND ("to" = (SELECT wallet FROM treasury) OR "from" = (SELECT wallet FROM treasury))GROUP BY 1, 2, 3), latest_prices AS (SELECTcontract_address,max_by(price, timestamp) AS price_usd,max_by(symbol, timestamp) AS price_symbolFROM prices.latestWHERE blockchain = 'base'AND contract_address IN (SELECT token_address FROM movements WHERE token_address <> 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeUNION ALL SELECT 0x4200000000000000000000000000000000000006)GROUP BY 1), priced AS (SELECTm.asset,m.token_address,m.token_standard,m.balance,COALESCE(p.price_usd, weth.price_usd) AS price_usd,m.balance * COALESCE(p.price_usd, weth.price_usd, 0) AS balance_usdFROM movements mLEFT JOIN latest_prices p ON m.token_address = p.contract_addressLEFT JOIN latest_prices weth ON m.token_address = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeAND weth.contract_address = 0x4200000000000000000000000000000000000006WHERE ABS(m.balance) > 1e-12), totals AS (SELECT SUM(COALESCE(balance_usd, 0)) AS total_balance_usd FROM priced)SELECTasset,token_address,token_standard,balance,price_usd,balance_usd,balance_usd / NULLIF((SELECT total_balance_usd FROM totals), 0) AS pct_of_treasury,(SELECT total_balance_usd FROM totals) AS total_balance_usdFROM pricedWHERE balance > 0ORDER BY balance_usd DESC NULLS LAST, balance DESC