An 1931/sushi borrowing and lending table bsc (#15)

* sushi borrow and lend on BSC

* docs
This commit is contained in:
Vahid-flipside 2022-08-25 15:57:26 -04:00 committed by GitHub
parent 2c1d9c969f
commit 0b0f63b62a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 800 additions and 2 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,5 @@
{% docs borrow_symbol %}
The symbol of the asset that is repayed or borrowed, depending on the action
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs borrower %}
Its the address of the user who is Borrowing or repaying the loan, depending on the action.
{% enddocs %}

View File

@ -0,0 +1,7 @@
{% docs borrow_action %}
The action that the user is taking.
Borrow: user is borrowing an asset
Repay: user is repaying the asset that they have borrowed in a previous loan
{% enddocs %}

View File

@ -0,0 +1,9 @@
{% docs borrow_amount %}
The meaning depends on the action:
Borrow: The amount of the asset that the user is borrowing or
Repay: The amount of the asset that the user is repaying
Deposit_collateral: The amount of collateral that the user is depositing
Withdraw_collateral: The amount of collateral that the user is withdrawing
{% enddocs %}

View File

@ -0,0 +1,8 @@
{% docs borrow_amount_usd %}
The meaning depends on the action:
Borrow: The amount of the asset in USD that the user is borrowing or
Repay: The amount of the asset in USD that the user is repaying
Deposit_collateral: The amount of collateral in USD that the user is depositing
Withdraw_collateral: The amount of collateral in USD that the user is withdrawing
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs borrow_asset %}
The address of the asset (token) that is being borrowed/repayed, depending on the action
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs collateral_address %}
The address of the asset that is used for collateral when borrowing funds.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs collateral_symbol %}
The symbol of the asset that is used for collateral when borrowing funds.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_depositor %}
Its the address of the user who is depositing for lending or withdrawing, depending on the action.
{% enddocs %}

View File

@ -0,0 +1,4 @@
{% docs lending_asset_address %}
The address of the asset in the token pair. This asset is either deposited ot withdrawn for lending purposes.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_pool_address %}
The address of the lending pool. For sushi this will be the address of the kashi pair.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_symbol %}
The symbol of the asset that is lent or withdrawn, depending on the action
{% enddocs %}

View File

@ -0,0 +1,7 @@
{% docs lending_action %}
The action that the user is taking.
Deposit: user is depositing funds to be used for lending
Withdraw: user has changed their mind and are no longer willing to lend, so they withdraw their asset
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_amount %}
The amount of the asset that the user is depositing or withdrawing, depending on the action.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_amount_usd %}
The amount of the asset that the user is depositing or withdrawing, depending on the action.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_asset %}
The address of the asset (token) that is being deposited/withdrawn, depending on the action
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_borrower_is_a_contract %}
If the depositor of collateral is a contract then its a Yes, if the depositor of collateral is a normal address it is a No.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_lender_is_a_contract %}
If the depositor is a contract then its a Yes, if the depositor is a normal address it is a No.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_lending_pool %}
The name of the lending pool.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_origin_from_address %}
The address of the user who initiates the transaction.
{% enddocs %}

View File

@ -0,0 +1,5 @@
{% docs lending_origin_to_address %}
The person who initiates the depositing transaction has to interact with this address. This address belongs to the lending platform or directs the transactio there.
{% enddocs %}

View File

@ -0,0 +1,18 @@
{{ config(
materialized = 'table'
) }}
SELECT
lower(kashi_pair_address) as pair_address,
kashi_pair as pair_name,
asset_symbol,
lower(asset_address) as asset_address,
collateral_symbol,
lower(collateral_address) as collateral_address,
asset_decimals,
collateral_decimals
FROM
{{ source(
'bsc_pools',
'SUSHI_DIM_KASHI_PAIRS'
) }}

View File

@ -0,0 +1,23 @@
version: 2
models:
- name: sushi__dim_kashi_pairs
description: 'This table contains details on different Kashi pairs belonging to sushiswap on the BSC blockchain'
columns:
- name: PAIR_ADDRESS
description: '{{ doc("lending_pool_address") }}'
- name: PAIR_NAME
description: '{{ doc("lending_lending_pool") }}'
- name: ASSET_SYMBOL
description: '{{ doc("lending_symbol") }}'
- name: ASSET_ADDRESS
description: '{{ doc("lending_asset_address") }}'
- name: COLLATERAL_SYMBOL
description: '{{ doc("collateral_symbol") }}'
- name: COLLATERAL_ADDRESS
description: '{{ doc("collateral_address") }}'
- name: ASSET_DECIMALS
description: '{{ doc("bsc_decimals") }}'
- name: COLLATERAL_DECIMALS
description: '{{ doc("bsc_decimals") }}'

View File

@ -0,0 +1,203 @@
{{ config(
materialized = 'incremental',
incremental_strategy = 'delete+insert',
persist_docs ={ "relation": true,
"columns": true },
unique_key = '_log_id',
cluster_by = ['block_timestamp::DATE']
) }}
with borrow_txns as (
select distinct tx_hash,contract_address
from {{ ref('silver__logs') }}
where event_name = 'LogBorrow'
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Repay_txns as (
select distinct tx_hash,contract_address
from {{ ref('silver__logs') }}
where event_name = 'LogRepay'
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Borrow as (
select block_timestamp,
block_number,
tx_hash,
'Borrow' as action,
origin_from_address,
origin_to_address,
origin_function_signature,
event_index,
CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS asset,
CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS Lending_pool_address,
origin_from_address as Borrower,
CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) as Borrower2,
TRY_TO_NUMBER(
public.udf_hex_to_int(SUBSTR(DATA, 3, len(DATA)))::integer
) as amount,
case when Borrower = Borrower2 then 'no'
else 'yes' end as Borrower_is_a_contract,
_log_id,
_inserted_timestamp
from {{ ref('silver__logs') }}
where topics [0]::string = '0x6eabe333476233fd382224f233210cb808a7bc4c4de64f9d76628bf63c677b1a' and tx_hash in (select tx_hash from borrow_txns)
and CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) in (select pair_address from {{ ref('sushi__dim_kashi_pairs') }} )
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Repay as (
select block_timestamp,
block_number,
tx_hash,
'Repay' as action,
origin_from_address,
origin_to_address,
origin_function_signature,
event_index,
CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) as asset,
CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) as Lending_pool_address,
origin_from_address as Borrower,
CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) as Borrower2,
TRY_TO_NUMBER(
public.udf_hex_to_int(SUBSTR(DATA, 3, len(DATA)))::integer
) as amount,
case when Borrower = Borrower2 then 'no'
else 'yes' end as Lender_is_a_contract,
_log_id,
_inserted_timestamp
from {{ ref('silver__logs') }}
where topics [0]::string = '0x6eabe333476233fd382224f233210cb808a7bc4c4de64f9d76628bf63c677b1a' and tx_hash in (select tx_hash from Repay_txns)
and CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) in (select pair_address from {{ ref('sushi__dim_kashi_pairs') }} )
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Final as (
select * from Borrow
union all
select * from Repay
),
BSC_prices AS (
select
symbol,
date_trunc('hour',recorded_at) as hour,
avg(price) as price
from
{{ source('prices','prices_v2') }} a
join {{ ref('sushi__dim_kashi_pairs') }} b
on a.symbol = b.asset_symbol
WHERE
1 = 1
{% if is_incremental() %}
AND hour :: DATE IN (
SELECT
DISTINCT block_timestamp :: DATE
FROM
borrow
)
{% else %}
AND hour :: DATE >= '2021-09-01'
{% endif %}
group by 1,2
),
labels as (
select *
from {{ ref('sushi__dim_kashi_pairs') }}
),
Labled_WO_prices as (
select
a.block_timestamp,
a.block_number,
a.tx_hash,
a.action,
a.origin_from_address,
a.origin_to_address,
a.origin_function_signature,
a.asset,
a.Borrower2 as Borrower,
a.Borrower_is_a_contract,
a.lending_pool_address,
a.event_index,
b.asset_decimals,
case when b.asset_decimals is null then a.amount else (a.amount/pow(10,b.asset_decimals)) end as asset_amount,
b.pair_name as lending_pool,
b.asset_symbol as symbol,
a._log_id,
substring(b.pair_name,3,charindex('/',b.pair_name)-3) as collateral_symbol,
_inserted_timestamp
from FINAL a
left join labels b
on a.Lending_pool_address = b.pair_address
)
select
a.block_timestamp,
a.block_number,
a.tx_hash,
a.action,
a.origin_from_address,
a.origin_to_address,
a.origin_function_signature,
a.asset,
a.Borrower,
a.Borrower_is_a_contract,
a.lending_pool_address,
a.event_index,
a.asset_amount,
a.lending_pool,
a.symbol,
a._log_id,
a.collateral_symbol,
case --when c.price is null then null
when a.asset_Decimals is null then null
else (a.asset_amount* c.price)
end as asset_amount_USD ,
_inserted_timestamp
from Labled_WO_prices a
LEFT JOIN BSC_prices c
ON a.symbol = c.symbol
AND DATE_TRUNC(
'hour',
a.block_timestamp
) = c.hour

View File

@ -0,0 +1,119 @@
version: 2
models:
- name: sushi__ez_borrowing
description: 'This is a table that shows all the events on BSC that are related to Borrowing or repaying the loan for sushi'
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- _LOG_ID
columns:
- name: BLOCK_NUMBER
description: '{{ doc("bsc_block_number") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: BLOCK_TIMESTAMP
description: '{{ doc("bsc_block_timestamp") }}'
tests:
- not_null
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 45 #borrowing transactions are not very frequent
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- TIMESTAMP_NTZ
- name: TX_HASH
description: '{{ doc("bsc_logs_tx_hash") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: LENDING_POOL_ADDRESS
description: '{{ doc("lending_pool_address") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: ACTION
description: '{{ doc("borrow_action") }}'
- name: ASSET_AMOUNT
description: '{{ doc("borrow_amount") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: ASSET_AMOUNT_USD
description: '{{ doc("borrow_amount_usd") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: ASSET
description: '{{ doc("borrow_asset") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: SYMBOL
description: '{{ doc("borrow_symbol") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: Borrower
description: '{{ doc("borrower") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: EVENT_INDEX
description: '{{ doc("bsc_event_index") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: _LOG_ID
description: '{{ doc("bsc_log_id_events") }}'
tests:
- not_null
- name: ORIGIN_FROM_ADDRESS
description: '{{ doc("lending_origin_from_address") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: ORIGIN_TO_ADDRESS
description: '{{ doc("lending_origin_to_address") }}'
tests:
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: LENDING_POOL
description: '{{ doc("lending_lending_pool") }}'
tests:
- not_null
- name: BORROWER_IS_A_CONTRACT
description: '{{ doc("lending_borrower_is_a_contract") }}'
tests:
- not_null

View File

@ -0,0 +1,201 @@
{{ config(
materialized = 'incremental',
incremental_strategy = 'delete+insert',
persist_docs ={ "relation": true,
"columns": true },
unique_key = '_log_id',
cluster_by = ['block_timestamp::DATE']
) }}
with borrow_txns as (
select distinct tx_hash,contract_address
from {{ ref('silver__logs') }}
where topics[0]::string = '0x30a8c4f9ab5af7e1309ca87c32377d1a83366c5990472dbf9d262450eae14e38'
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Repay_txns as (
select distinct tx_hash,contract_address
from {{ ref('silver__logs') }}
where topics[0]::string = '0x6e853a5fd6b51d773691f542ebac8513c9992a51380d4c342031056a64114228'
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Lending as (
select block_timestamp,
block_number,
tx_hash,
'Deposit' as action,
origin_from_address,
origin_to_address,
origin_function_signature,
event_index,
CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS asset,
CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS Lending_pool_address,
origin_from_address as Lender,
CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) as Lender2,
TRY_TO_NUMBER(
public.udf_hex_to_int(SUBSTR(DATA, 3, len(DATA)))::integer
) as amount,
case when Lender = Lender2 then 'no'
else 'yes' end as Lender_is_a_contract,
_log_id,
_inserted_timestamp
from {{ ref('silver__logs') }}
where topics [0]::string = '0x6eabe333476233fd382224f233210cb808a7bc4c4de64f9d76628bf63c677b1a' and tx_hash in (select tx_hash from borrow_txns)
and CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) in (select pair_address from {{ ref('sushi__dim_kashi_pairs') }} )
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Withdraw as (
select block_timestamp,
block_number,
tx_hash,
'Withdraw' as action,
origin_from_address,
origin_to_address,
origin_function_signature,
event_index,
CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) as asset,
CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) as Lending_pool_address,
origin_from_address as Lender,
CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) as Lender2,
TRY_TO_NUMBER(
public.udf_hex_to_int(SUBSTR(DATA, 3, len(DATA)))::integer
) as amount,
case when Lender = Lender2 then 'no'
else 'yes' end as Lender_is_a_contract,
_log_id,
_inserted_timestamp
from {{ ref('silver__logs') }}
where topics [0]::string = '0x6eabe333476233fd382224f233210cb808a7bc4c4de64f9d76628bf63c677b1a' and tx_hash in (select tx_hash from Repay_txns)
and CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) in (select pair_address from {{ ref('sushi__dim_kashi_pairs') }} )
{% if is_incremental() %}
AND _inserted_timestamp::DATE >= (
SELECT
MAX(_inserted_timestamp) ::DATE - 2
FROM
{{ this }}
)
{% endif %}
),
Final as (
select * from Lending
union all
select * from Withdraw
),
prices AS (
select
symbol,
date_trunc('hour',recorded_at) as hour,
avg(price) as price
from
{{ source('prices','prices_v2') }} a
join {{ ref('sushi__dim_kashi_pairs') }} b
on a.symbol = b.asset_symbol
WHERE
1 = 1
{% if is_incremental() %}
AND hour :: DATE IN (
SELECT
DISTINCT block_timestamp :: DATE
FROM
Lending
)
{% else %}
AND hour :: DATE >= '2021-09-01'
{% endif %}
group by 1,2
),
labels as (
select *
from {{ ref('sushi__dim_kashi_pairs') }}
),
Labled_WO_prices as (
select
a.block_timestamp,
a.block_number,
a.tx_hash,
a.action,
a.origin_from_address,
a.origin_to_address,
a.origin_function_signature,
a.asset,
a.Lender2 as depositor,
a.Lender_is_a_contract,
a.lending_pool_address,
a.event_index,
b.asset_decimals,
case when b.asset_decimals is null then a.amount else (a.amount/pow(10,b.asset_decimals)) end as asset_amount,
b.pair_name as lending_pool,
b.asset_symbol as symbol,
a._log_id,
_inserted_timestamp
from FINAL a
left join labels b
on a.Lending_pool_address = b.pair_address
)
select
a.block_timestamp,
a.block_number,
a.tx_hash,
a.action,
a.origin_from_address,
a.origin_to_address,
a.origin_function_signature,
a.asset,
a.depositor,
a.Lender_is_a_contract,
a.lending_pool_address,
a.event_index,
a.asset_amount,
a.lending_pool,
a.symbol,
a._log_id,
case --when c.price is null then null
when a.asset_Decimals is null then null
else (a.asset_amount* c.price)
end as asset_amount_USD ,
_inserted_timestamp
from Labled_WO_prices a
LEFT JOIN prices c
ON a.symbol = c.symbol
AND DATE_TRUNC(
'hour',
a.block_timestamp
) = c.hour

View File

@ -0,0 +1,119 @@
version: 2
models:
- name: sushi__ez_lending
description: 'This is a table that shows all the events on BSC that are related to providing capital for depositing/withdrawing capital to/from sushi lending pools'
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- _LOG_ID
columns:
- name: BLOCK_NUMBER
description: '{{ doc("bsc_block_number") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: BLOCK_TIMESTAMP
description: '{{ doc("bsc_block_timestamp") }}'
tests:
- not_null
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 45 #lending transactions are not very frequent
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- TIMESTAMP_NTZ
- name: TX_HASH
description: '{{ doc("bsc_logs_tx_hash") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: LENDING_POOL_ADDRESS
description: '{{ doc("lending_pool_address") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: ACTION
description: '{{ doc("lending_action") }}'
- name: ASSET_AMOUNT
description: '{{ doc("lending_amount") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: ASSET_AMOUNT_USD
description: '{{ doc("lending_amount_usd") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: ASSET
description: '{{ doc("lending_asset") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: SYMBOL
description: '{{ doc("lending_symbol") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- STRING
- VARCHAR
- name: DEPOSITOR
description: '{{ doc("lending_depositor") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: EVENT_INDEX
description: '{{ doc("bsc_event_index") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: _LOG_ID
description: '{{ doc("bsc_log_id_events") }}'
tests:
- not_null
- name: ORIGIN_FROM_ADDRESS
description: '{{ doc("lending_origin_from_address") }}'
tests:
- not_null
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: ORIGIN_TO_ADDRESS
description: '{{ doc("lending_origin_to_address") }}'
tests:
- dbt_expectations.expect_column_values_to_match_regex:
regex: 0[xX][0-9a-fA-F]+
- name: LENDING_POOL
description: '{{ doc("lending_lending_pool") }}'
tests:
- not_null
- name: LENDER_IS_A_CONTRACT
description: '{{ doc("lending_lender_is_a_contract") }}'
tests:
- not_null