|
2 | 2 | macro lending_aave_v3_compatible_market( |
3 | 3 | blockchain, |
4 | 4 | project = 'aave', |
5 | | - version = '3', |
| 5 | + version = 'v3', |
6 | 6 | project_decoded_as = 'aave_v3', |
7 | 7 | decoded_contract_name = 'Pool' |
8 | 8 | ) |
@@ -61,117 +61,37 @@ from reserve_data |
61 | 61 | macro lending_aave_v3_compatible_market_hourly_agg( |
62 | 62 | blockchain, |
63 | 63 | project = 'aave', |
64 | | - version = '3' |
| 64 | + version = 'v3' |
65 | 65 | ) |
66 | 66 | %} |
67 | 67 |
|
68 | 68 | with |
69 | 69 |
|
70 | | -reserve_data_base as ( |
| 70 | +reserve_data as ( |
71 | 71 | select * |
72 | 72 | from {{ ref('lending_' ~ blockchain ~ '_base_market') }} |
73 | 73 | where blockchain = '{{ blockchain }}' |
74 | 74 | and project = '{{ project }}' |
75 | 75 | 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 |
107 | 76 | {% 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') }} |
125 | 78 | {% 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) |
169 | 79 | ) |
170 | 80 |
|
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 |
176 | 96 |
|
177 | 97 | {% endmacro %} |
0 commit comments