Query editor
WITH raw_user_flows AS (-- 1. Supply: The real collateral owner is 'onBehalfOf'SELECT "onBehalfOf" AS account, reserve AS token_address, CAST(amount AS DOUBLE) AS supply_amt, 0.0 AS withdraw_amt, 0.0 AS borrow_amt, 0.0 AS repay_amtFROM aave_horizon_ethereum.poolinstance_evt_supply WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8UNION ALL-- 2. Withdraw: The owner is 'user' burning their receipt tokensSELECT user AS account, reserve AS token_address, 0.0, CAST(amount AS DOUBLE), 0.0, 0.0FROM aave_horizon_ethereum.poolinstance_evt_withdraw WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8UNION ALL-- 3. Borrow: The real debtor is 'onBehalfOf'SELECT "onBehalfOf" AS account, reserve AS token_address, 0.0, 0.0, CAST(amount AS DOUBLE), 0.0FROM aave_horizon_ethereum.poolinstance_evt_borrow WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8UNION ALL-- 4. Repay: The debtor being paid down is 'user'SELECT user AS account, reserve AS token_address, 0.0, 0.0, 0.0, CAST(amount AS DOUBLE)FROM aave_horizon_ethereum.poolinstance_evt_repay WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8),user_balances AS (SELECTaccount,token_address,SUM(supply_amt) - SUM(withdraw_amt) AS net_supplied_raw,SUM(borrow_amt) - SUM(repay_amt) AS net_borrowed_rawFROM raw_user_flowsGROUP BY account, token_address),latest_indices AS (SELECT reserve AS token_address, CAST(liquidityIndex AS DOUBLE) / 1e27 AS liquidity_index, CAST(variableBorrowIndex AS DOUBLE) / 1e27 AS borrow_indexFROM (SELECT reserve, liquidityIndex, variableBorrowIndex, ROW_NUMBER() OVER(PARTITION BY reserve ORDER BY evt_block_time DESC) AS rn FROM aave_horizon_ethereum.poolinstance_evt_reservedataupdated) WHERE rn = 1),aave_oracle_prices AS (SELECT asset AS token_address, CAST(output_0 AS DOUBLE) / 1e8 AS oracle_price_usdFROM (SELECT asset, output_0, ROW_NUMBER() OVER(PARTITION BY asset ORDER BY call_block_time DESC) AS rn FROM aave_horizon_ethereum.aaveoracle_call_getassetprice WHERE call_success = true) WHERE rn = 1),user_usd_balances AS (SELECTu.account,COALESCE(t.symbol, CAST(u.token_address AS VARCHAR)) AS symbol,CASE WHEN COALESCE(t.symbol, '') IN ('USTB', 'mGLOBAL', 'JAAA', 'USCC', 'VBILL', 'JTRSY', 'USYC', 'BUIDL') THEN 'RWA' ELSE 'STABLECOIN' END AS asset_type,-- Apply the 3-Layer Pricing Engine(u.net_supplied_raw / POWER(10, COALESCE(t.decimals, 18))) * COALESCE(i.liquidity_index, 1.0) * COALESCE(o.oracle_price_usd, pr.price, 1.0) AS supplied_usd,(u.net_borrowed_raw / POWER(10, COALESCE(t.decimals, 18))) * COALESCE(i.borrow_index, 1.0) * COALESCE(o.oracle_price_usd, pr.price, 1.0) AS borrowed_usdFROM user_balances uLEFT JOIN latest_indices i ON u.token_address = i.token_addressLEFT JOIN aave_oracle_prices o ON u.token_address = o.token_addressLEFT JOIN prices.usd_latest pr ON u.token_address = pr.contract_address AND pr.blockchain = 'ethereum'LEFT JOIN tokens.erc20 t ON u.token_address = t.contract_address AND t.blockchain = 'ethereum')SELECT-- Address Truncation (Matches exactly 0xb87...a313c)CONCAT(SUBSTRING(CAST(account AS VARCHAR), 1, 5), '...', SUBSTRING(CAST(account AS VARCHAR), 39, 4)) AS "User",SUM(supplied_usd) AS "Total Supplied",