Skip to content

Commit

Permalink
Nexus Mutual - staking rewards & staked history (duneanalytics#6861)
Browse files Browse the repository at this point in the history
* refactor: staking events

* fix: amend product_id to non-nullable & trigger incremental re-run

* feat: add staking rewards and staked per pool & token

* fix: staking deposit entensions uniqueness
  • Loading branch information
tomfutago authored Oct 2, 2024
1 parent 389e9af commit bd7f463
Show file tree
Hide file tree
Showing 10 changed files with 482 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ models:
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking pools event history for Nexus Mutual on Ethereum"
description: "Staking pools event history"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
Expand All @@ -147,6 +147,9 @@ models:
- &block_time
name: block_time
description: "UTC event block time"
- &block_date
name: block_date
description: "event block date"
- *pool_address
- &token_id
name: token_id
Expand Down Expand Up @@ -182,35 +185,145 @@ models:
name: tx_hash
description: "unique transaction hash value"

- name: nexusmutual_ethereum_staking_deposit_ordered
meta:
blockchain: ethereum
project: nexusmutual
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking pools deposit ordered"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- flow_type
- block_time
- evt_index
- tx_hash
columns:
- *block_time
- *flow_type
- &stake_start_date
name: stake_start_date
- &stake_end_date
name: stake_end_date
- *pool_address
- *token_id
- *tranche_id
- *init_tranche_id
- *new_tranche_id
- *tranche_expiry_date
- *is_active
- *amount
- *topup_amount
- *user
- *evt_index
- *tx_hash
- name: deposit_rn

- name: nexusmutual_ethereum_staking_deposit_extensions
meta:
blockchain: ethereum
project: nexusmutual
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking pools deposit extensions for Nexus Mutual on Ethereum"
description: "Staking pools deposit extensions"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- pool_address
- token_id
- init_tranche_id
- current_tranche_id
- stake_start_date
- stake_end_date
columns:
- *block_time
- *block_date
- *pool_address
- *token_id
- *init_tranche_id
- name: current_tranche_id
description: "current staking tranche period (most recent one in the chain of extensions)"
- name: total_amount
- name: amount
description: "total staking amount (initial deposit + all top-up amounts)"
- *tranche_expiry_date
- *stake_start_date
- *stake_end_date
- *is_active
- name: chain_level
description: "chain (recursion) level"
- name: token_tranche_rn
description: "descending row number per pool, token & tranche"
- *evt_index
- *tx_hash

- name: nexusmutual_ethereum_staking_rewards
meta:
blockchain: ethereum
project: nexusmutual
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking rewards minted at cover buy event - to be streamed over cover period"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- pool_id
- cover_id
columns:
- *block_time
- *block_date
- *pool_id
- *product_id
- name: cover_id
- name: cover_start_date
- name: cover_end_date
- name: reward_amount_expected_total
- name: reward_amount_per_second
- name: reward_amount_per_day
- *tx_hash

- name: nexusmutual_ethereum_staked_per_pool
meta:
blockchain: ethereum
project: nexusmutual
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking rewards minted at cover buy event - to be streamed over cover period"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- pool_id
columns:
- *block_date
- *pool_id
- *pool_address
- &total_staked_nxm
name: total_staked_nxm
- name: pool_date_rn

- name: nexusmutual_ethereum_staked_per_token
meta:
blockchain: ethereum
project: nexusmutual
contributors: tomfutago
config:
tags: ["ethereum", "nexusmutual", "staking"]
description: "Staking rewards minted at cover buy event - to be streamed over cover period"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- pool_id
- token_id
columns:
- *block_date
- *pool_id
- *pool_address
- *token_id
- *total_staked_nxm
- name: stake_expiry_date
- name: token_date_rn
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,16 @@ products as (
select
cast(p.product_id as int) as product_id,
p.product_name,
pt.product_type_id,
cast(pt.product_type_id as int) as product_type_id,
pt.product_type_name as product_type
from {{ ref('nexusmutual_ethereum_product_types_v2') }} pt
inner join {{ ref('nexusmutual_ethereum_products_v2') }} p on pt.product_type_id = p.product_type_id
union all
select
-1 as product_id,
null as product_name,
-1 as product_type_id,
null as product_type
)

select
Expand Down Expand Up @@ -247,6 +253,6 @@ select
) as tx_hash_updated
from staking_pools_created_ext sp
inner join staking_pool_products_combined spc on sp.pool_id = spc.pool_id
left join products p on spc.product_id = p.product_id
inner join products p on spc.product_id = p.product_id
left join staking_pool_managers spm on sp.pool_id = spm.pool_id
left join staking_pool_fee_updates spf on sp.pool_address = spf.pool_address
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{{
config(
schema = 'nexusmutual_ethereum',
alias = 'staked_per_pool',
materialized = 'view',
unique_key = ['block_date', 'pool_id'],
post_hook = '{{ expose_spells(blockchains = \'["ethereum"]\',
spell_type = "project",
spell_name = "nexusmutual",
contributors = \'["tomfutago"]\') }}'
)
}}

with

staking_pools as (
select distinct
sp.pool_id,
sp.pool_address,
se.first_stake_event_date
from {{ ref('nexusmutual_ethereum_staking_pools') }} sp
inner join (
select
pool_address,
cast(min(block_time) as date) as first_stake_event_date
from {{ ref('nexusmutual_ethereum_staking_events') }}
group by 1
) se on sp.pool_address = se.pool_address
),

staking_pool_day_sequence as (
select
sp.pool_id,
sp.pool_address,
s.block_date
from staking_pools sp
cross join unnest (
sequence(
cast(date_trunc('day', sp.first_stake_event_date) as timestamp),
cast(date_trunc('day', now()) as timestamp),
interval '1' day
)
) as s(block_date)
),

staked_nxm_per_pool as (
select
block_date,
pool_id,
pool_address,
sum(coalesce(total_amount, 0)) as total_staked_nxm,
dense_rank() over (partition by pool_id order by block_date desc) as pool_date_rn
from (
-- deposits & deposit extensions
select
d.block_date,
d.pool_id,
d.pool_address,
sum(se.amount) as total_amount
from staking_pool_day_sequence d
left join {{ ref('nexusmutual_ethereum_staking_deposit_extensions') }} se
on d.pool_address = se.pool_address
and d.block_date >= se.stake_start_date
and d.block_date < se.stake_end_date
group by 1, 2, 3
union all
-- withdrawals & burns
select
d.block_date,
d.pool_id,
d.pool_address,
sum(se.amount) as total_amount
from staking_pool_day_sequence d
left join {{ ref('nexusmutual_ethereum_staking_events') }} se
on d.pool_address = se.pool_address
and d.block_date >= date_trunc('day', se.block_time)
and d.block_date < coalesce(se.tranche_expiry_date, current_date)
where flow_type in ('withdraw', 'stake burn')
group by 1, 2, 3
) t
group by 1, 2, 3
)

select
block_date,
pool_id,
pool_address,
total_staked_nxm,
pool_date_rn
from staked_nxm_per_pool
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{{
config(
schema = 'nexusmutual_ethereum',
alias = 'staked_per_token',
materialized = 'view',
unique_key = ['block_date', 'pool_id', 'token_id'],
post_hook = '{{ expose_spells(blockchains = \'["ethereum"]\',
spell_type = "project",
spell_name = "nexusmutual",
contributors = \'["tomfutago"]\') }}'
)
}}

with

staking_pools as (
select distinct
sp.pool_id,
sp.pool_address,
se.first_stake_event_date
from {{ ref('nexusmutual_ethereum_staking_pools') }} sp
inner join (
select
pool_address,
cast(min(block_time) as date) as first_stake_event_date
from {{ ref('nexusmutual_ethereum_staking_events') }}
group by 1
) se on sp.pool_address = se.pool_address
),

staking_pool_day_sequence as (
select
sp.pool_id,
sp.pool_address,
s.block_date
from staking_pools sp
cross join unnest (
sequence(
cast(date_trunc('day', sp.first_stake_event_date) as timestamp),
cast(date_trunc('day', now()) as timestamp),
interval '1' day
)
) as s(block_date)
),

staked_nxm_per_pool_n_token as (
select
block_date,
pool_id,
pool_address,
token_id,
sum(coalesce(total_amount, 0)) as total_staked_nxm,
max(stake_expiry_date) as stake_expiry_date,
dense_rank() over (partition by pool_id, token_id order by block_date desc) as token_date_rn
from (
-- deposits & deposit extensions
select
d.block_date,
d.pool_id,
d.pool_address,
se.token_id,
sum(se.amount) as total_amount,
max(se.stake_end_date) as stake_expiry_date
from staking_pool_day_sequence d
left join {{ ref('nexusmutual_ethereum_staking_deposit_extensions') }} se
on d.pool_address = se.pool_address
and d.block_date >= se.stake_start_date
and d.block_date < se.stake_end_date
group by 1, 2, 3, 4
union all
-- withdrawals & burns?
select
d.block_date,
d.pool_id,
d.pool_address,
se.token_id,
sum(se.amount) as total_amount,
cast(null as date) as stake_expiry_date -- no point pulling stake_expiry_date for withdrawals
from staking_pool_day_sequence d
left join {{ ref('nexusmutual_ethereum_staking_events') }} se
on d.pool_address = se.pool_address
and d.block_date >= date_trunc('day', se.block_time)
and d.block_date < coalesce(se.tranche_expiry_date, current_date)
where flow_type in ('withdraw', 'stake burn')
group by 1, 2, 3, 4
) t
group by 1, 2, 3, 4
)

select
block_date,
pool_id,
pool_address,
token_id,
total_staked_nxm,
stake_expiry_date,
token_date_rn
from staked_nxm_per_pool_n_token
Loading

0 comments on commit bd7f463

Please sign in to comment.