Skip to content

Commit

Permalink
Wes/hivemapper (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwes authored Dec 18, 2024
1 parent c6d300a commit 700227b
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 0 deletions.
82 changes: 82 additions & 0 deletions macros/solana_utils/get_solana_token_mints_burns_transfers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{% macro get_solana_token_mints_burns_transfers(token_address) %}
-- mints
SELECT
m.block_timestamp,
m.tx_id,
m.block_id,
index,
inner_index,
'mint' as action,
m.mint,
m.token_account as tx_to_account,
null as tx_from_account,
m.mint_amount as amount_native,
m.mint_authority as token_authority,
fact_token_mint_actions_id as unique_id
from
solana_flipside.defi.fact_token_mint_actions m
where
1 = 1
and mint = '{{ token_address }}'
{% if is_incremental() %}
and block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% else %}
and block_timestamp >= '2022-11-01'
{% endif %}


union all

-- transfers
SELECT
tf.block_timestamp,
tf.tx_id,
tf.block_id,
SPLIT_PART(index, '.', 1) as index,
COALESCE(NULLIF(SPLIT_PART(index, '.', 2), ''), 0) as inner_index,
'transfer' as action,
tf.mint,
tf.tx_to as tx_to_account,
tf.tx_from as tx_from_account,
tf.amount as amount_native,
null as token_authority,
fact_transfers_id as unique_id
from
solana_flipside.core.fact_transfers tf
where
1 = 1
and mint = '{{ token_address }}'
{% if is_incremental() %}
and block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% else %}
and block_timestamp >= '2022-11-01'
{% endif %}

union all

-- burns
SELECT
b.block_timestamp,
b.tx_id,
b.block_id,
index,
inner_index,
'burn' as action,
b.mint,
null as tx_to_account,
b.token_account as tx_from_account,
b.burn_amount as amount_native,
b.burn_authority as token_authority,
fact_token_burn_actions_id as unique_id
from
solana_flipside.defi.fact_token_burn_actions b
where
1 = 1
and mint = '{{ token_address }}'
{% if is_incremental() %}
and block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% else %}
and block_timestamp >= '2022-11-01'
{% endif %}

{% endmacro %}
74 changes: 74 additions & 0 deletions models/projects/hivemapper/core/ez_hivemapper_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{{
config(
materialized="table",
snowflake_warehouse="HIVEMAPPER",
database="hivemapper",
schema="core",
alias="ez_metrics",
)
}}

with
date_spine as (
select * from {{ ref('dim_date_spine') }}
where date between '2022-12-20' and to_date(sysdate())
)

, bme_reward_types_cte as (
SELECT
distinct reward_type as bme_reward_types
FROM
{{ ref('fact_hivemapper_stats') }}
WHERE reward_type like 'Honey Burst%' or reward_type like 'Map Consumption%'
)
, stats as (
select
block_timestamp::date as date,
SUM(
CASE
WHEN reward_type in (SELECT bme_reward_types FROM bme_reward_types_cte) AND action = 'mint' THEN amount_usd
END
) as supply_side_fees,
SUM(
CASE
WHEN action = 'burn' and block_timestamp::date >= '2024-04-17' THEN amount_usd * 0.75 -- MIP 15 was introduced on 2024-04-17 and changed the burn fee structure such that 75% of burns are permanent (prev 0%)
END
) as revenue,
SUM(
CASE
WHEN action = 'burn' THEN amount_usd -- MIP 15 was introduced on 2024-04-17 and changed the burn fee structure such that 75% of burns are permanent (prev 0%)
END
) as fees,
SUM(
CASE WHEN action = 'mint'
THEN amount_native
END
) as mints_native,
SUM(
CASE WHEN action = 'burn'
THEN amount_native
END
) as burn_native,
COUNT( distinct
CASE WHEN action = 'mint'
THEN tx_to_account
WHEN action = 'transfer' AND reward_type in ('Bounty')
THEN tx_to_account
END
) AS contributors
from
{{ ref('fact_hivemapper_stats') }}
GROUP BY
1
)
SELECT
date_spine.date,
COALESCE(stats.fees, 0) as fees,
COALESCE(stats.supply_side_fees, 0) as primary_supply_side_revenue,
COALESCE(stats.revenue, 0) as revenue,
COALESCE(stats.mints_native, 0) as mints_native,
COALESCE(stats.burn_native, 0) as burn_native,
COALESCE(stats.contributors, 0) as dau
FROM date_spine
LEFT JOIN stats ON date_spine.date = stats.date
WHERE date_spine.date < to_date(sysdate())
20 changes: 20 additions & 0 deletions models/projects/hivemapper/core/ez_hivemapper_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{
config(
materialized="table",
snowflake_warehouse="HIVEMAPPER",
database="hivemapper",
schema="core",
alias="ez_metrics_by_chain",
)
}}

select
date,
'solana' as chain,
fees,
primary_supply_side_revenue,
revenue,
mints_native,
burn_native,
dau
from {{ ref('ez_hivemapper_metrics') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{
config(
materialized="incremental",
snowflake_warehouse="HIVEMAPPER",
unique_key=["tx_id", "action", "index", "inner_index"]
)
}}

with events as (
{{ get_solana_token_mints_burns_transfers('4vMsoUT2BWatFweudnQM1xedRLfJgJ7hswhcpz4xgBTy') }}
)

SELECT
e.block_timestamp,
e.tx_id,
e.index,
e.inner_index,
tx.log_messages,
e.action,
e.tx_to_account,
e.tx_from_account,
e.amount_native / 1e9 as amount_native
from
events e
join solana_flipside.core.fact_transactions tx on e.tx_id = tx.tx_id
where
1 = 1
{% if is_incremental() %}
and tx.block_timestamp >= (SELECT dateadd('day', -3, max(block_timestamp)) FROM {{ this }})
{% else %}
and tx.block_timestamp >= '2022-11-01'
{% endif %}
and tx.succeeded
45 changes: 45 additions & 0 deletions models/staging/hivemapper/fact_hivemapper_stats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{{
config(
materialized="incremental",
snowflake_warehouse="HIVEMAPPER",
unique_key=["tx_id", "action", "index", "inner_index"]
)
}}

select
block_timestamp,
tx_id,
index,
inner_index,
log_messages,
action,
case
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 12): "Map Coverage"%' then 'Map Coverage'
when ARRAY_TO_STRING(log_messages, ',') like 'Program log: Memo (len 20): \"Map Coverage (Fleet)\"' then 'Map Coverage (Fleet)'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 6): "Bounty"%' then 'Bounty'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 4): "Buzz"%' then 'Buzz'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 18): "Map Editing and QA"%' then 'Map Editing and QA'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 15): "Map Consumption"%' then 'Map Consumption'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 23): "Map Consumption (fleet)"%' then 'Map Consumption (fleet)'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 17): "Foundation Reward"%' then 'FTM'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 11): "Honey Burst"%' then 'Honey Burst'
when ARRAY_TO_STRING(log_messages, ',') like '%Program log: Memo (len 19): "Honey Burst (fleet)"%' then 'Honey Burst (fleet)'
else null
end as reward_type,
tx_to_account,
o1.owner as tx_to_owner,
tx_from_account,
o2.owner as tx_from_owner,
amount_native,
amount_native * p.price as amount_usd
from {{ref('fact_hivemapper_mints_burns_transfers')}}
left join solana_flipside.core.fact_token_account_owners o1 on o1.account_address = tx_to_account
left join solana_flipside.core.fact_token_account_owners o2 on o2.account_address = tx_from_account
left join solana_flipside.price.ez_prices_hourly p on p.hour = date_trunc('hour', block_timestamp) and p.token_address = '4vMsoUT2BWatFweudnQM1xedRLfJgJ7hswhcpz4xgBTy'
where 1=1
and (
not(action = 'transfer' AND reward_type is null)
)
{% if is_incremental() %}
and block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

0 comments on commit 700227b

Please sign in to comment.