Query editor
/* Final aggregation by month */WITH monthly_series AS (SELECTDATE_TRUNC('month', evt_block_time) AS monthFROM woofi_staking_v2_arbitrum.WooStakingManager_evt_StakeWooOnStakingManagerWHEREDATE(evt_block_time) >= TRY_CAST('2023-05-01' AS DATE)GROUP BYDATE_TRUNC('month', evt_block_time)ORDER BYmonth), user_monthly_staking AS (SELECTuser,DATE_TRUNC('month', evt_block_time) AS month,SUM(amount / 1e18) AS monthly_stakedFROM woofi_staking_v2_arbitrum.WooStakingManager_evt_StakeWooOnStakingManagerGROUP BYuser,DATE_TRUNC('month', evt_block_time)), user_monthly_unstaking AS (SELECTuser,DATE_TRUNC('month', evt_block_time) AS month,SUM(amount / 1e18) AS monthly_unstakedFROM woofi_staking_v2_arbitrum.WooStakingManager_evt_UnstakeWooOnStakingManagerGROUP BYuser,DATE_TRUNC('month', evt_block_time)), all_users AS (SELECT DISTINCTuserFROM user_monthly_stakingUNIONSELECT DISTINCTuserFROM user_monthly_unstaking), user_monthly_balances AS (SELECTms.month,au.user,SUM(COALESCE(ums.monthly_staked, 0) - COALESCE(umu.monthly_unstaked, 0)) OVER (PARTITION BY au.user ORDER BY ms.month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_balanceFROM monthly_series AS msCROSS JOIN all_users AS auLEFT JOIN user_monthly_staking AS umsON ms.month = ums.month AND au.user = ums.userLEFT JOIN user_monthly_unstaking AS umuON ms.month = umu.month AND au.user = umu.user), compounding_events AS (SELECTuser,evt_block_time,'add' AS event_typeFROM woofi_staking_v2_arbitrum.WooStakingCompounder_evt_AddUserUNION ALLSELECTuser,evt_block_time,'remove' AS event_typeFROM woofi_staking_v2_arbitrum.WooStakingCompounder_evt_RemoveUser