Skip to content
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
21 changes: 21 additions & 0 deletions transformers/synthetix/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,27 @@ models:
seeds:
synthetix:
+schema: seeds
tokens:
arbitrum_mainnet_tokens:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we drop arbitrum? or do we keep it for the vibes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, I will remove it, we will soon be deprecating arbitrum from remaining dashboards too.

+column_types:
token_address: text
token_symbol: text
yield_token_symbol: text
base_mainnet_tokens:
+column_types:
token_address: text
token_symbol: text
yield_token_symbol: text
eth_mainnet_tokens:
+column_types:
token_address: text
token_symbol: text
yield_token_symbol: text
optimism_mainnet_tokens:
+column_types:
token_address: text
token_symbol: text
yield_token_symbol: text
reward_distributors:
eth_mainnet_reward_distributors:
+column_types:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{{
config(
materialized='table',
indexes=[
{'columns': ['account_id'], 'type': 'hash'}
]
)
}}

with recursive yield_tokens as (
select distinct collateral_type
from {{ ref('fct_token_yields_base_mainnet') }}
),

delegations as (
select
block_timestamp,
account_id,
pool_id,
collateral_type,
{{ convert_wei('amount') }} as amount
from
{{ ref('core_delegation_updated_base_mainnet') }}
where lower(collateral_type) in (select lower(collateral_type) from yield_tokens)
),

hourly_delegation as (
select
date_trunc('hour', block_timestamp) as block_timestamp,
account_id,
pool_id,
collateral_type,
last(amount) over (
partition by account_id, pool_id, collateral_type, date_trunc('hour', block_timestamp)
order by block_timestamp
rows between unbounded preceding and unbounded following
) as amount
from delegations
order by block_timestamp asc
),

account_bounds AS (
select
account_id,
pool_id,
collateral_type,
min(block_timestamp) as min_time,
max(block_timestamp) as max_time
from hourly_delegation
group by account_id, pool_id, collateral_type
),

hourly_series AS (
select
ab.account_id,
ab.pool_id,
ab.collateral_type,
ab.min_time as series_time
from account_bounds ab

union all

select
hs.account_id,
hs.pool_id,
hs.collateral_type,
hs.series_time + INTERVAL '1 hour' as series_time
from hourly_series hs
join account_bounds ab on hs.account_id = ab.account_id
where hs.series_time < ab.max_time
),

last_known_values AS (
select
hs.account_id,
hs.pool_id,
hs.collateral_type,
hs.series_time,
(
select t.amount
from hourly_delegation as t
where t.account_id = hs.account_id
and t.block_timestamp <= hs.series_time
order by t.block_timestamp desc
limit 1
) as amount
from hourly_series hs
),

final_result as (
select
last_known_values.account_id,
last_known_values.pool_id,
last_known_values.collateral_type,
last_known_values.series_time as block_timestamp,
last_known_values.amount,
token_yields.hourly_exchange_rate_pnl
from last_known_values
left join {{ ref('fct_token_yields_base_mainnet') }} as token_yields
on last_known_values.series_time = token_yields.ts
and last_known_values.pool_id = token_yields.pool_id
and lower(last_known_values.collateral_type) = lower(token_yields.collateral_type)
)

select
account_id,
pool_id,
collateral_type,
coalesce(sum(amount * hourly_exchange_rate_pnl), 0) as yield_usd
from final_result
group by account_id, pool_id, collateral_type
26 changes: 26 additions & 0 deletions transformers/synthetix/models/marts/base/mainnet/core/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,29 @@ models:
data_type: numeric
tests:
- not_null
- name: fct_core_account_underlying_yields_base_mainnet
columns:
- name: account_id
description: "ID of the account"
data_type: numeric
tests:
- not_null
- name: pool_id
description: "ID of the pool"
data_type: numeric
tests:
- not_null
- name: collateral_type
description: "Type of delegated collateral"
data_type: text
tests:
- not_null
- name: yield_usd
description: "Yield value (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{{
config(
materialized='table',
indexes=[
{'columns': ['account_id'], 'type': 'hash'}
]
)
}}

with recursive yield_tokens as (
select distinct collateral_type
from {{ ref('fct_token_yields_eth_mainnet') }}
),

delegations as (
select
block_timestamp,
account_id,
pool_id,
collateral_type,
{{ convert_wei('amount') }} as amount
from
{{ ref('core_delegation_updated_eth_mainnet') }}
where lower(collateral_type) in (select lower(collateral_type) from yield_tokens)
),

hourly_delegation as (
select
date_trunc('hour', block_timestamp) as block_timestamp,
account_id,
pool_id,
collateral_type,
last(amount) over (
partition by account_id, pool_id, collateral_type, date_trunc('hour', block_timestamp)
order by block_timestamp
rows between unbounded preceding and unbounded following
) as amount
from delegations
order by block_timestamp asc
),

account_bounds AS (
select
account_id,
pool_id,
collateral_type,
min(block_timestamp) as min_time,
max(block_timestamp) as max_time
from hourly_delegation
group by account_id, pool_id, collateral_type
),

hourly_series AS (
select
ab.account_id,
ab.pool_id,
ab.collateral_type,
ab.min_time as series_time
from account_bounds ab

union all

select
hs.account_id,
hs.pool_id,
hs.collateral_type,
hs.series_time + INTERVAL '1 hour' as series_time
from hourly_series hs
join account_bounds ab on hs.account_id = ab.account_id
where hs.series_time < ab.max_time
),

last_known_values AS (
select
hs.account_id,
hs.pool_id,
hs.collateral_type,
hs.series_time,
(
select t.amount
from hourly_delegation as t
where t.account_id = hs.account_id
and t.block_timestamp <= hs.series_time
order by t.block_timestamp desc
limit 1
) as amount
from hourly_series hs
),

final_result as (
select
last_known_values.account_id,
last_known_values.pool_id,
last_known_values.collateral_type,
last_known_values.series_time as block_timestamp,
last_known_values.amount,
token_yields.hourly_exchange_rate_pnl
from last_known_values
left join {{ ref('fct_token_yields_eth_mainnet') }} as token_yields
on last_known_values.series_time = token_yields.ts
and last_known_values.pool_id = token_yields.pool_id
and lower(last_known_values.collateral_type) = lower(token_yields.collateral_type)
)

select
account_id,
pool_id,
collateral_type,
coalesce(sum(amount * hourly_exchange_rate_pnl), 0) as yield_usd
from final_result
group by account_id, pool_id, collateral_type
25 changes: 25 additions & 0 deletions transformers/synthetix/models/marts/eth/mainnet/core/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,28 @@ models:
data_type: numeric
tests:
- not_null
- name: fct_core_account_underlying_yields_eth_mainnet
columns:
- name: account_id
description: "ID of the account"
data_type: numeric
tests:
- not_null
- name: pool_id
description: "ID of the pool"
data_type: numeric
tests:
- not_null
- name: collateral_type
description: "Type of delegated collateral"
data_type: text
tests:
- not_null
- name: yield_usd
description: "Yield value (USD)"
data_type: numeric
tests:
- not_null
- dbt_utils.accepted_range:
min_value: 0
inclusive: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
token_address,token_symbol,yield_token_symbol
0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,WETH,WETH
0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,WETH
0x912CE59144191C1204E64559FE8253a0e49E6548,ARB
0xaf88d065e77c8cC2239327C5EDb3A432268e5831,USDC
0x5d3a1Ff2b6BAb83b63cd9AD0787074081a52ef34,USDe
Expand Down
Loading