An 1914/sushi borrowing for arbitrum (#13)

* new borrowing table for sushi

* new borrowing table for sushi sql model

* source and catalog
This commit is contained in:
Vahid-flipside 2022-08-17 14:11:15 -04:00 committed by GitHub
parent d7604aec6a
commit 03c316aaa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 329 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

@ -28,3 +28,8 @@ sources:
tables:
- name: SUSHI_DIM_DEX_POOLS
- name: SUSHI_DIM_KASHI_POOLS
- name: prices
database: FLIPSIDE_PROD_DB
schema: SILVER
tables:
- name: prices_v2

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
),
Arbitrum_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 Arbitrum_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 arbitrum 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("arb_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("arb_block_timestamp") }}'
tests:
- not_null
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 7 #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("arb_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("arb_event_index") }}'
tests:
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list:
- NUMBER
- FLOAT
- name: _LOG_ID
description: '{{ doc("arb_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