Query editor
WITH sUSDe AS (-- sUSDe APY calculationWITH contract AS (-- Step 1: Select metadata for the specific contract by its addressSELECT *FROM ethereum.contractsWHERE address = 0x9D39A5DE30e57443BfF2A8307A4256c8797A3497),logs AS (-- Step 2: Extract and filter logs related to the target contractSELECTl.*,-- Classify the event type (deposit or withdraw) based on `topic0`CASEWHEN topic0 = 0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7 THEN 'deposit'WHEN topic0 = 0xfbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db THEN 'withdraw'END AS event_type,-- Extract numerical values `assets` and `shares` from the log's binary datavarbinary_to_uint256(varbinary_substring(data, 1, 32)) AS assets,varbinary_to_uint256(varbinary_substring(data, 33, 32)) AS sharesFROM (-- Step 2.1: Filter logs by contract address, time range, and specific event topicsSELECT *,-- Assign a row number to pick the first log in each hourROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('hour', block_time) ORDER BY block_time) AS row_numFROM ethereum.logsWHERE contract_address = 0x9D39A5DE30e57443BfF2A8307A4256c8797A3497-- Filter logs within the given time rangeAND block_time >= TIMESTAMP '2023-11-14'AND block_time <= CURRENT_TIMESTAMP-- Include only deposit and withdraw eventsAND topic0 IN (0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7,0xfbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db)) l-- Step 2.2: Keep only the first log entry for each hourWHERE row_num = 1),logs_with_ratio AS (-- Step 3: Calculate the ratio of `assets` to `shares` for each logSELECT*,ROUND(assets * 1e10 / shares) AS ratio -- Multiply by a scaling factor for precisionFROM logs),data AS (-- Step 4: Calculate differences in ratios and timestamps between consecutive logsSELECT*,-- Get the previous ratio and block timeLAG(ratio, 1) OVER (ORDER BY block_time) AS prev_ratio,LAG(block_time, 1) OVER (ORDER BY block_time) AS prev_block_timeFROM logs_with_ratio),result AS (-- Step 5: Calculate the Annualized Percentage Rate (APR)SELECT*,-- Calculate the time difference in milliseconds