diff --git a/models/gold/defi/lending/defi__ez_lending_liquidations.sql b/models/gold/defi/lending/defi__ez_lending_liquidations.sql index 73a46b6..c4a9784 100644 --- a/models/gold/defi/lending/defi__ez_lending_liquidations.sql +++ b/models/gold/defi/lending/defi__ez_lending_liquidations.sql @@ -1,6 +1,5 @@ {{ config( materialized = 'view', - enabled = false, persist_docs ={ "relation": true, "columns": true }, meta={ 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 index 406cf1e..f140a67 100644 --- a/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql +++ b/models/silver/defi/lending/complete_lending/silver__complete_lending_liquidations.sql @@ -1,7 +1,6 @@ -- depends_on: {{ ref('silver__complete_token_prices') }} {{ config( materialized = 'incremental', - enabled = false, incremental_strategy = 'delete+insert', unique_key = ['block_number','platform'], cluster_by = ['block_timestamp::DATE','platform'], @@ -25,21 +24,97 @@ WITH init AS ( amount AS liquidated_amount, NULL AS liquidated_amount_usd, token AS protocol_collateral_asset, - liquidation_contract_address AS debt_asset, - liquidation_contract_symbol AS debt_asset_symbol, + debt_token AS debt_asset, + debt_token_symbol AS debt_asset_symbol, collateral_token AS collateral_asset, - collateral_symbol AS collateral_asset_symbol, + collateral_token_symbol AS collateral_asset_symbol, platform, - 'base' AS blockchain, + 'blast' AS blockchain, l._LOG_ID, - l._INSERTED_TIMESTAMP + l.modified_timestamp as _inserted_timestamp FROM {{ ref('silver__init_liquidations') }} l {% if is_incremental() and 'init' not in var('HEAL_MODELS') %} WHERE - l._inserted_timestamp >= ( + _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 @@ -53,6 +128,16 @@ liquidation_union AS ( * FROM init + UNION ALL + SELECT + * + FROM + orbit + UNION ALL + SELECT + * + FROM + juice ), complete_lending_liquidations AS ( SELECT @@ -94,7 +179,7 @@ complete_lending_liquidations AS ( platform, A.blockchain, A._LOG_ID, - A._INSERTED_TIMESTAMP + A._inserted_timestamp FROM liquidation_union A LEFT JOIN {{ ref('price__ez_prices_hourly') }} @@ -142,7 +227,7 @@ heal_model AS ( platform, t0.blockchain, t0._LOG_ID, - t0._INSERTED_TIMESTAMP + t0._inserted_timestamp FROM {{ this }} t0 @@ -232,7 +317,7 @@ SELECT platform, blockchain, _LOG_ID, - _INSERTED_TIMESTAMP + _inserted_timestamp FROM heal_model {% endif %} diff --git a/models/silver/defi/lending/init/silver__init_liquidations.sql b/models/silver/defi/lending/init/silver__init_liquidations.sql index 57c4a0c..e9d0bf8 100644 --- a/models/silver/defi/lending/init/silver__init_liquidations.sql +++ b/models/silver/defi/lending/init/silver__init_liquidations.sql @@ -36,7 +36,6 @@ init_liquidations AS ( event_index, origin_from_address, origin_to_address, - origin_from_address AS borrower, origin_function_signature, contract_address, topics, @@ -59,64 +58,84 @@ init_liquidations AS ( contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' AND topics [0] :: STRING = '0x6df71caf4cddb1620bcf376243248e0077da98913d65a7e9315bc9984e5fff72' AND tx_status = 'SUCCESS' - {% if is_incremental() %} - AND modified_timestamp > ( - SELECT - max(modified_timestamp) - FROM - {{ this }} - ) - AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' - {% endif %} + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} ), -token_transfer AS ( +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, - contract_address, - from_address, - to_address, - raw_amount AS underlying_amount_raw, - token_decimals, - token_symbol, - token_name + protocol_market, + token_address AS debt_token, + token_symbol AS debt_token_symbol, + posId, + amount_unadj, + amount FROM - {{ ref('core__fact_token_transfers') }} a - LEFT JOIN {{ ref('silver__contracts') }} USING(contract_address) + {{ ref('silver__init_repayments') }} WHERE - 1 = 1 - AND ( - contract_address IN ( - '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad', - '0x4300000000000000000000000000000000000003', - '0x4300000000000000000000000000000000000004' - ) - OR contract_address IN ( - SELECT - underlying_asset_address - FROM - asset_details - ) - ) - AND ( - from_address IN ( - SELECT - token_address - FROM - asset_details - ) - OR from_address IN ( - SELECT - underlying_asset_address - FROM - asset_details - ) - ) - AND tx_hash IN ( + 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 + FROM + {{ ref('core__fact_event_logs') }} + WHERE + contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' + AND topics [0] :: STRING = '0xe6a96441ecc85d0943a914f4750f067a912798ec2543bc68c00e18291da88d14' -- createposition + AND tx_status = 'SUCCESS' + AND posId IN ( + SELECT + posId + FROM + init_liquidations + ) ) SELECT l.block_number, @@ -127,31 +146,24 @@ SELECT origin_to_address, origin_function_signature, l.contract_address, - l.liquidator, --double check with SY + l.contract_address as token, + liquidator, borrower, - token AS protocol_market, - token, --double check with SY - C.token_symbol, --double check with SY - l.sharesamt AS amount_unadj, - underlying_amount_raw, - l.sharesamt / pow( - 10, - C.token_decimals - ) AS tokens_seized, - underlying_amount_raw / pow( - 10, - d.token_decimals - ) AS amount, - C.underlying_decimals, - C.underlying_asset_address AS collateral_token, - C.underlying_symbol AS collateral_symbol, - l.platform, - l.modified_timestamp, - l._log_id + 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, + _log_id FROM - init_liquidations l - LEFT JOIN asset_details C - ON l.poolOut = C.token_address - LEFT JOIN token_transfer d - ON l.tx_hash = d.tx_hash - AND C.underlying_asset_address = d.from_address + 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 _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 index 107ad22..a24dd69 100644 --- a/models/silver/defi/lending/init/silver__init_liquidations.yml +++ b/models/silver/defi/lending/init/silver__init_liquidations.yml @@ -18,9 +18,6 @@ models: - name: TOKEN tests: - not_null - - name: TOKEN_SYMBOL - tests: - - not_null - name: LIQUIDATOR tests: - not_null @@ -43,7 +40,13 @@ models: - name: COLLATERAL_TOKEN tests: - not_null - - name: COLLATERAL_SYMBOL + - name: COLLATERAL_TOKEN_SYMBOL + tests: + - not_null + - name: DEBT_TOKEN + tests: + - not_null + - name: DEBT_TOKEN_SYMBOL tests: - not_null - name: EVENT_INDEX diff --git a/models/silver/defi/lending/init/silver__init_repayments.sql b/models/silver/defi/lending/init/silver__init_repayments.sql index 14378b0..a2e739e 100644 --- a/models/silver/defi/lending/init/silver__init_repayments.sql +++ b/models/silver/defi/lending/init/silver__init_repayments.sql @@ -20,8 +20,7 @@ WITH asset_details AS ( underlying_unwrap_address, underlying_unwrap_name, underlying_unwrap_symbol, - underlying_unwrap_decimals, - _inserted_timestamp + underlying_unwrap_decimals FROM {{ ref('silver__init_asset_details') }} ), @@ -42,6 +41,7 @@ init_repayments AS ( 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, @@ -58,15 +58,16 @@ init_repayments AS ( contract_address = '0xa7d36f2106b5a5d528a7e2e7a3f436d703113a10' AND topics [0] :: STRING = '0x77673b670822baca14a7caf6f8038f811649ab73e4c06083b0e58a53389bece7' AND tx_status = 'SUCCESS' - {% if is_incremental() %} - AND modified_timestamp > ( - SELECT - max(modified_timestamp) - FROM - {{ this }} - ) - AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' - {% endif %} + +{% if is_incremental() %} +AND modified_timestamp > ( + SELECT + MAX(modified_timestamp) + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +{% endif %} ), native_transfer AS ( SELECT @@ -80,7 +81,7 @@ native_transfer AS ( FROM {{ ref('core__fact_traces') }} WHERE - to_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') + to_address IN ('0xf683ce59521aa464066783d78e40cd9412f33d21') -- hard code wweth contract here AND tx_hash IN ( SELECT tx_hash @@ -102,11 +103,10 @@ token_transfer AS ( token_symbol, token_name FROM - {{ ref('core__fact_token_transfers') }} a + {{ ref('core__fact_token_transfers') }} LEFT JOIN {{ ref('silver__contracts') }} USING(contract_address) WHERE - 1 = 1 - AND contract_address IN ( + contract_address IN ( '0xb1a5700fa2358173fe465e6ea4ff52e36e88e2ad', '0x4300000000000000000000000000000000000003', '0x4300000000000000000000000000000000000004' @@ -141,18 +141,15 @@ init_combine AS ( origin_from_address, origin_to_address, origin_function_signature, + posId, b.contract_address, + b.pool AS protocol_market, borrower, - token, - C.token_symbol, - C.token_decimals, + repayer AS payer, amttorepay, sharesAmt, C.underlying_asset_address AS repay_contract_address, C.underlying_symbol AS repay_contract_symbol, - d.token_decimals AS decimals1, - d.contract_address AS contract_address1, - d.token_symbol AS symbol1, underlying_decimals AS underlying_wrap_decimals, COALESCE( eth_value, @@ -170,6 +167,7 @@ init_combine AS ( ) AS underlying_decimals, COALESCE( eth_address, + d.contract_address, C.underlying_asset_address ) AS underlying_asset_address, b.platform, @@ -193,10 +191,13 @@ SELECT origin_function_signature, contract_address, borrower, - token AS token_address, - token_symbol, + protocol_market, + posId, + payer, repay_contract_address, repay_contract_symbol, + underlying_asset_address AS token_address, + underlying_symbol AS token_symbol, underlying_amount_raw AS amount_unadj, amttorepay / pow( 10, diff --git a/models/silver/defi/lending/juice/silver__juice_liquidations.sql b/models/silver/defi/lending/juice/silver__juice_liquidations.sql index 195e352..09bae71 100644 --- a/models/silver/defi/lending/juice/silver__juice_liquidations.sql +++ b/models/silver/defi/lending/juice/silver__juice_liquidations.sql @@ -53,21 +53,25 @@ juice_liquidations AS ( l.modified_timestamp, l._log_id FROM - {{ ref('core__fact_event_logs') }} l - INNER JOIN asset_details ad - ON l.contract_address = ad.contract_address + {{ 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 l.tx_status = 'SUCCESS' - {% if is_incremental() %} - AND l.modified_timestamp > ( - SELECT - max(modified_timestamp) - FROM - {{ this }} - ) - AND l.modified_timestamp >= SYSDATE() - INTERVAL '7 day' - {% endif %} + +{% 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 @@ -82,10 +86,9 @@ token_transfer AS ( event_index ASC ) FROM - {{ ref('core__fact_event_logs') }} a + {{ ref('core__fact_event_logs') }} WHERE - 1 = 1 - AND contract_address IN ( + contract_address IN ( SELECT token_address FROM @@ -99,20 +102,34 @@ token_transfer AS ( FROM juice_liquidations ) - AND tx_status = 'SUCCESS' - {% if is_incremental() %} - AND a.modified_timestamp > ( - SELECT - max(modified_timestamp) - FROM - {{ this }} - ) - AND a.modified_timestamp >= SYSDATE() - INTERVAL '7 day' - {% endif %} - qualify(ROW_NUMBER() over(PARTITION BY tx_hash + AND tx_status = 'SUCCESS' 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 + 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_status = 'SUCCESS' +), liquidation_union AS ( SELECT block_number, @@ -124,9 +141,9 @@ liquidation_union AS ( origin_function_signature, contract_address, borrower, - liquidator, token, asd1.token_symbol AS token_symbol, + liquidator, seizeTokens_raw, seizeTokens_raw / pow( 10, @@ -140,7 +157,9 @@ liquidation_union AS ( ) AS amount, asd1.underlying_decimals, asd1.underlying_asset_address AS collateral_token, - asd1.underlying_symbol AS collateral_symbol, + asd1.underlying_symbol AS collateral_token_symbol, + debt_token, + debt_token_symbol, l.platform, l.modified_timestamp, l._log_id @@ -149,6 +168,7 @@ liquidation_union AS ( 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 * diff --git a/models/silver/defi/lending/juice/silver__juice_liquidations.yml b/models/silver/defi/lending/juice/silver__juice_liquidations.yml index 94c4195..b3e7b48 100644 --- a/models/silver/defi/lending/juice/silver__juice_liquidations.yml +++ b/models/silver/defi/lending/juice/silver__juice_liquidations.yml @@ -42,7 +42,7 @@ models: - name: COLLATERAL_TOKEN tests: - not_null - - name: COLLATERAL_SYMBOL + - name: COLLATERAL_TOKEN_SYMBOL tests: - not_null - name: EVENT_INDEX diff --git a/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql b/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql index 1d9cbb4..5214a3b 100644 --- a/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql +++ b/models/silver/defi/lending/orbit/silver__orbit_liquidations.sql @@ -7,6 +7,7 @@ ) }} WITH asset_details AS ( + SELECT token_address, token_name, @@ -56,13 +57,13 @@ orbit_liquidations AS ( AND tx_status = 'SUCCESS' {% if is_incremental() %} - AND modified_timestamp > ( - SELECT - max(modified_timestamp) - FROM - {{ this }} - ) - AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' +AND modified_timestamp >= ( + SELECT + MAX(modified_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +AND modified_timestamp >= SYSDATE() - INTERVAL '7 day' {% endif %} ), liquidation_union AS ( @@ -85,10 +86,8 @@ liquidation_union AS ( asd2.token_decimals ) AS tokens_seized, tokenCollateral AS protocol_market, - asd2.token_symbol AS collateral_token_symbol, asd2.underlying_asset_address AS collateral_token, - tokenCollateral, - asd2.underlying_symbol AS collateral_symbol, + asd2.underlying_symbol AS collateral_token_symbol, repayAmount_raw AS amount_unadj, repayAmount_raw / pow( 10, @@ -108,7 +107,29 @@ liquidation_union AS ( 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 diff --git a/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml b/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml index 2e259e9..742e544 100644 --- a/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml +++ b/models/silver/defi/lending/orbit/silver__orbit_liquidations.yml @@ -36,10 +36,10 @@ models: column_type_list: - decimal - float - - name: LIQUIDATION_CONTRACT_ADDRESS + - name: DEBT_TOKEN tests: - not_null - - name: LIQUIDATION_CONTRACT_SYMBOL + - name: DEBT_TOKEN_SYMBOL tests: - not_null - name: TX_HASH @@ -48,7 +48,7 @@ models: - name: COLLATERAL_TOKEN tests: - not_null - - name: COLLATERAL_SYMBOL + - name: COLLATERAL_TOKEN_SYMBOL tests: - not_null - name: EVENT_INDEX