Skip to content

Commit

Permalink
Combine Historical Yield Models (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
akan72 authored Dec 9, 2024
1 parent e6afba7 commit f9d43c7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions models/staging/defillama/_defillama_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions models/staging/defillama/fact_defillama_yield_historical.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{ 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
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,
FROM protocol_data, lateral flatten(input => data)
2 changes: 2 additions & 0 deletions models/staging/defillama/fact_defillama_yields.sql
Original file line number Diff line number Diff line change
@@ -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") }}
Expand Down

0 comments on commit f9d43c7

Please sign in to comment.