diff --git a/models/projects/drift/core/ez_drift_metrics.sql b/models/projects/drift/core/ez_drift_metrics.sql index bf3acc20..784eb3a7 100644 --- a/models/projects/drift/core/ez_drift_metrics.sql +++ b/models/projects/drift/core/ez_drift_metrics.sql @@ -28,7 +28,8 @@ SELECT fact_drift_prediction_markets.date, fact_drift_float_borrow_lending_revenue.date, defillama_data.date, - parsed_log_metrics.date + parsed_log_metrics.date, + fact_drift_amm_revenue.date ) as date, 'drift' AS app, 'DeFi' AS category, @@ -45,12 +46,24 @@ SELECT parsed_log_metrics.spot_fees, parsed_log_metrics.spot_revenue, parsed_log_metrics.spot_trading_volume, + total_revenue as excess_pnl_daily_change, + coalesce(float_revenue, 0) + + coalesce(lending_revenue, 0) + + coalesce(parsed_log_metrics.perp_revenue, 0) + + coalesce(parsed_log_metrics.spot_revenue, 0) as old_revenue, coalesce(float_revenue, 0) + coalesce(lending_revenue, 0) + coalesce(parsed_log_metrics.perp_revenue, 0) + coalesce(parsed_log_metrics.spot_revenue, 0) as revenue, - coalesce(parsed_log_metrics.perp_fees + parsed_log_metrics.spot_fees, 0) as fees + total_revenue - (coalesce(float_revenue, 0) + + coalesce(lending_revenue, 0) + + coalesce(parsed_log_metrics.perp_revenue, 0) + + coalesce(parsed_log_metrics.spot_revenue, 0)) as amm_revenue, + coalesce(parsed_log_metrics.perp_fees + parsed_log_metrics.spot_fees, 0) as fees, + latest_excess_pnl as daily_latest_excess_pnl FROM price_data +LEFT JOIN {{ ref("fact_drift_amm_revenue") }} as fact_drift_amm_revenue + ON price_data.date = fact_drift_amm_revenue.date FULL JOIN {{ ref("fact_drift_prediction_markets") }} as fact_drift_prediction_markets ON price_data.date = fact_drift_prediction_markets.date FULL JOIN {{ ref("fact_drift_float_borrow_lending_revenue") }} as fact_drift_float_borrow_lending_revenue diff --git a/models/staging/drift/fact_drift_amm_revenue.sql b/models/staging/drift/fact_drift_amm_revenue.sql new file mode 100644 index 00000000..b6c235ff --- /dev/null +++ b/models/staging/drift/fact_drift_amm_revenue.sql @@ -0,0 +1,16 @@ +{{ config(materialized="table") }} + +WITH fact_drift_daily_perp_data AS ( + SELECT + date + , sum(latest_excess_pnl) as latest_excess_pnl + FROM + {{ref('fact_drift_daily_perp_data')}} + GROUP BY 1 +) +SELECT + date + , latest_excess_pnl - LAG(latest_excess_pnl) OVER (ORDER BY date) as total_revenue + , latest_excess_pnl +FROM + fact_drift_daily_perp_data diff --git a/models/staging/drift/fact_drift_daily_perp_data.sql b/models/staging/drift/fact_drift_daily_perp_data.sql index 32160d58..647c904c 100644 --- a/models/staging/drift/fact_drift_daily_perp_data.sql +++ b/models/staging/drift/fact_drift_daily_perp_data.sql @@ -10,7 +10,7 @@ SELECT AVG(value:oi::float) as daily_avg_open_interest, AVG(value:oi_net::float) as daily_avg_net_open_interest, AVG(value:net_user_pnl::float) as daily_avg_net_user_pnl, - AVG(value:excess_pnl::float) as daily_avg_excess_pnl, + MAX_BY(value:excess_pnl::float, TO_TIMESTAMP(value:timestamp::string, 'YYYY/MM/DD HH24:MI:SS')) as latest_excess_pnl, AVG(value:est_funding_dollars::float) as daily_avg_est_funding_dollars FROM {{ source("PROD_LANDING", "raw_drift_perp_market_data") }} , lateral flatten(input => parse_json(source_json))