Skip to content

Commit

Permalink
Consolidate lending sector - borrow macro (duneanalytics#4704)
Browse files Browse the repository at this point in the history
* lending aave borrow macros

* borrow setup

* complete init setup

* date_trunc fix

* fix partition_by

* add exposed views

* remove old models

* missing columns

* more chains, custom v3 for arbitrum

* rename models & clean up

* rearrange models

* fix messed up folders

* moola celo ref fix

* column name fix

* exposed views column names fix

* fix tests

* fix schema

* unique_key fix

* missed base..

* 2 more chains

* complete renamin

* fixes and added arbitrum seed

* fix amount data type

* seed fix

* fix and add avalanche seed

* update avalanche seed

* all seeds

* fix eth seed

* add fantom to seed schema

* (un)round seed amounts..

---------

Co-authored-by: Huang Geyang <[email protected]>
  • Loading branch information
tomfutago and Hosuke authored Nov 20, 2023
1 parent f848645 commit 3c296e6
Show file tree
Hide file tree
Showing 70 changed files with 3,053 additions and 850 deletions.
322 changes: 322 additions & 0 deletions macros/models/_sector/lending/lending_aave_compatible_borrow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
{%
macro lending_aave_v1_compatible_borrow(
blockchain,
project,
version,
aave_mock_address,
native_token_address,
project_decoded_as = 'aave'
)
%}

with

src_LendingPool_evt_Borrow as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_Borrow') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_Repay as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_Repay') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_LiquidationCall as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_LiquidationCall') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

base_borrow as (
select
'borrow' as transaction_type,
case
when _borrowRateMode = uint256 '1' then 'stable'
when _borrowRateMode = uint256 '2' then 'variable'
end as loan_type,
case
when _reserve = {{ aave_mock_address }} then {{ native_token_address }} --using native_token_address instead of Aave "mock" address
else _reserve
end as token_address,
_user as borrower,
cast(null as varbinary) as repayer,
cast(null as varbinary) as liquidator,
cast(_amount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Borrow
union all
select
'repay' as transaction_type,
null as loan_type,
case
when _reserve = {{ aave_mock_address }} then {{ native_token_address }} --using native_token_address instead of Aave "mock" address
else _reserve
end as token_address,
_user as borrower,
_repayer as repayer,
cast(null as varbinary) as liquidator,
-1 * cast(_amountMinusFees as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Repay
union all
select
'borrow_liquidation' as transaction_type,
null as loan_type,
case
when _reserve = {{ aave_mock_address }} then {{ native_token_address }} --using native_token_address instead of Aave "mock" address
else _reserve
end as token_address,
_user as borrower,
_liquidator as repayer,
_liquidator as liquidator,
-1 * cast(_purchaseAmount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_LiquidationCall
)

select
'{{ blockchain }}' as blockchain,
'{{ project }}' as project,
'{{ version }}' as version,
transaction_type,
loan_type,
token_address,
borrower,
repayer,
liquidator,
amount,
cast(date_trunc('month', evt_block_time) as date) as block_month,
evt_block_time as block_time,
evt_block_number as block_number,
evt_tx_hash as tx_hash,
evt_index
from base_borrow

{% endmacro %}

{# ######################################################################### #}

{%
macro lending_aave_v2_compatible_borrow(
blockchain,
project,
version,
project_decoded_as = 'aave_v2'
)
%}

with

src_LendingPool_evt_Borrow as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_Borrow') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_Repay as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_Repay') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_LiquidationCall as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, 'LendingPool_evt_LiquidationCall') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

base_borrow as (
select
'borrow' as transaction_type,
case
when borrowRateMode = uint256 '1' then 'stable'
when borrowRateMode = uint256 '2' then 'variable'
end as loan_type,
reserve as token_address,
user as borrower,
cast(null as varbinary) as repayer,
cast(null as varbinary) as liquidator,
cast(amount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Borrow
union all
select
'repay' as transaction_type,
null as loan_type,
reserve as token_address,
user as borrower,
repayer as repayer,
cast(null as varbinary) as liquidator,
-1 * cast(amount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Repay
union all
select
'borrow_liquidation' as transaction_type,
null as loan_type,
debtAsset as token_address,
user as borrower,
liquidator as repayer,
liquidator as liquidator,
-1 * cast(debtToCover as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_LiquidationCall
)

select
'{{ blockchain }}' as blockchain,
'{{ project }}' as project,
'{{ version }}' as version,
transaction_type,
loan_type,
token_address,
borrower,
repayer,
liquidator,
amount,
cast(date_trunc('month', evt_block_time) as date) as block_month,
evt_block_time as block_time,
evt_block_number as block_number,
evt_tx_hash as tx_hash,
evt_index
from base_borrow

{% endmacro %}

{# ######################################################################### #}

{%
macro lending_aave_v3_compatible_borrow(
blockchain,
project,
version,
project_decoded_as = 'aave_v3'
)
%}

{%- set decoded_contract_name = 'L2Pool' if blockchain == 'arbitrum' else 'Pool' -%}

with

src_LendingPool_evt_Borrow as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, decoded_contract_name ~ '_evt_Borrow') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_Repay as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, decoded_contract_name ~ '_evt_Repay') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

src_LendingPool_evt_LiquidationCall as (
select *
from {{ source(project_decoded_as ~ '_' ~ blockchain, decoded_contract_name ~ '_evt_LiquidationCall') }}
{% if is_incremental() %}
where {{ incremental_predicate('evt_block_time') }}
{% endif %}
),

base_borrow as (
select
'borrow' as transaction_type,
case
when interestRateMode = 1 then 'stable'
when interestRateMode = 2 then 'variable'
end as loan_type,
reserve as token_address,
user as borrower,
cast(null as varbinary) as repayer,
cast(null as varbinary) as liquidator,
cast(amount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Borrow
union all
select
'repay' as transaction_type,
null as loan_type,
reserve as token_address,
user as borrower,
repayer as repayer,
cast(null as varbinary) as liquidator,
-1 * cast(amount as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_Repay
union all
select
'borrow_liquidation' as transaction_type,
null as loan_type,
debtAsset as token_address,
user as borrower,
liquidator as repayer,
liquidator as liquidator,
-1 * cast(debtToCover as double) as amount,
evt_tx_hash,
evt_index,
evt_block_time,
evt_block_number
from src_LendingPool_evt_LiquidationCall
)

select
'{{ blockchain }}' as blockchain,
'{{ project }}' as project,
'{{ version }}' as version,
transaction_type,
loan_type,
token_address,
borrower,
repayer,
liquidator,
amount,
cast(date_trunc('month', evt_block_time) as date) as block_month,
evt_block_time as block_time,
evt_block_number as block_number,
evt_tx_hash as tx_hash,
evt_index
from base_borrow

{% endmacro %}
34 changes: 34 additions & 0 deletions macros/models/_sector/lending/lending_enrich_borrow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% macro lending_enrich_borrow(model) %}

select
borrow.blockchain,
borrow.project,
borrow.version,
borrow.transaction_type,
borrow.loan_type,
erc20.symbol,
borrow.token_address,
borrow.borrower,
borrow.repayer,
borrow.liquidator,
borrow.amount / power(10, coalesce(erc20.decimals, 18)) as amount,
borrow.amount / power(10, coalesce(p.decimals, erc20.decimals, 18)) * p.price as usd_amount,
borrow.block_month,
borrow.block_time,
borrow.block_number,
borrow.tx_hash,
borrow.evt_index
from {{ model }} borrow
left join {{ ref('tokens_erc20') }} erc20
on borrow.token_address = erc20.contract_address
and borrow.blockchain = erc20.blockchain
left join {{ source('prices', 'usd') }} p
on date_trunc('minute', borrow.block_time) = p.minute
and erc20.symbol = p.symbol
and borrow.token_address = p.contract_address
and borrow.blockchain = p.blockchain
{% if is_incremental() %}
and {{ incremental_predicate('p.minute') }}
{% endif %}

{% endmacro %}
Loading

0 comments on commit 3c296e6

Please sign in to comment.