Skip to content

Commit

Permalink
Stablecoin V2: Ethereum (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebMelendez01 authored Jul 18, 2024
1 parent 9772828 commit a5d295c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 36 deletions.
64 changes: 29 additions & 35 deletions macros/stablecoins/stablecoin_balances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- take > 4 hours on an XL to run
-- Make sure to set to '' after backfill is complete

{% set backfill_date = '2021-01-01' %}
{% set backfill_date = '' %}
with
stablecoin_balances as (
select
Expand All @@ -25,33 +25,31 @@ with
where block_timestamp < to_date(sysdate())
{% if chain == 'tron' %}
and lower(address) != lower('t9yd14nj9j7xab4dbgeix9h8unkkhxuwwb') --Tron Burn Address
and stablecoin_supply > 0
{% endif %}
{% if backfill_date != '' %}
and block_timestamp < '{{ backfill_date }}'
{% endif %}
{% if is_incremental() %}
and block_timestamp >= (select dateadd('day', -1, max(date)) from {{ this }})
and block_timestamp > (select dateadd('day', -3, max(date)) from {{ this }})
{% endif %}
)
{% if is_incremental() %}
--Get the most recent data in the existing table
, stale_stablecoin_balances as (
select
date as block_timestamp
, t.contract_address
, t.symbol
, t.address
, t.stablecoin_supply
from {{this}} t
left join (
select distinct address, contract_address
from stablecoin_balances
) sb on t.address = sb.address and t.contract_address = sb.contract_address
where date >= (select dateadd('day', -1, max(date)) from {{ this }})
and sb.address is null and sb.contract_address is null
from {{ this }} t
where date = (select dateadd('day', -3, max(date)) from {{ this }})
)
{% endif %}
, heal_balance_table as (
-- stablecoin_balances and stale_stablecoin_balances do not over lap
-- stablecoin balances select every row greater than the most recent date in the table
-- stale_stablecoin_balances selects the most recent date in the table
select
block_timestamp
, contract_address
Expand All @@ -70,6 +68,26 @@ with
from stale_stablecoin_balances
{% endif %}
)
-- get the latest balance for each address for each date
, balances as (
select
block_timestamp::date as date
, contract_address
, symbol
, address
, stablecoin_supply
from (
select
block_timestamp
, contract_address
, symbol
, address
, stablecoin_supply
, row_number() over (partition by block_timestamp::date, contract_address, address, symbol order by block_timestamp desc) AS rn
from heal_balance_table
)
where rn = 1
)
, date_range as (
select
min(block_timestamp)::date as date
Expand All @@ -92,25 +110,6 @@ with
and date < dateadd(day, -1, '{{ backfill_date }}')
{% endif %}
)
, balances as (
select
block_timestamp::date as date
, contract_address
, symbol
, address
, stablecoin_supply
from (
select
block_timestamp
, contract_address
, symbol
, address
, stablecoin_supply
, row_number() over (partition by block_timestamp::date, contract_address, address, symbol order by block_timestamp desc) AS rn
from heal_balance_table
)
where rn = 1
)
, historical_supply_by_address_balances as (
select
date
Expand All @@ -127,9 +126,6 @@ with
) as stablecoin_supply
from date_range
left join balances using (date, contract_address, symbol, address)
{% if is_incremental() %}
where date >= (select dateadd('day', -1, max(date)) from {{ this }})
{% endif %}
)
, stablecoin_balances_with_price as (
select
Expand All @@ -147,9 +143,6 @@ with
left join {{ ref( "fact_coingecko_token_realtime_data") }} d
on lower(c.coingecko_id) = lower(d.token_id)
and st.date = d.date::date
{% if is_incremental() %}
where st.date >= (select dateadd('day', -1, max(date)) from {{ this }})
{% endif %}
)
select
date
Expand All @@ -161,5 +154,6 @@ select
, '{{ chain }}' as chain
, date || '-' || address || '-' || contract_address as unique_id
from stablecoin_balances_with_price
where date < to_date(sysdate())

{% endmacro %}
9 changes: 8 additions & 1 deletion macros/stablecoins/stablecoin_metrics.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
, contract_address
, symbol
, address
, stablecoin_supply
{% if chain in ('ethereum') %}
, case
when lower(address) in (select lower(premint_address) from {{ref("fact_"~chain~"_stablecoin_bridge_addresses")}}) then 0
else stablecoin_supply
end as stablecoin_supply
{% else %}
, stablecoin_supply
{% endif %}
, unique_id
from {{ ref("fact_" ~ chain ~ "_stablecoin_balances")}}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{
config(
materialized="table",
unique_key="unique_id",
database="ethereum",
schema="core",
alias="ez_stablecoin_metrics_by_address",
snowflake_warehouse="STABLECOIN_V2_LG_2",
)
}}


{{stablecoin_metrics("ethereum")}}
10 changes: 10 additions & 0 deletions models/staging/ethereum/fact_ethereum_stablecoin_balances.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized="incremental",
unique_key="unique_id",
snowflake_warehouse="STABLECOIN_V2_LG_2",
)
}}


{{stablecoin_balances("ethereum")}}
10 changes: 10 additions & 0 deletions models/staging/ethereum/fact_ethereum_stablecoin_metrics_all.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized="table",
unique_key="unique_id",
snowflake_warehouse="STABLECOIN_V2_LG_2",
)
}}


{{stablecoin_metrics_all("ethereum")}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized="table",
unique_key="unique_id",
snowflake_warehouse="STABLECOIN_V2_LG_2",
)
}}


{{stablecoin_metrics_p2p("ethereum")}}
10 changes: 10 additions & 0 deletions models/staging/ethereum/fact_ethereum_stablecoin_metrics_p2p.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized="table",
unique_key="unique_id",
snowflake_warehouse="STABLECOIN_V2_LG_2",
)
}}


{{stablecoin_metrics_p2p("ethereum")}}

0 comments on commit a5d295c

Please sign in to comment.