-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from FlipsideCrypto/AN-4285/revalidate-staking…
…-and-gov AN-4285/revalidate-staking-and-gov
- Loading branch information
Showing
20 changed files
with
1,038 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{{ config( | ||
materialized = 'view', | ||
tags = ['daily'] | ||
) }} | ||
|
||
SELECT | ||
block_id, | ||
block_timestamp, | ||
tx_id, | ||
msg_group, | ||
delegator_address AS address, | ||
CASE | ||
WHEN action = 'delegate' THEN amount | ||
ELSE - amount | ||
END AS amount, | ||
SUM( | ||
CASE | ||
WHEN action = 'delegate' THEN amount | ||
ELSE - amount | ||
END | ||
) over( | ||
PARTITION BY address, | ||
currency | ||
ORDER BY | ||
block_timestamp, | ||
msg_group rows unbounded preceding | ||
) AS balance, | ||
currency, | ||
_inserted_timestamp | ||
FROM | ||
{{ ref('silver__staking') }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
version: 2 | ||
models: | ||
- name: silver__balances_staked | ||
description: A table of each staked balance change for wallets on the Axelar chain. | ||
columns: | ||
- name: BLOCK_ID | ||
description: "{{ doc('block_id') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- NUMBER | ||
- FLOAT | ||
- name: BLOCK_TIMESTAMP | ||
description: "{{ doc('block_timestamp') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- TIMESTAMP_NTZ | ||
- name: ADDRESS | ||
description: "{{ doc('address') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- STRING | ||
- VARCHAR | ||
- dbt_expectations.expect_column_values_to_match_regex: | ||
regex: axelar[0-9a-z]{38,38} | ||
- name: BALANCE | ||
description: The amount of the currency the wallet held at the given time | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- NUMBER | ||
- FLOAT | ||
- name: CURRENCY | ||
description: "{{ doc('currency') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- STRING | ||
- VARCHAR | ||
- name: _INSERTED_TIMESTAMP | ||
description: "{{ doc('inserted_timestamp') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- TIMESTAMP_NTZ | ||
- TIMESTAMP_LTZ | ||
|
185 changes: 185 additions & 0 deletions
185
models/silver/core/balance/silver__daily_balances_staked.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
unique_key = ['date', 'address', 'currency'], | ||
incremental_strategy = 'merge', | ||
merge_exclude_columns = ["inserted_timestamp"], | ||
cluster_by = ['DATE'], | ||
post_hook = "ALTER TABLE {{ this }} ADD SEARCH OPTIMIZATION ON EQUALITY(address);", | ||
tags = ['daily'] | ||
) }} | ||
|
||
WITH all_staked AS ( | ||
|
||
SELECT | ||
block_id, | ||
block_timestamp, | ||
address, | ||
balance, | ||
currency | ||
FROM | ||
{{ ref('silver__balances_staked') }} | ||
) | ||
|
||
{% if is_incremental() %}, | ||
recent AS ( | ||
SELECT | ||
DATE, | ||
address, | ||
balance, | ||
currency | ||
FROM | ||
{{ this }} | ||
WHERE | ||
DATE = ( | ||
SELECT | ||
DATEADD('day', -1, MAX(DATE)) | ||
FROM | ||
{{ this }}) | ||
), | ||
NEW AS ( | ||
SELECT | ||
block_timestamp :: DATE AS DATE, | ||
address, | ||
balance, | ||
currency, | ||
1 AS RANK | ||
FROM | ||
all_staked | ||
WHERE | ||
block_timestamp :: DATE >= ( | ||
SELECT | ||
DATEADD('day', -1, MAX(DATE)) | ||
FROM | ||
{{ this }}) qualify(ROW_NUMBER() over (PARTITION BY block_timestamp :: DATE, address, currency | ||
ORDER BY | ||
block_timestamp DESC)) = 1 | ||
), | ||
incremental AS ( | ||
SELECT | ||
DATE, | ||
address, | ||
balance, | ||
currency | ||
FROM | ||
( | ||
SELECT | ||
DATE, | ||
address, | ||
balance, | ||
currency, | ||
2 AS RANK | ||
FROM | ||
recent | ||
UNION | ||
SELECT | ||
DATE, | ||
address, | ||
balance, | ||
currency, | ||
1 AS RANK | ||
FROM | ||
NEW | ||
) qualify(ROW_NUMBER() over (PARTITION BY DATE, address, currency | ||
ORDER BY | ||
RANK ASC)) = 1 | ||
) | ||
{% endif %}, | ||
base AS ( | ||
|
||
{% if is_incremental() %} | ||
SELECT | ||
DATE AS block_timestamp, address, balance, currency | ||
FROM | ||
incremental | ||
{% else %} | ||
SELECT | ||
block_timestamp, address, balance, currency | ||
FROM | ||
all_staked | ||
{% endif %}), | ||
address_ranges AS ( | ||
SELECT | ||
address, | ||
currency, | ||
MIN( | ||
block_timestamp :: DATE | ||
) AS min_block_date, | ||
MAX ( | ||
CURRENT_TIMESTAMP :: DATE | ||
) AS max_block_date | ||
FROM | ||
base | ||
GROUP BY | ||
address, | ||
currency | ||
), | ||
ddate AS ( | ||
SELECT | ||
date_day :: DATE AS DATE | ||
FROM | ||
{{ source( | ||
'crosschain', | ||
'dim_dates' | ||
) }} | ||
GROUP BY | ||
DATE | ||
), | ||
all_dates AS ( | ||
SELECT | ||
d.date, | ||
A.address, | ||
A.currency | ||
FROM | ||
ddate d | ||
LEFT JOIN address_ranges A | ||
ON d.date BETWEEN A.min_block_date | ||
AND A.max_block_date | ||
WHERE | ||
A.address IS NOT NULL | ||
), | ||
sei_balances AS ( | ||
SELECT | ||
block_timestamp, | ||
address, | ||
balance, | ||
currency | ||
FROM | ||
base qualify(ROW_NUMBER() over (PARTITION BY block_timestamp :: DATE, address, currency | ||
ORDER BY | ||
block_timestamp DESC)) = 1 | ||
), | ||
balance_temp AS ( | ||
SELECT | ||
d.date, | ||
d.address, | ||
b.balance, | ||
d.currency | ||
FROM | ||
all_dates d | ||
LEFT JOIN sei_balances b | ||
ON d.date = b.block_timestamp :: DATE | ||
AND d.address = b.address | ||
AND d.currency = b.currency | ||
) | ||
SELECT | ||
DATE, | ||
'staked' AS balance_type, | ||
address, | ||
currency, | ||
LAST_VALUE( | ||
balance ignore nulls | ||
) over( | ||
PARTITION BY address, | ||
currency, | ||
balance_type | ||
ORDER BY | ||
DATE ASC rows unbounded preceding | ||
) AS balance, | ||
{{ dbt_utils.generate_surrogate_key( | ||
['address','currency','date'] | ||
) }} AS daily_balances_staked_id, | ||
SYSDATE() AS inserted_timestamp, | ||
SYSDATE() AS modified_timestamp, | ||
'{{ invocation_id }}' AS _invocation_id | ||
FROM | ||
balance_temp |
53 changes: 53 additions & 0 deletions
53
models/silver/core/balance/silver__daily_balances_staked.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
version: 2 | ||
models: | ||
- name: silver__daily_balances_staked | ||
description: A table that contains a daily balance entry for both staked and liquid balance of wallets on the Axelar chain. | ||
tests: | ||
- dbt_utils.unique_combination_of_columns: | ||
combination_of_columns: | ||
- DATE | ||
- ADDRESS | ||
- BALANCE_TYPE | ||
- CURRENCY | ||
columns: | ||
- name: DATE | ||
description: The day the balance was recorded on. | ||
tests: | ||
- not_null | ||
- name: BALANCE_TYPE | ||
description: Either "staked" or "liquid" corresponding to how the currency is in the wallet. | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- STRING | ||
- VARCHAR | ||
- dbt_expectations.expect_column_values_to_be_in_set: | ||
value_set: ['staked', 'liquid', 'locked liquidity', 'superfluid staked'] | ||
- name: ADDRESS | ||
description: "{{ doc('address') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- STRING | ||
- VARCHAR | ||
- dbt_expectations.expect_column_values_to_match_regex: | ||
regex: axelar[0-9a-z]{39,39} | ||
- name: BALANCE | ||
description: The amount of the currency the wallet held at the given time | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- NUMBER | ||
- FLOAT | ||
- name: CURRENCY | ||
description: "{{ doc('currency') }}" | ||
tests: | ||
- not_null | ||
- dbt_expectations.expect_column_values_to_be_in_type_list: | ||
column_type_list: | ||
- STRING | ||
- VARCHAR | ||
|
Oops, something went wrong.