Skip to content

Commit d2246ce

Browse files
authored
Revert "lending markets - forward fill on hourly models (duneanalytics#8176)" (duneanalytics#8203)
This reverts commit 98441d9.
1 parent f3a5fc3 commit d2246ce

File tree

1 file changed

+19
-99
lines changed

1 file changed

+19
-99
lines changed

dbt_subprojects/hourly_spellbook/macros/sector/lending/lending_aave_compatible_market.sql

Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
macro lending_aave_v3_compatible_market(
33
blockchain,
44
project = 'aave',
5-
version = '3',
5+
version = 'v3',
66
project_decoded_as = 'aave_v3',
77
decoded_contract_name = 'Pool'
88
)
@@ -61,117 +61,37 @@ from reserve_data
6161
macro lending_aave_v3_compatible_market_hourly_agg(
6262
blockchain,
6363
project = 'aave',
64-
version = '3'
64+
version = 'v3'
6565
)
6666
%}
6767

6868
with
6969

70-
reserve_data_base as (
70+
reserve_data as (
7171
select *
7272
from {{ ref('lending_' ~ blockchain ~ '_base_market') }}
7373
where blockchain = '{{ blockchain }}'
7474
and project = '{{ project }}'
7575
and version = '{{ version }}'
76-
),
77-
78-
reserve_data_hourly_agg as (
79-
select
80-
blockchain,
81-
project,
82-
version,
83-
block_month,
84-
block_hour,
85-
token_address,
86-
symbol,
87-
max_by(liquidity_index, block_hour) as liquidity_index,
88-
max_by(variable_borrow_index, block_hour) as variable_borrow_index,
89-
avg(cast(deposit_rate as double)) / 1e27 as deposit_rate,
90-
avg(cast(stable_borrow_rate as double)) / 1e27 as stable_borrow_rate,
91-
avg(cast(variable_borrow_rate as double)) / 1e27 as variable_borrow_rate
92-
from reserve_data_base
93-
group by 1,2,3,4,5,6,7
94-
),
95-
96-
reserve_data_hourly_changes as (
97-
select
98-
*,
99-
lead(block_hour) over (partition by token_address order by block_hour) as next_update_block_hour
100-
from (
101-
-- straight up incremental
102-
select * from reserve_data_hourly_agg
103-
{% if is_incremental() %}
104-
where {{ incremental_predicate('block_hour') }}
105-
{% endif %}
106-
-- retrieve last known hourly agg update from before the current window to correctly populate the forward fill
10776
{% if is_incremental() %}
108-
union all
109-
select
110-
blockchain,
111-
project,
112-
version,
113-
max(block_month) as block_month,
114-
max(block_hour) as block_hour,
115-
token_address,
116-
symbol,
117-
max(liquidity_index) as liquidity_index,
118-
max(variable_borrow_index) as variable_borrow_index,
119-
max(deposit_rate) as deposit_rate,
120-
max(stable_borrow_rate) as stable_borrow_rate,
121-
max(variable_borrow_rate) as variable_borrow_rate
122-
from reserve_data_hourly_agg
123-
where not {{ incremental_predicate('block_hour') }}
124-
group by 1,2,3,6,7
77+
and {{ incremental_predicate('block_time') }}
12578
{% endif %}
126-
) t
127-
),
128-
129-
reserve_token_start as (
130-
select
131-
blockchain,
132-
project,
133-
version,
134-
token_address,
135-
min(block_hour) as block_hour_start
136-
from reserve_data_hourly_changes
137-
group by 1,2,3,4
138-
),
139-
140-
token_hourly_sequence as (
141-
select
142-
rts.blockchain,
143-
rts.project,
144-
rts.version,
145-
rts.token_address,
146-
h.timestamp as block_hour
147-
from reserve_token_start rts
148-
inner join {{ source('utils', 'hours') }} h on rts.block_hour_start <= h.timestamp
149-
),
150-
151-
forward_fill as (
152-
select
153-
ths.blockchain,
154-
ths.project,
155-
ths.version,
156-
cast(date_trunc('month', ths.block_hour) as date) as block_month,
157-
ths.block_hour,
158-
ths.token_address,
159-
rdhc.symbol,
160-
rdhc.liquidity_index,
161-
rdhc.variable_borrow_index,
162-
rdhc.deposit_rate,
163-
rdhc.stable_borrow_rate,
164-
rdhc.variable_borrow_rate
165-
from token_hourly_sequence ths
166-
left join reserve_data_hourly_changes rdhc
167-
on ths.block_hour >= rdhc.block_hour
168-
and (ths.block_hour < rdhc.next_update_block_hour or rdhc.next_update_block_hour is null)
16979
)
17080

171-
select *
172-
from forward_fill
173-
{% if is_incremental() %}
174-
where {{ incremental_predicate('block_hour') }}
175-
{% endif %}
81+
select
82+
blockchain,
83+
project,
84+
version,
85+
block_month,
86+
block_hour,
87+
token_address,
88+
symbol,
89+
max_by(liquidity_index, block_hour) as liquidity_index,
90+
max_by(variable_borrow_index, block_hour) as variable_borrow_index,
91+
avg(cast(deposit_rate as double)) / 1e27 as deposit_rate,
92+
avg(cast(stable_borrow_rate as double)) / 1e27 as stable_borrow_rate,
93+
avg(cast(variable_borrow_rate as double)) / 1e27 as variable_borrow_rate
94+
from reserve_data
95+
group by 1,2,3,4,5,6,7
17696

17797
{% endmacro %}

0 commit comments

Comments
 (0)