Skip to content

Commit

Permalink
RWA Pipeline (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwes authored Nov 26, 2024
1 parent 37ebeb3 commit 7c2b4b3
Show file tree
Hide file tree
Showing 62 changed files with 870 additions and 0 deletions.
16 changes: 16 additions & 0 deletions macros/rwa/ez_rwa_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% macro ez_rwa_by_chain(issuer_id) %}

select
tvl.date,
tvl.chain,
tvl.symbol,
tvl.issuer_friendly_name,
sum(tvl.tokenized_supply_change) as tokenized_supply_change,
sum(tvl.tokenized_mcap_change) as tokenized_mcap_change,
sum(tvl.tokenized_supply) as tokenized_supply,
sum(tvl.tokenized_mcap) as tokenized_mcap,
from {{ ref('fact_' ~ issuer_id ~ '_tvl_by_chain') }} tvl
where date < to_date(sysdate())
group by 1, 2, 3, 4

{% endmacro %}
14 changes: 14 additions & 0 deletions macros/rwa/ez_rwa_by_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% macro ez_rwa_by_product(issuer_id) %}

select
tvl.date,
tvl.issuer_friendly_name,
tvl.symbol,
sum(tvl.tokenized_supply_change) as tokenized_supply_change,
sum(tvl.tokenized_mcap_change) as tokenized_mcap_change,
sum(tvl.tokenized_supply) as tokenized_supply,
sum(tvl.tokenized_mcap) as tokenized_mcap,
from {{ ref('fact_' ~ issuer_id ~ '_tvl_by_product') }} tvl
where date < to_date(sysdate())
group by 1, 2, 3
{% endmacro %}
173 changes: 173 additions & 0 deletions macros/rwa/rwa_balances.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{% macro rwa_balances(chain) %}
-- This macro takes our balances table and forward fills the values for each address for each rwa

{% set new_rwa_address = '' %}

with
rwa_balances as (
select
block_timestamp
, t1.contract_address
, symbol
, address
{% if chain in ('solana') %}
, amount as rwa_supply_native
{% else %}
, balance_token / pow(10, num_decimals) as rwa_supply_native
{% endif %}
from {{ ref("fact_" ~ chain ~ "_address_balances_by_token")}} t1
inner join {{ ref("fact_" ~ chain ~ "_rwa_addresses")}} t2
on lower(t1.contract_address) = lower(t2.contract_address)
where block_timestamp < to_date(sysdate())
{% if new_rwa_address != '' %}
and lower(t1.contract_address) = lower('{{ new_rwa_address }}')
{% endif %}
{% if is_incremental() and new_rwa_address == '' %}
and block_timestamp > (select dateadd('day', -3, max(date)) from {{ this }})
{% endif %}
)
{% if is_incremental() and new_rwa_address == '' %}
--Get the most recent data in the existing table
, stale_rwa_balances as (
select
date as block_timestamp
, t.contract_address
, t.symbol
, t.address
, t.rwa_supply_native
from {{ this }} t
where date = (select dateadd('day', -3, max(date)) from {{ this }})
)
{% endif %}
, heal_balance_table as (
-- rwa_balances and stale_rwa_balances do not over lap
-- rwa_balances select every row greater than the most recent date in the table
-- stale_rwa_balances selects the most recent date in the table
select
block_timestamp
, contract_address
, symbol
, address
, rwa_supply_native
from rwa_balances
{% if is_incremental() and new_rwa_address == '' %}
union
select
block_timestamp
, contract_address
, symbol
, address
, rwa_supply_native
from stale_rwa_balances
{% endif %}
)
-- get the latest balance for each address for each date
, balances as (
select
block_timestamp::date as date
, contract_address
, symbol
, address
, rwa_supply_native
from (
select
block_timestamp
, contract_address
, symbol
, address
, rwa_supply_native
, 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
, contract_address
, symbol
, address
from heal_balance_table
group by contract_address, address, symbol

union all

select
dateadd(day, 1, date) as date
, contract_address
, symbol
, address
from date_range
where date < dateadd(day, -1, to_date(sysdate()))
)
, historical_supply_by_address_balances as (
select
date
, address
, contract_address
, symbol
, coalesce(
rwa_supply_native,
LAST_VALUE(balances.rwa_supply_native ignore nulls) over (
partition by contract_address, address, symbol
order by date
rows between unbounded preceding and current row
)
) as rwa_supply_native
from date_range
left join balances using (date, contract_address, symbol, address)
)
, rwa_balances_with_price as (
select
st.date
, address
, st.contract_address
, st.symbol
, coalesce( d.shifted_token_price_usd,
case
when c.coingecko_id in ('blackrock-usd-institutional-digital-liquidity-fund', 'franklin-onchain-u-s-government-money-fund')
then 1
when c.coingecko_id = 'hashnote-usyc'
then coalesce(h.rate, 1)
when c.coingecko_id = 'ousg'
then o.price
end
) as price
, rwa_supply_native
, rwa_supply_native *
coalesce( d.shifted_token_price_usd,
case
when c.coingecko_id in ('blackrock-usd-institutional-digital-liquidity-fund', 'franklin-onchain-u-s-government-money-fund')
then 1
when c.coingecko_id = 'hashnote-usyc'
then coalesce(h.rate, 1)
when c.coingecko_id = 'ousg'
then o.price
end
) as rwa_supply_usd
from historical_supply_by_address_balances st
left join {{ ref( "fact_" ~ chain ~ "_rwa_addresses") }} c
on lower(st.contract_address) = lower(c.contract_address)
left join {{ ref( "fact_coingecko_token_date_adjusted_gold") }} d
on lower(c.coingecko_id) = lower(d.coingecko_id)
and st.date = d.date::date
left join {{ ref( "fact_hashnote_usyc_rate") }} h
on st.date = h.date
and st.symbol = 'USYC'
left join {{ ref( "fact_ousg_prices") }} o
on st.date = o.date
and st.symbol = 'OUSG'
)
select
date
, address
, contract_address
, symbol
, price
, rwa_supply_native
, rwa_supply_usd
, '{{ chain }}' as chain
, date || '-' || address || '-' || contract_address as unique_id
from rwa_balances_with_price
where date < to_date(sysdate())
{% endmacro %}
15 changes: 15 additions & 0 deletions macros/rwa/rwa_data_by_chain_for_issuer.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro rwa_data_by_chain_for_issuer(issuer_id) %}
select
date
, chain
, symbol
, issuer_friendly_name
, avg(price) as price
, sum(rwa_supply_usd) as tokenized_mcap
, sum(net_rwa_supply_usd_change) as tokenized_mcap_change
, sum(rwa_supply_native) as tokenized_supply
, sum(net_rwa_supply_native_change) as tokenized_supply_change
from {{ ref( "agg_rwa_by_product_and_chain") }}
where issuer_id = '{{ issuer_id }}'
group by 1, 2, 3, 4
{% endmacro %}
15 changes: 15 additions & 0 deletions macros/rwa/rwa_data_by_product_for_issuer.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro rwa_data_by_product_for_issuer(issuer_id) %}
select
date
, chain
, symbol
, issuer_friendly_name
, avg(price) as price
, sum(rwa_supply_usd) as tokenized_mcap
, sum(net_rwa_supply_usd_change) as tokenized_mcap_change
, sum(rwa_supply_native) as tokenized_supply
, sum(net_rwa_supply_native_change) as tokenized_supply_change
from {{ ref( "agg_rwa_by_product_and_chain") }}
where issuer_id = '{{ issuer_id }}'
group by 1, 2, 3, 4
{% endmacro %}
24 changes: 24 additions & 0 deletions macros/rwa/rwa_supply_by_date_and_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% macro rwa_supply_by_date_and_chain(chain) %}
with rwa_balances as (
select
date
, chain
, symbol
, avg(price) as price
, sum(rwa_supply_native) as rwa_supply_native
, sum(rwa_supply_usd) as rwa_supply_usd
from {{ ref("fact_" ~ chain ~ "_rwa_balances") }}
where rwa_supply_native >= 1e-9
group by 1, 2, 3
)
select
date
, chain
, symbol
, price
, rwa_supply_native - lag(rwa_supply_native) over (partition by symbol order by date) as net_rwa_supply_native_change
, rwa_supply_usd - lag(rwa_supply_usd) over (partition by symbol order by date) as net_rwa_supply_usd_change
, rwa_supply_native
, rwa_supply_usd
from rwa_balances
{% endmacro %}
11 changes: 11 additions & 0 deletions models/projects/blackrock/core/ez_blackrock_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'blackrock',
schema = 'core',
snowflake_warehouse = 'BLACKROCK',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('blackrock') }}
11 changes: 11 additions & 0 deletions models/projects/blackrock/core/ez_blackrock_metrics_by_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'blackrock',
schema = 'core',
snowflake_warehouse = 'BLACKROCK',
alias = 'ez_metrics_by_product'
)
}}

{{ ez_rwa_by_product('blackrock') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'franklin_templeton',
schema = 'core',
snowflake_warehouse = 'FRANKLIN_TEMPLETON',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('franklin_templeton') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'franklin_templeton',
schema = 'core',
snowflake_warehouse = 'FRANKLIN_TEMPLETON',
alias = 'ez_metrics_by_product'
)
}}

{{ ez_rwa_by_product('franklin_templeton') }}
11 changes: 11 additions & 0 deletions models/projects/hashnote/core/ez_hashnote_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'hashnote',
schema = 'core',
snowflake_warehouse = 'HASHNOTE',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('hashnote') }}
11 changes: 11 additions & 0 deletions models/projects/hashnote/core/ez_hashnote_metrics_by_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'hashnote',
schema = 'core',
snowflake_warehouse = 'HASHNOTE',
alias = 'ez_metrics_by_product'
)
}}

{{ ez_rwa_by_product('hashnote') }}
11 changes: 11 additions & 0 deletions models/projects/ondo/core/ez_ondo_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'ondo',
schema = 'core',
snowflake_warehouse = 'ONDO',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('ondo') }}
11 changes: 11 additions & 0 deletions models/projects/ondo/core/ez_ondo_metrics_by_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'ondo',
schema = 'core',
snowflake_warehouse = 'ONDO',
alias = 'ez_metrics_by_product'
)
}}

{{ ez_rwa_by_product('ondo') }}
11 changes: 11 additions & 0 deletions models/projects/openeden/ez_openeden_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'openeden',
schema = 'core',
snowflake_warehouse = 'OPENEDEN',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('openeden') }}
11 changes: 11 additions & 0 deletions models/projects/openeden/ez_openeden_metrics_by_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'openeden',
schema = 'core',
snowflake_warehouse = 'OPENEDEN',
alias = 'ez_metrics_by_product'
)
}}

{{ ez_rwa_by_product('openeden') }}
11 changes: 11 additions & 0 deletions models/projects/paxos/core/ez_paxos_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{
config(
materialized = 'table',
database = 'paxos',
schema = 'core',
snowflake_warehouse = 'PAXOS',
alias = 'ez_metrics_by_chain'
)
}}

{{ ez_rwa_by_chain('paxos') }}
Loading

0 comments on commit 7c2b4b3

Please sign in to comment.