diff --git a/transformers/synthetix/dbt_project.yml b/transformers/synthetix/dbt_project.yml index 407cd5fb..cf12a9ef 100644 --- a/transformers/synthetix/dbt_project.yml +++ b/transformers/synthetix/dbt_project.yml @@ -278,6 +278,27 @@ models: seeds: synthetix: +schema: seeds + tokens: + arbitrum_mainnet_tokens: + +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: diff --git a/transformers/synthetix/models/marts/base/mainnet/core/fct_core_account_underlying_yields_base_mainnet.sql b/transformers/synthetix/models/marts/base/mainnet/core/fct_core_account_underlying_yields_base_mainnet.sql new file mode 100644 index 00000000..856c7d8f --- /dev/null +++ b/transformers/synthetix/models/marts/base/mainnet/core/fct_core_account_underlying_yields_base_mainnet.sql @@ -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 diff --git a/transformers/synthetix/models/marts/base/mainnet/core/schema.yml b/transformers/synthetix/models/marts/base/mainnet/core/schema.yml index 39cf4531..0ae7308d 100644 --- a/transformers/synthetix/models/marts/base/mainnet/core/schema.yml +++ b/transformers/synthetix/models/marts/base/mainnet/core/schema.yml @@ -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 + diff --git a/transformers/synthetix/models/marts/eth/mainnet/core/fct_core_account_underlying_yields_eth_mainnet.sql b/transformers/synthetix/models/marts/eth/mainnet/core/fct_core_account_underlying_yields_eth_mainnet.sql new file mode 100644 index 00000000..09e18b45 --- /dev/null +++ b/transformers/synthetix/models/marts/eth/mainnet/core/fct_core_account_underlying_yields_eth_mainnet.sql @@ -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 diff --git a/transformers/synthetix/models/marts/eth/mainnet/core/schema.yml b/transformers/synthetix/models/marts/eth/mainnet/core/schema.yml index 7c37b284..07b195d3 100644 --- a/transformers/synthetix/models/marts/eth/mainnet/core/schema.yml +++ b/transformers/synthetix/models/marts/eth/mainnet/core/schema.yml @@ -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 diff --git a/transformers/synthetix/seeds/tokens/arbitrum_mainnet_tokens.csv b/transformers/synthetix/seeds/tokens/arbitrum_mainnet_tokens.csv index 65b76d3c..00db0282 100644 --- a/transformers/synthetix/seeds/tokens/arbitrum_mainnet_tokens.csv +++ b/transformers/synthetix/seeds/tokens/arbitrum_mainnet_tokens.csv @@ -1,5 +1,5 @@ token_address,token_symbol,yield_token_symbol -0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,WETH,WETH +0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,WETH 0x912CE59144191C1204E64559FE8253a0e49E6548,ARB 0xaf88d065e77c8cC2239327C5EDb3A432268e5831,USDC 0x5d3a1Ff2b6BAb83b63cd9AD0787074081a52ef34,USDe