Skip to content

Commit

Permalink
Merge branch 'main' into blast-asset
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnirmal committed Apr 17, 2024
2 parents 9c5b157 + c69ec70 commit eb5c7fc
Show file tree
Hide file tree
Showing 71 changed files with 1,746 additions and 498 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@ Temporary Items
# iCloud generated files
*.icloud

dbt_scripts/grant_permissions.sql
dbt_scripts/grant_permissions.sql

.vscode
1 change: 1 addition & 0 deletions assets.csv
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ETHEREUM_FLIPSIDE
FLOW_FLIPSIDE
GEODNET
GNOSIS_FLIPSIDE
HELIUM
INJECTIVE
LANDING_DATABASE
NEAR
Expand Down
22 changes: 22 additions & 0 deletions macros/address_balances/address_credits_quicknode.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This model currently does not take into account native tokens.
-- We do not have access to traces from quicknode yet.
-- Only use ("celo") address balances for stablecoin data
{% macro address_credits_quicknode(chain) %}
select
to_address as address,
contract_address,
block_timestamp,
cast(amount as float) as credit,
null as credit_usd,
transaction_hash as tx_hash,
null as trace_index,
event_index
from {{ ref("fact_" ~ chain ~ "_token_transfers") }}
where
to_address <> lower('0x0000000000000000000000000000000000000000')
and to_date(block_timestamp) < to_date(sysdate())
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endmacro %}
22 changes: 22 additions & 0 deletions macros/address_balances/address_debits_quicknode.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This model currently does not take into account native tokens.
-- We do not have access to traces from quicknode yet.
-- Only use ("celo") address balances for stablecoin data
{% macro address_debits_quicknode(chain) %}
select
from_address as address,
contract_address,
block_timestamp,
cast(amount * -1 as float) as debit,
null as debit_usd,
transaction_hash as tx_hash,
null as trace_index,
event_index
from {{ ref("fact_" ~ chain ~ "_token_transfers")}}
where
to_date(block_timestamp) < to_date(sysdate())
and from_address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endmacro %}
1 change: 1 addition & 0 deletions macros/agg_chain_stablecoin_breakdown.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
select dateadd({{ granularity }}, -1, max(block_timestamp))
{% if chain in ("tron") %} from tron_allium.assets.trc20_token_transfers
{% elif chain in ("solana") %} from solana_flipside.core.fact_transfers
{% elif chain in ("celo") %} from {{ref("fact_" ~ chain ~ "_decoded_events")}}
{% else %} from {{ chain }}_flipside.core.ez_decoded_event_logs
{% endif %}
)
Expand Down
24 changes: 15 additions & 9 deletions macros/agg_chain_stablecoin_transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@
-- Notably, we do NOT use the Mint / Burn events here because the
-- basic IERC20 interface does not require them to be implemented
coalesce(
decoded_log:
from
,
-- DAI on ETH Mainnet does not follow the IERC20 interface
decoded_log:src
decoded_log:from,
-- DAI on ETH Mainnet does not follow the IERC20 interface
decoded_log:src
) as from_address,
coalesce(decoded_log:to, decoded_log:dst) as to_address,
from_address = '0x0000000000000000000000000000000000000000'
Expand All @@ -104,20 +102,28 @@
case
when not is_mint and not is_burn then amount else 0
end as transfer_volume,
ez_decoded_event_logs.contract_address,
t1.contract_address,
fact_{{ chain }}_stablecoin_contracts.symbol
from {{ chain }}_flipside.core.ez_decoded_event_logs
{% if chain in ("celo")%}
from {{ref("fact_" ~ chain ~ "_decoded_events")}} t1
{% else %}
from {{ chain }}_flipside.core.ez_decoded_event_logs t1
{% endif %}
join
fact_{{ chain }}_stablecoin_contracts
on lower(ez_decoded_event_logs.contract_address)
on lower(t1.contract_address)
= lower(fact_{{ chain }}_stablecoin_contracts.contract_address)
where
lower(ez_decoded_event_logs.contract_address) in (
lower(t1.contract_address) in (
select lower(contract_address)
from fact_{{ chain }}_stablecoin_contracts
)
-- DO NOT include mint / burn events here - they will be duped
and event_name in ('Transfer', 'Issue', 'Redeem')
{% if chain in ("celo") %}
and tx_status = 1
{% else %}
and tx_status = 'SUCCESS'
{% endif %}
{% endif %}
{% endmacro %}
6 changes: 4 additions & 2 deletions macros/decode/decode_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
contract_address,
origin_from_address,
origin_to_address,
status,
status as tx_status,
t1.topic_zero,
t2.event_name,
{{ target.schema }}.decode_evm_event_log(
event_data, t2.event_info
) as decoded_event
) as decoded_log,
data
from {{ ref("fact_" ~ chain ~ "_events") }} t1
inner join {{ ref("dim_events_silver") }} t2 on t1.topic_zero = t2.topic_zero
{% if is_incremental() %}
Expand Down
24 changes: 24 additions & 0 deletions macros/decode/token_transfer_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% macro token_transfer_events(chain) %}
select
block_timestamp,
block_number,
transaction_hash,
event_index,
origin_from_address,
origin_to_address,
contract_address,
decoded_log:"from"::string as from_address,
decoded_log:"to"::string as to_address,
try_to_number(decoded_log:"value"::string) as amount,
tx_status
from {{ ref("fact_" ~ chain ~ "_decoded_events") }}
where topic_zero = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
and data != '0x'
and amount is not null
and tx_status = 1
{% if is_incremental() %}
and
block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endmacro %}
79 changes: 79 additions & 0 deletions macros/retention/get_cohort_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{% macro get_cohort_retention(chain) %}
with
min_date as (
select
date_trunc('month', min(raw_date)) as first_month
from {{ ref( "ez_" ~ chain ~ "_transactions") }}
),
base_table as(
select
date_trunc('month', raw_date) as base_month,
from_address as address
from {{ ref( "ez_" ~ chain ~ "_transactions") }}
group by 1,2
),
-- from user_cohorts onward, should be able to reuse ctes on any chain

-- grab each users first action timestamp on chain
user_cohorts as (
select
address,
min(base_month) as cohort_month
from
base_table
group by 1
),

--compute cohort size per distinct cohort period
cohort_size as (
select
cohort_month,
count(distinct(address)) as cohort_size
from
user_cohorts
group by 1
),

-- determine if/ when users came back for another interaction based on period number
following_months as (
select
bt.address,
timestampdiff(month, uc.cohort_month, bt.base_month) as month_number
from
base_table as bt
inner join user_cohorts as uc on bt.address = uc.address
where
bt.base_month > uc.cohort_month
group by
bt.address,
month_number
),
--aggregate and calcualte the retained user amount per time period per cohort
retention_data as(
select
uc.cohort_month as cohort_month,
fm.month_number,
count(distinct(fm.address)) as retained_user_count
from
following_months as fm
inner join user_cohorts as uc on fm.address = uc.address
where
cohort_month >= (select first_month from min_date) -- Grab data from the beginning of the month 24 months ago
group by
uc.cohort_month,
fm.month_number
)
select
'{{ chain }}' as chain,
r.cohort_month,
c.cohort_size,
r.month_number,
round(r.retained_user_count::numeric / c.cohort_size::numeric , 2) as retention_ratio
from
retention_data as r
inner join cohort_size as c on r.cohort_month = c.cohort_month
order by
r.cohort_month,
r.month_number

{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ where
)
and category is not null
and app is null
and gas is not null
and txns is not null
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ with
'tron'
)
and app is not null
and gas is not null
and txns is not null
),
namespace_coingecko as (
select
Expand Down
33 changes: 33 additions & 0 deletions models/data_hubs/fact_retention_datahub.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{ config(materialized="table", snowflake_warehouse="RETENTION") }}

with retention_data as (
{{
dbt_utils.union_relations(
relations=[
ref("ez_arbitrum_retention"),
ref("ez_avalanche_retention"),
ref("ez_base_retention"),
ref("ez_bsc_retention"),
ref("ez_ethereum_retention"),
ref("ez_near_retention"),
ref("ez_optimism_retention"),
ref("ez_polygon_retention"),
ref("ez_tron_retention")
],
)
}}
)
SELECT
concat(
coalesce(cast(chain as string), '_this_is_null_'),
'|',
coalesce(cast(cohort_month as string), '_this_is_null_'),
'|',
coalesce(cast(month_number as string), '_this_is_null_')
) as unique_id,
chain,
cohort_month,
cohort_size,
month_number,
retention_ratio
FROM retention_data
13 changes: 9 additions & 4 deletions models/dimensions/apps/dim_apps_silver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,36 @@ with

select
coalesce(
sigma_app_tagged.namespace, dune_namespace.namespace, flipside.namespace
sigma_app_tagged.namespace, sui.namespace, dune_namespace.namespace, flipside.namespace
) as namespace,
coalesce(
sigma_app_tagged.friendly_name,
sui.friendly_name,
initcap(replace(dune_namespace.namespace, '_', ' ')),
flipside.friendly_name
) as friendly_name,
coalesce(sigma_app_tagged.sub_category, flipside.sub_category) as sub_category,
coalesce(sigma_app_tagged.category, flipside.category) as category,
coalesce(sigma_app_tagged.sub_category, sui.sub_category, flipside.sub_category) as sub_category,
coalesce(sigma_app_tagged.category, sui.category, flipside.category) as category,
artemis_id,
coingecko_id,
ecosystem_id,
cast(defillama_protocol_id as string) as defillama_protocol_id,
parent_app,
coalesce(visibility, 1) as visibility,
symbol,
icon
coalesce(sigma_app_tagged.icon, sui.icon) as icon
from sigma_app_tagged
full join
{{ ref("dim_dune_namespaces") }} as dune_namespace
on sigma_app_tagged.namespace = dune_namespace.namespace
full join
{{ ref("dim_flipside_namespaces") }} as flipside
on sigma_app_tagged.namespace = flipside.namespace
full join
{{ ref("dim_sui_namespaces") }} as sui
on sigma_app_tagged.namespace = sui.namespace
where
sigma_app_tagged.namespace is not null
or dune_namespace.namespace is not null
or flipside.namespace is not null
or sui.namespace is not null
Loading

0 comments on commit eb5c7fc

Please sign in to comment.