Query editor
WITHapx AS (SELECT chain, address FROM dune.apyx_fi.dataset_apyx_tokensWHERE symbol = 'apxUSD' AND chain IN ('ethereum','base' /*, 'bnb'*/)),stbl AS (SELECT chain, address FROM dune.apyx_fi.dataset_stable_coinsWHERE chain IN ('ethereum','base' /*, 'bnb'*/)),dt AS ( -- one dex.trades scan: apxUSD trades with the counter leg, apxUSD amount, usdSELECT d.blockchain AS chain, d.project_contract_address AS pool,CASE WHEN d.token_bought_address = a.address THEN d.token_sold_address ELSE d.token_bought_address END AS counter,CASE WHEN d.token_bought_address = a.address THEN d.token_bought_amount ELSE d.token_sold_amount END AS apx_amount,d.amount_usd, d.block_timeFROM dex.trades dJOIN apx a ON a.chain = d.blockchain AND (d.token_bought_address = a.address OR d.token_sold_address = a.address)WHERE d.blockchain IN ('ethereum','base' /*, 'bnb'*/)AND d.block_month >= DATE '2026-02-01' AND d.block_date >= DATE '2026-02-01'AND d.project_contract_address NOT IN (0x000000000004444c5dc75cb358380d2e3de08a90, -- Uniswap V4 PoolManager (Ethereum)0x498581ff718922c3f8e6a244956af099b2652b2b -- Uniswap V4 PoolManager (Base)-- , <bnb Uniswap V4 PoolManager> -- TODO: add when enabling BNB)),stable_pools AS ( -- apxUSD pools whose counter leg is a curated stablecoin (drops apyUSD/sUSDat/STRCx)SELECT DISTINCT dt.chain, dt.pool FROM dt JOIN stbl s ON s.chain = dt.chain AND s.address = dt.counter),apx_locked AS ( -- apxUSD net transfers held by the stable pools (apxUSD volume is small => fast)SELECT t.blockchain AS chain,SUM(CASE WHEN t."to" = sp.pool THEN CAST(t.amount_raw AS DECIMAL(38,0))WHEN t."from" = sp.pool THEN -CAST(t.amount_raw AS DECIMAL(38,0)) END) / 1e18 AS apxusd_units,MAX(t.block_number) AS last_blockFROM tokens.transfers tJOIN apx a ON a.chain = t.blockchain AND t.contract_address = a.addressJOIN stable_pools sp ON sp.chain = t.blockchain AND (t."to" = sp.pool OR t."from" = sp.pool)WHERE t.blockchain IN ('ethereum','base' /*, 'bnb'*/) AND t.block_date >= DATE '2026-02-01'GROUP BY 1),stable_locked AS ( -- stable reserves of those pools, from the spell, curated stables onlySELECT chain, SUM(latest_balance) AS stable_units FROM (SELECT b.blockchain AS chain, b.address, b.token_address, max_by(b.balance, b.day) AS latest_balanceFROM stablecoins_multichain.balances bJOIN stable_pools sp ON sp.chain = b.blockchain AND lower(concat('0x', to_hex(sp.pool))) = b.addressJOIN stbl s ON s.chain = b.blockchain AND lower(concat('0x', to_hex(s.address))) = b.token_addressWHERE b.blockchain IN ('ethereum','base' /*, 'bnb'*/) AND b.day >= CURRENT_DATE - INTERVAL '14' DAYGROUP BY 1, 2, 3) GROUP BY 1),per_chain AS ( -- every stable pool holds apxUSD, so apx_locked is the chain spineSELECT al.chain, al.apxusd_units, COALESCE(sl.stable_units, 0) AS stable_units, al.last_blockFROM apx_locked al LEFT JOIN stable_locked sl ON sl.chain = al.chain),rolled AS ( -- per-chain rows + all_chains via GROUPING SETS (no second scan)SELECT COALESCE(chain, 'all_chains') AS chain, GROUPING(chain) AS g,SUM(apxusd_units) AS apxusd_units, SUM(stable_units) AS stable_units, MAX(last_block) AS last_blockFROM per_chain GROUP BY GROUPING SETS ((chain), ()))SELECT r.chain, r.apxusd_units, r.stable_units,r.apxusd_units / NULLIF(r.apxusd_units + r.stable_units, 0) AS apxusd_nominal_share,r.apxusd_units * p.vwap AS apxusd_value_usd,