Skip to content

Commit

Permalink
Ton stablecoin models (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny77D authored Jul 2, 2024
1 parent da53e89 commit 6d534ec
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 6 deletions.
2 changes: 1 addition & 1 deletion macros/address_balances/current_address_balances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
135 changes: 135 additions & 0 deletions macros/stablecoins/agg_chain_stablecoin_breakdown.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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") }}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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") }}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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") }}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
)
}}
Expand Down
6 changes: 6 additions & 0 deletions models/projects/ton/core/__ez_ton_core_sources__.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sources:
- name: BALANCES
schema: PROD_CORE
database: TON
tables:
- name: ez_ton_current_balances
14 changes: 14 additions & 0 deletions models/projects/ton/core/ez_ton_address_balances_by_token.sql
Original file line number Diff line number Diff line change
@@ -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") }}
12 changes: 12 additions & 0 deletions models/projects/ton/core/ez_ton_current_balances.sql
Original file line number Diff line number Diff line change
@@ -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") }}
2 changes: 1 addition & 1 deletion models/projects/ton/raw/ez_ton_stablecoin_transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions models/staging/ton/fact_ton_address_credit_by_token.sql
Original file line number Diff line number Diff line change
@@ -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 %}
30 changes: 30 additions & 0 deletions models/staging/ton/fact_ton_address_debit_by_token.sql
Original file line number Diff line number Diff line change
@@ -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 %}
2 changes: 1 addition & 1 deletion models/staging/ton/fact_ton_stablecoin_contracts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from
(
values
(
'USD₮',
'USDT',
'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs',
6,
'tether',
Expand Down
9 changes: 7 additions & 2 deletions models/staging/ton/fact_ton_stablecoin_transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6d534ec

Please sign in to comment.