Query editor
/* ============================================================================QUERY: DeFi Saver - Monthly Churn (Omni-Protocol Unified Model)AUTHOR: Antoine | ContangoACTIVE USER DEFINITION:A user is "Active" in a given month if they meet AT LEAST ONE of:1. Executed a manual recipe action via DefisaverLogger (caller of RecipeEvent)2. Has an active automation subscription on ANY supported protocol(Aave V2, Aave V3, Spark, Morpho, MakerDAO, Compound)CHURNED USER DEFINITION:A user is considered "Churned" in a given month if:- They were Active the previous month (met at least one criterion above)- They are NOT Active the current month (no manual action AND no activeautomation subscription anywhere on the platform)DESIGN NOTES:- Backward-Shifted (LAG): Churn is plotted on the first month a useris no longer active.- Zero-Floor Calendar: Forces empty months to render as 0, not NULL.- Subscription tracking uses a running delta model (subscribe = +1,unsubscribe = -1) to produce a cumulative subscription count per user.- Calendar starts: 2020-01-01 (earliest DeFi Saver activity on Ethereum).============================================================================ */WITH calendar AS (SELECT CAST(d AS DATE) AS monthFROM UNNEST(sequence(DATE('2020-01-01'), DATE_TRUNC('month', CURRENT_DATE), INTERVAL '1' MONTH)) AS t(d)),-- =====================================================================-- 1. TRACK INDIVIDUAL BEHAVIORS (Manual vs Automation)-- =====================================================================user_manual_activity AS (SELECTDATE_TRUNC('month', evt_block_time) AS month,caller AS user_address,1 as manual_activeFROM defisaver_multichain.defisaverlogger_evt_recipeeventWHERE chain = 'ethereum'GROUP BY 1, 2),user_sub_deltas AS (-- --- AAVE V2 ---SELECT DATE_TRUNC('month', evt_block_time) AS month, "user" AS user_address, 1 AS sub_deltaFROM defisaver_ethereum.aavesubscriptionsv2_evt_subscribedUNION ALLSELECT DATE_TRUNC('month', evt_block_time) AS month, "user" AS user_address, -1 AS sub_deltaFROM defisaver_ethereum.aavesubscriptionsv2_evt_unsubscribedUNION ALL-- --- GENERAL (Aave V3, Spark, Morpho) ----- FIX: Schema uses 'owner' instead of 'user'SELECT DATE_TRUNC('month', evt_block_time) AS month, owner AS user_address, 1 AS sub_deltaFROM defisaver_ethereum.subscriptionsv2_evt_subscribedUNION ALLSELECT DATE_TRUNC('month', evt_block_time) AS month, owner AS user_address, -1 AS sub_delta