Query editor
WITH raw_flows AS (-- 1. Track all cash flows to find out what is sitting in the contractSELECT 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 ALLSELECT reserve, 0.0, CAST(amount AS DOUBLE), 0.0, 0.0FROM aave_horizon_ethereum.poolinstance_evt_withdraw WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8UNION ALLSELECT reserve, 0.0, 0.0, CAST(amount AS DOUBLE), 0.0FROM aave_horizon_ethereum.poolinstance_evt_borrow WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8UNION ALLSELECT reserve, 0.0, 0.0, 0.0, CAST(amount AS DOUBLE)FROM aave_horizon_ethereum.poolinstance_evt_repay WHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8),balances AS (-- 2. Net balances per tokenSELECTtoken_address,SUM(supply_amt) - SUM(withdraw_amt) AS net_supplied_raw,SUM(borrow_amt) - SUM(repay_amt) AS net_borrowed_rawFROM raw_flowsGROUP BY token_address),latest_rates AS (-- 3. Fetch the live Aave interest ratesSELECTreserve AS token_address,CAST(liquidityRate AS DOUBLE) / 1e25 AS supply_apr_pct,CAST(variableBorrowRate AS DOUBLE) / 1e25 AS borrow_apr_pctFROM (SELECTreserve, liquidityRate, variableBorrowRate,ROW_NUMBER() OVER(PARTITION BY reserve ORDER BY evt_block_time DESC) AS rnFROM aave_horizon_ethereum.poolinstance_evt_reservedataupdatedWHERE contract_address = 0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8)WHERE rn = 1),latest_prices AS (-- 4. Fetch standard stablecoin pricesSELECT contract_address AS token_address, priceFROM prices.usd_latestWHERE blockchain = 'ethereum')SELECTCOALESCE(t.symbol, CAST(b.token_address AS VARCHAR)) AS "Asset",ROUND(r.supply_apr_pct, 2) AS "Supply APR (%)",ROUND(r.borrow_apr_pct, 2) AS "Borrow APR (%)",ROUND(r.borrow_apr_pct - r.supply_apr_pct, 2) AS "Interest Rate Spread (%)",-- RVL Formula: (Net Supplied - Net Borrowed) = Idle tokens sitting in contractROUND(((b.net_supplied_raw - b.net_borrowed_raw) / POWER(10, COALESCE(t.decimals, 18))) * COALESCE(p.price, 1.0),2) AS "Available Liquidity (USD)"