Skip to content

Commit

Permalink
Maple Metrics (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwes authored Nov 6, 2024
1 parent d23f17f commit 403e34a
Show file tree
Hide file tree
Showing 54 changed files with 2,559 additions and 21 deletions.
3 changes: 3 additions & 0 deletions macros/addresses/hex_string_to_evm_address.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro hex_string_to_evm_address(hex_string) %}
PC_DBT_DB.PROD.HEX_STRING_TO_EVM_ADDRESS({{ hex_string }})
{% endmacro %}
3 changes: 3 additions & 0 deletions macros/solana_utils/base58_to_hex.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro base58_to_hex(base58_string) %}
PC_DBT_DB.PROD.BASE58_TO_HEX({{ base58_string }})
{% endmacro %}
3 changes: 3 additions & 0 deletions macros/solana_utils/big_endian_hex_to_decimal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro big_endian_hex_to_decimal(hex_string) %}
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL({{ hex_string }})
{% endmacro %}
3 changes: 3 additions & 0 deletions macros/solana_utils/hex_to_base58.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro hex_to_base58(hex_string) %}
PC_DBT_DB.PROD.HEX_TO_BASE58({{ hex_string }})
{% endmacro %}
109 changes: 109 additions & 0 deletions models/projects/maple/prod/ez_maple_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

{{
config(
materialized='table',
snowflake_warehouse='MAPLE',
database='MAPLE',
schema='core',
alias='ez_metrics'
)
}}



with fees as (
SELECT
date,
SUM(net_interest_usd) AS fees,
SUM(net_interest_usd) AS supply_side_fees,
SUM(platform_fees_usd) AS platform_fees,
SUM(delegate_fees_usd) AS delegate_fees
FROM {{ ref('fact_maple_fees') }}
GROUP BY 1
)
, revenues as (
SELECT
date,
SUM(revenue) AS revenue
FROM {{ ref('fact_maple_revenue') }}
GROUP BY 1
)
, token_incentives as (
SELECT
DATE(block_timestamp) AS date,
SUM(incentive_usd) AS token_incentives
FROM {{ ref('fact_maple_token_incentives') }}
GROUP BY 1
)
, tvl as (
SELECT
date,
SUM(tvl) AS tvl,
SUM(outstanding_supply) AS outstanding_supply
FROM {{ ref('fact_maple_agg_tvl') }}
GROUP BY 1
)
, treasury as (
SELECT
date,
SUM(usd_balance) AS treasury_value
FROM {{ ref('fact_maple_treasury') }}
GROUP BY 1
)
, net_treasury as (
SELECT
date,
SUM(usd_balance) AS net_treasury_value
FROM {{ ref('fact_maple_treasury') }}
WHERE token <> 'MPL'
GROUP BY 1
)
, treasury_native as (
SELECT
date,
SUM(native_balance) AS treasury_value_native
FROM {{ ref('fact_maple_treasury') }}
WHERE token = 'MPL'
GROUP BY 1
)
, price as(
{{ get_coingecko_metrics('maple')}}
)
, tokenholders as (
SELECT * FROM {{ ref('fact_maple_tokenholder_count')}}
)

SELECT
price.date,
coalesce(fees.fees, 0) as interest_fees,
coalesce(fees.platform_fees, 0) as platform_fees,
coalesce(fees.delegate_fees, 0) as delegate_fees,
coalesce(fees.fees, 0) as fees,
coalesce(interest_fees, 0) - coalesce(platform_fees, 0) - coalesce(delegate_fees, 0) as primary_supply_side_revenue,
coalesce(primary_supply_side_revenue, 0) as total_supply_side_revenue,
coalesce(revenues.revenue, 0) as revenue,
coalesce(token_incentives.token_incentives, 0) as token_incentives,
coalesce(token_incentives.token_incentives, 0) as total_expenses,
coalesce(revenue, 0) - coalesce(total_expenses, 0) as protocol_earnings,
coalesce(treasury.treasury_value, 0) as treasury_value,
coalesce(treasury_native.treasury_value_native, 0) as treasury_value_native,
coalesce(net_treasury.net_treasury_value, 0) as net_treasury_value,
coalesce(tvl.tvl, 0) as tvl,
coalesce(tvl.tvl, 0) as net_deposits,
coalesce(tvl.outstanding_supply, 0) as outstanding_supply,
coalesce(price.price, 0) as price,
coalesce(price.market_cap, 0) as market_cap,
coalesce(price.fdmc, 0) as fdmc,
price.token_turnover_circulating,
price.token_turnover_fdv,
price.token_volume,
tokenholders.token_holder_count
FROM price
LEFT JOIN fees USING(date)
LEFT JOIN revenues USING(date)
LEFT JOIN token_incentives USING(date)
LEFT JOIN tvl USING(date)
LEFT JOIN treasury USING(date)
LEFT JOIN treasury_native USING(date)
LEFT JOIN net_treasury USING(date)
LEFT JOIN tokenholders USING(date)
34 changes: 34 additions & 0 deletions models/projects/maple/prod/ez_maple_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{
config(
materialized = 'view',
snowflake_warehouse = 'MAPLE',
database = 'MAPLE',
schema = 'core',
alias = 'ez_metrics_by_chain'
)
}}

SELECT
date,
'ethereum' as chain,
interest_fees,
primary_supply_side_revenue,
total_supply_side_revenue,
revenue,
token_incentives,
total_expenses,
protocol_earnings,
treasury_value,
treasury_value_native,
net_treasury_value,
tvl,
net_deposits,
outstanding_supply,
price,
market_cap,
fdmc,
token_turnover_circulating,
token_turnover_fdv,
token_volume,
token_holder_count
FROM {{ ref('ez_maple_metrics') }}
40 changes: 40 additions & 0 deletions models/projects/maple/prod/ez_maple_metrics_by_pool.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{
config(
materialized='table',
snowflake_warehouse='MAPLE',
database='MAPLE',
schema='core',
alias='ez_metrics_by_pool'
)
}}

with fees as (
SELECT
date,
pool_name,
SUM(net_interest_usd) AS fees,
SUM(platform_fees_usd) AS platform_fees,
SUM(delegate_fees_usd) AS delegate_fees
FROM {{ ref('fact_maple_fees') }}
GROUP BY 1, 2
)
, tvl as (
SELECT
date,
pool_name,
SUM(tvl_native) AS tvl_native,
SUM(tvl) AS tvl,
SUM(outstanding_supply) AS outstanding_supply
FROM {{ ref('fact_maple_agg_tvl') }}
GROUP BY 1, 2
)
SELECT
coalesce(fees.date, tvl.date) as date,
coalesce(fees.pool_name, tvl.pool_name) as pool_name,
fees.fees,
fees.platform_fees,
fees.delegate_fees,
tvl.tvl,
tvl.outstanding_supply
FROM fees
FULL OUTER JOIN tvl ON fees.date = tvl.date AND fees.pool_name = tvl.pool_name
98 changes: 98 additions & 0 deletions models/projects/maple/prod/ez_maple_metrics_by_token.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

{{
config(
materialized='table',
snowflake_warehouse='MAPLE',
database='MAPLE',
schema='core',
alias='ez_metrics_by_token'
)
}}

with fees as (
SELECT
date,
asset as token,
SUM(net_interest_native) AS fees_native,
SUM(platform_fees_native) AS platform_fees_native,
SUM(delegate_fees_native) AS delegate_fees_native
FROM {{ ref('fact_maple_fees') }}
GROUP BY 1, 2
)
, revenues as (
SELECT
date,
token,
SUM(revenue_native) AS revenue_native
FROM {{ ref('fact_maple_revenue') }}
GROUP BY 1, 2
)
, token_incentives as (
SELECT
DATE(block_timestamp) AS date,
token,
SUM(incentive_native) AS token_incentives_native
FROM {{ ref('fact_maple_token_incentives') }}
GROUP BY 1, 2
)
, tvl as (
SELECT
date,
asset as token,
SUM(tvl_native) AS tvl_native
FROM {{ ref('fact_maple_agg_tvl') }}
GROUP BY 1, 2
)
, treasury as (
SELECT
date,
token,
SUM(native_balance) AS treasury_value_native
FROM {{ ref('fact_maple_treasury') }}
GROUP BY 1, 2
)
, treasury_native as (
SELECT
date,
token,
SUM(native_balance) AS treasury_native
FROM {{ ref('fact_maple_treasury') }}
WHERE token = 'MPL'
GROUP BY 1, 2
)
, net_treasury as (
SELECT
date,
token,
SUM(native_balance) AS net_treasury_value
FROM {{ ref('fact_maple_treasury') }}
WHERE token <> 'MPL'
GROUP BY 1, 2
)

SELECT
coalesce(fees.date, revenues.date, token_incentives.date, tvl.date, treasury.date) as date,
coalesce(fees.token, revenues.token, token_incentives.token, tvl.token, treasury.token) as token,
fees.fees_native as interest_fees_native,
fees.platform_fees_native as platform_fees_native,
fees.delegate_fees_native as delegate_fees_native,
fees.fees_native - fees.platform_fees_native - fees.delegate_fees_native as supply_side_revenue_native,
supply_side_revenue_native as total_supply_side_revenue_native,
revenues.revenue_native,
token_incentives.token_incentives_native,
token_incentives.token_incentives_native as expenses_native,
revenues.revenue_native - token_incentives.token_incentives_native as protocol_earnings_native
, tvl.tvl_native
, tvl.tvl_native as net_deposits_native
, treasury.treasury_value_native
, treasury_native.treasury_native
, net_treasury.net_treasury_value
FROM
fees
full join revenues using(date, token)
full join token_incentives using(date, token)
full join tvl using(date, token)
full join treasury using(date, token)
full join treasury_native using(date, token)
full join net_treasury using(date, token)
WHERE coalesce(fees.date, revenues.date, token_incentives.date, tvl.date, treasury.date) < to_date(sysdate())
6 changes: 3 additions & 3 deletions models/staging/jupiter/fact_jupiter_dca_fees_silver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
with hex_cte as(
SELECT
date(block_timestamp) as date,
PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data) as hex_data,
{{ base58_to_hex("f.value:data") }} as hex_data,
f.value:data as base58_data,
tx_id
FROM
Expand All @@ -28,8 +28,8 @@ with hex_cte as(
processed_ic as( -- processed hex data
SELECT
date,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,224+1,16)) as amount, -- fee amount in token units
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data, 160+1, 64)) as mint,
{{ big_endian_hex_to_decimal("SUBSTR(hex_data,224+1,16)") }} as amount, -- fee amount in token units
{{ hex_to_base58("SUBSTR(hex_data, 160+1, 64)") }} as mint,
hex_data,
tx_id
FROM hex_cte
Expand Down
Loading

0 comments on commit 403e34a

Please sign in to comment.