Query editor
/* ===================================================STAGE 2d: STABLECOIN FLOWS (aggregated)Save as dune.boc.result_stablecoin_flows.Stablecoin transfers in and out of every wallet that passes the 2bscreens, 2021-2025, from stablecoins_evm.activity_enriched (one row pertransfer, so no deduplication), summed to:wallet x week x month x direction x token group x categoryA week that spans two months is split into two rows, so weekly totals(velocity) and monthly totals (Chainalysis) are both exact.Wallets with no stablecoin transfers have no rows.- token_group: USDT / USDC / other- category: Dune's activity category (e.g. transfer, dex, lending,flashloan); flash-loan legs are kept under their own category so flowmeasures can leave them out- transfers from a wallet to itself are dropped- n_tx counts distinct transactions within the row (for transfers pertransaction; not additive across rows)Counterparty-level detail (destinations, within-sample transfers for theChainalysis comparison) is built later for the final sample only.=================================================== */WITH wallets AS (SELECT DISTINCT walletFROM dune.boc.result_wallet_screensWHERE passes_screens),a AS (SELECT block_date, block_month, tx_hash, token_address, amount_usd,from_address, to_address, categoryFROM stablecoins_evm.activity_enrichedWHERE blockchain = 'ethereum'AND block_month >= DATE '2021-01-01'AND block_month < DATE '2026-01-01'AND from_address <> to_address),legs AS (SELECT w.wallet, 'out' AS direction, a.*FROM a INNER JOIN wallets w ON a.from_address = w.walletUNION ALLSELECT w.wallet, 'in', a.*FROM a INNER JOIN wallets w ON a.to_address = w.wallet)SELECTwallet,CAST(date_trunc('week', block_date) AS date) AS week,CAST(block_month AS date) AS month,direction,CASE WHEN token_address = 0xdac17f958d2ee523a2206206994597c13d831ec7 THEN 'USDT'WHEN token_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 THEN 'USDC'ELSE 'other' END AS token_group,category,SUM(amount_usd) AS usd,COUNT(*) AS n_transfers,COUNT(DISTINCT tx_hash) AS n_tx