From 56b45eaad1956f54d2b43f293c6024b5771c030b Mon Sep 17 00:00:00 2001 From: marcus-snx Date: Fri, 7 Mar 2025 22:20:13 +0200 Subject: [PATCH 1/5] Fix seed file data types --- transformers/synthetix/dbt_project.yml | 21 +++++++++++++++++++ .../seeds/tokens/arbitrum_mainnet_tokens.csv | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) 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/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 From ccd66b8487451e2d85d9342f1a5b5fad843b16a4 Mon Sep 17 00:00:00 2001 From: marcus-snx Date: Fri, 7 Mar 2025 22:20:29 +0200 Subject: [PATCH 2/5] Add Account Underlying Yields models --- ...unt_underlying_yields_arbitrum_mainnet.sql | 139 ++++++++++++++++++ .../marts/arbitrum/mainnet/core/schema.yml | 25 ++++ ...account_underlying_yields_base_mainnet.sql | 139 ++++++++++++++++++ .../models/marts/base/mainnet/core/schema.yml | 26 ++++ ..._account_underlying_yields_eth_mainnet.sql | 139 ++++++++++++++++++ .../models/marts/eth/mainnet/core/schema.yml | 25 ++++ 6 files changed, 493 insertions(+) create mode 100644 transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql create mode 100644 transformers/synthetix/models/marts/base/mainnet/core/fct_core_account_underlying_yields_base_mainnet.sql create mode 100644 transformers/synthetix/models/marts/eth/mainnet/core/fct_core_account_underlying_yields_eth_mainnet.sql diff --git a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql new file mode 100644 index 00000000..f667cde8 --- /dev/null +++ b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql @@ -0,0 +1,139 @@ +{{ + config( + materialized='table', + indexes=[ + {'columns': ['account_id'], 'type': 'hash'} + ] + ) +}} + +with recursive yield_tokens as ( + select distinct collateral_type + from {{ ref('fct_token_yields_arbitrum_mainnet') }} +), + +delegation_changes as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + amount + - LAG(amount, 1, 0) over ( + partition by + account_id, + pool_id, + collateral_type + order by + block_timestamp + ) as change_in_amount + from + {{ ref('core_delegation_updated_arbitrum_mainnet') }} + where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) +), + +cumulative_delegation as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + SUM(change_in_amount) over ( + partition by + pool_id, + account_id, + collateral_type + order by + block_timestamp + )/1e18 as cumulative_amount_delegated + from + delegation_changes +), + +hourly_delegation as ( + select + date_trunc('hour', block_timestamp) as block_timestamp, + account_id, + pool_id, + collateral_type, + last(cumulative_amount_delegated) 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 cumulative_amount_delegated + from cumulative_delegation + 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.cumulative_amount_delegated + from hourly_delegation t + where t.account_id = hs.account_id + and t.block_timestamp <= hs.series_time + order by t.block_timestamp desc + limit 1 + ) as cumulative_amount_delegated + 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.cumulative_amount_delegated, + token_yields.hourly_exchange_rate_pnl + from last_known_values + left join {{ ref('fct_token_yields_arbitrum_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) + order by account_id, block_timestamp +) + +select + account_id, + pool_id, + collateral_type, + coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd +from final_result +group by account_id, pool_id, collateral_type +order by yield_usd desc \ No newline at end of file diff --git a/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml b/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml index 9d540ab2..195433f6 100644 --- a/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml +++ b/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml @@ -754,3 +754,28 @@ models: data_type: numeric tests: - not_null + - name: fct_core_account_underlying_yields_arbitrum_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/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..bfa2a14e --- /dev/null +++ b/transformers/synthetix/models/marts/base/mainnet/core/fct_core_account_underlying_yields_base_mainnet.sql @@ -0,0 +1,139 @@ +{{ + 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') }} +), + +delegation_changes as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + amount + - LAG(amount, 1, 0) over ( + partition by + account_id, + pool_id, + collateral_type + order by + block_timestamp + ) as change_in_amount + from + {{ ref('core_delegation_updated_base_mainnet') }} + where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) +), + +cumulative_delegation as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + SUM(change_in_amount) over ( + partition by + pool_id, + account_id, + collateral_type + order by + block_timestamp + )/1e18 as cumulative_amount_delegated + from + delegation_changes +), + +hourly_delegation as ( + select + date_trunc('hour', block_timestamp) as block_timestamp, + account_id, + pool_id, + collateral_type, + last(cumulative_amount_delegated) 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 cumulative_amount_delegated + from cumulative_delegation + 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.cumulative_amount_delegated + from hourly_delegation t + where t.account_id = hs.account_id + and t.block_timestamp <= hs.series_time + order by t.block_timestamp desc + limit 1 + ) as cumulative_amount_delegated + 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.cumulative_amount_delegated, + 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) + order by account_id, block_timestamp +) + +select + account_id, + pool_id, + collateral_type, + coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd +from final_result +group by account_id, pool_id, collateral_type +order by yield_usd desc \ No newline at end of file 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..0e3bbdfb --- /dev/null +++ b/transformers/synthetix/models/marts/eth/mainnet/core/fct_core_account_underlying_yields_eth_mainnet.sql @@ -0,0 +1,139 @@ +{{ + 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') }} +), + +delegation_changes as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + amount + - LAG(amount, 1, 0) over ( + partition by + account_id, + pool_id, + collateral_type + order by + block_timestamp + ) as change_in_amount + from + {{ ref('core_delegation_updated_eth_mainnet') }} + where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) +), + +cumulative_delegation as ( + select + block_timestamp, + account_id, + pool_id, + collateral_type, + SUM(change_in_amount) over ( + partition by + pool_id, + account_id, + collateral_type + order by + block_timestamp + )/1e18 as cumulative_amount_delegated + from + delegation_changes +), + +hourly_delegation as ( + select + date_trunc('hour', block_timestamp) as block_timestamp, + account_id, + pool_id, + collateral_type, + last(cumulative_amount_delegated) 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 cumulative_amount_delegated + from cumulative_delegation + 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.cumulative_amount_delegated + from hourly_delegation t + where t.account_id = hs.account_id + and t.block_timestamp <= hs.series_time + order by t.block_timestamp desc + limit 1 + ) as cumulative_amount_delegated + 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.cumulative_amount_delegated, + 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) + order by account_id, block_timestamp +) + +select + account_id, + pool_id, + collateral_type, + coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd +from final_result +group by account_id, pool_id, collateral_type +order by yield_usd desc \ No newline at end of file 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 From 94022fd6deb374220a949737e4ab57efb4391c3d Mon Sep 17 00:00:00 2001 From: marcus-snx Date: Fri, 7 Mar 2025 22:33:45 +0200 Subject: [PATCH 3/5] Simplify models --- ...unt_underlying_yields_arbitrum_mainnet.sql | 44 ++++--------------- ...account_underlying_yields_base_mainnet.sql | 44 ++++--------------- ..._account_underlying_yields_eth_mainnet.sql | 44 ++++--------------- 3 files changed, 27 insertions(+), 105 deletions(-) diff --git a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql index f667cde8..b8913699 100644 --- a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql +++ b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql @@ -12,56 +12,30 @@ with recursive yield_tokens as ( from {{ ref('fct_token_yields_arbitrum_mainnet') }} ), -delegation_changes as ( +delegations as ( select block_timestamp, account_id, pool_id, collateral_type, - amount - - LAG(amount, 1, 0) over ( - partition by - account_id, - pool_id, - collateral_type - order by - block_timestamp - ) as change_in_amount + {{ convert_wei('amount') }} as amount from {{ ref('core_delegation_updated_arbitrum_mainnet') }} where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) ), -cumulative_delegation as ( - select - block_timestamp, - account_id, - pool_id, - collateral_type, - SUM(change_in_amount) over ( - partition by - pool_id, - account_id, - collateral_type - order by - block_timestamp - )/1e18 as cumulative_amount_delegated - from - delegation_changes -), - hourly_delegation as ( select date_trunc('hour', block_timestamp) as block_timestamp, account_id, pool_id, collateral_type, - last(cumulative_amount_delegated) over ( + 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 cumulative_amount_delegated - from cumulative_delegation + ) as amount + from delegations order by block_timestamp asc ), @@ -103,13 +77,13 @@ last_known_values AS ( hs.collateral_type, hs.series_time, ( - select t.cumulative_amount_delegated + select t.amount from hourly_delegation t where t.account_id = hs.account_id and t.block_timestamp <= hs.series_time order by t.block_timestamp desc limit 1 - ) as cumulative_amount_delegated + ) as amount from hourly_series hs ), @@ -119,7 +93,7 @@ final_result as ( last_known_values.pool_id, last_known_values.collateral_type, last_known_values.series_time as block_timestamp, - last_known_values.cumulative_amount_delegated, + last_known_values.amount, token_yields.hourly_exchange_rate_pnl from last_known_values left join {{ ref('fct_token_yields_arbitrum_mainnet') }} as token_yields @@ -133,7 +107,7 @@ select account_id, pool_id, collateral_type, - coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd + coalesce(sum(amount * hourly_exchange_rate_pnl), 0) as yield_usd from final_result group by account_id, pool_id, collateral_type order by yield_usd desc \ No newline at end of file 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 index bfa2a14e..f10b7493 100644 --- 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 @@ -12,56 +12,30 @@ with recursive yield_tokens as ( from {{ ref('fct_token_yields_base_mainnet') }} ), -delegation_changes as ( +delegations as ( select block_timestamp, account_id, pool_id, collateral_type, - amount - - LAG(amount, 1, 0) over ( - partition by - account_id, - pool_id, - collateral_type - order by - block_timestamp - ) as change_in_amount + {{ convert_wei('amount') }} as amount from {{ ref('core_delegation_updated_base_mainnet') }} where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) ), -cumulative_delegation as ( - select - block_timestamp, - account_id, - pool_id, - collateral_type, - SUM(change_in_amount) over ( - partition by - pool_id, - account_id, - collateral_type - order by - block_timestamp - )/1e18 as cumulative_amount_delegated - from - delegation_changes -), - hourly_delegation as ( select date_trunc('hour', block_timestamp) as block_timestamp, account_id, pool_id, collateral_type, - last(cumulative_amount_delegated) over ( + 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 cumulative_amount_delegated - from cumulative_delegation + ) as amount + from delegations order by block_timestamp asc ), @@ -103,13 +77,13 @@ last_known_values AS ( hs.collateral_type, hs.series_time, ( - select t.cumulative_amount_delegated + select t.amount from hourly_delegation t where t.account_id = hs.account_id and t.block_timestamp <= hs.series_time order by t.block_timestamp desc limit 1 - ) as cumulative_amount_delegated + ) as amount from hourly_series hs ), @@ -119,7 +93,7 @@ final_result as ( last_known_values.pool_id, last_known_values.collateral_type, last_known_values.series_time as block_timestamp, - last_known_values.cumulative_amount_delegated, + 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 @@ -133,7 +107,7 @@ select account_id, pool_id, collateral_type, - coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd + coalesce(sum(amount * hourly_exchange_rate_pnl), 0) as yield_usd from final_result group by account_id, pool_id, collateral_type order by yield_usd desc \ No newline at end of file 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 index 0e3bbdfb..1c9d5131 100644 --- 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 @@ -12,56 +12,30 @@ with recursive yield_tokens as ( from {{ ref('fct_token_yields_eth_mainnet') }} ), -delegation_changes as ( +delegations as ( select block_timestamp, account_id, pool_id, collateral_type, - amount - - LAG(amount, 1, 0) over ( - partition by - account_id, - pool_id, - collateral_type - order by - block_timestamp - ) as change_in_amount + {{ convert_wei('amount') }} as amount from {{ ref('core_delegation_updated_eth_mainnet') }} where lower(collateral_type) in (select lower(collateral_type) from yield_tokens) ), -cumulative_delegation as ( - select - block_timestamp, - account_id, - pool_id, - collateral_type, - SUM(change_in_amount) over ( - partition by - pool_id, - account_id, - collateral_type - order by - block_timestamp - )/1e18 as cumulative_amount_delegated - from - delegation_changes -), - hourly_delegation as ( select date_trunc('hour', block_timestamp) as block_timestamp, account_id, pool_id, collateral_type, - last(cumulative_amount_delegated) over ( + 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 cumulative_amount_delegated - from cumulative_delegation + ) as amount + from delegations order by block_timestamp asc ), @@ -103,13 +77,13 @@ last_known_values AS ( hs.collateral_type, hs.series_time, ( - select t.cumulative_amount_delegated + select t.amount from hourly_delegation t where t.account_id = hs.account_id and t.block_timestamp <= hs.series_time order by t.block_timestamp desc limit 1 - ) as cumulative_amount_delegated + ) as amount from hourly_series hs ), @@ -119,7 +93,7 @@ final_result as ( last_known_values.pool_id, last_known_values.collateral_type, last_known_values.series_time as block_timestamp, - last_known_values.cumulative_amount_delegated, + 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 @@ -133,7 +107,7 @@ select account_id, pool_id, collateral_type, - coalesce(sum(cumulative_amount_delegated * hourly_exchange_rate_pnl), 0) as yield_usd + coalesce(sum(amount * hourly_exchange_rate_pnl), 0) as yield_usd from final_result group by account_id, pool_id, collateral_type order by yield_usd desc \ No newline at end of file From ded6bcf32c3c980640d55c28160fcdad584756b3 Mon Sep 17 00:00:00 2001 From: marcus-snx Date: Fri, 7 Mar 2025 22:46:56 +0200 Subject: [PATCH 4/5] Clean SQL --- ...re_account_underlying_yields_arbitrum_mainnet.sql | 12 +++++------- ...t_core_account_underlying_yields_base_mainnet.sql | 12 +++++------- ...ct_core_account_underlying_yields_eth_mainnet.sql | 12 +++++------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql index b8913699..772941a5 100644 --- a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql +++ b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql @@ -40,7 +40,7 @@ hourly_delegation as ( ), account_bounds AS ( - SELECT + select account_id, pool_id, collateral_type, @@ -51,7 +51,7 @@ account_bounds AS ( ), hourly_series AS ( - select + select ab.account_id, ab.pool_id, ab.collateral_type, @@ -60,7 +60,7 @@ hourly_series AS ( union all - select + select hs.account_id, hs.pool_id, hs.collateral_type, @@ -78,7 +78,7 @@ last_known_values AS ( hs.series_time, ( select t.amount - from hourly_delegation t + 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 @@ -100,14 +100,12 @@ final_result as ( 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) - order by account_id, block_timestamp ) -select +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 -order by yield_usd desc \ No newline at end of file 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 index f10b7493..856c7d8f 100644 --- 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 @@ -40,7 +40,7 @@ hourly_delegation as ( ), account_bounds AS ( - SELECT + select account_id, pool_id, collateral_type, @@ -51,7 +51,7 @@ account_bounds AS ( ), hourly_series AS ( - select + select ab.account_id, ab.pool_id, ab.collateral_type, @@ -60,7 +60,7 @@ hourly_series AS ( union all - select + select hs.account_id, hs.pool_id, hs.collateral_type, @@ -78,7 +78,7 @@ last_known_values AS ( hs.series_time, ( select t.amount - from hourly_delegation t + 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 @@ -100,14 +100,12 @@ final_result as ( 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) - order by account_id, block_timestamp ) -select +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 -order by yield_usd desc \ No newline at end of file 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 index 1c9d5131..09e18b45 100644 --- 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 @@ -40,7 +40,7 @@ hourly_delegation as ( ), account_bounds AS ( - SELECT + select account_id, pool_id, collateral_type, @@ -51,7 +51,7 @@ account_bounds AS ( ), hourly_series AS ( - select + select ab.account_id, ab.pool_id, ab.collateral_type, @@ -60,7 +60,7 @@ hourly_series AS ( union all - select + select hs.account_id, hs.pool_id, hs.collateral_type, @@ -78,7 +78,7 @@ last_known_values AS ( hs.series_time, ( select t.amount - from hourly_delegation t + 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 @@ -100,14 +100,12 @@ final_result as ( 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) - order by account_id, block_timestamp ) -select +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 -order by yield_usd desc \ No newline at end of file From e7962fb2e8c61e07149444dcb96a3e34df1976ea Mon Sep 17 00:00:00 2001 From: marcus-snx Date: Tue, 18 Mar 2025 19:37:51 +0200 Subject: [PATCH 5/5] Remove arbitrum --- ...unt_underlying_yields_arbitrum_mainnet.sql | 111 ------------------ .../marts/arbitrum/mainnet/core/schema.yml | 25 ---- 2 files changed, 136 deletions(-) delete mode 100644 transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql diff --git a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql b/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql deleted file mode 100644 index 772941a5..00000000 --- a/transformers/synthetix/models/marts/arbitrum/mainnet/core/fct_core_account_underlying_yields_arbitrum_mainnet.sql +++ /dev/null @@ -1,111 +0,0 @@ -{{ - config( - materialized='table', - indexes=[ - {'columns': ['account_id'], 'type': 'hash'} - ] - ) -}} - -with recursive yield_tokens as ( - select distinct collateral_type - from {{ ref('fct_token_yields_arbitrum_mainnet') }} -), - -delegations as ( - select - block_timestamp, - account_id, - pool_id, - collateral_type, - {{ convert_wei('amount') }} as amount - from - {{ ref('core_delegation_updated_arbitrum_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_arbitrum_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/arbitrum/mainnet/core/schema.yml b/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml index 195433f6..9d540ab2 100644 --- a/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml +++ b/transformers/synthetix/models/marts/arbitrum/mainnet/core/schema.yml @@ -754,28 +754,3 @@ models: data_type: numeric tests: - not_null - - name: fct_core_account_underlying_yields_arbitrum_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