Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pool-level rewards #154

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
) }}

with dim as (

select
p.pool_id,
p.collateral_type,
Expand Down Expand Up @@ -85,22 +86,20 @@ ffill as (
dim.ts,
dim.pool_id,
dim.collateral_type,
coalesce(
last(debt) over (
partition by dim.collateral_type, dim.pool_id
order by dim.ts
rows between unbounded preceding and current row
),
0
) as debt,
coalesce(
last(collateral_value) over (
partition by dim.collateral_type, dim.pool_id
order by dim.ts
rows between unbounded preceding and current row
),
0
) as collateral_value
coalesce(last(debt.debt) over (
partition by dim.collateral_type, dim.pool_id
order by
dim.ts
rows between unbounded preceding
and current row
), 0) as debt,
coalesce(last(collateral.collateral_value) over (
partition by dim.collateral_type, dim.pool_id
order by
dim.ts
rows between unbounded preceding
and current row
), 0) as collateral_value
from
dim
left join debt
Expand Down Expand Up @@ -131,14 +130,74 @@ hourly_pnl as (
ffill
),

pool_rewards as (
select
r.ts,
r.pool_id,
r.token_symbol,
pnl.collateral_type,
pnl.collateral_value,
sum(
pnl.collateral_value
) over (
partition by
r.ts,
r.pool_id, r.token_symbol
) as pool_collateral_value,
pnl.collateral_value / sum(
pnl.collateral_value
) over (
partition by
r.ts,
r.pool_id, r.token_symbol
) as collateral_type_share,
r.rewards_usd as pool_rewards,
-- reward share of pool rewards
r.rewards_usd
* (
pnl.collateral_value
/ sum(pnl.collateral_value)
over (partition by r.ts, r.pool_id, r.token_symbol)
) as rewards_usd
from
(
select
r.ts,
r.pool_id,
r.token_symbol,
r.rewards_usd
from
{{ ref('fct_pool_rewards_pool_hourly_base_mainnet') }} as r
) as r
inner join hourly_pnl as pnl
on
r.ts = pnl.ts
and r.pool_id = pnl.pool_id
),

hourly_rewards as (
select
ts,
pool_id,
collateral_type,
rewards_usd
sum(rewards_usd) as rewards_usd
from
{{ ref('fct_pool_rewards_hourly_base_mainnet') }}
(
select
ts,
pool_id,
collateral_type,
rewards_usd
from {{ ref('fct_pool_rewards_token_hourly_base_mainnet') }}
union all
select
ts,
pool_id,
collateral_type,
rewards_usd
from pool_rewards
) as all_rewards
group by ts, pool_id, collateral_type
),

hourly_returns as (
Expand Down Expand Up @@ -175,21 +234,27 @@ hourly_returns as (
end as hourly_pnl_pct,
case
when pnl.collateral_value = 0 then 0
else
(
coalesce(rewards.rewards_usd, 0)
+ pnl.hourly_pnl
+ coalesce(iss.hourly_issuance, 0)
else (
coalesce(
rewards.rewards_usd,
0
) + pnl.hourly_pnl + coalesce(
iss.hourly_issuance,
0
)
/ pnl.collateral_value
) / pnl.collateral_value
end as hourly_total_pct
from
hourly_pnl as pnl
left join hourly_rewards as rewards
on
pnl.ts = rewards.ts
and pnl.pool_id = rewards.pool_id
and lower(pnl.collateral_type) = lower(rewards.collateral_type)
and lower(
pnl.collateral_type
) = lower(
rewards.collateral_type
)
left join issuance as iss
on
pnl.ts = iss.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ with dim as (
cross join (
select distinct token_symbol
from
{{ ref('fct_pool_rewards_token_hourly_base_mainnet') }}
(
select token_symbol
from {{ ref('fct_pool_rewards_token_hourly_base_mainnet') }}
union all
select token_symbol
from {{ ref('fct_pool_rewards_pool_hourly_base_mainnet') }}
) as tokens
) as p
group by
t.ts,
Expand All @@ -55,6 +61,82 @@ reward_hourly_token as (
pool_id,
collateral_type,
token_symbol
),

reward_hourly_pool as (
select
dim.ts,
dim.pool_id,
dim.collateral_type,
dim.reward_token,
dim.collateral_value,
SUM(
dim.collateral_value
) over (
partition by
dim.ts,
dim.pool_id,
dim.reward_token
) as pool_collateral_value,
dim.collateral_value / SUM(
dim.collateral_value
) over (
partition by
dim.ts,
dim.pool_id,
dim.reward_token
) as collateral_type_share,
r.rewards_usd as pool_rewards,
r.rewards_usd
* (
dim.collateral_value
/ SUM(dim.collateral_value)
over (partition by dim.ts, dim.pool_id, dim.reward_token)
) as rewards_usd
from
(
select
r.ts,
r.pool_id,
r.token_symbol,
r.rewards_usd
from
{{ ref('fct_pool_rewards_pool_hourly_base_mainnet') }} as r
) as r
inner join dim
on
r.ts = dim.ts
and r.pool_id = dim.pool_id
and r.token_symbol = dim.reward_token
),


reward_hourly as (
select
ts,
pool_id,
collateral_type,
reward_token,
SUM(rewards_usd) as rewards_usd
from
(
select
ts,
pool_id,
collateral_type,
reward_token,
rewards_usd
from reward_hourly_token
union all
select
ts,
pool_id,
collateral_type,
reward_token,
rewards_usd
from reward_hourly_pool
) as all_rewards
group by ts, pool_id, collateral_type, reward_token
)

select
Expand All @@ -64,22 +146,22 @@ select
dim.collateral_value,
dim.reward_token,
COALESCE(
reward_hourly_token.rewards_usd,
reward_hourly.rewards_usd,
0
) as rewards_usd,
case
when dim.collateral_value = 0 then 0
else COALESCE(
reward_hourly_token.rewards_usd,
reward_hourly.rewards_usd,
0
) / dim.collateral_value
end as hourly_rewards_pct
from
dim
left join reward_hourly_token
left join reward_hourly
on
dim.ts = reward_hourly_token.ts
and dim.pool_id = reward_hourly_token.pool_id
dim.ts = reward_hourly.ts
and dim.pool_id = reward_hourly.pool_id
and LOWER(dim.collateral_type)
= LOWER(reward_hourly_token.collateral_type)
and dim.reward_token = reward_hourly_token.reward_token
= LOWER(reward_hourly.collateral_type)
and dim.reward_token = reward_hourly.reward_token

This file was deleted.

Loading
Loading