Query editor
-- Aave V4 frxUSD Market: What borrowed frxUSD is SWAPPED INTO [borrower-behavior 2b/3]-- One layer deeper than 08. VENUE-AGNOSTIC method: in each tx where a borrower-- SENT frxUSD out, find the non-frxUSD token they RECEIVED BACK in that same tx-- (the swap output), from raw ERC20 transfers. This captures aggregator/RFQ-- swaps (Kyber/CoW/0x) that dex.trades attributes to the solver, not the user.-- Chart: pie/bar (frxusd_sold by bought asset) + table. Depends on BASE #1.---- Each sell-tx is attributed to its PRIMARY bought token (largest USD leg), so a-- tx's frxUSD contributes once. Repay/supply txs (no token returns) drop out.-- CAVEATS: assumes atomic same-tx swaps (true for DEX/aggregator fills; misses-- limit orders settled in a later tx). USD uses latest (24h) price as a proxy.WITH frx_borrowers AS (SELECT wallet, MIN(block_time) AS first_borrowFROM query_7919889 -- 00_frxusd_events_baseWHERE side = 'borrow'GROUP BY walletHAVING SUM(net_amount) > 0),-- Txs where a borrower sent frxUSD out (the sell leg), + frxUSD amount per txsell_txs AS (SELECT tr.evt_tx_hash AS tx_hash,tr."from" AS borrower,SUM(CAST(tr.value AS DOUBLE) / 1e18) AS frxusd_outFROM erc20_ethereum.evt_Transfer trJOIN frx_borrowers bON tr."from" = b.walletAND tr.evt_block_time >= b.first_borrowWHERE tr.contract_address = 0xcacd6fd266af91b8aed52accc382b4e165586e29AND tr.to <> 0x0000000000000000000000000000000000000000GROUP BY 1, 2),latest_prices AS (SELECT contract_address, AVG(price) AS priceFROM prices.usdWHERE blockchain = 'ethereum' AND minute >= CURRENT_TIMESTAMP - INTERVAL '24' HOURGROUP BY 1),-- Tokens the borrower RECEIVED in those same txs (the buy leg), valuedreceived AS (SELECT s.tx_hash, s.borrower, s.frxusd_out,rin.contract_address AS bought_token,SUM(CAST(rin.value AS DOUBLE) / POWER(10, COALESCE(tk.decimals, 18))) AS bought_amount,SUM(CAST(rin.value AS DOUBLE) / POWER(10, COALESCE(tk.decimals, 18)) * COALESCE(lp.price, 0)) AS bought_usd,MAX(tk.symbol) AS bought_symbolFROM sell_txs sJOIN erc20_ethereum.evt_Transfer rinON rin.evt_tx_hash = s.tx_hashAND rin.to = s.borrowerAND rin.contract_address <> 0xcacd6fd266af91b8aed52accc382b4e165586e29 -- exclude frxUSD change-- prune the transfer scan to the market's lifetime (Aave V4 launch)AND rin.evt_block_time >= DATE '2026-03-23'LEFT JOIN tokens.erc20 tk ON tk.blockchain = 'ethereum' AND tk.contract_address = rin.contract_addressLEFT JOIN latest_prices lp ON lp.contract_address = rin.contract_addressGROUP BY 1, 2, 3, rin.contract_address),