diff --git a/models/core/core__dim_staking_actions.sql b/models/core/core__dim_staking_actions.sql new file mode 100644 index 0000000..469f3b0 --- /dev/null +++ b/models/core/core__dim_staking_actions.sql @@ -0,0 +1,12 @@ +{{ config( + materialized = 'view', + secure = true +) }} + +with staking_actions as ( + select + * + from {{ ref('silver__staking_actions') }} +) + +select * from staking_actions diff --git a/models/core/core__dim_staking_actions.yml b/models/core/core__dim_staking_actions.yml new file mode 100644 index 0000000..de4202d --- /dev/null +++ b/models/core/core__dim_staking_actions.yml @@ -0,0 +1,73 @@ +version: 2 + +models: + - name: core__dim_staking_actions + description: |- + This table records all the staking and delegation transactions occurring in NEAR. + + Be aware, however, not all transactions here necessarily actually contribute to securing the NEAR network. + Each epoch can have a different set of validators. + + columns: + - name: tx_hash + description: "{{ doc('tx_hash') }}" + tests: + - unique + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - 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: pool_address + description: "{{ doc('staking_pool_address') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: tx_signer + description: "{{ doc('tx_signer') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: stake_amount + description: "{{ doc('staking_stake_amount') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + + - name: action + description: "{{ doc('staking_action') }}" + 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: ['Stake', 'Unstake'] + + - 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 diff --git a/models/descriptions/staking_action.md b/models/descriptions/staking_action.md new file mode 100644 index 0000000..fb4512b --- /dev/null +++ b/models/descriptions/staking_action.md @@ -0,0 +1,5 @@ +{% docs staking_action %} + +The staking action performed in this transaction. Can be `"Stake"` or `"Unstake"`. + +{% enddocs %} diff --git a/models/descriptions/staking_pool_address.md b/models/descriptions/staking_pool_address.md index d1fcb21..938ced4 100644 --- a/models/descriptions/staking_pool_address.md +++ b/models/descriptions/staking_pool_address.md @@ -1,5 +1,5 @@ {% docs staking_pool_address %} -The address of the staking pool. +The pool address delegated into. {% enddocs %} diff --git a/models/descriptions/staking_stake_amount.md b/models/descriptions/staking_stake_amount.md new file mode 100644 index 0000000..c2b3238 --- /dev/null +++ b/models/descriptions/staking_stake_amount.md @@ -0,0 +1,5 @@ +{% docs staking_stake_amount %} + +The amount staked or delegated toward securing the NEAR network. + +{% enddocs %} diff --git a/models/silver/silver__staking_actions.sql b/models/silver/silver__staking_actions.sql new file mode 100644 index 0000000..64606cb --- /dev/null +++ b/models/silver/silver__staking_actions.sql @@ -0,0 +1,141 @@ +{{ config( + materialized = 'incremental', + incremental = 'merge', + cluster_by = ['block_timestamp'], + unique_key = 'tx_hash' +) }} + +with actions_events_function_call as ( + select + tx_hash, + method_name, + _inserted_timestamp + from {{ ref('silver__actions_events_function_call') }} + where {{ incremental_load_filter('_inserted_timestamp') }} + and method_name in ( + 'deposit_and_stake', + 'stake', + 'unstake', + 'unstake_all' + ) +), + +base_txs as ( + select + * + from {{ ref('silver__transactions') }} + where {{ incremental_load_filter('_inserted_timestamp') }} +), + +txs as ( + select + * + from base_txs + where (tx_receiver like '%.pool.near' or tx_receiver like '%.poolv1.near') +), + +pool_txs as ( + select + txs.tx_hash as tx_hash, + block_timestamp, + tx_receiver, + tx_signer, + tx, + method_name, + txs._inserted_timestamp as _inserted_timestamp + from txs + inner join actions_events_function_call + on txs.tx_hash = actions_events_function_call.tx_hash +), + +deposit_and_stake_txs as ( + select + tx_hash, + block_timestamp, + tx_receiver as pool_address, + tx_signer, + regexp_substr(array_to_string(tx:receipt[0]:outcome:logs, ','), 'staking (\\d+)', 1, 1, 'e')::number as stake_amount, + 'Stake' as action, + _inserted_timestamp + from pool_txs + where method_name = 'deposit_and_stake' + and tx:receipt[0]:outcome:status:SuccessValue is not null +), + +stake_txs as ( + select + tx_hash, + block_timestamp, + tx_receiver as pool_address, + tx_signer, + regexp_substr(array_to_string(tx:receipt[0]:outcome:logs, ','), 'staking (\\d+)', 1, 1, 'e')::number as stake_amount, + 'Stake' as action, + _inserted_timestamp + from pool_txs + where method_name = 'stake' + and tx:receipt[0]:outcome:status:SuccessValue is not null +), + +stake_all_txs as ( + select + tx_hash, + block_timestamp, + tx_receiver as pool_address, + tx_signer, + regexp_substr(array_to_string(tx:receipt[0]:outcome:logs, ','), 'staking (\\d+)', 1, 1, 'e')::number as stake_amount, + 'Stake' as action, + _inserted_timestamp + from pool_txs + where method_name = 'stake_all' + and tx:receipt[0]:outcome:status:SuccessValue is not null +), + +unstake_txs as ( + select + tx_hash, + block_timestamp, + tx_receiver as pool_address, + tx_signer, + regexp_substr(array_to_string(tx:receipt[0]:outcome:logs, ','), 'unstaking (\\d+)', 1, 1, 'e')::number as stake_amount, + 'Unstake' as action, + _inserted_timestamp + from pool_txs + where method_name = 'unstake' + and tx:receipt[0]:outcome:status:SuccessValue is not null +), + +unstake_all_txs as ( + select + tx_hash, + block_timestamp, + tx_receiver as pool_address, + tx_signer, + regexp_substr(array_to_string(tx:receipt[0]:outcome:logs, ','), 'unstaking (\\d+)', 1, 1, 'e')::number as stake_amount, + 'Unstake' as action, + _inserted_timestamp + from pool_txs + where method_name = 'unstake_all' + and tx:receipt[0]:outcome:status:SuccessValue is not null +), + +final as ( + select + * + from deposit_and_stake_txs + union + select + * + from stake_all_txs + union + select + * + from unstake_txs + union + select + * + from unstake_all_txs +) + +select +* +from final diff --git a/models/silver/silver__staking_actions.yml b/models/silver/silver__staking_actions.yml new file mode 100644 index 0000000..8511053 --- /dev/null +++ b/models/silver/silver__staking_actions.yml @@ -0,0 +1,75 @@ +version: 2 + +models: + - name: silver__staking_actions + description: |- + This table records all the staking and delegation transactions occurring in NEAR. + + Be aware, however, not all transactions here necessarily actually contribute to securing the NEAR network. + Each epoch can have a different set of validators. + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + + columns: + - name: tx_hash + description: "{{ doc('tx_hash') }}" + tests: + - unique + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - 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: pool_address + description: "{{ doc('staking_pool_address') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: tx_signer + description: "{{ doc('tx_signer') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: stake_amount + description: "{{ doc('staking_stake_amount') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + + - name: action + description: "{{ doc('staking_action') }}" + 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