From b83a2baaaaba54d81bd5261a374cc2fb8c527ff2 Mon Sep 17 00:00:00 2001 From: Alex Kan <29241719+akan72@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:50:41 -0500 Subject: [PATCH 1/3] Add DFL historical yield model, update materialization types for yield models --- .../staging/defillama/_defillama_sources.yml | 1 + .../fact_defillama_yield_historical.sql | 24 +++++++++++++++++++ .../defillama/fact_defillama_yields.sql | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 models/staging/defillama/fact_defillama_yield_historical.sql diff --git a/models/staging/defillama/_defillama_sources.yml b/models/staging/defillama/_defillama_sources.yml index 502812ac..ccd42c79 100644 --- a/models/staging/defillama/_defillama_sources.yml +++ b/models/staging/defillama/_defillama_sources.yml @@ -5,6 +5,7 @@ sources: tables: - name: raw_defillama_chain_data - name: raw_defillama_yield_data + - name: raw_defillama_yield_historical_data - name: raw_defillama_protocol_data - name: raw_defillama_protocol_tvls - name: raw_defillama_chain_tvls diff --git a/models/staging/defillama/fact_defillama_yield_historical.sql b/models/staging/defillama/fact_defillama_yield_historical.sql new file mode 100644 index 00000000..cc5041d0 --- /dev/null +++ b/models/staging/defillama/fact_defillama_yield_historical.sql @@ -0,0 +1,24 @@ +{{ config(materialized="table") }} + +WITH max_extraction AS ( + SELECT max(extraction_date) AS max_date + FROM {{ source("PROD_LANDING", "raw_defillama_yield_historical_data") }} + ), + protocol_data AS ( + SELECT + extraction_date::date as date, + parse_json(source_json):"pool" AS pool, + parse_json(source_json):"data" AS data + FROM {{ source("PROD_LANDING", "raw_defillama_yield_historical_data") }} + WHERE extraction_date = (SELECT max_date FROM max_extraction) + + ) +SELECT + date, + pool::varchar AS pool, + value:"apy"::float AS apy, + value:"apyBase"::float AS apy_base, + value:"apyBase7d"::float AS apy_base_7d, + value:"apyReward"::float AS apy_reward, + value:"timestamp"::date AS date +FROM protocol_data, lateral flatten(input => data) \ No newline at end of file diff --git a/models/staging/defillama/fact_defillama_yields.sql b/models/staging/defillama/fact_defillama_yields.sql index ed3b8eee..fd5ea7bf 100644 --- a/models/staging/defillama/fact_defillama_yields.sql +++ b/models/staging/defillama/fact_defillama_yields.sql @@ -1,3 +1,5 @@ +{{ config(materialized="table") }} + WITH max_extraction AS ( SELECT max(extraction_date) AS max_date FROM {{ source("PROD_LANDING", "raw_defillama_yield_data") }} From 642ebf867fe42df112bb0035d77954e55d08b51d Mon Sep 17 00:00:00 2001 From: Alex Kan <29241719+akan72@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:08:02 -0500 Subject: [PATCH 2/3] Assign proper date column --- models/staging/defillama/fact_defillama_yield_historical.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/staging/defillama/fact_defillama_yield_historical.sql b/models/staging/defillama/fact_defillama_yield_historical.sql index cc5041d0..3ffca1be 100644 --- a/models/staging/defillama/fact_defillama_yield_historical.sql +++ b/models/staging/defillama/fact_defillama_yield_historical.sql @@ -14,11 +14,10 @@ WITH max_extraction AS ( ) SELECT - date, + value:"timestamp"::date AS date, pool::varchar AS pool, value:"apy"::float AS apy, value:"apyBase"::float AS apy_base, value:"apyBase7d"::float AS apy_base_7d, value:"apyReward"::float AS apy_reward, - value:"timestamp"::date AS date FROM protocol_data, lateral flatten(input => data) \ No newline at end of file From bd5bfd58062838dce15d1dd9021507e000844f77 Mon Sep 17 00:00:00 2001 From: Alex Kan <29241719+akan72@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:43:52 -0500 Subject: [PATCH 3/3] Combine DFL and Artemis data --- .../fact_stablecoin_yield_historical.sql | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 models/metrics/stablecoins/yield/fact_stablecoin_yield_historical.sql diff --git a/models/metrics/stablecoins/yield/fact_stablecoin_yield_historical.sql b/models/metrics/stablecoins/yield/fact_stablecoin_yield_historical.sql new file mode 100644 index 00000000..878b2f13 --- /dev/null +++ b/models/metrics/stablecoins/yield/fact_stablecoin_yield_historical.sql @@ -0,0 +1,44 @@ +{{ config( materialized="table") }} + +WITH defillama_data AS ( + SELECT + a.date, + UPPER(b.chain) AS chain, + UPPER(b.symbol) AS stablecoin, + a.apy, + UPPER(b.project) AS project, + 'defillama' AS source, + MAX(a.date) OVER() AS max_date + FROM {{ ref("fact_defillama_yield_historical") }} a + INNER JOIN {{ ref("fact_defillama_yields") }} b + ON a.pool = b.pool + ORDER BY tvl_usd DESC +), artemis_data AS ( + SELECT + date, + 'SOLANA' AS chain, + UPPER(market) AS stablecoin, + daily_avg_deposit_rate AS apy, + 'DRIFT' AS project, + 'artemis' AS source, + MAX(date) OVER() AS max_date + FROM {{ ref("fact_drift_daily_spot_data") }} +), agg AS ( +SELECT + * +FROM defillama_data +UNION ALL +SELECT + * +FROM artemis_data +WHERE + stablecoin IN ( + SELECT + DISTINCT stablecoin + FROM defillama_data + ) +) +SELECT + * +FROM agg +WHERE date <= (SELECT MIN(max_date) FROM agg)