diff --git a/macros/address_balances/current_address_balances.sql b/macros/address_balances/current_address_balances.sql index dab11580..684fe456 100644 --- a/macros/address_balances/current_address_balances.sql +++ b/macros/address_balances/current_address_balances.sql @@ -4,7 +4,7 @@ max(block_timestamp) as block_timestamp, address, contract_address - from prod.fact_{{ chain }}_address_balances_by_token + from {% if chain in ('ton') %} ton.prod_core.ez_ton_address_balances_by_token {% else %} prod.fact_{{ chain }}_address_balances_by_token {% endif %} {% if is_incremental() %} where block_timestamp >= dateadd('day', -7, to_date(sysdate())) {% endif %} diff --git a/macros/stablecoins/agg_chain_stablecoin_breakdown.sql b/macros/stablecoins/agg_chain_stablecoin_breakdown.sql index a7f47f99..9b3e739e 100644 --- a/macros/stablecoins/agg_chain_stablecoin_breakdown.sql +++ b/macros/stablecoins/agg_chain_stablecoin_breakdown.sql @@ -148,3 +148,138 @@ select * from results_dollar_denom {% endmacro %} + + +{% macro agg_chain_stablecoin_breakdown_ton(granularity) %} + with + transfer_transactions as ( + select * from ton.prod_raw.ez_stablecoin_transfers + where block_timestamp >= ( + select dateadd({{ granularity }}, -1, max(block_timestamp)) + from ton.prod_raw.ez_stablecoin_transfers + ) + and (not is_mint or is_mint is null) + and (not is_burn or is_burn is null) + ), + latest_balances as ( + select + address, + balances.contract_address contract_address, + greatest(max_by(balance_token, block_timestamp), 0) stablecoin_supply, + max(fact_ton_stablecoin_contracts.symbol) symbol + from ton.prod_core.ez_ton_address_balances_by_token as balances + join + fact_ton_stablecoin_contracts + on lower(balances.contract_address) + = lower(fact_ton_stablecoin_contracts.contract_address) + -- TODO if there are other chains with pre-mints, add them here. Probably + -- generalize + where + balances.address not in ( + select distinct (premint_address) + from fact_ton_stablecoin_contracts + ) + group by + address, balances.contract_address + ), + filtered_contracts as ( + select * from dim_contracts_gold where chain = 'ton' + ), + transfer_transactions_agg as ( + select + transfer_transactions.from_address::string from_address, + transfer_transactions.contract_address contract_address, + max(transfer_transactions.symbol) symbol, + sum(transfer_volume) stablecoin_transfer_volume, + sum( + case + when transfer_transactions.from_address is not null + then 1 + else 0 + end + ) as stablecoin_daily_txns, + count(distinct(to_address)) stablecoin_dau + from transfer_transactions + group by 1, 2 + order by stablecoin_transfer_volume desc + ), + results as ( + select + coalesce( + transfer_transactions_agg.from_address, latest_balances.address + ) from_address, + coalesce( + transfer_transactions_agg.contract_address, + latest_balances.contract_address + ) contract_address, + max(filtered_contracts.name) contract_name, + coalesce( + max(filtered_contracts.name), + coalesce( + transfer_transactions_agg.from_address, latest_balances.address + ) + ) as contract, + max(filtered_contracts.friendly_name) application, + max(dim_apps_gold.icon) icon, + max(filtered_contracts.app) app, + max(filtered_contracts.category) category, + coalesce( + max(transfer_transactions_agg.symbol), max(latest_balances.symbol) + ) symbol, + coalesce(sum(stablecoin_transfer_volume), 0) stablecoin_transfer_volume, + coalesce(sum(stablecoin_daily_txns), 0) as stablecoin_daily_txns, + coalesce(sum(stablecoin_dau), 0) stablecoin_dau, + coalesce(max(latest_balances.stablecoin_supply), 0) stablecoin_supply, + 'ton' as chain + from transfer_transactions_agg + full outer join + latest_balances + on lower(transfer_transactions_agg.contract_address) + = lower(latest_balances.contract_address) + and lower(transfer_transactions_agg.from_address) + = lower(latest_balances.address) + left join + filtered_contracts + on lower( + coalesce( + transfer_transactions_agg.from_address, latest_balances.address + ) + ) + = lower(filtered_contracts.address) + left join dim_apps_gold on filtered_contracts.app = dim_apps_gold.namespace + group by 1, 2 + order by stablecoin_transfer_volume desc + ), + results_dollar_denom as ( + select + from_address, + results.contract_address, + contract_name, + contract, + application, + icon, + app, + category, + results.symbol, + stablecoin_transfer_volume * coalesce( + fact_coingecko_token_realtime_data.token_current_price, 1 + ) as stablecoin_transfer_volume, + stablecoin_daily_txns, + stablecoin_dau, + stablecoin_supply * coalesce( + fact_coingecko_token_realtime_data.token_current_price, 1 + ) as stablecoin_supply, + chain + from results + join + fact_ton_stablecoin_contracts + on lower(results.contract_address) + = lower(fact_ton_stablecoin_contracts.contract_address) + left join + fact_coingecko_token_realtime_data + on lower(fact_ton_stablecoin_contracts.coingecko_id) + = lower(fact_coingecko_token_realtime_data.token_id) + ) + select * + from results_dollar_denom +{% endmacro %} diff --git a/models/metrics/stablecoins/breakdowns/daily/agg_stablecoin_breakdown_daily.sql b/models/metrics/stablecoins/breakdowns/daily/agg_stablecoin_breakdown_daily.sql index cd7dbf3c..724becd8 100644 --- a/models/metrics/stablecoins/breakdowns/daily/agg_stablecoin_breakdown_daily.sql +++ b/models/metrics/stablecoins/breakdowns/daily/agg_stablecoin_breakdown_daily.sql @@ -14,6 +14,7 @@ with ref("agg_optimism_stablecoin_breakdown_daily"), ref("agg_polygon_stablecoin_breakdown_daily"), ref("agg_solana_stablecoin_breakdown_daily"), + ref("agg_ton_stablecoin_breakdown_daily"), ref("agg_tron_stablecoin_breakdown_daily"), ] ) diff --git a/models/metrics/stablecoins/breakdowns/daily/agg_ton_stablecoin_breakdown_daily.sql b/models/metrics/stablecoins/breakdowns/daily/agg_ton_stablecoin_breakdown_daily.sql new file mode 100644 index 00000000..8dc6f5a4 --- /dev/null +++ b/models/metrics/stablecoins/breakdowns/daily/agg_ton_stablecoin_breakdown_daily.sql @@ -0,0 +1,5 @@ +-- depends_on: {{ ref('fact_ton_stablecoin_contracts') }} +-- depends_on: {{ ref('ez_ton_address_balances_by_token') }} +-- depends_on: {{ ref('ez_ton_stablecoin_transfers') }} +{{ config(materialized="table", snowflake_warehouse="TON_MD") }} +{{ agg_chain_stablecoin_breakdown_ton("day") }} diff --git a/models/metrics/stablecoins/breakdowns/monthly/agg_stablecoin_breakdown_monthly.sql b/models/metrics/stablecoins/breakdowns/monthly/agg_stablecoin_breakdown_monthly.sql index 286dba9f..52ac1dfb 100644 --- a/models/metrics/stablecoins/breakdowns/monthly/agg_stablecoin_breakdown_monthly.sql +++ b/models/metrics/stablecoins/breakdowns/monthly/agg_stablecoin_breakdown_monthly.sql @@ -14,6 +14,7 @@ with ref("agg_optimism_stablecoin_breakdown_monthly"), ref("agg_polygon_stablecoin_breakdown_monthly"), ref("agg_solana_stablecoin_breakdown_monthly"), + ref("agg_ton_stablecoin_breakdown_monthly"), ref("agg_tron_stablecoin_breakdown_monthly"), ] ) diff --git a/models/metrics/stablecoins/breakdowns/monthly/agg_ton_stablecoin_breakdown_monthly.sql b/models/metrics/stablecoins/breakdowns/monthly/agg_ton_stablecoin_breakdown_monthly.sql new file mode 100644 index 00000000..cc29e9af --- /dev/null +++ b/models/metrics/stablecoins/breakdowns/monthly/agg_ton_stablecoin_breakdown_monthly.sql @@ -0,0 +1,5 @@ +-- depends_on: {{ ref('fact_ton_stablecoin_contracts') }} +-- depends_on: {{ ref('ez_ton_address_balances_by_token') }} +-- depends_on: {{ ref('ez_ton_stablecoin_transfers') }} +{{ config(materialized="table", snowflake_warehouse="TON_MD") }} +{{ agg_chain_stablecoin_breakdown_ton("month") }} diff --git a/models/metrics/stablecoins/breakdowns/weekly/agg_stablecoin_breakdown_weekly.sql b/models/metrics/stablecoins/breakdowns/weekly/agg_stablecoin_breakdown_weekly.sql index 89a8bf70..6bdd92b6 100644 --- a/models/metrics/stablecoins/breakdowns/weekly/agg_stablecoin_breakdown_weekly.sql +++ b/models/metrics/stablecoins/breakdowns/weekly/agg_stablecoin_breakdown_weekly.sql @@ -14,6 +14,7 @@ with ref("agg_optimism_stablecoin_breakdown_weekly"), ref("agg_polygon_stablecoin_breakdown_weekly"), ref("agg_solana_stablecoin_breakdown_weekly"), + ref("agg_ton_stablecoin_breakdown_weekly"), ref("agg_tron_stablecoin_breakdown_weekly"), ] ) diff --git a/models/metrics/stablecoins/breakdowns/weekly/agg_ton_stablecoin_breakdown_weekly.sql b/models/metrics/stablecoins/breakdowns/weekly/agg_ton_stablecoin_breakdown_weekly.sql new file mode 100644 index 00000000..dde0fa9e --- /dev/null +++ b/models/metrics/stablecoins/breakdowns/weekly/agg_ton_stablecoin_breakdown_weekly.sql @@ -0,0 +1,5 @@ +-- depends_on: {{ ref('fact_ton_stablecoin_contracts') }} +-- depends_on: {{ ref('ez_ton_address_balances_by_token') }} +-- depends_on: {{ ref('ez_ton_stablecoin_transfers') }} +{{ config(materialized="table", snowflake_warehouse="TON_MD") }} +{{ agg_chain_stablecoin_breakdown_ton("week") }} diff --git a/models/metrics/stablecoins/metrics/agg_daily_stablecoin_metrics_silver.sql b/models/metrics/stablecoins/metrics/agg_daily_stablecoin_metrics_silver.sql index 4c6feaf7..6014e825 100644 --- a/models/metrics/stablecoins/metrics/agg_daily_stablecoin_metrics_silver.sql +++ b/models/metrics/stablecoins/metrics/agg_daily_stablecoin_metrics_silver.sql @@ -13,8 +13,9 @@ with ref("agg_ethereum_stablecoin_metrics"), ref("agg_optimism_stablecoin_metrics"), ref("agg_polygon_stablecoin_metrics"), - ref("agg_tron_stablecoin_metrics"), ref("agg_solana_stablecoin_metrics"), + ref("agg_ton_stablecoin_metrics"), + ref("agg_tron_stablecoin_metrics"), ] ) }} diff --git a/models/projects/ton/core/__ez_ton_core_sources__.yml b/models/projects/ton/core/__ez_ton_core_sources__.yml new file mode 100644 index 00000000..7306a805 --- /dev/null +++ b/models/projects/ton/core/__ez_ton_core_sources__.yml @@ -0,0 +1,6 @@ +sources: + - name: BALANCES + schema: PROD_CORE + database: TON + tables: + - name: ez_ton_current_balances diff --git a/models/projects/ton/core/ez_ton_address_balances_by_token.sql b/models/projects/ton/core/ez_ton_address_balances_by_token.sql new file mode 100644 index 00000000..7eb41968 --- /dev/null +++ b/models/projects/ton/core/ez_ton_address_balances_by_token.sql @@ -0,0 +1,14 @@ +-- depends_on: {{ ref("fact_ton_address_credit_by_token") }} +-- depends_on: {{ ref("fact_ton_address_debit_by_token") }} +-- depends_on: {{ source("BALANCES", "ez_ton_current_balances") }} +{{ + config( + materialized="table", + database="ton", + schema="core", + name="ez_address_balances_by_token", + snowflake_warehouse="TON_MD", + ) +}} + +{{ address_balances("ton") }} \ No newline at end of file diff --git a/models/projects/ton/core/ez_ton_current_balances.sql b/models/projects/ton/core/ez_ton_current_balances.sql new file mode 100644 index 00000000..49b3861d --- /dev/null +++ b/models/projects/ton/core/ez_ton_current_balances.sql @@ -0,0 +1,12 @@ +-- depends_on: {{ ref("ez_ton_address_balances_by_token") }} +{{ + config( + materialized="table", + database="ton", + schema="core", + name="ez_current_balances", + snowflake_warehouse="TON_MD", + ) +}} + +{{ current_balances("ton") }} \ No newline at end of file diff --git a/models/projects/ton/raw/ez_ton_stablecoin_transfers.sql b/models/projects/ton/raw/ez_ton_stablecoin_transfers.sql index f2995902..329c9814 100644 --- a/models/projects/ton/raw/ez_ton_stablecoin_transfers.sql +++ b/models/projects/ton/raw/ez_ton_stablecoin_transfers.sql @@ -48,7 +48,7 @@ select then amount / POWER(10, decimal) else 0 end as transfer_volume, - fact_ton_stablecoin_contracts.symbol, + case when fact_ton_stablecoin_contracts.symbol = 'USD₮' then 'USDT' else fact_ton_stablecoin_contracts.symbol end as symbol, fact_ton_stablecoin_contracts.contract_address from {{ ref('fact_ton_stablecoin_transfers') }} as transfers diff --git a/models/staging/ton/fact_ton_address_credit_by_token.sql b/models/staging/ton/fact_ton_address_credit_by_token.sql new file mode 100644 index 00000000..36395bbb --- /dev/null +++ b/models/staging/ton/fact_ton_address_credit_by_token.sql @@ -0,0 +1,30 @@ +{{ + config( + materialized="table", + unique_key=["block_timestamp", "address"], + snowflake_warehouse="TON_MD", + ) +}} + +with ton_transfers as ( + {{ + dbt_utils.union_relations( + relations=[ + ref("ez_ton_stablecoin_transfers"), + ] + ) + }} +) +select + to_address as address, + contract_address, + block_timestamp, + cast(amount as float) as credit, + tx_hash +from ton_transfers +where + block_timestamp < to_date(sysdate()) + {% if is_incremental() %} + and block_timestamp + >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) + {% endif %} diff --git a/models/staging/ton/fact_ton_address_debit_by_token.sql b/models/staging/ton/fact_ton_address_debit_by_token.sql new file mode 100644 index 00000000..8ef5a1a6 --- /dev/null +++ b/models/staging/ton/fact_ton_address_debit_by_token.sql @@ -0,0 +1,30 @@ +{{ + config( + materialized="table", + unique_key=["block_timestamp", "address"], + snowflake_warehouse="TON_MD", + ) +}} + +with ton_transfers as ( + {{ + dbt_utils.union_relations( + relations=[ + ref("ez_ton_stablecoin_transfers"), + ] + ) + }} +) +select + from_address as address, + contract_address, + block_timestamp, + cast(amount * -1 as float) as debit, + tx_hash +from ton_transfers +where + block_timestamp < to_date(sysdate()) + {% if is_incremental() %} + and block_timestamp + >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) + {% endif %} diff --git a/models/staging/ton/fact_ton_stablecoin_contracts.sql b/models/staging/ton/fact_ton_stablecoin_contracts.sql index e4e9e331..616e4790 100644 --- a/models/staging/ton/fact_ton_stablecoin_contracts.sql +++ b/models/staging/ton/fact_ton_stablecoin_contracts.sql @@ -4,7 +4,7 @@ from ( values ( - 'USD₮', + 'USDT', 'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs', 6, 'tether', diff --git a/models/staging/ton/fact_ton_stablecoin_transfers.sql b/models/staging/ton/fact_ton_stablecoin_transfers.sql index 3edcdd5a..5f7ffbdb 100644 --- a/models/staging/ton/fact_ton_stablecoin_transfers.sql +++ b/models/staging/ton/fact_ton_stablecoin_transfers.sql @@ -35,7 +35,12 @@ select , max_by(to_address, extraction_date) as to_address , max_by(to_type, extraction_date) as to_type , max_by(decimal, extraction_date) as decimal - , max_by(symbol, extraction_date) as symbol + , case when max_by(symbol, extraction_date) = 'USD₮' then 'USDT' else max_by(symbol, extraction_date) end as symbol from raw_data -where from_address is not null and to_address is not null and timestamp is not null +where + from_address is not null and + to_address is not null and + timestamp is not null and + -- Black list transaction: https://tonviewer.com/transaction/513bacb858a3148367643ec9da96ba9e8af8bdf02c1950b106e9a88fe3e94935 + tx_hash not in ('513bacb858a3148367643ec9da96ba9e8af8bdf02c1950b106e9a88fe3e94935') group by tx_hash