Query editor
-- Normal reward calculation assuming positions end at their endTimeWITH all_positions AS (-- Created positionsSELECT"user",stakeIndex,FLOOR((to_unixtime(evt_block_time) - 1752170400) / 86400) + 1 as start_day,FLOOR((endTime - 1752170400) / 86400) + 1 as end_day,FLOOR((endTime - to_unixtime(evt_block_time)) / 86400) as duration_days,torusAmount as principal_amount,torusAmount * POWER(FLOOR((endTime - to_unixtime(evt_block_time)) / 86400), 2) as shares,'creation' as position_typeFROM torus_ethereum.toruscreateandstake_evt_createdUNION ALL-- Staked positionsSELECT"user",stakeIndex,FLOOR((to_unixtime(evt_block_time) - 1752170400) / 86400) + 1 as start_day,FLOOR((to_unixtime(evt_block_time) - 1752170400) / 86400) + 1 + stakingDays as end_day,stakingDays as duration_days,principal as principal_amount,principal * POWER(stakingDays, 2) as shares,'staked' as position_typeFROM torus_ethereum.toruscreateandstake_evt_staked),-- Calculate total active shares per daydaily_total_shares AS (SELECTday_num as protocol_day,SUM(CASEWHEN day_num >= ap.start_day AND day_num < ap.end_day THEN ap.sharesELSE 0END) as total_active_sharesFROM UNNEST(SEQUENCE((SELECT CAST(MIN(start_day) AS BIGINT) FROM all_positions),(SELECT CAST(MAX(end_day) AS BIGINT) FROM all_positions))) AS t(day_num)CROSS JOIN all_positions apGROUP BY day_numHAVING SUM(CASEWHEN day_num >= ap.start_day AND day_num < ap.end_day THEN ap.sharesELSE 0END) > 0),-- Calculate daily reward poolsdaily_reward_pools AS (SELECTprotocol_day,100000 * 1e18 * POWER(0.9992, protocol_day - 1) as daily_reward_poolFROM daily_total_shares),