Query editor
-- calc daily GMX/esGMX net qty stakedwith daily_gmx_net_qty_staked as (select date_trunc('day', block_time) as day, sum(qty) as net_qty_stakedfrom (-- get staked GMX and esGMX qtyselect evt_block_time as block_time, cast(amount as double)/1e18 as qty from gmx_arbitrum.RewardRouter_evt_StakeGmxunion allselect evt_block_time as block_time, cast(amount as double)/1e18 as qty from gmx_arbitrum.RewardRouterV2_evt_StakeGmxunion all-- get unstaked GMX and esGMX qtyselect evt_block_time as block_time, -cast(amount as double)/1e18 as qty from gmx_arbitrum.RewardRouter_evt_UnstakeGmxunion allselect evt_block_time as block_time, -cast(amount as double)/1e18 as qty from gmx_arbitrum.RewardRouterV2_evt_UnstakeGmx) group by 1),-- calc 7d and 30d moving averages for net qty staked, cumulative qty staked, anddaily_gmx_staked as (select d.day,d.net_qty_staked,avg(d.net_qty_staked) over (order by d.day rows 6 preceding) as ma7d, -- current row and 6 rows before, so total rows used for calculation is 7avg(d.net_qty_staked) over (order by d.day rows 29 preceding) as ma30d, -- current row and 29 rows before, so total rows used for calculation is 30sum(d.net_qty_staked) over (order by d.day) as cumu_qty_staked,-- d.net_qty_staked * q.price as net_dollar_staked,-- q.price,sum(d.net_qty_staked) over(order by d.day) * q.price as staked_gmx_tvlfrom daily_gmx_net_qty_staked dleft join query_1414167 q on q.day = d.dayorder by 1 nulls first),-- calc daily income for (es)GMX stakers by subtracting 1% keeper cost from protocol revenue and multiplying the result by 30%.-- the result excludes esGMX rewards.daily_gmx_income as (select day, (revenue * 0.99 * 0.3) as gmx_income_usdfrom query_2649580 -- daily revenue)-- join the two tables and calculate cumulative income, APR, avg APR, and median APRselect s.*, i.gmx_income_usd,sum(gmx_income_usd) over(order by s.day) as gmx_cumu_income,-- calc APRgmx_income_usd/staked_gmx_tvl * 365 as gmx_apr,avg(gmx_income_usd/staked_gmx_tvl * 365) over(order by s.day) as gmx_avg_apr,approx_percentile(gmx_income_usd/staked_gmx_tvl * 365, 0.5) over(order by s.day) as gmx_med_apr,-- multiply by 100 for counter displaygmx_income_usd/staked_gmx_tvl * 365 * 100 as gmx_apr_times100,avg(gmx_income_usd/staked_gmx_tvl * 365 * 100) over(order by s.day) as gmx_avg_apr_times100,approx_percentile(gmx_income_usd/staked_gmx_tvl * 365 * 100, 0.5) over(order by s.day) as gmx_med_apr_times100from daily_gmx_staked sjoin daily_gmx_income i on s.day = i.daywhere s.day > cast('2021-09-08' as timestamp) -- drop data between 2021-08-31 (inception) and 2021-09-08 cuz data in week 1 are outliers (for example, huge daily APRs that we don't get when protocol matures)