unstake_events replacement with withdraw_events

This commit is contained in:
Eric Laurello 2022-12-01 10:46:30 -05:00
parent d8358e8539
commit 5eb960de74
11 changed files with 294 additions and 6 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,31 @@
{{ config(
materialized = 'view'
) }}
SELECT
tx,
chain,
from_addr,
to_addr,
asset,
asset_e8,
emit_asset_e8,
emit_rune_e8,
memo,
pool,
stake_units,
basis_points,
asymmetry,
imp_loss_protection_e8,
_EMIT_ASSET_IN_RUNE_E8,
event_id,
block_timestamp,
__HEVO__DATABASE_NAME,
__HEVO__SCHEMA_NAME,
__HEVO__INGESTED_AT,
__HEVO__LOADED_AT
FROM
{{ source(
'thorchain_midgard',
'midgard_withdraw_events'
) }}

View File

@ -1,7 +1,7 @@
version: 2
models:
- name: core__fact_unstake_events
description: "Fact table containing unstake events"
description: "Deprecating soon! This table has been renamed/replaced by Fact_withdraw_events"
columns:
- name: FACT_UNSTAKE_EVENTS_ID
description: "{{ doc('sk') }}"

View File

@ -0,0 +1,74 @@
{{ config(
materialized = 'incremental',
unique_key = 'fact_withdraw_events_id',
incremental_strategy = 'merge',
cluster_by = ['block_timestamp::DATE']
) }}
WITH base AS (
SELECT
e.tx_id,
e.blockchain,
e.from_address,
e.to_address,
e.asset,
e.asset_e8,
e.emit_asset_e8,
e.emit_rune_e8,
e.memo,
e.pool_name,
e.stake_units,
e.basis_points,
e.asymmetry,
e.imp_loss_protection_e8,
e._emit_asset_in_rune_e8,
e.block_timestamp,
_INSERTED_TIMESTAMP
FROM
{{ ref('silver__withdraw_events') }}
e
{% if is_incremental() %}
WHERE
_inserted_timestamp >= (
SELECT
MAX(
_inserted_timestamp
)
FROM
{{ this }}
) - INTERVAL '48 HOURS'
{% endif %}
)
SELECT
{{ dbt_utils.surrogate_key(
['a.tx_id', 'a.blockchain', 'a.from_address', 'a.to_address', 'a.asset', 'a.asset_e8', 'a.emit_asset_e8', 'a.emit_rune_e8', 'a.memo', 'a.pool_name', 'a.stake_units', 'a.basis_points', 'a.asymmetry', 'a.imp_loss_protection_e8', 'a._emit_asset_in_rune_e8','a.block_timestamp']
) }} AS fact_withdraw_events_id,
b.block_timestamp,
COALESCE(
b.dim_block_id,
'-1'
) AS dim_block_id,
A.tx_id,
A.blockchain,
A.from_address,
A.to_address,
A.asset,
A.asset_e8,
A.emit_asset_e8,
A.emit_rune_e8,
A.memo,
A.pool_name,
A.stake_units,
A.basis_points,
A.asymmetry,
A.imp_loss_protection_e8,
A._emit_asset_in_rune_e8,
A._inserted_timestamp,
'{{ env_var("DBT_CLOUD_RUN_ID", "manual") }}' AS _audit_run_id
FROM
base A
LEFT JOIN {{ ref('core__dim_block') }}
b
ON A.block_timestamp = b.timestamp

View File

@ -0,0 +1,85 @@
version: 2
models:
- name: core__fact_withdraw_events
description: "Fact table containing all withdraw events"
columns:
- name: FACT_WITHDRAW_EVENTS_ID
description: "{{ doc('sk') }}"
tests:
- dbt_expectations.expect_column_to_exist
- unique
- name: BLOCK_TIMESTAMP
description: "{{ doc('block_timestamp') }}"
tests:
- not_null:
where: DIM_BLOCK_ID not in ('-1','-2')
- name: DIM_BLOCK_ID
description: "FK to DIM_BLOCK table"
tests:
- negative_one:
where: _inserted_timestamp < (CURRENT_TIMESTAMP - INTERVAL '8 HOURS')
- name: TX_ID
description: "{{ doc('tx_id') }}"
tests:
- not_null
- name: BLOCKCHAIN
description: "{{ doc('blockchain') }}"
tests:
- not_null
- name: FROM_ADDRESS
description: "{{ doc('from_address') }}"
tests:
- not_null
- name: TO_ADDRESS
description: "{{ doc('to_address') }}"
tests:
- not_null
- name: ASSET
description: "{{ doc('asset') }}"
- name: ASSET_E8
description: "The asset amount for this bond, using the price table we can calculate the rune amount by asset amount"
tests:
- not_null
- name: EMIT_ASSET_E8
description: ""
tests:
- not_null
- name: EMIT_RUNE_E8
description: ""
tests:
- not_null
- name: MEMO
description: "{{ doc('memo') }}"
tests:
- not_null
- name: POOL_NAME
description: "{{ doc('pool_name') }}"
tests:
- not_null
- name: STAKE_UNITS
description: ""
tests:
- not_null
- name: BASIS_POINTS
description: ""
tests:
- not_null
- name: ASYMMETRY
description: ""
tests:
- not_null
- name: IMP_LOSS_PROTECTION_E8
description: ""
tests:
- not_null
- name: _EMIT_ASSET_IN_RUNE_E8
description: ""
tests:
- not_null
tests:
- dbt_constraints.primary_key:
column_name: FACT_WITHDRAW_EVENTS_ID
- dbt_constraints.foreign_key:
fk_column_name: DIM_BLOCK_ID
pk_table_name: ref('core__dim_block')
pk_column_name: DIM_BLOCK_ID

View File

@ -51,7 +51,7 @@ unstakes AS (
event_id,
_inserted_timestamp
FROM
{{ ref('silver__unstake_events') }}
{{ ref('silver__withdraw_events') }}
{% if is_incremental() %}
WHERE

View File

@ -130,7 +130,7 @@ withdraw_tbl AS (
SUM(stake_units) AS withdrawn_stake,
SUM(imp_loss_protection_e8) AS impermanent_loss_protection_paid
FROM
{{ ref("silver__unstake_events") }} A
{{ ref("silver__withdraw_events") }} A
JOIN {{ ref('silver__block_log') }}
b
ON A.block_timestamp = b.timestamp
@ -385,7 +385,7 @@ unstake_umc AS (
pool_name,
SUM(stake_units) AS unstake_liquidity_units
FROM
{{ ref("silver__unstake_events") }} A
{{ ref("silver__withdraw_events") }} A
JOIN {{ ref('silver__block_log') }}
b
ON A.block_timestamp = b.timestamp

View File

@ -0,0 +1,34 @@
{{ config(
materialized = 'view'
) }}
SELECT
tx AS tx_id,
chain AS blockchain,
from_addr AS from_address,
to_addr AS to_address,
asset,
asset_e8,
emit_asset_e8,
emit_rune_e8,
memo,
pool AS pool_name,
stake_units,
basis_points,
asymmetry,
imp_loss_protection_e8,
_emit_asset_in_rune_e8,
event_id,
block_timestamp,
DATEADD(
ms,
__HEVO__LOADED_AT,
'1970-01-01'
) AS _INSERTED_TIMESTAMP
FROM
{{ ref(
'bronze__withdraw_events'
) }}
e qualify(ROW_NUMBER() over(PARTITION BY event_id, tx, chain, memo, stake_units, basis_points, block_timestamp, pool_name, asset, from_addr, to_addr
ORDER BY
__HEVO__LOADED_AT DESC)) = 1

View File

@ -0,0 +1,63 @@
version: 2
models:
- name: silver__withdraw_events
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- TX_ID
- BLOCKCHAIN
- FROM_ADDRESS
- TO_ADDRESS
- ASSET
- MEMO
- POOL_NAME
- BLOCK_TIMESTAMP
columns:
- name: TX_ID
tests:
- not_null
- name: BLOCKCHAIN
tests:
- not_null
- name: FROM_ADDRESS
tests:
- not_null
- name: TO_ADDRESS
tests:
- not_null
- name: ASSET
tests:
- not_null
- name: ASSET_E8
tests:
- not_null
- name: EMIT_ASSET_E8
tests:
- not_null
- name: EMIT_RUNE_E8
tests:
- not_null
- name: MEMO
tests:
- not_null
- name: POOL_NAME
tests:
- not_null
- name: STAKE_UNITS
tests:
- not_null
- name: BASIS_POINTS
tests:
- not_null
- name: ASYMMETRY
tests:
- not_null
- name: IMP_LOSS_PROTECTION_E8
tests:
- not_null
- name: _EMIT_ASSET_IN_RUNE_E8
tests:
- not_null
- name: BLOCK_TIMESTAMP
tests:
- not_null

View File

@ -48,6 +48,7 @@ sources:
- name: midgard_unstake_events
- name: midgard_update_node_account_status_events
- name: midgard_validator_request_leave_events
- name: midgard_withdraw_events
- name: crosschain
database: flipside_prod_db
schema: silver_crosschain