diff --git a/models/core/core__ez_token_transfers.sql b/models/core/core__ez_token_transfers.sql new file mode 100644 index 0000000..02d7485 --- /dev/null +++ b/models/core/core__ez_token_transfers.sql @@ -0,0 +1,18 @@ +{{ config( + materialized = 'view' +) }} + + +SELECT + block_height, + block_timestamp, + tx_id, + sender, + recipient, + token_contract, + amount, + tx_succeeded +FROM + {{ ref('silver__token_transfers') }} +WHERE + block_timestamp::date >= '2022-04-20' \ No newline at end of file diff --git a/models/core/core__ez_token_transfers.yml b/models/core/core__ez_token_transfers.yml new file mode 100644 index 0000000..62ca107 --- /dev/null +++ b/models/core/core__ez_token_transfers.yml @@ -0,0 +1,82 @@ +version: 2 + +models: + - name: core__ez_token_transfers + description: |- + This table records all token transfers on the FLOW blockchain. + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_id + - sender + - recipient + - token_contract + + columns: + - name: tx_id + description: "{{ doc('tx_id') }}" + tests: + - not_null + + - name: block_timestamp + description: "{{ doc('block_timestamp') }}" + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - TIMESTAMP_NTZ + + - name: block_height + description: "{{ doc('block_height') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + - FLOAT + + - name: sender + description: "{{ doc('sender') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: recipient + description: "{{ doc('recipient') }}" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: token_contract + description: "{{ doc('token_contract') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: amount + description: "{{ doc('amount') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + - FLOAT + + - name: tx_succeeded + description: "{{ doc('tx_succeeded') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - BOOLEAN \ No newline at end of file diff --git a/models/descriptions/recipient.md b/models/descriptions/recipient.md new file mode 100644 index 0000000..4a4088e --- /dev/null +++ b/models/descriptions/recipient.md @@ -0,0 +1,5 @@ +{% docs recipient %} + +Address receiving the transferred token. + +{% enddocs %} \ No newline at end of file diff --git a/models/descriptions/sender.md b/models/descriptions/sender.md new file mode 100644 index 0000000..057dd60 --- /dev/null +++ b/models/descriptions/sender.md @@ -0,0 +1,5 @@ +{% docs sender %} + +Address sending the transferred token. + +{% enddocs %} \ No newline at end of file diff --git a/models/silver/silver__token_transfers.sql b/models/silver/silver__token_transfers.sql new file mode 100644 index 0000000..9f75b71 --- /dev/null +++ b/models/silver/silver__token_transfers.sql @@ -0,0 +1,119 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + cluster_by = ['_inserted_timestamp::date'], + unique_key = "CONCAT_WS('-', tx_id, sender, recipient, token_contract)" +) }} + +WITH transfers AS ( + +SELECT + _inserted_timestamp, + tx_id, + COUNT(event_type) AS event_count, + MAX(event_index + 1) AS max_index +FROM + {{ ref('silver__events_final') }} +WHERE + event_type IN ('TokensDeposited', 'TokensWithdrawn', 'FeesDeducted') + +{% if is_incremental() %} +AND + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) +{% endif %} +GROUP BY + _inserted_timestamp, tx_id +HAVING + event_count = max_index +), + + + +withdraws AS ( + +SELECT + block_height, + _inserted_timestamp, + block_timestamp, + tx_id, + event_data:from::STRING AS sender, + event_contract AS token_contract, + event_data:amount::FLOAT AS amount, + tx_succeeded +FROM + {{ ref('silver__events_final') }} +WHERE + tx_id IN (SELECT tx_id FROM transfers) +AND + event_type = 'TokensWithdrawn' + +{% if is_incremental() %} +AND + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) +{% endif %} + +GROUP BY + block_height, _inserted_timestamp, block_timestamp, tx_id, sender, token_contract, amount, tx_succeeded + + ), + +deposits AS ( + +SELECT + tx_id, + _inserted_timestamp, + event_data:to::STRING AS recipient, + event_contract AS token_contract, + event_data:amount::FLOAT AS amount +FROM + {{ ref('silver__events_final') }} +WHERE + tx_id IN (SELECT tx_id FROM transfers) +AND + event_type = 'TokensDeposited' + +{% if is_incremental() %} +AND + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) +{% endif %} + +GROUP BY + tx_id, _inserted_timestamp, recipient, token_contract, amount + + ) + +SELECT + block_height, + w._inserted_timestamp AS _inserted_timestamp, + block_timestamp, + w.tx_id, + sender, + recipient, + w.token_contract, + SUM(COALESCE(d.amount, w.amount)) AS amount, + tx_succeeded +FROM + withdraws w +LEFT JOIN + deposits d +ON w.tx_id = d.tx_id +AND w.token_contract = d.token_contract +AND w.amount = d.amount +WHERE sender IS NOT NULL +GROUP BY + block_height, w._inserted_timestamp, block_timestamp, w.tx_id, sender, recipient, w.token_contract, tx_succeeded \ No newline at end of file diff --git a/models/silver/silver__token_transfers.yml b/models/silver/silver__token_transfers.yml new file mode 100644 index 0000000..c143165 --- /dev/null +++ b/models/silver/silver__token_transfers.yml @@ -0,0 +1,82 @@ +version: 2 + +models: + - name: silver__token_transfers + description: |- + This table records all token transfers on the FLOW blockchain. + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_id + - sender + - recipient + - token_contract + + columns: + - name: tx_id + description: "{{ doc('tx_id') }}" + tests: + - not_null + + - name: block_timestamp + description: "{{ doc('block_timestamp') }}" + tests: + - not_null + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 1 + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - TIMESTAMP_NTZ + + - name: block_height + description: "{{ doc('block_height') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + - FLOAT + + - name: sender + description: "{{ doc('sender') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: recipient + description: "{{ doc('recipient') }}" + tests: + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: token_contract + description: "{{ doc('token_contract') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - STRING + - VARCHAR + + - name: amount + description: "{{ doc('amount') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - NUMBER + - FLOAT + + - name: tx_succeeded + description: "{{ doc('tx_succeeded') }}" + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - BOOLEAN \ No newline at end of file