deprecations and prices

This commit is contained in:
Eric Laurello 2024-07-02 13:43:07 -04:00
parent 0a494dac55
commit 884abc5dbd
14 changed files with 59 additions and 112 deletions

View File

@ -4,8 +4,9 @@
SELECT
owner,
collateral_up,
{# collateral_up,
debt_up,
#}
collateralization_ratio,
collateral_asset,
target_asset,

View File

@ -4,8 +4,9 @@
SELECT
owner,
collateral_down,
{# collateral_down,
debt_down,
#}
collateral_asset,
event_id,
block_timestamp,

View File

@ -15,7 +15,7 @@ SELECT
out_asset,
out_e8,
failed_swaps,
failed_swaps_reasons,
{# failed_swaps_reasons, #}
event_id,
block_timestamp,
failed_swap_reasons,

View File

@ -10,8 +10,9 @@ WITH base AS (
SELECT
owner,
collateral_up,
{# collateral_up,
debt_up,
#}
collateralization_ratio,
collateral_asset,
target_asset,
@ -46,8 +47,8 @@ SELECT
'-1'
) AS dim_block_id,
owner,
collateral_up,
debt_up,
NULL AS collateral_up,
NULL AS debt_up,
collateralization_ratio,
collateral_asset,
target_asset,

View File

@ -23,9 +23,9 @@ models:
tests:
- not_null
- name: collateral_up
description: "The collateral amount"
description: "DEPRECATING SOON! The collateral amount"
- name: debt_up
description: "The debt amount"
description: "DEPRECATING SOON! The debt amount"
- name: collateralization_ratio
description: "The collateralization ratio"
tests:

View File

@ -10,8 +10,9 @@ WITH base AS (
SELECT
owner,
collateral_down,
{# collateral_down,
debt_down,
#}
collateral_asset,
event_id,
block_timestamp,
@ -44,8 +45,9 @@ SELECT
'-1'
) AS dim_block_id,
owner,
collateral_down,
{# collateral_down,
debt_down,
#}
collateral_asset,
collateral_withdrawn,
debt_repaid,

View File

@ -23,9 +23,9 @@ models:
tests:
- not_null
- name: collateral_down
description: "The collateral amount"
description: "DEPRECATING SOON! The collateral amount"
- name: debt_down
description: "The debt amount"
description: "DEPRECATING SOON! The debt amount"
- name: collateral_asset
description: "The collateral asset"
tests:

View File

@ -21,7 +21,7 @@ WITH base AS (
out_asset,
out_e8,
failed_swaps,
failed_swaps_reasons,
failed_swap_reasons AS failed_swaps_reasons,
event_id,
block_timestamp,
failed_swap_reasons,

View File

@ -43,9 +43,11 @@ models:
- name: failed_swaps
description: "the count of failed swaps"
- name: failed_swaps_reasons
description: "the reason of failed swaps"
description: "DEPRECATING SOON! the reason of failed swaps"
- name: event_id
description: ""
- name: failed_swap_reasons
description: "the reason of failed swaps"
- name: INSERTED_TIMESTAMP
description: '{{ doc("inserted_timestamp") }}'
- name: MODIFIED_TIMESTAMP

View File

@ -4,8 +4,9 @@
SELECT
owner,
collateral_up,
{# collateral_up,
debt_up,
#}
collateralization_ratio,
collateral_asset,
target_asset,

View File

@ -4,8 +4,9 @@
SELECT
owner,
collateral_down,
{# collateral_down,
debt_down,
#}
collateral_asset,
event_id,
block_timestamp,

View File

@ -25,102 +25,21 @@ WHERE
b.block_timestamp :: DATE >= CURRENT_DATE -7
{% endif %}
),
max_pool_blocks AS (
price AS (
SELECT
MAX(block_id) AS max_block,
pool_name
height AS block_id,
b.block_timestamp,
rune_price_e8 AS rune_usd
FROM
blocks
WHERE
pool_name IN (
'BNB.USDT-6D8',
'BNB.BUSD-BD1',
'ETH.USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7'
)
GROUP BY
pool_name
),
reference_pool AS (
SELECT
bpd.block_id,
block_timestamp,
bpd.pool_name,
rune_e8
FROM
blocks bpd
JOIN max_pool_blocks mpb
ON bpd.pool_name = mpb.pool_name
AND bpd.block_id = mpb.max_block
ORDER BY
rune_e8 DESC
LIMIT
1
), -- step 2 use that pool to determine the price of rune
rune_usd_max_tbl AS (
SELECT
block_timestamp,
block_id,
asset_e8 / nullif(rune_e8,0) AS rune_usd_max
FROM
blocks
WHERE
pool_name = (
SELECT
pool_name
FROM
reference_pool
)
),
rune_usd_sup_tbl AS (
SELECT
block_timestamp,
block_id,
AVG(rune_usd) AS rune_usd_sup
FROM
(
SELECT
block_timestamp,
block_id,
asset_e8 / rune_e8 AS rune_usd
FROM
blocks
WHERE
rune_e8 > 0
AND asset_e8 > 0
)
GROUP BY
block_timestamp,
block_id
),
rune_usd AS (
SELECT
block_timestamp,
block_id,
CASE
WHEN rune_usd_max IS NULL THEN LAG(rune_usd_max) ignore nulls over (
ORDER BY
block_id
)
ELSE rune_usd_max
END AS rune_usd
FROM
(
SELECT
COALESCE(
A.block_timestamp,
b.block_timestamp
) AS block_timestamp,
COALESCE(
A.block_id,
b.block_id
) AS block_id,
rune_usd_max
FROM
rune_usd_max_tbl A full
JOIN rune_usd_sup_tbl b
ON A.block_timestamp = b.block_timestamp
AND A.block_id = b.block_id
)
{{ ref('silver__rune_price') }} A
JOIN {{ ref('silver__block_log') }}
b
ON A.block_timestamp = b.timestamp
{% if is_incremental() %}
WHERE
b.block_timestamp :: DATE >= CURRENT_DATE -7
{% endif %}
) -- step 3 calculate the prices of assets by pool, in terms of tokens per tokens
-- and in USD for both tokens
SELECT
@ -147,7 +66,7 @@ SELECT
) AS _unique_key
FROM
blocks b
JOIN rune_usd ru
JOIN price ru
ON b.block_id = ru.block_id
WHERE
rune_e8 > 0

View File

@ -0,0 +1,19 @@
{{ config(
materialized = 'view'
) }}
SELECT
rune_price_e8,
block_timestamp,
DATEADD(
ms,
__HEVO__LOADED_AT,
'1970-01-01'
) AS _INSERTED_TIMESTAMP
FROM
{{ ref(
'bronze__rune_price'
) }}
e qualify(ROW_NUMBER() over(PARTITION BY block_timestamp
ORDER BY
__HEVO__LOADED_AT DESC)) = 1

View File

@ -15,7 +15,7 @@ SELECT
out_asset,
out_e8,
failed_swaps,
failed_swaps_reasons,
{# failed_swaps_reasons, #}
event_id,
block_timestamp,
failed_swap_reasons,