From b78256eb9541050e24a8827f4c61548943dd7fcf Mon Sep 17 00:00:00 2001 From: Matt Romano <42412983+mattromano@users.noreply.github.com> Date: Tue, 21 Jan 2025 14:39:01 -0800 Subject: [PATCH] AN-5302/build-and-deploy-defi-lending-tables (#93) * add orbit lending models * add juice and orbit * add init silver * tidy + incrementals * lending QA fixes * add completes * add gold * add SY liquidation updates * clean up asset detail models * juice asset fixes and init timestamps * model fixes * update tests * update events * add inc * overview * remove user * remove comments * pr updates -add new column standards * remove coalesce --- .../complete_lending_table_docs.md | 10 +- .../doc_descriptions/general/__overview__.md | 5 + .../defi/lending/defi__ez_lending_borrows.sql | 37 ++ .../defi/lending/defi__ez_lending_borrows.yml | 46 ++ .../lending/defi__ez_lending_deposits.sql | 37 ++ .../lending/defi__ez_lending_deposits.yml | 46 ++ .../lending/defi__ez_lending_liquidations.sql | 40 ++ .../lending/defi__ez_lending_liquidations.yml | 52 +++ .../lending/defi__ez_lending_repayments.sql | 38 ++ .../lending/defi__ez_lending_repayments.yml | 48 ++ .../lending/defi__ez_lending_withdraws.sql | 37 ++ .../lending/defi__ez_lending_withdraws.yml | 46 ++ .../silver__complete_lending_borrows.sql | 296 ++++++++++++ .../silver__complete_lending_borrows.yml | 14 + .../silver__complete_lending_deposits.sql | 294 ++++++++++++ .../silver__complete_lending_deposits.yml | 14 + .../silver__complete_lending_liquidations.sql | 332 ++++++++++++++ .../silver__complete_lending_liquidations.yml | 14 + .../silver__complete_lending_repayments.sql | 300 +++++++++++++ .../silver__complete_lending_repayments.yml | 58 +++ .../silver__complete_lending_withdraws.sql | 301 +++++++++++++ .../silver__complete_lending_withdraws.yml | 14 + .../init/silver__init_asset_details.sql | 118 +++++ .../init/silver__init_asset_details.yml | 12 + .../lending/init/silver__init_borrows.sql | 224 ++++++++++ .../lending/init/silver__init_borrows.yml | 105 +++++ .../lending/init/silver__init_deposits.sql | 323 +++++++++++++ .../lending/init/silver__init_deposits.yml | 110 +++++ .../init/silver__init_liquidations.sql | 187 ++++++++ .../init/silver__init_liquidations.yml | 119 +++++ .../lending/init/silver__init_repayments.sql | 228 ++++++++++ .../lending/init/silver__init_repayments.yml | 105 +++++ .../lending/init/silver__init_withdraws.sql | 333 ++++++++++++++ .../lending/init/silver__init_withdraws.yml | 95 ++++ .../juice/silver__juice_asset_details.sql | 423 ++++++++++++++++++ .../juice/silver__juice_asset_details.yml | 13 + .../lending/juice/silver__juice_borrows.sql | 134 ++++++ .../lending/juice/silver__juice_borrows.yml | 89 ++++ .../lending/juice/silver__juice_deposits.sql | 267 +++++++++++ .../lending/juice/silver__juice_deposits.yml | 110 +++++ .../juice/silver__juice_liquidations.sql | 203 +++++++++ .../juice/silver__juice_liquidations.yml | 119 +++++ .../juice/silver__juice_repayments.sql | 130 ++++++ .../juice/silver__juice_repayments.yml | 105 +++++ .../lending/juice/silver__juice_withdraws.sql | 260 +++++++++++ .../lending/juice/silver__juice_withdraws.yml | 95 ++++ .../orbit/silver__orbit_asset_details.sql | 146 ++++++ .../orbit/silver__orbit_asset_details.yml | 12 + .../lending/orbit/silver__orbit_borrows.sql | 128 ++++++ .../lending/orbit/silver__orbit_borrows.yml | 89 ++++ .../lending/orbit/silver__orbit_deposits.sql | 131 ++++++ .../lending/orbit/silver__orbit_deposits.yml | 110 +++++ .../orbit/silver__orbit_liquidations.sql | 144 ++++++ .../orbit/silver__orbit_liquidations.yml | 119 +++++ .../orbit/silver__orbit_repayments.sql | 125 ++++++ .../orbit/silver__orbit_repayments.yml | 105 +++++ .../lending/orbit/silver__orbit_withdraws.sql | 131 ++++++ .../lending/orbit/silver__orbit_withdraws.yml | 95 ++++ 58 files changed, 7316 insertions(+), 5 deletions(-) create mode 100644 models/gold/defi/lending/defi__ez_lending_borrows.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_borrows.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_deposits.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_deposits.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_liquidations.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_liquidations.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_repayments.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_repayments.yml create mode 100644 models/gold/defi/lending/defi__ez_lending_withdraws.sql create mode 100644 models/gold/defi/lending/defi__ez_lending_withdraws.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql create mode 100644 models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml create mode 100644 models/silver/defi/lending/init/silver__init_asset_details.sql create mode 100644 models/silver/defi/lending/init/silver__init_asset_details.yml create mode 100644 models/silver/defi/lending/init/silver__init_borrows.sql create mode 100644 models/silver/defi/lending/init/silver__init_borrows.yml create mode 100644 models/silver/defi/lending/init/silver__init_deposits.sql create mode 100644 models/silver/defi/lending/init/silver__init_deposits.yml create mode 100644 models/silver/defi/lending/init/silver__init_liquidations.sql create mode 100644 models/silver/defi/lending/init/silver__init_liquidations.yml create mode 100644 models/silver/defi/lending/init/silver__init_repayments.sql create mode 100644 models/silver/defi/lending/init/silver__init_repayments.yml create mode 100644 models/silver/defi/lending/init/silver__init_withdraws.sql create mode 100644 models/silver/defi/lending/init/silver__init_withdraws.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_asset_details.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_asset_details.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_borrows.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_borrows.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_deposits.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_deposits.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_liquidations.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_liquidations.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_repayments.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_repayments.yml create mode 100644 models/silver/defi/lending/juice/silver__juice_withdraws.sql create mode 100644 models/silver/defi/lending/juice/silver__juice_withdraws.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_asset_details.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_asset_details.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_borrows.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_borrows.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_deposits.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_deposits.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_liquidations.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_liquidations.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_repayments.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_repayments.yml create mode 100644 models/silver/defi/lending/orbit/silver__orbit_withdraws.sql create mode 100644 models/silver/defi/lending/orbit/silver__orbit_withdraws.yml diff --git a/models/doc_descriptions/complete_lending/complete_lending_table_docs.md b/models/doc_descriptions/complete_lending/complete_lending_table_docs.md index 66c1ee8..5cb1cea 100644 --- a/models/doc_descriptions/complete_lending/complete_lending_table_docs.md +++ b/models/doc_descriptions/complete_lending/complete_lending_table_docs.md @@ -1,13 +1,13 @@ {% docs complete_lending_borrows_table_doc %} -This table contains transactions where users borrowed assets across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. In order to borrow assets, a user must first deposit their preferred asset and amount as collateral. +This table contains transactions where users borrowed assets across ORBIT, JUICE AND INIT protocols. In order to borrow assets, a user must first deposit their preferred asset and amount as collateral. {% enddocs %} {% docs complete_lending_deposits_table_doc %} -This table contains deposit transactions across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. A user deposits their preferred asset and amount. After depositing, users earn passive income based on the market borrowing demand. Additionally, depositing allows users to borrow by using their deposited assets as a collateral. Any interest earned by depositing funds helps offset the interest rate accumulated by borrowing. +This table contains deposit transactions across ORBIT, JUICE AND INIT protocols. A user deposits their preferred asset and amount. After depositing, users earn passive income based on the market borrowing demand. Additionally, depositing allows users to borrow by using their deposited assets as a collateral. Any interest earned by depositing funds helps offset the interest rate accumulated by borrowing. {% enddocs %} @@ -19,18 +19,18 @@ This table contains flash loan transactions across AAVE and RADIANT protocols. F {% docs complete_lending_liquidations_table_doc %} -This table contains transactions in which a borrower's collateral asset is liquidated across AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Liquidations occur when a borrower's health factor goes below 1 due to their collateral value not properly covering their loan/debt value. This might happen when the collateral decreases in value or the borrowed debt increases in value against each other. This collateral vs loan value ratio is shown in the health factor. In a liquidation, up to 50% of a borrower's debt is repaid and that value + liquidation fee is taken from the collateral available, so after a liquidation the amount liquidated from one's debt is repaid. +This table contains transactions in which a borrower's collateral asset is liquidated across ORBIT, JUICE AND INIT protocols. Liquidations occur when a borrower's health factor goes below 1 due to their collateral value not properly covering their loan/debt value. This might happen when the collateral decreases in value or the borrowed debt increases in value against each other. This collateral vs loan value ratio is shown in the health factor. In a liquidation, up to 50% of a borrower's debt is repaid and that value + liquidation fee is taken from the collateral available, so after a liquidation the amount liquidated from one's debt is repaid. {% enddocs %} {% docs complete_lending_repayments_table_doc %} -This table contains transactions in which a borrower repays their loan (debt) across the AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Loans are repaid in the same asset borrowed, plus accrued interest. Borrowers can pay back their loan based on the USD price as they can borrow any of the available stable coins (USDC, DAI, USDT, etc.). +This table contains transactions in which a borrower repays their loan (debt) across the ORBIT, JUICE AND INIT protocols. Loans are repaid in the same asset borrowed, plus accrued interest. Borrowers can pay back their loan based on the USD price as they can borrow any of the available stable coins (USDC, DAI, USDT, etc.). {% enddocs %} {% docs complete_lending_withdraws_table_doc %} -This table contains transactions in which a user withdraws liquidity across the AAVE, COMPOUND, SILO, DFORCE, RADIANT, and LODESTAR protocols. Users need to make sure there is enough liquidity (not borrowed) in order to withdraw, if this is not the case, users need to wait for more liquidity from depositors or borrowers repaying. +This table contains transactions in which a user withdraws liquidity across the ORBIT, JUICE AND INIT protocols. Users need to make sure there is enough liquidity (not borrowed) in order to withdraw, if this is not the case, users need to wait for more liquidity from depositors or borrowers repaying. {% enddocs %} \ No newline at end of file diff --git a/models/doc_descriptions/general/__overview__.md b/models/doc_descriptions/general/__overview__.md index 7071e93..1ab8c4f 100644 --- a/models/doc_descriptions/general/__overview__.md +++ b/models/doc_descriptions/general/__overview__.md @@ -45,6 +45,11 @@ There is more information on how to use dbt docs in the last section of this doc - [ez_dex_swaps](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_dex_swaps) - [dim_dex_liquidity_pools](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__dim_dex_liquidity_pools) - [ez_bridge_activity](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_bridge_activity) +- [ez_lending_borrows](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_lending_borrows) +- [ez_lending_deposits](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_lending_deposits) +- [ez_lending_liquidations](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_lending_liquidations) +- [ez_lending_repayments](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_lending_repayments) +- [ez_lending_withdraws](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.defi__ez_lending_withdraws) ### Stats Tables (blast.stats) - [ez_core_metrics_hourly](https://flipsidecrypto.github.io/blast-models/#!/model/model.blast_models.stats__ez_core_metrics_hourly) diff --git a/models/gold/defi/lending/defi__ez_lending_borrows.sql b/models/gold/defi/lending/defi__ez_lending_borrows.sql new file mode 100644 index 0000000..5d32149 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_borrows.sql @@ -0,0 +1,37 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'INIT, ORBIT, JUICE', + 'PURPOSE': 'LENDING, BORROWS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + protocol_market, + borrower, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + complete_lending_borrows_id AS ez_lending_borrows_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__complete_lending_borrows') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_borrows.yml b/models/gold/defi/lending/defi__ez_lending_borrows.yml new file mode 100644 index 0000000..aa8b752 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_borrows.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_borrows + description: '{{ doc("complete_lending_borrows_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("blast_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("blast_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("blast_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("blast_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("blast_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("blast_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("blast_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("blast_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("blast_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_BORROWS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_deposits.sql b/models/gold/defi/lending/defi__ez_lending_deposits.sql new file mode 100644 index 0000000..37c2cb3 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_deposits.sql @@ -0,0 +1,37 @@ +{{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'INIT, ORBIT, JUICE', + 'PURPOSE': 'LENDING, DEPOSITS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + protocol_market, + depositor, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + complete_lending_deposits_id AS ez_lending_deposits_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__complete_lending_deposits') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_deposits.yml b/models/gold/defi/lending/defi__ez_lending_deposits.yml new file mode 100644 index 0000000..2a74f00 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_deposits.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_deposits + description: '{{ doc("complete_lending_deposits_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("blast_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("blast_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("blast_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("blast_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("blast_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("blast_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("blast_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("blast_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: DEPOSITOR + description: '{{ doc("complete_lending_depositor") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("blast_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_DEPOSITS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_liquidations.sql b/models/gold/defi/lending/defi__ez_lending_liquidations.sql new file mode 100644 index 0000000..037d0c9 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_liquidations.sql @@ -0,0 +1,40 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'INIT, ORBIT, JUICE', + 'PURPOSE': 'LENDING, LIQUIDATIONS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + liquidator, + borrower, + protocol_market, + collateral_token, + collateral_token_symbol, + amount_unadj, + amount, + amount_usd, + debt_token, + debt_token_symbol, + complete_lending_liquidations_id AS ez_lending_liquidations_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__complete_lending_liquidations') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_liquidations.yml b/models/gold/defi/lending/defi__ez_lending_liquidations.yml new file mode 100644 index 0000000..625de12 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_liquidations.yml @@ -0,0 +1,52 @@ +version: 2 +models: + - name: defi__ez_lending_liquidations + description: '{{ doc("complete_lending_liquidations_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("blast_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("blast_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("blast_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("blast_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("blast_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("blast_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("blast_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("blast_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: LIQUIDATOR + description: '{{ doc("complete_lending_liquidator") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: COLLATERAL_TOKEN + description: '{{ doc("complete_lending_collateral_asset") }}' + - name: COLLATERAL_TOKEN_SYMBOL + description: '{{ doc("blast_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: DEBT_TOKEN + description: '{{ doc("complete_lending_debt_asset") }}' + - name: DEBT_TOKEN_SYMBOL + description: '{{ doc("blast_symbol") }}' + - name: EZ_LENDING_LIQUDATIONS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_repayments.sql b/models/gold/defi/lending/defi__ez_lending_repayments.sql new file mode 100644 index 0000000..061ecce --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_repayments.sql @@ -0,0 +1,38 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'INIT, ORBIT, JUICE', + 'PURPOSE': 'LENDING, REPAYMENTS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + origin_from_address, + origin_to_address, + origin_function_signature, + tx_hash, + event_index, + contract_address, + event_name, + platform, + payer, + borrower, + protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + complete_lending_repayments_id AS ez_lending_repayments_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__complete_lending_repayments') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_repayments.yml b/models/gold/defi/lending/defi__ez_lending_repayments.yml new file mode 100644 index 0000000..9a40f58 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_repayments.yml @@ -0,0 +1,48 @@ +version: 2 +models: + - name: defi__ez_lending_repayments + description: '{{ doc("complete_lending_repayments_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("blast_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("blast_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("blast_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("blast_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("blast_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("blast_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("blast_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("blast_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: PAYER + description: '{{ doc("complete_lending_payer") }}' + - name: BORROWER + description: '{{ doc("complete_lending_borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("complete_lending_token_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_REPAYMENTS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_withdraws.sql b/models/gold/defi/lending/defi__ez_lending_withdraws.sql new file mode 100644 index 0000000..6ece5b3 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_withdraws.sql @@ -0,0 +1,37 @@ + {{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'INIT, ORBIT, JUICE', + 'PURPOSE': 'LENDING, WITHDRAWS' + } + } + } +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + platform, + depositor, + protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd, + complete_lending_withdraws_id AS ez_lending_withdraws_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__complete_lending_withdraws') }} \ No newline at end of file diff --git a/models/gold/defi/lending/defi__ez_lending_withdraws.yml b/models/gold/defi/lending/defi__ez_lending_withdraws.yml new file mode 100644 index 0000000..9b252a0 --- /dev/null +++ b/models/gold/defi/lending/defi__ez_lending_withdraws.yml @@ -0,0 +1,46 @@ +version: 2 +models: + - name: defi__ez_lending_withdraws + description: '{{ doc("complete_lending_withdraws_table_doc") }}' + + columns: + - name: BLOCK_NUMBER + description: '{{ doc("blast_block_number") }}' + - name: BLOCK_TIMESTAMP + description: '{{ doc("blast_block_timestamp") }}' + - name: TX_HASH + description: '{{ doc("blast_logs_tx_hash") }}' + - name: CONTRACT_ADDRESS + description: '{{ doc("blast_logs_contract_address") }}' + - name: EVENT_NAME + description: '{{ doc("blast_event_name") }}' + - name: EVENT_INDEX + description: '{{ doc("blast_event_index") }}' + - name: ORIGIN_FUNCTION_SIGNATURE + description: '{{ doc("nft_origin_sig") }}' + - name: ORIGIN_FROM_ADDRESS + description: '{{ doc("blast_origin_from") }}' + - name: ORIGIN_TO_ADDRESS + description: '{{ doc("blast_origin_to") }}' + - name: PLATFORM + description: '{{ doc("complete_lending_platform") }}' + - name: DEPOSITOR + description: '{{ doc("borrower") }}' + - name: PROTOCOL_MARKET + description: '{{ doc("complete_lending_protocol_token") }}' + - name: TOKEN_ADDRESS + description: '{{ doc("complete_lending_token_address") }}' + - name: TOKEN_SYMBOL + description: '{{ doc("complete_lending_token_symbol") }}' + - name: AMOUNT_UNADJ + description: '{{ doc("complete_lending_amount_unadj") }}' + - name: AMOUNT + description: '{{ doc("complete_lending_amount") }}' + - name: AMOUNT_USD + description: '{{ doc("complete_lending_amount_usd") }}' + - name: EZ_LENDING_WITHDRAWS_ID + description: '{{ doc("pk") }}' + - name: INSERTED_TIMESTAMP + description: '{{ doc("inserted_timestamp") }}' + - name: MODIFIED_TIMESTAMP + description: '{{ doc("modified_timestamp") }}' \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql new file mode 100644 index 0000000..96965b3 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.sql @@ -0,0 +1,296 @@ +-- depends_on: {{ ref('silver__complete_token_prices') }} +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE','platform'], + post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, origin_from_address, origin_to_address, origin_function_signature, contract_address, event_name, token_address, token_symbol, borrower, protocol_market), SUBSTRING(origin_function_signature, event_name, token_address, token_symbol, borrower, protocol_market)", + tags = ['reorg','curated','heal'] +) }} + +WITH init AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address AS protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + A._LOG_ID, + A.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__init_borrows') }} A + +{% if is_incremental() and 'init' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +juice AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address AS protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + A._LOG_ID, + A.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__juice_borrows') }} A + +{% if is_incremental() and 'juice' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +orbit AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address AS protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + A._LOG_ID, + A.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__orbit_borrows') }} A + +{% if is_incremental() and 'orbit' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +borrow_union AS ( + SELECT + * + FROM + init + UNION ALL + SELECT + * + FROM + juice + UNION ALL + SELECT + * + FROM + orbit +), +complete_lending_borrows AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + b.contract_address, + 'Borrow' AS event_name, + borrower, + protocol_market, + b.token_address, + b.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + b.blockchain, + b._LOG_ID, + b._inserted_timestamp + FROM + borrow_union b + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON b.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour +), + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +heal_model AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + t0.contract_address, + event_name, + borrower, + protocol_market, + t0.token_address, + t0.token_symbol, + amount_unadj, + amount, + ROUND( + amount * p.price, + 2 + ) AS amount_usd_heal, + platform, + t0.blockchain, + t0._LOG_ID, + t0._inserted_timestamp + FROM + {{ this }} + t0 + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON t0.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + WHERE + CONCAT( + t0.block_number, + '-', + t0.platform + ) IN ( + SELECT + CONCAT( + t1.block_number, + '-', + t1.platform + ) + FROM + {{ this }} + t1 + WHERE + t1.amount_usd IS NULL + AND t1._inserted_timestamp < ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) + AND EXISTS ( + SELECT + 1 + FROM + {{ ref('silver__complete_token_prices') }} + p + WHERE + p._inserted_timestamp > DATEADD('DAY', -14, SYSDATE()) + AND p.price IS NOT NULL + AND p.token_address = t1.token_address + AND p.hour = DATE_TRUNC( + 'hour', + t1.block_timestamp + ) + ) + GROUP BY + 1 + ) +), +{% endif %} + +FINAL AS ( + SELECT + * + FROM + complete_lending_borrows + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +UNION ALL +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + event_name, + borrower, + protocol_market, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd_heal AS amount_usd, + platform, + blockchain, + _LOG_ID, + _inserted_timestamp +FROM + heal_model +{% endif %} +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key(['_log_id']) }} AS complete_lending_borrows_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml new file mode 100644 index 0000000..0772a1a --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_borrows.yml @@ -0,0 +1,14 @@ +version: 2 +models: + - name: silver__complete_lending_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql new file mode 100644 index 0000000..36bbb07 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.sql @@ -0,0 +1,294 @@ +-- depends_on: {{ ref('silver__complete_token_prices') }} +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE','platform'], + post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, origin_from_address, origin_to_address, origin_function_signature, contract_address, event_name, token_address, token_symbol, depositor, protocol_market), SUBSTRING(origin_function_signature, event_name, token_address, token_symbol, depositor, protocol_market)", + tags = ['reorg','curated','heal'] +) }} + +with init AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_address AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__init_deposits') }} + +{% if is_incremental() and 'init' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +juice AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_address AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__juice_deposits') }} + +{% if is_incremental() and 'juice' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +orbit AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier AS depositor_address, + token_address AS protocol_market, + supplied_contract_address AS token_address, + supplied_symbol AS token_symbol, + amount_unadj, + amount, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__orbit_deposits') }} + +{% if is_incremental() and 'orbit' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +deposit_union AS ( + SELECT + * + FROM + init + UNION ALL + SELECT + * + FROM + juice + UNION ALL + SELECT + * + FROM + orbit +), +complete_lending_deposits AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'INIT Capital' THEN 'Collateralize' + WHEN platform = 'Orbit' THEN 'Mint' + ELSE 'Deposit' + END AS event_name, + protocol_market, + depositor_address AS depositor, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + A.blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + deposit_union A + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour +), + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +heal_model AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + t0.contract_address, + event_name, + protocol_market, + depositor, + t0.token_address, + t0.token_symbol, + amount_unadj, + amount, + ROUND( + amount * p.price, + 2 + ) AS amount_usd_heal, + platform, + t0.blockchain, + t0._LOG_ID, + t0._INSERTED_TIMESTAMP + FROM + {{ this }} + t0 + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON t0.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + WHERE + CONCAT( + t0.block_number, + '-', + t0.platform + ) IN ( + SELECT + CONCAT( + t1.block_number, + '-', + t1.platform + ) + FROM + {{ this }} + t1 + WHERE + t1.amount_usd IS NULL + AND t1._inserted_timestamp < ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) + AND EXISTS ( + SELECT + 1 + FROM + {{ ref('silver__complete_token_prices') }} + p + WHERE + p._inserted_timestamp > DATEADD('DAY', -14, SYSDATE()) + AND p.price IS NOT NULL + AND p.token_address = t1.token_address + AND p.hour = DATE_TRUNC( + 'hour', + t1.block_timestamp + ) + ) + GROUP BY + 1 + ) +), +{% endif %} + +FINAL AS ( + SELECT + * + FROM + complete_lending_deposits + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +UNION ALL +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + event_name, + protocol_market, + depositor, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd_heal AS amount_usd, + platform, + blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP +FROM + heal_model +{% endif %} +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key(['_log_id']) }} AS complete_lending_deposits_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml new file mode 100644 index 0000000..a6f120a --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_deposits.yml @@ -0,0 +1,14 @@ +version: 2 +models: + - name: silver__complete_lending_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql new file mode 100644 index 0000000..8c008a0 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql @@ -0,0 +1,332 @@ +-- depends_on: {{ ref('silver__complete_token_prices') }} +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE','platform'], + post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, origin_from_address, origin_to_address, origin_function_signature, contract_address, event_name, liquidator, borrower, collateral_token, collateral_token_symbol, debt_token, debt_token_symbol, protocol_market), SUBSTRING(origin_function_signature, event_name, liquidator, borrower, collateral_token, collateral_token_symbol, debt_token, debt_token_symbol, protocol_market)", + tags = ['reorg','curated','heal'] +) }} + +WITH init AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + debt_token AS debt_asset, + debt_token_symbol AS debt_asset_symbol, + collateral_token AS collateral_asset, + collateral_token_symbol AS collateral_asset_symbol, + platform, + 'blast' AS blockchain, + l._LOG_ID, + l.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__init_liquidations') }} + l + +{% if is_incremental() and 'init' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +orbit AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + debt_token AS debt_asset, + debt_token_symbol AS debt_asset_symbol, + collateral_token AS collateral_asset, + collateral_token_symbol AS collateral_asset_symbol, + platform, + 'blast' AS blockchain, + l._LOG_ID, + l.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__orbit_liquidations') }} + l + +{% if is_incremental() and 'orbit' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +juice AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + liquidator, + borrower, + amount_unadj, + amount AS liquidated_amount, + NULL AS liquidated_amount_usd, + token AS protocol_collateral_asset, + debt_token AS debt_asset, + debt_token_symbol AS debt_asset_symbol, + collateral_token AS collateral_asset, + collateral_token_symbol AS collateral_asset_symbol, + platform, + 'blast' AS blockchain, + l._LOG_ID, + l.modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__juice_liquidations') }} + l + +{% if is_incremental() and 'juice' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), + +liquidation_union AS ( + SELECT + * + FROM + init + UNION ALL + SELECT + * + FROM + orbit + UNION ALL + SELECT + * + FROM + juice +), +complete_lending_liquidations AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'Orbit' THEN 'LiquidateBorrow' + WHEN platform = 'INIT Capital' THEN 'Liquidate' + WHEN platform = 'Juice' THEN 'CollateralLiquidation' + ELSE NULL + END AS event_name, + liquidator, + borrower, + protocol_collateral_asset AS protocol_market, + collateral_asset AS collateral_token, + collateral_asset_symbol AS collateral_token_symbol, + amount_unadj, + liquidated_amount AS amount, + CASE + WHEN platform <> 'Compound V3' THEN ROUND( + liquidated_amount * p.price, + 2 + ) + ELSE ROUND( + liquidated_amount_usd, + 2 + ) + END AS amount_usd, + debt_asset AS debt_token, + debt_asset_symbol AS debt_token_symbol, + platform, + A.blockchain, + A._LOG_ID, + A._inserted_timestamp + FROM + liquidation_union A + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON collateral_asset = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour +), + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +heal_model AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + t0.contract_address, + event_name, + liquidator, + borrower, + protocol_market, + collateral_token, + collateral_token_symbol, + amount_unadj, + amount, + CASE + WHEN platform <> 'Compound V3' THEN ROUND( + amount * p.price, + 2 + ) + ELSE ROUND( + amount_usd, + 2 + ) + END AS amount_usd_heal, + debt_token, + debt_token_symbol, + platform, + t0.blockchain, + t0._LOG_ID, + t0._inserted_timestamp + FROM + {{ this }} + t0 + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON t0.collateral_token = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + WHERE + CONCAT( + t0.block_number, + '-', + t0.platform + ) IN ( + SELECT + CONCAT( + t1.block_number, + '-', + t1.platform + ) + FROM + {{ this }} + t1 + WHERE + t1.amount_usd IS NULL + AND t1._inserted_timestamp < ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) + AND EXISTS ( + SELECT + 1 + FROM + {{ ref('silver__complete_token_prices') }} + p + WHERE + p._inserted_timestamp > DATEADD('DAY', -14, SYSDATE()) + AND p.price IS NOT NULL + AND p.token_address = t1.collateral_token + AND p.hour = DATE_TRUNC( + 'hour', + t1.block_timestamp + ) + ) + GROUP BY + 1 + ) +), +{% endif %} + +FINAL AS ( + SELECT + * + FROM + complete_lending_liquidations + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +UNION ALL +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + event_name, + liquidator, + borrower, + protocol_market, + collateral_token, + collateral_token_symbol, + amount_unadj, + amount, + amount_usd_heal AS amount_usd, + debt_token, + debt_token_symbol, + platform, + blockchain, + _LOG_ID, + _inserted_timestamp +FROM + heal_model +{% endif %} +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key(['_log_id']) }} AS complete_lending_liquidations_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml new file mode 100644 index 0000000..5f793d0 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.yml @@ -0,0 +1,14 @@ +version: 2 +models: + - name: silver__complete_lending_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql new file mode 100644 index 0000000..a193243 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.sql @@ -0,0 +1,300 @@ +-- depends_on: {{ ref('silver__complete_token_prices') }} +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE','platform'], + post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, origin_from_address, origin_to_address, origin_function_signature, contract_address, event_name, token_address, token_symbol, payer, borrower, protocol_market), SUBSTRING(origin_function_signature, event_name, token_address, token_symbol, payer, borrower, protocol_market)", + tags = ['reorg','curated','heal'] +) }} + +WITH init AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + origin_from_address AS payer_address, + borrower, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__init_repayments') }} + +{% if is_incremental() and 'init' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +juice AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + origin_from_address AS payer_address, + borrower, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__juice_repayments') }} + +{% if is_incremental() and 'juice' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +orbit AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + repay_contract_address AS token_address, + token_address AS protocol_market, + amount_unadj, + amount, + repay_contract_symbol AS token_symbol, + origin_from_address AS payer_address, + borrower, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__orbit_repayments') }} + +{% if is_incremental() and 'orbit' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), + +repayments_union AS ( + SELECT + * + FROM + init + UNION ALL + SELECT + * + FROM + juice + UNION ALL + SELECT + * + FROM + orbit +), +complete_lending_repayments AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'Orbit' THEN 'RepayBorrow' + ELSE 'Repay' + END AS event_name, + protocol_market, + payer_address AS payer, + borrower, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + A.blockchain, + A._LOG_ID, + A._INSERTED_TIMESTAMP + FROM + repayments_union A + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour +), + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +heal_model AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + t0.contract_address, + event_name, + protocol_market, + payer, + borrower, + t0.token_address, + t0.token_symbol, + amount_unadj, + amount, + ROUND( + amount * p.price, + 2 + ) AS amount_usd_heal, + platform, + t0.blockchain, + t0._LOG_ID, + t0._INSERTED_TIMESTAMP + FROM + {{ this }} + t0 + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON t0.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + WHERE + CONCAT( + t0.block_number, + '-', + t0.platform + ) IN ( + SELECT + CONCAT( + t1.block_number, + '-', + t1.platform + ) + FROM + {{ this }} + t1 + WHERE + t1.amount_usd IS NULL + AND t1._inserted_timestamp < ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) + AND EXISTS ( + SELECT + 1 + FROM + {{ ref('silver__complete_token_prices') }} + p + WHERE + p._inserted_timestamp > DATEADD('DAY', -14, SYSDATE()) + AND p.price IS NOT NULL + AND p.token_address = t1.token_address + AND p.hour = DATE_TRUNC( + 'hour', + t1.block_timestamp + ) + ) + GROUP BY + 1 + ) +), +{% endif %} + +FINAL AS ( + SELECT + * + FROM + complete_lending_repayments + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +UNION ALL +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + event_name, + protocol_market, + payer, + borrower, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd_heal AS amount_usd, + platform, + blockchain, + _LOG_ID, + _INSERTED_TIMESTAMP +FROM + heal_model +{% endif %} +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key(['_log_id']) }} AS complete_lending_repayments_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml new file mode 100644 index 0000000..6f3b8a8 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_repayments.yml @@ -0,0 +1,58 @@ +version: 2 +models: + - name: silver__complete_lending_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: TX_HASH + tests: + - not_null + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 + - name: EVENT_INDEX + tests: + - not_null + - name: PROTOCOL_MARKET + tests: + - not_null + - name: PAYER + tests: + - not_null: + where: PLATFORM <> 'Silo' + - name: BORROWER + tests: + - not_null + - name: TOKEN_ADDRESS + tests: + - not_null + - name: TOKEN_SYMBOL + - name: AMOUNT_UNADJ + tests: + - not_null + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_USD + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: PLATFORM + tests: + - not_null + - name: BLOCKCHAIN + tests: + - not_null \ No newline at end of file diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql new file mode 100644 index 0000000..4459e53 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.sql @@ -0,0 +1,301 @@ +-- depends_on: {{ ref('silver__complete_token_prices') }} +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = ['block_number','platform'], + cluster_by = ['block_timestamp::DATE','platform'], + post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(tx_hash, origin_from_address, origin_to_address, origin_function_signature, contract_address, event_name, token_address, token_symbol, depositor, protocol_market), SUBSTRING(origin_function_signature, event_name, token_address, token_symbol, depositor, protocol_market)", + tags = ['reorg','curated','heal'] +) }} + +WITH init AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_symbol AS token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__init_withdraws') }} + +{% if is_incremental() and 'init' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +orbit AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_symbol AS token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__orbit_withdraws') }} + +{% if is_incremental() and 'orbit' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +juice AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address AS protocol_market, + received_contract_address AS token_address, + received_symbol AS token_symbol, + amount_unadj, + amount, + redeemer AS depositor_address, + platform, + 'blast' AS blockchain, + _LOG_ID, + modified_timestamp as _inserted_timestamp + FROM + {{ ref('silver__juice_withdraws') }} + +{% if is_incremental() and 'juice' not in var('HEAL_MODELS') %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) +{% endif %} +), +withdraws_union AS ( + SELECT + * + FROM + init + UNION ALL + SELECT + * + FROM + orbit + UNION ALL + SELECT + * + FROM + juice +), +complete_lending_withdraws AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + A.contract_address, + CASE + WHEN platform = 'Orbit' THEN 'Mint' + WHEN platform = 'INIT Capital' THEN 'Decollateralize' + WHEN platform = 'Juice' THEN 'CollateralWithdrawal' + ELSE NULL + END AS event_name, + protocol_market, + depositor_address AS depositor, + A.token_address, + A.token_symbol, + amount_unadj, + amount, + ROUND( + amount * price, + 2 + ) AS amount_usd, + platform, + A.blockchain, + A._log_id, + A._inserted_timestamp + FROM + withdraws_union A + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON A.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour +), + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +heal_model AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + t0.contract_address, + event_name, + protocol_market, + depositor, + t0.token_address, + t0.token_symbol, + amount_unadj, + amount, + ROUND( + amount * p.price, + 2 + ) AS amount_usd_heal, + platform, + t0.blockchain, + t0._log_id, + t0._inserted_timestamp + FROM + {{ this }} + t0 + LEFT JOIN {{ ref('price__ez_prices_hourly') }} + p + ON t0.token_address = p.token_address + AND DATE_TRUNC( + 'hour', + block_timestamp + ) = p.hour + WHERE + CONCAT( + t0.block_number, + '-', + t0.platform + ) IN ( + SELECT + CONCAT( + t1.block_number, + '-', + t1.platform + ) + FROM + {{ this }} + t1 + WHERE + t1.amount_usd IS NULL + AND t1._inserted_timestamp < ( + SELECT + MAX( + _inserted_timestamp + ) - INTERVAL '{{ var("LOOKBACK", "4 hours") }}' + FROM + {{ this }} + ) + AND EXISTS ( + SELECT + 1 + FROM + {{ ref('silver__complete_token_prices') }} + p + WHERE + p._inserted_timestamp > DATEADD('DAY', -14, SYSDATE()) + AND p.price IS NOT NULL + AND p.token_address = t1.token_address + AND p.hour = DATE_TRUNC( + 'hour', + t1.block_timestamp + ) + ) + GROUP BY + 1 + ) +), +{% endif %} + +FINAL AS ( + SELECT + * + FROM + complete_lending_withdraws + +{% if is_incremental() and var( + 'HEAL_MODEL' +) %} +UNION ALL +SELECT + tx_hash, + block_number, + block_timestamp, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + event_name, + protocol_market, + depositor, + token_address, + token_symbol, + amount_unadj, + amount, + amount_usd_heal AS amount_usd, + platform, + blockchain, + _log_id, + _inserted_timestamp +FROM + heal_model +{% endif %} +) +SELECT + *, + {{ dbt_utils.generate_surrogate_key(['_log_id']) }} AS complete_lending_withdraws_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + FINAL qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml new file mode 100644 index 0000000..140f091 --- /dev/null +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_withdraws.yml @@ -0,0 +1,14 @@ +version: 2 +models: + - name: silver__complete_lending_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 30 \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_asset_details.sql b/models/silver/defi/lending/init/silver__init_asset_details.sql new file mode 100644 index 0000000..9f177c1 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_asset_details.sql @@ -0,0 +1,118 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH blast_contracts AS ( + + SELECT + address, + symbol, + NAME, + decimals, + modified_timestamp + FROM + {{ ref('core__dim_contracts') }} + WHERE + NAME LIKE 'INIT%' + {% if is_incremental() %} + AND modified_timestamp > ( + SELECT + max(modified_timestamp) + FROM + {{ this }} + ) + AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' + {% endif %} +), +contracts AS ( + + SELECT + address, + symbol, + NAME, + decimals, + modified_timestamp + FROM + {{ ref('core__dim_contracts') }} +), +underlying AS ( + SELECT + tx_hash, + block_timestamp, + block_number, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + from_address AS token_address, + to_address AS underlying_asset_address + FROM + {{ ref('core__fact_traces') }} + WHERE + identifier = 'CALL_0_1' + AND LEFT( + input, + 10 + ) = '0x095ea7b3' + AND trace_succeeded + AND from_address IN ( + SELECT + address + FROM + blast_contracts + ) +), +unwrapped AS ( + SELECT + from_address AS underlying_asset_address, + to_address AS underlying_unwrap_address, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded + FROM + {{ ref('core__fact_traces') }} + WHERE + identifier = 'CALL_0_0' + AND LEFT( + input, + 10 + ) = '0x1a33757d' + AND trace_succeeded + AND from_address IN ( + SELECT + underlying_asset_address + FROM + underlying + ) +) +SELECT + A.block_timestamp, + A.block_number, + A.tx_hash, + token_address, + b.name AS token_name, + b.symbol AS token_symbol, + b.decimals AS token_decimals, + underlying_asset_address, + C.name AS underlying_name, + C.symbol AS underlying_symbol, + C.decimals AS underlying_decimals, + d.underlying_unwrap_address, + e.name AS underlying_unwrap_name, + e.symbol AS underlying_unwrap_symbol, + e.decimals AS underlying_unwrap_decimals, + b.modified_timestamp +FROM + underlying A + INNER JOIN blast_contracts b + ON b.address = token_address + LEFT JOIN unwrapped d USING(underlying_asset_address) + INNER JOIN contracts C + ON C.address = underlying_asset_address + LEFT JOIN contracts e + ON e.address = underlying_unwrap_address \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_asset_details.yml b/models/silver/defi/lending/init/silver__init_asset_details.yml new file mode 100644 index 0000000..693934a --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_asset_details.yml @@ -0,0 +1,12 @@ +version: 2 +models: + - name: silver__init_asset_details + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TOKEN_ADDRESS + + + + + diff --git a/models/silver/defi/lending/init/silver__init_borrows.sql b/models/silver/defi/lending/init/silver__init_borrows.sql new file mode 100644 index 0000000..86c81c4 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_borrows.sql @@ -0,0 +1,224 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__init_asset_details') }} +), +init_borrows AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_from_address AS borrower, + origin_function_signature, + contract_address, + topics, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + topics [2] :: STRING + ) :: FLOAT AS posId, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS pool, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: FLOAT AS loan_amount_raw, + -- receipt token + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: FLOAT AS sharesAmt, + -- receipt token + contract_address AS token, + 'INIT Capital' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0x49dd87b26edb1c92c93f83b092bd5a425c6bf7a562c0fed02f2576c49f477ba4' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +token_transfer AS ( + SELECT + tx_hash, + contract_address, + from_address, + to_address, + raw_amount AS underlying_amount_raw, + token_decimals, + token_symbol, + token_name + FROM + {{ ref('core__fact_token_transfers') }} + LEFT JOIN {{ ref('silver__contracts') }} USING(contract_address) + WHERE + contract_address IN ( + '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad', + '0x4300000000000000000000000000000000000003', + '0x4300000000000000000000000000000000000004' + ) + AND ( + from_address IN ( + SELECT + token_address + FROM + asset_details + ) -- for Blast + OR from_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + ) + ) -- to get USDB from WUSDB or WETH FROM WWETH + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_borrows + ) +), +native_transfer AS ( + SELECT + tx_hash, + from_address AS wrapped_address, + to_address, + value_precise_raw AS eth_value, + 'WETH' AS eth_symbol, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + 18 AS eth_decimals, + '0x4300000000000000000000000000000000000004' AS eth_address + FROM + {{ ref('core__fact_traces') }} + WHERE + from_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_borrows + ) + AND TYPE = 'CALL' + AND trace_succeeded + AND input = '0x' +), +init_combine AS ( + SELECT + block_number, + block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + b.contract_address, + borrower, + pool, + loan_amount_raw, + C.underlying_decimals AS underlying_wrap_decimals, + COALESCE( + eth_value, + underlying_amount_raw + ) AS underlying_amount_raw, + COALESCE( + eth_symbol, + d.token_symbol, + C.underlying_symbol + ) AS underlying_symbol, + COALESCE( + eth_decimals, + d.token_decimals, + C.underlying_decimals + ) AS underlying_decimals, + COALESCE( + eth_address, + C.underlying_asset_address + ) AS underlying_asset_address, + underlying_asset_address AS borrows_contract_address, + underlying_symbol AS borrows_symbol, + b.contract_address AS token, + C.token_symbol, + C.token_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + init_borrows b + LEFT JOIN asset_details C + ON b.pool = C.token_address + LEFT JOIN token_transfer d + ON b.tx_hash = d.tx_hash + LEFT JOIN native_transfer e + ON b.tx_hash = e.tx_hash +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address, + borrows_symbol, + token AS token_address, + token_symbol, + underlying_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_wrap_decimals + ) AS amount, + underlying_amount_raw, + underlying_amount_raw / pow( + 10, + underlying_decimals + ) AS underlyingAmount, + platform, + modified_timestamp, + _log_id +FROM + init_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/init/silver__init_borrows.yml b/models/silver/defi/lending/init/silver__init_borrows.yml new file mode 100644 index 0000000..ed2dfc0 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_borrows.yml @@ -0,0 +1,105 @@ +version: 2 +models: + - name: silver__init_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: UNDERLYING_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: UNDERLYING_AMOUNT_RAW + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: UNDERLYING_AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_deposits.sql b/models/silver/defi/lending/init/silver__init_deposits.sql new file mode 100644 index 0000000..1722679 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_deposits.sql @@ -0,0 +1,323 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + block_timestamp, + block_number, + tx_hash, + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals, + underlying_unwrap_address, + underlying_unwrap_name, + underlying_unwrap_symbol, + underlying_unwrap_decimals, + modified_timestamp + FROM + {{ ref('silver__init_asset_details') }} +), +init_deposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + topics [1] :: STRING + ) :: FLOAT AS posId, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS pool, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: FLOAT AS minttokens_raw, + -- receipt token + 'INIT Capital' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0x722732c12c1c1ba3942aef8ee6e0357b01908558e142501c5f85b356c4dcadf8' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +token_transfer1 AS ( + SELECT + t1.tx_hash, + t2.contract_address, + t1.from_address, + t1.to_address, + t1.raw_amount, + t3.token_symbol, + t3.token_decimals, + t3.token_name, + t2.from_address AS from_address2, + t2.to_address AS to_address2, + t2.raw_amount AS base_amount + FROM + {{ ref('core__fact_token_transfers') }} + t1 + LEFT JOIN {{ ref('core__fact_token_transfers') }} + t2 + ON t1.tx_hash = t2.tx_hash + AND t1.contract_address = t2.to_address + LEFT JOIN {{ ref('silver__contracts') }} + t3 + ON t2.contract_address = t3.contract_address + WHERE + t1.contract_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + WHERE + underlying_unwrap_address IS NOT NULL + ) + AND t1.tx_hash IN ( + SELECT + tx_hash + FROM + init_deposits + ) + AND t1.to_address IN ( + SELECT + token_address + FROM + asset_details + ) -- for Blast + AND t2.contract_address IN ( + SELECT + underlying_unwrap_address + FROM + asset_details + ) + AND ( + t2.to_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + ) + ) +), +token_transfer2 AS ( + SELECT + t1.tx_hash, + t1.contract_address, + t1.from_address, + t1.to_address, + t1.raw_amount, + t3.token_symbol, + t3.token_decimals, + t3.token_name, + NULL AS from_address2, + NULL AS to_address2, + NULL AS base_amount, + NULL AS base_decimals, + NULL AS base_symbol, + NULL AS base_name + FROM + {{ ref('core__fact_token_transfers') }} + t1 + LEFT JOIN {{ ref('silver__contracts') }} + t3 + ON t1.contract_address = t3.contract_address + WHERE + 1 = 1 + AND t1.contract_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + WHERE + underlying_unwrap_address IS NULL + ) + AND t1.tx_hash IN ( + SELECT + tx_hash + FROM + init_deposits + ) + AND t1.to_address IN ( + SELECT + token_address + FROM + asset_details + ) +), +token_transfer AS ( + SELECT + tx_hash, + contract_address, + token_decimals, + token_symbol, + token_name, + COALESCE( + base_amount, + raw_amount + ) AS raw_amount, + to_address + FROM + ( + SELECT + tx_hash, + contract_address, + base_amount, + raw_amount, + to_address, + token_name, + token_symbol, + token_decimals + FROM + token_transfer1 + UNION ALL + SELECT + tx_hash, + contract_address, + base_amount, + raw_amount, + to_address, + token_name, + token_symbol, + token_decimals + FROM + token_transfer2 + ) +), +native_transfer AS ( + SELECT + tx_hash, + from_address AS wrapped_address, + to_address, + value_precise_raw AS eth_value, + 'WETH' AS eth_symbol, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + 18 AS eth_decimals, + '0x4300000000000000000000000000000000000004' AS eth_address + FROM + {{ ref('core__fact_traces') }} + WHERE + to_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') -- hard code wweth contract here + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_deposits + ) + AND TYPE = 'CALL' + AND trace_succeeded + AND input = '0x6ad481f3' +), +init_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_to_address, + origin_from_address, + origin_from_address AS supplier, + origin_function_signature, + b.contract_address, + pool, + minttokens_raw, + COALESCE( + eth_value, + raw_amount + ) AS mintAmount_raw, + COALESCE( + eth_decimals, + d.token_decimals + ) AS supplied_decimals, + COALESCE( + eth_symbol, + d.token_symbol + ) AS supplied_symbol, + COALESCE( + eth_address, + d.contract_address + ) AS supplied_contract_address, + C.token_address, + C.token_symbol, + C.token_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + init_deposits b + LEFT JOIN asset_details C + ON b.pool = C.token_address + LEFT JOIN token_transfer d + ON b.tx_hash = d.tx_hash + AND b.pool = d.to_address + LEFT JOIN native_transfer e + ON b.tx_hash = e.tx_hash +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_to_address, + origin_from_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + minttokens_raw / pow( + 10, + token_decimals + ) AS issued_tokens, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + supplied_decimals + ) AS amount, + supplied_contract_address, + supplied_symbol, + supplier, + platform, + modified_timestamp, + _log_id +FROM + init_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/init/silver__init_deposits.yml b/models/silver/defi/lending/init/silver__init_deposits.yml new file mode 100644 index 0000000..2a38643 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_deposits.yml @@ -0,0 +1,110 @@ +version: 2 +models: + - name: silver__init_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_liquidations.sql b/models/silver/defi/lending/init/silver__init_liquidations.sql new file mode 100644 index 0000000..02cec83 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_liquidations.sql @@ -0,0 +1,187 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + block_timestamp, + block_number, + tx_hash, + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals, + underlying_unwrap_address, + underlying_unwrap_name, + underlying_unwrap_symbol, + underlying_unwrap_decimals, + modified_timestamp + FROM + {{ ref('silver__init_asset_details') }} +), +init_liquidations AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + topics, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + topics [1] :: STRING + ) :: FLOAT AS posId, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS liquidator, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS poolOut, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: FLOAT AS sharesamt, + contract_address AS token, + 'INIT Capital' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0x6df71caf4cddb1620bcf376243248e0077da98913d65a7e9315bc9984e5fff72' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +liquidation_union AS ( + SELECT + l.block_number, + l.block_timestamp, + l.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + l.contract_address, + posId, + C.token_symbol AS collateral_token_symbol, + poolOut AS collateral_token, + liquidator, + l.sharesamt AS tokens_seized_raw, + l.sharesamt / pow( + 10, + C.token_decimals + ) AS tokens_seized, + -- in tokens + C.underlying_decimals, + l.platform, + l.modified_timestamp, + l._log_id + FROM + init_liquidations l + LEFT JOIN asset_details C + ON l.poolOut = C.token_address +), +init_repayment AS ( + SELECT + tx_hash, + protocol_market, + token_address AS debt_token, + token_symbol AS debt_token_symbol, + posId, + amount_unadj, + amount + FROM + {{ ref('silver__init_repayments') }} + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + init_liquidations + ) +), +position_owner AS ( + SELECT + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS borrower, + utils.udf_hex_to_int( + topics [2] :: STRING + ) :: STRING AS posId, -- using string as it handles better than float + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0xe6a96441ecc85d0943a914f4750f067a912798ec2543bc68c00e18291da88d14' -- createposition + AND tx_succeeded + AND posId IN ( + SELECT + posId + FROM + init_liquidations + ) +) +SELECT + l.block_number, + l.block_timestamp, + l.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + l.contract_address, + l.contract_address AS token, + liquidator, + borrower, + protocol_market, + collateral_token, + collateral_token_symbol, + tokens_seized_raw AS amount_unadj, + tokens_seized AS amount, + debt_token, + debt_token_symbol, + platform, + modified_timestamp, + l._log_id +FROM + liquidation_union l + LEFT JOIN position_owner po + ON l.posId = po.posId + LEFT JOIN init_repayment r + ON l.posId = r.posId + AND l.tx_hash = r.tx_hash qualify(ROW_NUMBER() over(PARTITION BY l._log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/init/silver__init_liquidations.yml b/models/silver/defi/lending/init/silver__init_liquidations.yml new file mode 100644 index 0000000..52be4b1 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_liquidations.yml @@ -0,0 +1,119 @@ +version: 2 +models: + - name: silver__init_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">14" + error_if: ">30" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: LIQUIDATOR + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEBT_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: DEBT_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_repayments.sql b/models/silver/defi/lending/init/silver__init_repayments.sql new file mode 100644 index 0000000..6140cb3 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_repayments.sql @@ -0,0 +1,228 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals, + underlying_unwrap_address, + underlying_unwrap_name, + underlying_unwrap_symbol, + underlying_unwrap_decimals + FROM + {{ ref('silver__init_asset_details') }} +), +init_repayments AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_from_address AS borrower, + origin_function_signature, + contract_address, + topics, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + topics [2] :: STRING + ) :: FLOAT AS posId, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS pool, + CONCAT('0x', SUBSTR(topics [3] :: STRING, 27, 40)) AS repayer, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: FLOAT AS sharesAmt, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: FLOAT AS amtToRepay, + contract_address AS token, + 'INIT Capital' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0x77673b670822baca14a7caf6f8038f811649ab73e4c06083b0e58a53389bece7' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +native_transfer AS ( + SELECT + tx_hash, + from_address, + to_address AS wrapped_address, + value_precise_raw AS eth_value, + 'WETH' AS eth_symbol, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + 18 AS eth_decimals, + '0x4300000000000000000000000000000000000004' AS eth_address + FROM + {{ ref('core__fact_traces') }} + WHERE + to_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') -- hard code wweth contract here + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_repayments + ) + AND TYPE = 'CALL' + AND trace_succeeded + AND input = '0x6ad481f3' +), +token_transfer AS ( + SELECT + tx_hash, + contract_address, + from_address, + to_address, + raw_amount AS underlying_amount_raw, + token_decimals, + token_symbol, + token_name + FROM + {{ ref('core__fact_token_transfers') }} + LEFT JOIN {{ ref('silver__contracts') }} USING(contract_address) + WHERE + contract_address IN ( + '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad', + '0x4300000000000000000000000000000000000003', + '0x4300000000000000000000000000000000000004' + ) + AND ( + to_address IN ( + SELECT + token_address + FROM + asset_details + ) + OR to_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + ) + ) + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_repayments + ) +), +init_combine AS ( + SELECT + block_number, + block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + posId, + b.contract_address, + b.pool AS protocol_market, + borrower, + repayer AS payer, + amttorepay, + sharesAmt, + C.underlying_asset_address AS repay_contract_address, + C.underlying_symbol AS repay_contract_symbol, + underlying_decimals AS underlying_wrap_decimals, + COALESCE( + eth_value, + underlying_amount_raw + ) AS underlying_amount_raw, + COALESCE( + eth_symbol, + d.token_symbol, + C.underlying_symbol + ) AS underlying_symbol, + COALESCE( + eth_decimals, + d.token_decimals, + C.underlying_decimals + ) AS underlying_decimals, + COALESCE( + eth_address, + d.contract_address, + C.underlying_asset_address + ) AS underlying_asset_address, + b.platform, + b._log_id, + b.modified_timestamp + FROM + init_repayments b + LEFT JOIN asset_details C + ON b.pool = C.token_address + LEFT JOIN native_transfer USING(tx_hash) + LEFT JOIN token_transfer d + ON b.tx_hash = d.tx_hash +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + posId, + contract_address, + borrower, + protocol_market, + payer, + repay_contract_address, + repay_contract_symbol, + underlying_asset_address AS token_address, + underlying_symbol AS token_symbol, + amtToRepay AS amount_unadj, + amttorepay / pow( + 10, + underlying_wrap_decimals + ) AS total_repaid, + amtToRepay / pow( + 10, + underlying_decimals + ) AS amount, + platform, + modified_timestamp, + _log_id +FROM + init_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/init/silver__init_repayments.yml b/models/silver/defi/lending/init/silver__init_repayments.yml new file mode 100644 index 0000000..26e38cd --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_repayments.yml @@ -0,0 +1,105 @@ +version: 2 +models: + - name: silver__init_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PAYER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOTAL_REPAID + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/init/silver__init_withdraws.sql b/models/silver/defi/lending/init/silver__init_withdraws.sql new file mode 100644 index 0000000..6120660 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_withdraws.sql @@ -0,0 +1,333 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + block_timestamp, + block_number, + tx_hash, + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals, + underlying_unwrap_address, + underlying_unwrap_name, + underlying_unwrap_symbol, + underlying_unwrap_decimals, + modified_timestamp + FROM + {{ ref('silver__init_asset_details') }} +), +init_redemption AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + topics [1] :: STRING + ) :: FLOAT AS posId, + CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS pool, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: FLOAT AS redeemed_token_raw, + 'INIT Capital' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0x09c2e7b3728acfd99b3f71e4c1a55bcd48019bcc0e45c741f7c2f3393f49ea91' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +token_transfer1 AS ( + SELECT + t1.tx_hash, + t2.contract_address, + t1.from_address, + t1.to_address, + t1.raw_amount, + t3.token_symbol, + t3.token_decimals, + t3.token_name, + t2.from_address AS from_address2, + t2.to_address AS to_address2, + t2.raw_amount AS base_amount + FROM + {{ ref('core__fact_token_transfers') }} + t1 + LEFT JOIN {{ ref('core__fact_token_transfers') }} + t2 + ON t1.tx_hash = t2.tx_hash + AND t1.contract_address = t2.from_address + LEFT JOIN {{ ref('silver__contracts') }} + t3 + ON t2.contract_address = t3.contract_address + WHERE + 1 = 1 + AND t1.contract_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + WHERE + underlying_unwrap_address IS NOT NULL + ) + AND t1.tx_hash IN ( + SELECT + tx_hash + FROM + init_redemption + ) + AND t1.from_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND t2.contract_address IN ( + SELECT + underlying_unwrap_address + FROM + asset_details + ) + AND ( + t2.from_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + ) + ) +), +token_transfer2 AS ( + SELECT + t1.tx_hash, + t1.contract_address, + t1.from_address, + t1.to_address, + t1.raw_amount, + t3.token_symbol, + t3.token_decimals, + t3.token_name, + NULL AS from_address2, + NULL AS to_address2, + NULL AS base_amount, + NULL AS base_decimals, + NULL AS base_symbol, + NULL AS base_name + FROM + {{ ref('core__fact_token_transfers') }} + t1 + LEFT JOIN {{ ref('silver__contracts') }} + t3 + ON t1.contract_address = t3.contract_address + WHERE + 1 = 1 + AND t1.contract_address IN ( + SELECT + underlying_asset_address + FROM + asset_details + WHERE + underlying_unwrap_address IS NULL + ) + AND t1.tx_hash IN ( + SELECT + tx_hash + FROM + init_redemption + ) {# AND t1.tx_hash NOT IN ( + SELECT + tx_hash + FROM + token_transfer1 +) #} --think we would want to remove this +AND t1.from_address IN ( + SELECT + token_address + FROM + asset_details +) +), +token_transfer AS ( + SELECT + tx_hash, + contract_address, + token_decimals, + token_symbol, + token_name, + COALESCE( + base_amount, + raw_amount + ) AS raw_amount, + from_address + FROM + ( + SELECT + tx_hash, + contract_address, + base_amount, + raw_amount, + from_address2, + from_address, + token_name, + token_symbol, + token_decimals + FROM + token_transfer1 + UNION ALL + SELECT + tx_hash, + contract_address, + base_amount, + raw_amount, + from_address2, + from_address, + token_name, + token_symbol, + token_decimals + FROM + token_transfer2 + ) +), +native_transfer AS ( + SELECT + tx_hash, + from_address AS wrapped_address, + to_address, + value_precise_raw AS eth_value, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + 'WETH' AS eth_symbol, + 18 AS eth_decimals, + '0x4300000000000000000000000000000000000004' AS eth_address + FROM + {{ ref('core__fact_traces') }} + WHERE + from_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') + AND tx_hash IN ( + SELECT + tx_hash + FROM + init_redemption + ) + AND TYPE = 'CALL' + AND trace_succeeded + AND input = '0x' +), +init_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_to_address AS redeemer, + origin_function_signature, + b.contract_address, + pool, + COALESCE( + eth_value, + raw_amount + ) AS received_amount_raw, + COALESCE( + eth_decimals, + d.token_decimals + ) AS received_decimals, + COALESCE( + eth_symbol, + d.token_symbol + ) AS received_symbol, + COALESCE( + eth_address, + d.contract_address + ) AS received_contract_address, + redeemed_token_raw, + C.token_address, + C.token_symbol, + C.token_decimals, + C.token_address AS redeemed_contract_address, + C.token_symbol AS redeemed_symbol, + C.token_decimals AS redeemed_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + init_redemption b + LEFT JOIN asset_details C + ON b.pool = C.token_address + LEFT JOIN token_transfer d + ON b.tx_hash = d.tx_hash + AND b.pool = d.from_address + LEFT JOIN native_transfer e + ON b.tx_hash = e.tx_hash +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + redeemer, + received_amount_raw AS amount_unadj, + received_amount_raw / pow( + 10, + received_decimals + ) AS amount, + received_contract_address, + received_symbol, + redeemed_token_raw / pow( + 10, + redeemed_decimals + ) AS redeemed_tokens, + platform, + modified_timestamp, + _log_id +FROM + init_combine ee qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/init/silver__init_withdraws.yml b/models/silver/defi/lending/init/silver__init_withdraws.yml new file mode 100644 index 0000000..1634046 --- /dev/null +++ b/models/silver/defi/lending/init/silver__init_withdraws.yml @@ -0,0 +1,95 @@ +version: 2 +models: + - name: silver__init_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: RECEIVED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_asset_details.sql b/models/silver/defi/lending/juice/silver__juice_asset_details.sql new file mode 100644 index 0000000..4a926ff --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_asset_details.sql @@ -0,0 +1,423 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_list AS ( + + SELECT + '0x44f33bc796f7d3df55040cd3c631628b560715c2' AS pool_address + UNION ALL + SELECT + '0x4a1d9220e11a47d8ab22ccd82da616740cf0920a' AS pool_address + UNION ALL + SELECT + '0x788654040d7e9a8bb583d7d8ccea1ebf1ae4ac06' AS pool_address + UNION ALL + SELECT + '0x60ed5493b35f833189406dfec0b631a6b5b57f66' AS pool_address +), +contracts AS ( + SELECT + address, + symbol, + NAME, + decimals + FROM + {{ ref('core__dim_contracts') }} +), +juice_contracts AS ( + SELECT + address, + symbol, + NAME, + decimals + FROM + contracts + WHERE + NAME LIKE 'Juice%Collateral%' +), +collateral_tokens AS ( + SELECT + '0x1d37383447ceceeedb7c92372d6993821d3d7b40' AS contract_address, + '0x211cc4dd073734da055fbf44a2b4667d5e5fe5d2' AS underlying_asset + UNION ALL + SELECT + '0x7e4afebe294345d72de6bb8405c871d7bb6c53d1' AS contract_address, + '0x04c0599ae5a44757c0af6f9ec3b93da8976c150a' AS underlying_asset + UNION ALL + SELECT + '0x295e17672f1290b66dd064ec6b7fdaf280b33cea' AS contract_address, + '0x5d3a1ff2b6bab83b63cd9ad0787074081a52ef34' AS underlying_asset + UNION ALL + SELECT + '0x0246937acacabe4e1b6045de9b68113d72966be2' AS contract_address, + '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad' AS underlying_asset + UNION ALL + SELECT + '0x2b1c36a733b1bab31f05ac8866d330e29c604b8f' AS contract_address, + '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad' AS underlying_asset + UNION ALL + SELECT + '0xc81a630806d1af3fd7509187e1afc501fd46e818' AS contract_address, + '0x2416092f143378750bb29b79ed961ab195cceea5' AS underlying_asset +), +tx_pull AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + event_index, + contract_address, + topics, + DATA, + event_removed, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + fact_event_logs_id, + inserted_timestamp, + modified_timestamp + FROM + {{ ref('core__fact_event_logs') }} + WHERE + origin_from_address = '0x0ee09b204ffebf9a1f14c99e242830a09958ba34' + AND origin_to_address = '0x4e59b44847b379578588920ca78fbf26c0b4956c' + AND CONCAT('0x', SUBSTR(topics [1], 27, 40)) IN ( + SELECT + pool_address + FROM + asset_list + ) + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +trace_pull AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + from_address, + to_address, + VALUE, + value_precise_raw, + value_precise, + gas, + gas_used, + input, + output, + TYPE, + identifier, + DATA, + tx_status, + sub_traces, + CASE + WHEN trace_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS trace_succeeded, + error_reason, + trace_index, + fact_traces_id, + inserted_timestamp, + modified_timestamp + FROM + {{ ref('core__fact_traces') }} + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + tx_pull + ) + AND identifier IN ( + 'CREATE_0_5', + 'CREATE_0_4' + ) +), +debt_token AS ( + SELECT + tx_hash, + block_timestamp, + block_number, + from_address AS pool_address, + to_address AS debt_address, + NAME AS debt_name, + decimals AS debt_decimals, + symbol AS debt_symbol, + l.modified_timestamp + FROM + trace_pull l + LEFT JOIN contracts + ON address = debt_address + WHERE + identifier = 'CREATE_0_4' +), +token AS ( + SELECT + tx_hash, + block_timestamp, + to_address AS token_address, + NAME AS token_name, + decimals AS token_decimals, + symbol AS token_symbol, + A.modified_timestamp + FROM + trace_pull A + LEFT JOIN contracts + ON address = token_address + WHERE + identifier = 'CREATE_0_5' +), +underlying AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + event_index, + contract_address, + topics, + DATA, + event_removed, + tx_succeeded, + _log_id, + fact_event_logs_id, + inserted_timestamp, + modified_timestamp + FROM + tx_pull + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + tx_pull + ) +), +underlying_asset AS ( + SELECT + tx_hash, + t2.contract_address AS underlying_asset_address, + CONCAT('0x', SUBSTR(topics [1], 27, 40)) AS contract, + NAME AS underlying_name, + decimals AS underlying_decimals, + symbol AS underlying_symbol, + t1.modified_timestamp, + t1._log_id + FROM + underlying t1 + INNER JOIN underlying t2 USING(tx_hash) + LEFT JOIN contracts t5 + ON underlying_asset_address = t5.address + WHERE + topics [1] IS NOT NULL + AND t2.contract_address != '0x2536fe9ab3f511540f2f9e2ec2a805005c3dd800' +), +logs_pull AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + event_index, + contract_address, + topics, + DATA, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [0], 25, 40)) AS contract_address, + event_removed, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + fact_event_logs_id, + inserted_timestamp, + modified_timestamp + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = LOWER('0x2536FE9ab3F511540F2f9e2eC2A805005C3Dd800') + AND topics [0] = '0x2da9afcf2ffbfd720263cc579aa9f8dfce34b31d447b0ba6d0bfefc40f713c84' + AND CONCAT('0x', SUBSTR(segmented_data [0], 25, 40)) IN ( + SELECT + address + FROM + juice_contracts + ) +), +get_underlying AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + event_index, + contract_address, + topics, + DATA, + event_removed, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + fact_event_logs_id, + inserted_timestamp, + modified_timestamp + FROM + {{ ref('core__fact_event_logs') }} + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + logs_pull + ) + AND topics [0] = '0xcaa97ab28bae75adcb5a02786c64b44d0d3139aa521bf831cdfbe280ef246e36' +), +collateral_base AS ( + SELECT + tx_hash, + block_timestamp, + block_number, + t1.contract_address, + t2.contract_address AS underlying_asset, + t1.modified_timestamp, + _log_id + FROM + logs_pull t1 + LEFT JOIN get_underlying t2 USING(tx_hash) +), +collateral_list AS ( + SELECT + tx_hash, + block_timestamp, + block_number, + C.address AS token_address, + C.name AS token_name, + C.symbol AS token_symbol, + C.decimals AS token_decimals, + contract_address, + COALESCE( + A.underlying_asset, + b.underlying_asset + ) AS underlying_asset_address, + d.name AS underlying_name, + d.symbol AS underlying_symbol, + d.decimals AS underlying_decimals, + A.modified_timestamp, + _log_id + FROM + collateral_base A + LEFT JOIN collateral_tokens b USING(contract_address) + LEFT JOIN contracts C + ON A.contract_address = C.address + LEFT JOIN contracts d + ON underlying_asset_address = d.address +), +combine_asset AS ( + SELECT + block_timestamp, + block_number, + tx_hash, + NULL AS contract_address, + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol, + modified_timestamp, + _log_id + FROM + underlying_asset + INNER JOIN token t3 USING(tx_hash) + INNER JOIN debt_token t4 USING(tx_hash) + UNION ALL + SELECT + block_timestamp, + block_number, + tx_hash, + contract_address, + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + NULL AS pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + NULL AS debt_address, + NULL AS debt_name, + NULL AS debt_decimals, + NULL AS debt_symbol, + modified_timestamp, + _log_id + FROM + collateral_list +) +SELECT + block_timestamp, + block_number, + tx_hash, + contract_address, + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol, + modified_timestamp, + _log_id +FROM + combine_asset diff --git a/models/silver/defi/lending/juice/silver__juice_asset_details.yml b/models/silver/defi/lending/juice/silver__juice_asset_details.yml new file mode 100644 index 0000000..91deadf --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_asset_details.yml @@ -0,0 +1,13 @@ +version: 2 + +models: + - name: silver__juice_asset_details + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TOKEN_ADDRESS + + + + + diff --git a/models/silver/defi/lending/juice/silver__juice_borrows.sql b/models/silver/defi/lending/juice/silver__juice_borrows.sql new file mode 100644 index 0000000..c7ba704 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_borrows.sql @@ -0,0 +1,134 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol + FROM + {{ ref('silver__juice_asset_details') }} +), +juice_borrows AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 25, 40)) AS borrower, + utils.udf_hex_to_int ( + segmented_data [0] + ) AS loan_amount_raw, + contract_address AS pool_address, + 'Juice' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} A + LEFT JOIN asset_details + ON contract_address = pool_address + WHERE + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xcbc04eca7e9da35cb1393a6135a199ca52e450d5e9251cbd99f7847d33a36750' + AND tx_succeeded + +{% if is_incremental() %} +AND A.modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND A.modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +juice_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + loan_amount_raw, + C.underlying_asset_address AS borrows_contract_address, + C.underlying_symbol AS borrows_symbol, + contract_address AS token, + C.token_symbol, + C.underlying_decimals, + debt_name, + debt_address, + debt_symbol, + debt_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_borrows b + LEFT JOIN asset_details C + ON b.contract_address = C.pool_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address, + borrows_symbol, + debt_name AS token_name, + debt_address AS token_address, + debt_symbol AS token_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + modified_timestamp, + _log_id +FROM + juice_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/juice/silver__juice_borrows.yml b/models/silver/defi/lending/juice/silver__juice_borrows.yml new file mode 100644 index 0000000..e9d2124 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_borrows.yml @@ -0,0 +1,89 @@ +version: 2 +models: + - name: silver__juice_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_deposits.sql b/models/silver/defi/lending/juice/silver__juice_deposits.sql new file mode 100644 index 0000000..4184f4d --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_deposits.sql @@ -0,0 +1,267 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol + FROM + {{ ref('silver__juice_asset_details') }} +), +deposit_logs AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + event_index, + contract_address, + topics, + DATA, + event_removed, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + fact_event_logs_id, + inserted_timestamp, + modified_timestamp + FROM + {{ ref('core__fact_event_logs') }} A + WHERE + ( + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + OR contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + ) + AND topics [0] :: STRING IN ( + '0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c', + '0xd88c5369d398bea6a7390a17ce98af43f4aacc78fd3587bc368993d98206a304', + '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + ) + +{% if is_incremental() %} +AND A.modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND A.modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +juice_deposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA :: STRING, 3, len(DATA)), '.{64}') AS segmented_data, + TRY_CAST( + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) AS DECIMAL( + 38, + 0 + ) + ) AS mintAmount_raw, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS supplier, + 'Juice' AS platform, + inserted_timestamp AS modified_timestamp, + _log_id + FROM + deposit_logs + WHERE + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c' + AND tx_succeeded +), +juice_collateraldeposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA :: STRING, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS mintAmount_raw, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS supplier, + 'Juice' AS platform, + inserted_timestamp AS modified_timestamp, + _log_id + FROM + deposit_logs + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0xd88c5369d398bea6a7390a17ce98af43f4aacc78fd3587bc368993d98206a304' + AND tx_succeeded +), +token_transfer AS ( + SELECT + block_timestamp, + tx_hash, + utils.udf_hex_to_int(DATA) AS minttokens_raw + FROM + deposit_logs + WHERE + 1 = 1 + AND contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [1] :: STRING = '0x0000000000000000000000000000000000000000000000000000000000000000' + AND topics [0] :: STRING = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + AND tx_hash IN ( + SELECT + tx_hash + FROM + juice_collateraldeposits + UNION ALL + SELECT + tx_hash + FROM + juice_deposits + ) + AND tx_succeeded +), +juice_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier, + minttokens_raw, + mintAmount_raw, + C.underlying_asset_address AS supplied_contract_address, + C.underlying_symbol AS supplied_symbol, + C.token_address, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_deposits b + LEFT JOIN token_transfer d USING(tx_hash) + LEFT JOIN asset_details C + ON b.token_address = C.pool_address + UNION ALL + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier, + minttokens_raw, + mintAmount_raw, + C.underlying_asset_address AS supplied_contract_address, + C.underlying_symbol AS supplied_symbol, + C.token_address, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_collateraldeposits b + LEFT JOIN token_transfer d USING(tx_hash) + LEFT JOIN asset_details C + ON b.token_address = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + minttokens_raw / pow( + 10, + token_decimals + ) AS issued_tokens, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + supplied_contract_address, + supplied_symbol, + supplier, + platform, + modified_timestamp, + _log_id +FROM + juice_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/juice/silver__juice_deposits.yml b/models/silver/defi/lending/juice/silver__juice_deposits.yml new file mode 100644 index 0000000..d157608 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_deposits.yml @@ -0,0 +1,110 @@ +version: 2 +models: + - name: silver__juice_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_liquidations.sql b/models/silver/defi/lending/juice/silver__juice_liquidations.sql new file mode 100644 index 0000000..7bf66cc --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_liquidations.sql @@ -0,0 +1,203 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + contract_address, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol + FROM + {{ ref('silver__juice_asset_details') }} +), +juice_liquidations AS ( + SELECT + l.block_number, + l.block_timestamp, + l.tx_hash, + l.event_index, + l.origin_from_address, + l.origin_to_address, + l.origin_function_signature, + l.contract_address, + regexp_substr_all(SUBSTR(l.data, 3, len(l.data)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS borrower, + l.origin_to_address as liquidator, + l.contract_address AS token, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS repayAmount_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS bonuscollateral_raw, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS debtamountneeded_raw, + 'Juice' AS platform, + l.modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + l + INNER JOIN asset_details cl + ON l.contract_address = LOWER( + cl.contract_address + ) + WHERE + l.topics [0] :: STRING = '0xe32ec3ea3154879f27d5367898ab3a5ac6b68bf921d7cc610720f417c5cb243c' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp >= ( + SELECT + MAX(modified_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +token_transfer AS ( + SELECT + block_timestamp, + tx_hash, + utils.udf_hex_to_int(DATA) AS seizeTokens_raw, + event_index, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id, + ROW_NUMBER() over ( + PARTITION BY _log_id + ORDER BY + event_index ASC + ) + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [2] = '0x0000000000000000000000000000000000000000000000000000000000000000' + AND topics [0] = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + AND tx_hash IN ( + SELECT + tx_hash + FROM + juice_liquidations + ) + AND tx_succeeded qualify(ROW_NUMBER() over(PARTITION BY tx_hash + ORDER BY + event_index ASC)) = 1 +), +debt_transfer AS ( + SELECT + block_timestamp, + tx_hash, + utils.udf_hex_to_int(DATA) AS debtamount, + debt_name, + debt_address AS debt_token, + debt_symbol AS debt_token_symbol, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + l + INNER JOIN asset_details + ON debt_address = l.contract_address + WHERE + topics [2] = '0x0000000000000000000000000000000000000000000000000000000000000000' + AND topics [0] = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + AND tx_hash IN ( + SELECT + tx_hash + FROM + juice_liquidations + ) + AND tx_succeeded +), +liquidation_union AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + asd1.token_symbol AS token_symbol, + liquidator, + seizeTokens_raw, + seizeTokens_raw / pow( + 10, + asd1.token_decimals + ) AS tokens_seized, + token AS protocol_market, + repayAmount_raw AS amount_unadj, + repayAmount_raw / pow( + 10, + asd1.underlying_decimals + ) AS amount, + asd1.underlying_decimals, + asd1.underlying_asset_address AS collateral_token, + asd1.underlying_symbol AS collateral_token_symbol, + debt_token, + debt_token_symbol, + l.platform, + l.modified_timestamp, + l._log_id + FROM + juice_liquidations l + LEFT JOIN asset_details asd1 + ON l.token = asd1.token_address + LEFT JOIN token_transfer USING(tx_hash) + LEFT JOIN debt_transfer USING(tx_hash) +) +SELECT + * +FROM + liquidation_union qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/juice/silver__juice_liquidations.yml b/models/silver/defi/lending/juice/silver__juice_liquidations.yml new file mode 100644 index 0000000..eeab3b6 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_liquidations.yml @@ -0,0 +1,119 @@ +version: 2 +models: + - name: silver__juice_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">14" + error_if: ">30" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: LIQUIDATOR + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEBT_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: DEBT_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_repayments.sql b/models/silver/defi/lending/juice/silver__juice_repayments.sql new file mode 100644 index 0000000..997c66b --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_repayments.sql @@ -0,0 +1,130 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol + FROM + {{ ref('silver__juice_asset_details') }} +), +juice_repayments AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(topics[1]:: STRING, 25, 40)) AS borrower, + contract_address AS token, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS repayed_amount_raw, + 'Juice' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} a + WHERE + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x5c16de4f8b59bd9caf0f49a545f25819a895ed223294290b408242e72a594231' + AND tx_succeeded + {% if is_incremental() %} + AND a.modified_timestamp > ( + SELECT + max(modified_timestamp) + FROM + {{ this }} + ) + AND a.modified_timestamp >= SYSDATE() - INTERVAL '7 day' + {% endif %} + ), + +juice_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + C.token_symbol, + repayed_amount_raw, + C.underlying_asset_address AS repay_contract_address, + C.underlying_symbol AS repay_contract_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_repayments b + LEFT JOIN asset_details C + ON b.token = C.pool_address +) + +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address as protocol_market, + origin_from_address as payer, + borrower, + token as token_address, + token_symbol, + repay_contract_address, + repay_contract_symbol, + repayed_amount_raw AS amount_unadj, + repayed_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + modified_timestamp, + _log_id +FROM + juice_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_repayments.yml b/models/silver/defi/lending/juice/silver__juice_repayments.yml new file mode 100644 index 0000000..155917f --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_repayments.yml @@ -0,0 +1,105 @@ +version: 2 +models: + - name: silver__juice_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PAYER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOTAL_REPAID + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/juice/silver__juice_withdraws.sql b/models/silver/defi/lending/juice/silver__juice_withdraws.sql new file mode 100644 index 0000000..1180831 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_withdraws.sql @@ -0,0 +1,260 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + underlying_asset_address, + underlying_name, + underlying_decimals, + underlying_symbol, + pool_address, + token_address, + token_name, + token_decimals, + token_symbol, + debt_address, + debt_name, + debt_decimals, + debt_symbol + FROM + {{ ref('silver__juice_asset_details') }} +), +withdraw_logs AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + DATA, + topics, + tx_status, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} A + WHERE + ( + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + OR contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + ) + AND topics [0] :: STRING IN ( + '0x884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364', + '0x0b260cc77140cab3405675836fc971314e656137208b77414be51fafd58ae34b', + '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + ) + +{% if is_incremental() %} +AND A.modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND A.modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +juice_redemption AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [0] :: STRING + ) :: INTEGER AS received_amount_raw, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS redeemer, + 'Juice' AS platform, + modified_timestamp, + _log_id + FROM + withdraw_logs + WHERE + contract_address IN ( + SELECT + pool_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x0b260cc77140cab3405675836fc971314e656137208b77414be51fafd58ae34b' + AND tx_succeeded +), +juice_collateralredeems AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA :: STRING, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS received_amount_raw, + CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS redeemer, + 'Juice' AS platform, + modified_timestamp, + _log_id + FROM + withdraw_logs + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x0b260cc77140cab3405675836fc971314e656137208b77414be51fafd58ae34b' + AND tx_succeeded +), +token_transfer AS ( + SELECT + block_timestamp, + tx_hash, + utils.udf_hex_to_int(DATA) AS redeemed_token_raw + FROM + withdraw_logs + WHERE + 1 = 1 + AND contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [2] = '0x0000000000000000000000000000000000000000000000000000000000000000' + AND topics [0] = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' + AND tx_hash IN ( + SELECT + tx_hash + FROM + juice_redemption + UNION ALL + SELECT + tx_hash + FROM + juice_collateralredeems + ) + AND tx_succeeded +), +juice_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + b.token AS token_address, + redeemer, + received_amount_raw, + redeemed_token_raw, + C.underlying_asset_address AS received_contract_address, + C.underlying_symbol AS received_symbol, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_redemption b + LEFT JOIN token_transfer USING(tx_hash) + LEFT JOIN asset_details C + ON b.token = C.pool_address + UNION ALL + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + b.token_address, + redeemer, + received_amount_raw, + redeemed_token_raw, + C.underlying_asset_address AS received_contract_address, + C.underlying_symbol AS received_symbol, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + juice_collateralredeems b + LEFT JOIN token_transfer d USING(tx_hash) + LEFT JOIN asset_details C + ON b.token_address = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + received_amount_raw AS amount_unadj, + received_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + received_contract_address, + received_symbol, + redeemed_token_raw / pow( + 10, + token_decimals + ) AS redeemed_tokens, + redeemer, + platform, + modified_timestamp, + _log_id +FROM + juice_combine ee qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/juice/silver__juice_withdraws.yml b/models/silver/defi/lending/juice/silver__juice_withdraws.yml new file mode 100644 index 0000000..8111a71 --- /dev/null +++ b/models/silver/defi/lending/juice/silver__juice_withdraws.yml @@ -0,0 +1,95 @@ +version: 2 +models: + - name: silver__juice_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: RECEIVED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_asset_details.sql b/models/silver/defi/lending/orbit/silver__orbit_asset_details.sql new file mode 100644 index 0000000..a4053e9 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_asset_details.sql @@ -0,0 +1,146 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH log_pull_1 AS ( + + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + l + WHERE + topics [0] :: STRING = '0x7ac369dbd14fa5ea3f473ed67cc9d598964a77501540ba6751eb0b3decf5870d' + AND origin_from_address = '0x6315f65843e7582508e4f0aac20a7203e7b09f02' + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +traces_pull AS ( + SELECT + t.from_address AS token_address, + t.to_address AS underlying_asset + FROM + {{ ref('core__fact_traces') }} + t + WHERE + tx_hash IN ( + SELECT + tx_hash + FROM + log_pull_1 + ) + AND input = '0x18160ddd' + AND TYPE = 'STATICCALL' +), +contracts AS ( + SELECT + address AS contract_address, + symbol AS token_symbol, + NAME AS token_name, + decimals AS token_decimals + FROM + {{ ref('core__dim_contracts') }} + WHERE + address IN ( + SELECT + token_address + FROM + traces_pull + ) + OR address IN ( + SELECT + underlying_asset + FROM + traces_pull + ) + OR address IN ( + SELECT + contract_address + FROM + log_pull_1 + ) + OR address = '0x4300000000000000000000000000000000000004' +), +log_pull AS ( + SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address, + C.token_name, + C.token_symbol, + C.token_decimals, + l.modified_timestamp, + l._log_id + FROM + log_pull_1 l + LEFT JOIN contracts C + ON C.contract_address = l.contract_address +), +underlying_details AS ( + SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address, + l.token_name, + l.token_symbol, + l.token_decimals, + CASE + WHEN t.underlying_asset IS NULL THEN '0x4300000000000000000000000000000000000004' + ELSE t.underlying_asset + END AS underlying_asset, + l.modified_timestamp, + l._log_id + FROM + log_pull l + LEFT JOIN traces_pull t + ON l.contract_address = t.token_address qualify(ROW_NUMBER() over(PARTITION BY l.contract_address + ORDER BY + block_timestamp ASC)) = 1 +) +SELECT + l.tx_hash, + l.block_number, + l.block_timestamp, + l.contract_address AS token_address, + l.token_name, + l.token_symbol, + l.token_decimals, + l.underlying_asset AS underlying_asset_address, + C.token_name AS underlying_name, + C.token_symbol AS underlying_symbol, + C.token_decimals AS underlying_decimals, + l.modified_timestamp, + l._log_id +FROM + underlying_details l + LEFT JOIN contracts C + ON C.contract_address = l.underlying_asset +WHERE + l.token_name IS NOT NULL diff --git a/models/silver/defi/lending/orbit/silver__orbit_asset_details.yml b/models/silver/defi/lending/orbit/silver__orbit_asset_details.yml new file mode 100644 index 0000000..fa8acc7 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_asset_details.yml @@ -0,0 +1,12 @@ +version: 2 +models: + - name: silver__orbit_asset_details + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - TOKEN_ADDRESS + + + + + diff --git a/models/silver/defi/lending/orbit/silver__orbit_borrows.sql b/models/silver/defi/lending/orbit/silver__orbit_borrows.sql new file mode 100644 index 0000000..f0f853b --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_borrows.sql @@ -0,0 +1,128 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__orbit_asset_details') }} +), +orbit_borrows AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS borrower, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS loan_amount_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS accountBorrows, + utils.udf_hex_to_int( + segmented_data [3] :: STRING + ) :: INTEGER AS totalBorrows, + contract_address AS token, + 'Orbit' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x13ed6866d4e1ee6da46f845c46d7e54120883d75c5ea9a2dacc1c4ca8984ab80' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +orbit_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + loan_amount_raw, + C.underlying_asset_address AS borrows_contract_address, + C.underlying_symbol AS borrows_symbol, + token, + C.token_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + orbit_borrows b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + borrows_contract_address, + borrows_symbol, + token AS token_address, + token_symbol, + loan_amount_raw AS amount_unadj, + loan_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + modified_timestamp, + _log_id +FROM + orbit_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/orbit/silver__orbit_borrows.yml b/models/silver/defi/lending/orbit/silver__orbit_borrows.yml new file mode 100644 index 0000000..0401598 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_borrows.yml @@ -0,0 +1,89 @@ +version: 2 +models: + - name: silver__orbit_borrows + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWS_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_deposits.sql b/models/silver/defi/lending/orbit/silver__orbit_deposits.sql new file mode 100644 index 0000000..4f3c78e --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_deposits.sql @@ -0,0 +1,131 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__orbit_asset_details') }} +), +orbit_deposits AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS minttokens_raw, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS mintAmount_raw, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS supplier, + 'Orbit' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +orbit_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + supplier, + minttokens_raw, + mintAmount_raw, + C.underlying_asset_address AS supplied_contract_address, + C.underlying_symbol AS supplied_symbol, + C.token_address, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + orbit_deposits b + LEFT JOIN asset_details C + ON b.token_address = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token_address, + token_symbol, + minttokens_raw / pow( + 10, + token_decimals + ) AS issued_tokens, + mintAmount_raw AS amount_unadj, + mintAmount_raw / pow( + 10, + underlying_decimals + ) AS amount, + supplied_contract_address, + supplied_symbol, + supplier, + platform, + modified_timestamp, + _log_id +FROM + orbit_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/orbit/silver__orbit_deposits.yml b/models/silver/defi/lending/orbit/silver__orbit_deposits.yml new file mode 100644 index 0000000..4664c70 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_deposits.yml @@ -0,0 +1,110 @@ +version: 2 +models: + - name: silver__orbit_deposits + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ISSUED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: SUPPLIED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: SUPPLIER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql b/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql new file mode 100644 index 0000000..e53d709 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql @@ -0,0 +1,144 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__orbit_asset_details') }} +), +orbit_liquidations AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 25, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS liquidator, + utils.udf_hex_to_int( + segmented_data [4] :: STRING + ) :: INTEGER AS seizeTokens_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS repayAmount_raw, + CONCAT('0x', SUBSTR(segmented_data [3] :: STRING, 25, 40)) AS tokenCollateral, + 'Orbit' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x298637f684da70674f26509b10f07ec2fbc77a335ab1e7d6215a4b2484d8bb52' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp >= ( + SELECT + MAX(modified_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +liquidation_union AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + asd1.token_symbol AS token_symbol, + liquidator, + seizeTokens_raw, + seizeTokens_raw / pow( + 10, + asd2.token_decimals + ) AS tokens_seized, + tokenCollateral AS protocol_market, + asd2.underlying_asset_address AS collateral_token, + asd2.underlying_symbol AS collateral_token_symbol, + repayAmount_raw AS amount_unadj, + repayAmount_raw / pow( + 10, + asd1.underlying_decimals + ) AS amount, + asd1.underlying_decimals, + asd1.underlying_asset_address AS liquidation_contract_address, + asd1.underlying_symbol AS liquidation_contract_symbol, + l.platform, + l.modified_timestamp, + l._log_id + FROM + orbit_liquidations l + LEFT JOIN asset_details asd1 + ON l.token = asd1.token_address + LEFT JOIN asset_details asd2 + ON l.tokenCollateral = asd2.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + contract_address, + 'LiquidateBorrow' AS event_name, + event_index, + origin_function_signature, + origin_from_address, + origin_to_address, + liquidator, + borrower, + token, + token_symbol, + contract_address AS protocol_market, + collateral_token, + collateral_token_symbol, + amount_unadj, + amount, + liquidation_contract_address AS debt_token, + liquidation_contract_symbol AS debt_token_symbol, + platform, + modified_timestamp, + _log_id +FROM + liquidation_union qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml b/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml new file mode 100644 index 0000000..fe9e25b --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml @@ -0,0 +1,119 @@ +version: 2 +models: + - name: silver__orbit_liquidations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">14" + error_if: ">30" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: LIQUIDATOR + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: DEBT_TOKEN + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: DEBT_TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PLATFORM + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: MODIFIED_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: _LOG_ID + tests: + - not_null: + error_if: ">10" + warn_if: ">0" \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_repayments.sql b/models/silver/defi/lending/orbit/silver__orbit_repayments.sql new file mode 100644 index 0000000..27d7bef --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_repayments.sql @@ -0,0 +1,125 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__orbit_asset_details') }} +), +orbit_repayments AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + CONCAT('0x', SUBSTR(segmented_data [1] :: STRING, 25, 40)) AS borrower, + contract_address AS token, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS payer, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS repayed_amount_raw, + 'Orbit' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x1a2a22cb034d26d1854bdc6666a5b91fe25efbbb5dcad3b0355478d6f5c362a1' + AND tx_succeeded + {% if is_incremental() %} + AND modified_timestamp > ( + SELECT + max(modified_timestamp) + FROM + {{ this }} + ) + AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' + {% endif %} +), +orbit_combine AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + borrower, + token, + C.token_symbol, + payer, + repayed_amount_raw, + C.underlying_asset_address AS repay_contract_address, + C.underlying_symbol AS repay_contract_symbol, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + orbit_repayments b + LEFT JOIN asset_details C + ON b.token = C.token_address +) + +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address as protocol_market, + borrower, + token as token_address, + token_symbol, + payer, + repay_contract_address, + repay_contract_symbol, + repayed_amount_raw AS amount_unadj, + repayed_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + platform, + modified_timestamp, + _log_id +FROM + orbit_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_repayments.yml b/models/silver/defi/lending/orbit/silver__orbit_repayments.yml new file mode 100644 index 0000000..52d121f --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_repayments.yml @@ -0,0 +1,105 @@ +version: 2 +models: + - name: silver__orbit_repayments + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BORROWER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PROTOCOL_MARKET + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: PAYER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REPAY_CONTRACT_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOTAL_REPAID + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file diff --git a/models/silver/defi/lending/orbit/silver__orbit_withdraws.sql b/models/silver/defi/lending/orbit/silver__orbit_withdraws.sql new file mode 100644 index 0000000..bb8e9a3 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_withdraws.sql @@ -0,0 +1,131 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = "block_number", + cluster_by = ['block_timestamp::DATE'], + tags = ['reorg','curated'] +) }} + +WITH asset_details AS ( + + SELECT + token_address, + token_name, + token_symbol, + token_decimals, + underlying_asset_address, + underlying_name, + underlying_symbol, + underlying_decimals + FROM + {{ ref('silver__orbit_asset_details') }} +), +orbit_redemptions AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + contract_address AS token, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + utils.udf_hex_to_int( + segmented_data [1] :: STRING + ) :: INTEGER AS received_amount_raw, + utils.udf_hex_to_int( + segmented_data [2] :: STRING + ) :: INTEGER AS redeemed_token_raw, + CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS redeemer, + 'Orbit' AS platform, + modified_timestamp, + CASE + WHEN tx_status = 'SUCCESS' THEN TRUE + ELSE FALSE + END AS tx_succeeded, + CONCAT( + tx_hash :: STRING, + '-', + event_index :: STRING + ) AS _log_id + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address IN ( + SELECT + token_address + FROM + asset_details + ) + AND topics [0] :: STRING = '0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f' + AND tx_succeeded + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} +), +orbit_combine AS ( + SELECT + b.block_number, + b.block_timestamp, + b.tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token, + redeemer, + received_amount_raw, + redeemed_token_raw, + C.underlying_asset_address AS received_contract_address, + C.underlying_symbol AS received_symbol, + C.token_symbol, + C.token_decimals, + C.underlying_decimals, + b.platform, + b._log_id, + b.modified_timestamp + FROM + orbit_redemptions b + LEFT JOIN asset_details C + ON b.token = C.token_address +) +SELECT + block_number, + block_timestamp, + tx_hash, + event_index, + origin_from_address, + origin_to_address, + origin_function_signature, + contract_address, + token AS token_address, + token_symbol, + received_amount_raw AS amount_unadj, + received_amount_raw / pow( + 10, + underlying_decimals + ) AS amount, + received_contract_address, + received_symbol, + redeemed_token_raw / pow( + 10, + token_decimals + ) AS redeemed_tokens, + redeemer, + platform, + modified_timestamp, + _log_id +FROM + orbit_combine qualify(ROW_NUMBER() over(PARTITION BY _log_id +ORDER BY + modified_timestamp DESC)) = 1 diff --git a/models/silver/defi/lending/orbit/silver__orbit_withdraws.yml b/models/silver/defi/lending/orbit/silver__orbit_withdraws.yml new file mode 100644 index 0000000..d80f698 --- /dev/null +++ b/models/silver/defi/lending/orbit/silver__orbit_withdraws.yml @@ -0,0 +1,95 @@ +version: 2 +models: + - name: silver__orbit_withdraws + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: BLOCK_TIMESTAMP + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 3 + warn_if: ">3" + error_if: ">14" + - name: TX_HASH + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: EVENT_INDEX + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FROM_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_TO_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: ORIGIN_FUNCTION_SIGNATURE + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: TOKEN_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMER + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT_UNADJ + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: AMOUNT + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float + - name: RECEIVED_CONTRACT_ADDRESS + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: RECEIVED_SYMBOL + tests: + - not_null: + error_if: ">10" + warn_if: ">0" + - name: REDEEMED_TOKENS + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - decimal + - float \ No newline at end of file