Query editor
WITH target_token AS (-- 1. Hardcode the cRECY Celo token addressSELECT0x34c11a932853ae24e845ad4b633e3cef91afe583 AS contract_address,'cRECY' AS symbol,18 AS decimals),manager_transfers AS (-- 2. Track balance inflows/outflows for cRECYSELECTdate_trunc('day', evt_block_time) AS day,CAST(value AS DOUBLE) AS amountFROM erc20_celo.evt_TransferWHERE "to" = 0x288dc841a52fca2707c6947b3a777c5e56cd87bcAND contract_address = (SELECT contract_address FROM target_token)UNION ALLSELECTdate_trunc('day', evt_block_time) AS day,-1 * CAST(value AS DOUBLE) AS amountFROM erc20_celo.evt_TransferWHERE "from" = 0x288dc841a52fca2707c6947b3a777c5e56cd87bcAND contract_address = (SELECT contract_address FROM target_token)),daily_net_flows AS (SELECT day, SUM(amount) AS daily_net_amountFROM manager_transfersGROUP BY day),pool_info AS (-- 3. Identify which pools contain cRECY and what the paired token isSELECTid AS pool_id,currency0,currency1FROM uniswap_v4_celo.PoolManager_evt_InitializeWHERE currency0 = (SELECT contract_address FROM target_token)OR currency1 = (SELECT contract_address FROM target_token)),swaps AS (-- 4. Get all Swap events for those specific poolsSELECTdate_trunc('day', s.evt_block_time) AS day,CAST(s.amount0 AS DOUBLE) AS amount0,CAST(s.amount1 AS DOUBLE) AS amount1,p.currency0,p.currency1FROM uniswap_v4_celo.PoolManager_evt_Swap sINNER JOIN pool_info p ON s.id = p.pool_id),swap_ratios AS (-- 5. Calculate the raw exchange ratio of the swap using flash accounting deltasSELECTday,CASEWHEN currency0 = (SELECT contract_address FROM target_token)THEN ABS(amount1) / NULLIF(ABS(amount0), 0)ELSE ABS(amount0) / NULLIF(ABS(amount1), 0)END AS raw_ratio,