Query editor
-- Dolomite Borrow Rates (Daily) — ETH and WBTC on Arbitrum-- Derives daily APR from LogIndexUpdate events on DolomiteMargin-- Source: arbitrum.logs (LogIndexUpdate is NOT decoded on Dune)WITH daily_hex AS (SELECTdate_trunc('day', block_time) as ts_day,topic1,max_by(to_hex(data), block_time) as eod_hexFROM arbitrum.logsWHERE contract_address = 0x6bd780e7fdf01d77e4d475c821f1e7ae05409072AND topic0 = 0xf4626fd1187f91e6761ffb8a6ac3e8d9235a4a92da54e43feb0c57c4a4a322abAND topic1 IN (0x0000000000000000000000000000000000000000000000000000000000000000,0x0000000000000000000000000000000000000000000000000000000000000004)AND block_time >= TIMESTAMP '2023-03-01'GROUP BY 1, 2),decoded AS (SELECTts_day,CASE topic1WHEN 0x0000000000000000000000000000000000000000000000000000000000000000 THEN 'ETH'WHEN 0x0000000000000000000000000000000000000000000000000000000000000004 THEN 'WBTC'END as symbol_name,from_base(ltrim(substr(eod_hex, 1, 64), '0'), 16) / 1e18 AS borrow_index,from_base(ltrim(substr(eod_hex, 65, 64), '0'), 16) / 1e18 AS supply_indexFROM daily_hex),daily_rates AS (SELECTts_day, symbol_name, borrow_index, supply_index,(borrow_index - LAG(borrow_index) OVER (PARTITION BY symbol_name ORDER BY ts_day))/ NULLIF(LAG(borrow_index) OVER (PARTITION BY symbol_name ORDER BY ts_day), 0) AS daily_borrow_rate,(supply_index - LAG(supply_index) OVER (PARTITION BY symbol_name ORDER BY ts_day))/ NULLIF(LAG(supply_index) OVER (PARTITION BY symbol_name ORDER BY ts_day), 0) AS daily_supply_rateFROM decoded)SELECTts_day, symbol_name,COALESCE(daily_borrow_rate, 0) AS daily_borrow_rate,COALESCE(daily_borrow_rate * 365, 0) AS borrow_apr,COALESCE(daily_supply_rate, 0) AS daily_supply_rate,COALESCE(daily_supply_rate * 365, 0) AS supply_aprFROM daily_ratesWHERE ts_day >= TIMESTAMP '2023-03-11'ORDER BY symbol_name, ts_day